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.

365 lines
14 KiB
PHP

<?php
namespace Isekai;
use MediaWiki\MediaWikiServices;
use Config;
use OutputPage;
use Html;
use Title;
use MediaWiki\Linker\LinkRenderer;
use MediaWiki\Linker\LinkTarget;
use User;
use EditPage;
class Isekai {
public static function onLoad(OutputPage $output) {
$title = $output->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',
<<<HTML
<script type="text/javascript">
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 = '<div class="spinner-box"><div class="circle-border"><div class="circle-core" style="background-color: ' + b + '"></div></div></div>';
document.body.appendChild(a);
});
</script>
HTML
);
}
// 百度统计
$outputPage->addHeadItem(
'baiduhm',
<<<HTML
<script type="text/javascript">
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
);
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', '<script src="/peel1.js"></script>');
if($wgIsekaiShowAds
&& ($showAdsSidebar || $showAdsBottom)
&& $outputPage->getTitle()->getNamespace() !== NS_SPECIAL
&& $isView){
// 谷歌广告
$outputPage->addHeadItem(
'googlead',
<<<HTML
<script async src="https://pagead2.googlesyndication.com/pagead/js/adsbygoogle.js?client=ca-pub-8607817613161226" crossorigin="anonymous"></script>
HTML
);
if ($showAdsSidebar && $outputPage->getSkin()->getSkinName() === 'timeless') {
$outputPage->addHTML(
<<<HTML
<script type="text/javascript">
window.addEventListener('load', function () {
if (window.innerWidth >= 1100) {
var adElem = document.createElement('div');
adElem.className = 'googlead-container googlead-container-sidebar';
adElem.innerHTML = '<ins class="adsbygoogle" style="display:block;" data-ad-client="ca-pub-8607817613161226" data-ad-slot="8245595332" data-ad-format="rectangle" data-full-width-responsive="true"></ins>';
var relatedNav = document.querySelector('#mw-related-navigation');
if (relatedNav) {
relatedNav.appendChild(adElem);
(adsbygoogle = window.adsbygoogle || []).push({});
}
}
})
</script>
HTML
);
}
if ($showAdsBottom) {
$outputPage->addHTML(
<<<HTML
<div class="googlead-container googlead-container-bottom">
<ins class="adsbygoogle"
style="display:block"
data-ad-client="ca-pub-8607817613161226"
data-ad-slot="3245545943"
data-ad-format="horizontal"
data-full-width-responsive="true"></ins>
</div>
HTML
);
$outputPage->addHTML(
<<<HTML
<script>
(adsbygoogle = window.adsbygoogle || []).push({});
</script>
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";
}
}
}