修改过时Hooks

master
落雨楓 2 years ago
parent ee3915fa73
commit 3ed72445d5

@ -13,7 +13,7 @@ use User;
use EditPage; use EditPage;
class Isekai { class Isekai {
public static function onLoad($output) { public static function onLoad(OutputPage $output) {
$title = $output->getTitle(); $title = $output->getTitle();
self::setHeader($output); self::setHeader($output);
@ -21,6 +21,10 @@ class Isekai {
if ($title->inNamespace(NS_USER)) { if ($title->inNamespace(NS_USER)) {
self::onUserPage($output); self::onUserPage($output);
} }
if ($title->inNamespace(NS_MAIN)) {
self::handleSubpageTitle($output);
}
} }
public static function setHeader(OutputPage $outputPage) { public static function setHeader(OutputPage $outputPage) {
@ -168,15 +172,21 @@ HTML
} }
} }
public static function onSkinTemplateOutputPageBeforeExec(\SkinTemplate &$skin, \QuickTemplate &$template) { public static function handleSubpageTitle(OutputPage $outputPage) {
if (!$skin->getOutput()->isArticle()) return; if (!$outputPage->isArticle()) return;
$title = $skin->getTitle();
$title = $outputPage->getTitle();
if ($title && $title->isSubpage()) { if ($title && $title->isSubpage()) {
//更改显示的标题 //更改显示的标题
$titleText = $template->get('title'); $titleText = $title->getPrefixedText();
if ($titleText) {
$template->set('title', basename($titleText)); $baseTitleText = basename($titleText);
$htmlTitle = $outputPage->getHTMLTitle();
if (strpos($htmlTitle, $baseTitleText) === 0) {
// 将HTML标题替换为完整标题
$outputPage->setHTMLTitle($titleText . substr($htmlTitle, strlen($baseTitleText)));
} }
//面包屑 //面包屑
$titlePathList = explode('/', $titleText); $titlePathList = explode('/', $titleText);
$titlePathLen = count($titlePathList); $titlePathLen = count($titlePathList);
@ -189,15 +199,36 @@ HTML
$breadcrumbsHtml[] = Html::element('li', ['class' => 'active'], $titleName); $breadcrumbsHtml[] = Html::element('li', ['class' => 'active'], $titleName);
} else { } else {
$titleSubPath = implode('/', array_slice($titlePathList, 0, $key + 1)); $titleSubPath = implode('/', array_slice($titlePathList, 0, $key + 1));
$breadcrumbsHtml[] = Html::openElement('li') . $currentTitle = Title::newFromText($titleSubPath);
$linkRender->makeLink(Title::newFromText($titleSubPath), $titleName) . Html::closeElement('li'); if ($currentTitle) {
$link = $linkRender->makeLink(Title::newFromText($titleSubPath), $titleName);
} else {
$link = $titleSubPath;
}
$breadcrumbsHtml[] = Html::openElement('li') . $link . Html::closeElement('li');
} }
} }
$breadcrumbsHtml[] = Html::closeElement('ol'); $breadcrumbsHtml[] = Html::closeElement('ol');
$template->extend('subtitle', implode($breadcrumbsHtml)); $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('<span class="mw-page-title-main">' . basename($titleText) . '</span>');
}
}
}
public static function onHtmlPageLinkRendererBegin( public static function onHtmlPageLinkRendererBegin(
LinkRenderer $linkRenderer, LinkTarget $target, LinkRenderer $linkRenderer, LinkTarget $target,
&$text, &$extraAttribs, &$query, &$ret &$text, &$extraAttribs, &$query, &$ret

@ -31,8 +31,8 @@
"BeforePageDisplay": [ "BeforePageDisplay": [
"Isekai\\Isekai::onLoad" "Isekai\\Isekai::onLoad"
], ],
"SkinTemplateOutputPageBeforeExec": [ "ParserBeforeInternalParse": [
"Isekai\\Isekai::onSkinTemplateOutputPageBeforeExec" "Isekai\\Isekai::onParserBeforeInternalParse"
], ],
"HtmlPageLinkRendererBegin": [ "HtmlPageLinkRendererBegin": [
"Isekai\\Isekai::onHtmlPageLinkRendererBegin" "Isekai\\Isekai::onHtmlPageLinkRendererBegin"

Loading…
Cancel
Save