From 3ed72445d5e2a461b1c0ba910849f12c2384adfd Mon Sep 17 00:00:00 2001 From: Lex Lim Date: Tue, 27 Dec 2022 22:13:16 +0800 Subject: [PATCH] =?UTF-8?q?=E4=BF=AE=E6=94=B9=E8=BF=87=E6=97=B6Hooks?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- Isekai.class.php | 51 ++++++++++++++++++++++++++++++++++++++---------- extension.json | 4 ++-- 2 files changed, 43 insertions(+), 12 deletions(-) diff --git a/Isekai.class.php b/Isekai.class.php index 2e50231..1ad69d2 100644 --- a/Isekai.class.php +++ b/Isekai.class.php @@ -13,7 +13,7 @@ use User; use EditPage; class Isekai { - public static function onLoad($output) { + public static function onLoad(OutputPage $output) { $title = $output->getTitle(); self::setHeader($output); @@ -21,6 +21,10 @@ class Isekai { if ($title->inNamespace(NS_USER)) { self::onUserPage($output); } + + if ($title->inNamespace(NS_MAIN)) { + self::handleSubpageTitle($output); + } } public static function setHeader(OutputPage $outputPage) { @@ -168,15 +172,21 @@ HTML } } - public static function onSkinTemplateOutputPageBeforeExec(\SkinTemplate &$skin, \QuickTemplate &$template) { - if (!$skin->getOutput()->isArticle()) return; - $title = $skin->getTitle(); + public static function handleSubpageTitle(OutputPage $outputPage) { + if (!$outputPage->isArticle()) return; + + $title = $outputPage->getTitle(); if ($title && $title->isSubpage()) { //更改显示的标题 - $titleText = $template->get('title'); - if ($titleText) { - $template->set('title', basename($titleText)); + $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); @@ -189,15 +199,36 @@ HTML $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'); + $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'); - $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('' . basename($titleText) . ''); + } + } + } + public static function onHtmlPageLinkRendererBegin( LinkRenderer $linkRenderer, LinkTarget $target, &$text, &$extraAttribs, &$query, &$ret diff --git a/extension.json b/extension.json index 9b87954..b0c3dbd 100644 --- a/extension.json +++ b/extension.json @@ -31,8 +31,8 @@ "BeforePageDisplay": [ "Isekai\\Isekai::onLoad" ], - "SkinTemplateOutputPageBeforeExec": [ - "Isekai\\Isekai::onSkinTemplateOutputPageBeforeExec" + "ParserBeforeInternalParse": [ + "Isekai\\Isekai::onParserBeforeInternalParse" ], "HtmlPageLinkRendererBegin": [ "Isekai\\Isekai::onHtmlPageLinkRendererBegin"