Use new hook OutputPageRenderCategoryLink

The new hook OutputPageRenderCategoryLink gets triggered on rendering a
single category.

This change prevents wgCategories = [] on
$wgCategoryTreeHijackPageCategories = true.

Bug: T372155
Depends-On: Id82a77a57d1f12233d974ea4c1b093f50c5ab74f
Change-Id: Ic86f210474cbc0e2dcebf664cf2309a4a4408f60
This commit is contained in:
Fomafix 2024-08-09 15:32:12 +00:00
parent 033b9cc159
commit f5f1f7de39
2 changed files with 28 additions and 16 deletions

View file

@ -86,7 +86,8 @@
"CategoryTree.CategoryCache",
"MainConfig",
"DBLoadBalancerFactory",
"LinkRenderer"
"LinkRenderer",
"TitleFormatter"
]
},
"config": {
@ -100,7 +101,7 @@
"SkinAfterPortlet": "default",
"SkinBuildSidebar": "default",
"ParserFirstCallInit": "default",
"OutputPageMakeCategoryLinks": "default",
"OutputPageRenderCategoryLink": "default",
"CategoryViewer::doCategoryQuery": "default",
"CategoryViewer::generateLink": "default"
},

View file

@ -35,13 +35,15 @@ use MediaWiki\Hook\SpecialTrackingCategories__preprocessHook;
use MediaWiki\Html\Html;
use MediaWiki\Linker\LinkRenderer;
use MediaWiki\Linker\LinkTarget;
use MediaWiki\Output\Hook\OutputPageMakeCategoryLinksHook;
use MediaWiki\Output\Hook\OutputPageRenderCategoryLinkHook;
use MediaWiki\Output\OutputPage;
use MediaWiki\Page\ProperPageIdentity;
use MediaWiki\Parser\Parser;
use MediaWiki\Parser\Sanitizer;
use MediaWiki\ResourceLoader as RL;
use MediaWiki\SpecialPage\SpecialPage;
use MediaWiki\Title\Title;
use MediaWiki\Title\TitleFormatter;
use PPFrame;
use Skin;
use Wikimedia\Rdbms\IConnectionProvider;
@ -58,7 +60,7 @@ class Hooks implements
SpecialTrackingCategories__generateCatLinkHook,
SkinBuildSidebarHook,
ParserFirstCallInitHook,
OutputPageMakeCategoryLinksHook,
OutputPageRenderCategoryLinkHook,
CategoryViewer__doCategoryQueryHook,
CategoryViewer__generateLinkHook
{
@ -66,17 +68,20 @@ class Hooks implements
private Config $config;
private IConnectionProvider $dbProvider;
private LinkRenderer $linkRenderer;
private TitleFormatter $titleFormatter;
public function __construct(
CategoryCache $categoryCache,
Config $config,
IConnectionProvider $dbProvider,
LinkRenderer $linkRenderer
LinkRenderer $linkRenderer,
TitleFormatter $titleFormatter
) {
$this->categoryCache = $categoryCache;
$this->config = $config;
$this->dbProvider = $dbProvider;
$this->linkRenderer = $linkRenderer;
$this->titleFormatter = $titleFormatter;
}
/**
@ -225,25 +230,31 @@ class Hooks implements
}
/**
* OutputPageMakeCategoryLinks hook, override category links
* OutputPageRenderCategoryLink hook
* @param OutputPage $out
* @param array $categories
* @param array &$links
* @return bool
* @param ProperPageIdentity $categoryTitle
* @param string $text
* @param ?string &$link
* @return void
*/
public function onOutputPageMakeCategoryLinks( $out, $categories, &$links ) {
public function onOutputPageRenderCategoryLink(
OutputPage $out,
ProperPageIdentity $categoryTitle,
string $text,
?string &$link
): void {
if ( !$this->config->get( 'CategoryTreeHijackPageCategories' ) ) {
// Not enabled, don't do anything
return true;
return;
}
$options = $this->config->get( 'CategoryTreePageCategoryOptions' );
foreach ( $categories as $category => $type ) {
$links[$type][] = $this->parserHook( $category, $options, null, null, true );
}
CategoryTree::setHeaders( $out );
return false;
$options = $this->config->get( 'CategoryTreePageCategoryOptions' );
$link = $this->parserHook(
$this->titleFormatter->getPrefixedText( $categoryTitle ),
$options, null, null, true
);
}
/**