getTitle(); self::setHeader($output); if ($title->inNamespace(NS_USER)) { self::onUserPage($output); } if ($title->inNamespace(NS_MAIN)) { self::handleSubpageTitle($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'); // 加载动画,三秒内未加载完成则强制显示 if ($outputPage->getSkin()->getSkinName() === 'timeless') { $outputPage->addHeadItem( 'loading-animate-script', << var c = document.documentElement.classList; c.add('content-loading'); setTimeout(function() { c.remove('content-loading'); c.add('content-loaded'); }, 3000); document.addEventListener('DOMContentLoaded', function() { var b = getComputedStyle(document.body, null).backgroundColor; var a = document.createElement('div'); a.classList.add('page-loading-spinner'); a.innerHTML = '
'; document.body.appendChild(a); }); HTML ); } // 百度统计 $outputPage->addHeadItem( 'baiduhm', << 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); })(); HTML ); self::showAds($outputPage); } public static function showAds(OutputPage $outputPage) { global $wgIsekaiShowAds; $service = MediaWikiServices::getInstance(); $isView = $outputPage->getContext()->getRequest()->getText( 'action', 'view' ) == 'view'; $showAdsSidebar = $service->getUserOptionsLookup()->getOption($outputPage->getUser(), 'isekai-show-ads-sidebar'); $showAdsBottom = $service->getUserOptionsLookup()->getOption($outputPage->getUser(), 'isekai-show-ads-bottom'); // 检测Adblock $outputPage->addHeadItem('adblock-detect', ''); if($wgIsekaiShowAds && ($showAdsSidebar || $showAdsBottom) && $outputPage->getTitle()->getNamespace() !== NS_SPECIAL && $isView){ // 谷歌广告 $outputPage->addHeadItem( 'googlead', << HTML ); if ($showAdsSidebar && $outputPage->getSkin()->getSkinName() === 'timeless') { $outputPage->addHTML( << if (window.innerWidth >= 1100) { var adElem = document.createElement('div'); adElem.className = 'googlead-container googlead-container-sidebar'; adElem.innerHTML = ''; var relatedNav = document.querySelector('#mw-related-navigation'); if (relatedNav) { relatedNav.appendChild(adElem); (adsbygoogle = window.adsbygoogle || []).push({}); } } HTML ); } if ($showAdsBottom) { $outputPage->addHTML( << HTML ); $outputPage->addHTML( << (adsbygoogle = window.adsbygoogle || []).push({}); HTML ); } } } public static function onUserPage(OutputPage $outputPage) { // 用户页 $title = $outputPage->getTitle(); if ($title->inNamespace(NS_USER)) { $userName = $title->getText(); $user = MediaWikiServices::getInstance()->getUserFactory()->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 handleSubpageTitle(OutputPage $outputPage) { if (!$outputPage->isArticle()) return; $title = $outputPage->getTitle(); if ($title && $title->isSubpage()) { //更改显示的标题 $titleText = $title->getPrefixedText(); $baseTitleText = basename($titleText); $htmlTitle = $outputPage->getHTMLTitle(); if (strpos($htmlTitle, $baseTitleText) === 0) { // 将HTML标题替换为完整标题 $outputPage->setHTMLTitle($titleText . substr($htmlTitle, strlen($baseTitleText))); } //面包屑 $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)); $currentTitle = Title::newFromText($titleSubPath); if ($currentTitle) { $link = $linkRender->makeLink(Title::newFromText($titleSubPath), $titleName); } else { $link = $titleSubPath; } $breadcrumbsHtml[] = Html::openElement('li') . $link . Html::closeElement('li'); } } $breadcrumbsHtml[] = Html::closeElement('ol'); $html = implode($breadcrumbsHtml); // 阻止出现subtitle空行 $subtitle = $outputPage->getSubtitle(); $outputPage->setSubtitle($subtitle . $html); } } public static function onParserBeforeInternalParse(\Parser $parser, &$text, $strip_state) { $title = $parser->getPage(); if ($title !== null && $title instanceof \Title && $title->isSubpage()) { //更改显示的标题 $titleText = $title->getPrefixedText(); if ($titleText) { $parser->getOutput()->setTitleText('' . basename($titleText) . ''); } } } public static function onHtmlPageLinkRendererBegin( LinkRenderer $linkRenderer, LinkTarget $target, &$text, &$extraAttribs, &$query, &$ret ) { if ($target->inNamespace(NS_USER)) { $userName = $target->getText(); $user = MediaWikiServices::getInstance()->getUserFactory()->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; } public static function onResourceLoaderGetConfigVars(array &$vars, string $skin, Config $config){ $vars['wgIsekaiShowAds'] = $config->get('IsekaiShowAds'); } public static function onGetPreferences(User $user, array &$preferences){ $preferences['isekai-show-ads-bottom'] = [ 'type' => 'toggle', 'label-message' => 'isekai-show-ads-bottom', 'section' => 'misc/isekai-ads', ]; $preferences['isekai-show-ads-sidebar'] = [ 'type' => 'toggle', 'label-message' => 'isekai-show-ads-sidebar', 'section' => 'misc/isekai-ads', ]; } }