You cannot select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

157 lines
4.7 KiB
PHP

<?php
namespace Isekai;
use MediaWiki\MediaWikiServices;
use OutputPage;
use Html;
use Title;
use MediaWiki\Linker\LinkRenderer;
use MediaWiki\Linker\LinkTarget;
use User;
use EditPage;
class Isekai {
public static function onLoad($output) {
$title = $output->getTitle();
self::setHeader($output);
if ($title->inNamespace(NS_USER)) {
self::onUserPage($output);
}
}
public static function setHeader(OutputPage $outputPage) {
$outputPage->addHeadItem('dns-prefetch-main', Html::element('link', ['rel' => 'dns-prefetch', 'href' => '//www.isekai.cn']));
$outputPage->addHeadItem('sitemap-link', Html::element('link', [
'rel' => 'sitemap', 'type' => 'application/xml',
'title' => 'Sitemap', 'href' => '/sitemap/sitemap-index-wiki.xml',
]));
// $outputPage->addHeadItem('dns-prefetch-static', \Html::element('link', ['rel' => 'dns-prefetch', 'href' => '//static-www.isekai.cn']));
$outputPage->addMeta('robots', 'all');
$outputPage->addMeta('revisit-after', '1 days');
$outputPage->addMeta('author', '异世界百科');
$outputPage->addModuleStyles('ext.isekai.styles');
$outputPage->addModules('ext.isekai');
$outputPage->addModules('ext.fontawesome');
//$outputPage->addModules('ext.treeview');
// 百度统计
$outputPage->addHeadItem(
'baiduhm',
<<<HTML
<script>
var _hmt = _hmt || [];
(function() {
var hm = document.createElement("script");
hm.src = "https://hm.baidu.com/hm.js?2f650f34db6f2bbffa0af4d4e91a09a9";
var s = document.getElementsByTagName("script")[0];
s.parentNode.insertBefore(hm, s);
})();
</script>
HTML
);
}
public static function onUserPage(OutputPage $outputPage) {
// 用户页
$title = $outputPage->getTitle();
if ($title->inNamespace(NS_USER)) {
$userName = $title->getText();
$user = User::newFromName($userName);
if ($user && $user->isRegistered()) {
$showName = $user->getRealName();
if (!$showName) {
$showName = $userName;
}
$outputPage->setPageTitle($showName);
$outputPage->setSubtitle('@' . $title->getText());
$outputPage->setHTMLTitle(wfMessage('group-user')->text() . ' - ' . $outputPage->getHTMLTitle());
}
}
}
public static function onSkinTemplateOutputPageBeforeExec(\SkinTemplate &$skin, \QuickTemplate &$template) {
if (!$skin->getOutput()->isArticle()) return;
$title = $skin->getTitle();
if ($title && $title->isSubpage()) {
//更改显示的标题
$titleText = $template->get('title');
if ($titleText) {
$template->set('title', basename($titleText));
}
//面包屑
$titlePathList = explode('/', $titleText);
$titlePathLen = count($titlePathList);
$breadcrumbsHtml = [
Html::openElement('ol', ['class' => 'breadcrumb']),
];
$linkRender = MediaWikiServices::getInstance()->getLinkRenderer();
foreach ($titlePathList as $key => $titleName) {
if ($key === $titlePathLen - 1) {
$breadcrumbsHtml[] = Html::element('li', ['class' => 'active'], $titleName);
} else {
$titleSubPath = implode('/', array_slice($titlePathList, 0, $key + 1));
$breadcrumbsHtml[] = Html::openElement('li') .
$linkRender->makeLink(Title::newFromText($titleSubPath), $titleName) . Html::closeElement('li');
}
}
$breadcrumbsHtml[] = Html::closeElement('ol');
$template->extend('subtitle', implode($breadcrumbsHtml));
}
}
public static function onHtmlPageLinkRendererBegin(
LinkRenderer $linkRenderer, LinkTarget $target,
&$text, &$extraAttribs, &$query, &$ret
) {
if ($target->inNamespace(NS_USER)) {
$userName = $target->getText();
$user = User::newFromName($userName);
if ($user && $user->isRegistered()) {
$showName = $user->getRealName();
if ($showName) {
$text = $showName;
}
}
}
}
public static function onAttemptSave(EditPage $editPage) {
$services = MediaWikiServices::getInstance();
$title = $editPage->getTitle();
if (
!$title || !$title->inNamespace(NS_MAIN) ||
!$title->isSubpage() || $title->exists()
) {
return true;
}
$path = explode('/', $title->getBaseText());
$categoryList = [];
$pathLen = count($path);
for ($i = 0; $i < $pathLen; $i ++) {
if ($i === 0) {
$categoryList[] = $path[0];
} else {
$categoryList[] = implode('/', array_slice($path, 0, $i + 1));
}
}
$categorySyntaxList = [];
$categoryNsText = $services->getContentLanguage()->
getNsText($services->getNamespaceInfo()->getSubject(NS_CATEGORY));
foreach ($categoryList as $categoryName) {
$categorySyntaxList[] = "[[{$categoryNsText}:{$categoryName}]]";
}
if (substr($editPage->textbox1, -1, 1) !== "\n") {
$editPage->textbox1 .= "\n" . implode("", $categorySyntaxList);
} else {
$editPage->textbox1 .= implode("", $categorySyntaxList);
}
return true;
}
}