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', 'ext.isekai.function-btn']); //$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 ); if ($outputPage->getUser() && $outputPage->getUser()->isRegistered()) { $outputPage->addBodyClasses(['logged-in']); } 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( << window.addEventListener('load', function () { 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) { if ($title->isSubpage()) { //更改显示的标题 $titleText = $title->getPrefixedText(); $currentDisplayTitle = $outputPage->getDisplayTitle(); $htmlTitle = $outputPage->getHTMLTitle(); if (strpos($htmlTitle, $currentDisplayTitle) === 0) { // 将HTML标题替换为完整标题 $htmlTitleText = $currentDisplayTitle . ' (' . dirname($titleText) . ')'; $outputPage->setHTMLTitle($htmlTitleText . substr($htmlTitle, strlen($currentDisplayTitle))); $outputPage->addMeta('title', $htmlTitleText); } //面包屑 $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; } if ($key === 0) { $breadcrumbsHtml[] = Html::openElement('li', ['class' => 'root']) . $link . Html::closeElement('li'); } else { $breadcrumbsHtml[] = Html::openElement('li') . $link . Html::closeElement('li'); } $breadcrumbsHtml[] = Html::openElement('span', ['class' => 'breadcrumb-divider']) . '/' . Html::closeElement('span'); } } $breadcrumbsHtml[] = Html::closeElement('ol'); $html = implode($breadcrumbsHtml); // 阻止出现subtitle空行 $subtitle = $outputPage->getSubtitle(); $outputPage->setSubtitle($subtitle . $html); } else { $currentDisplayTitle = $outputPage->getDisplayTitle(); if ($currentDisplayTitle) { $currentDisplayTitle = strip_tags($currentDisplayTitle); $outputPage->addMeta('title', $currentDisplayTitle); if ($currentDisplayTitle !== $title->getPrefixedText()) { // 输出原标题 $breadcrumbsHtml = [ Html::openElement('ol', ['class' => 'breadcrumb']), Html::element('li', ['class' => 'root active'], $title->getPrefixedText()), Html::closeElement('ol'), ]; $html = implode($breadcrumbsHtml); $subtitle = $outputPage->getSubtitle(); $outputPage->setSubtitle($subtitle . $html); } } else { $outputPage->addMeta('title', $title->getPrefixedText()); } } } } 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', ]; } public static function onCirrusSearchAnalysisConfig(array &$config, $builder) { if (isset($config['filter']['smartcn_stop'])) { // 处理中文Analysis Config } } public static function onMugenAppGetAvatar(&$avatarUrl, $size, $user) { if (!$user) { $avatarUrl = "https://account.isekai.cn/realms/isekai/avatar/default"; return; } $userName = $user->getName(); $avatarBase = 'https://account.isekai.cn/realms/isekai/avatar/by-username/' . $userName; if ($size <= 64) { $avatarUrl = "$avatarBase?size=md"; } else if ($size <= 128) { $avatarUrl = "$avatarBase?size=lg"; } else if ($size <= 256) { $avatarUrl = "$avatarBase?size=xl"; } else { $avatarUrl = "$avatarBase?size=xxl"; } } }