diff --git a/extension.json b/extension.json index 774ce097..993663a0 100644 --- a/extension.json +++ b/extension.json @@ -100,13 +100,14 @@ }, "Hooks": { "MediaWikiServices": "config", - "ArticleFromTitle": "default", "SpecialTrackingCategories::preprocess": "default", "SpecialTrackingCategories::generateCatLink": "default", "SkinAfterPortlet": "default", "SkinBuildSidebar": "default", "ParserFirstCallInit": "default", - "OutputPageMakeCategoryLinks": "default" + "OutputPageMakeCategoryLinks": "default", + "CategoryViewer::doCategoryQuery": "default", + "CategoryViewer::generateLink": "default" }, "config": { "CategoryTreeMaxChildren": { diff --git a/includes/CategoryCache.php b/includes/CategoryCache.php index a54620f0..e9e93dfd 100644 --- a/includes/CategoryCache.php +++ b/includes/CategoryCache.php @@ -23,6 +23,7 @@ namespace MediaWiki\Extension\CategoryTree; use Category; use MediaWiki\Linker\LinkTarget; use Wikimedia\Rdbms\ILoadBalancer; +use Wikimedia\Rdbms\IResultWrapper; /** * Caches Category::class objects @@ -91,6 +92,13 @@ class CategoryCache { ->caller( __METHOD__ ) ->fetchResultSet(); + $this->fillFromQuery( $rows ); + } + + /** + * @param IResultWrapper $rows + */ + public function fillFromQuery( IResultWrapper $rows ) { foreach ( $rows as $row ) { $this->cache[$row->cat_title] = Category::newFromRow( $row ); } diff --git a/includes/CategoryTreeCategoryPage.php b/includes/CategoryTreeCategoryPage.php deleted file mode 100644 index 60751184..00000000 --- a/includes/CategoryTreeCategoryPage.php +++ /dev/null @@ -1,28 +0,0 @@ -categorytree ) ) { - CategoryTree::setHeaders( $this->getOutput() ); - - $options = $this->getConfig()->get( 'CategoryTreeCategoryPageOptions' ); - - $mode = $this->getRequest()->getRawVal( 'mode' ); - if ( $mode !== null ) { - $options['mode'] = CategoryTree::decodeMode( $mode ); - } - - $this->categorytree = new CategoryTree( $options ); - } - - return $this->categorytree; - } - - /** - * Add a subcategory to the internal lists - * @param Category $cat - * @param string $sortkey - * @param int $pageLength - */ - public function addSubcategoryObject( Category $cat, $sortkey, $pageLength ) { - $title = $cat->getTitle(); - - if ( $this->getRequest()->getCheck( 'notree' ) ) { - parent::addSubcategoryObject( $cat, $sortkey, $pageLength ); - return; - } - - $tree = $this->getCategoryTree(); - - $this->children[] = $tree->renderNodeInfo( $title, $cat ); - - $this->children_start_char[] = $this->getSubcategorySortChar( $title, $sortkey ); - } -} diff --git a/includes/Hooks.php b/includes/Hooks.php index 68ca9ec7..4e441273 100644 --- a/includes/Hooks.php +++ b/includes/Hooks.php @@ -24,24 +24,25 @@ namespace MediaWiki\Extension\CategoryTree; -use Article; use Config; use Html; -use IContextSource; +use MediaWiki\Hook\CategoryViewer__doCategoryQueryHook; +use MediaWiki\Hook\CategoryViewer__generateLinkHook; use MediaWiki\Hook\OutputPageMakeCategoryLinksHook; use MediaWiki\Hook\ParserFirstCallInitHook; use MediaWiki\Hook\SkinBuildSidebarHook; use MediaWiki\Hook\SpecialTrackingCategories__generateCatLinkHook; use MediaWiki\Hook\SpecialTrackingCategories__preprocessHook; use MediaWiki\Linker\LinkTarget; -use MediaWiki\Page\Hook\ArticleFromTitleHook; use OutputPage; use Parser; use PPFrame; +use RequestContext; use Sanitizer; use Skin; use SpecialPage; use Title; +use Wikimedia\Rdbms\IResultWrapper; /** * Hooks for the CategoryTree extension, an AJAX based gadget @@ -50,12 +51,13 @@ use Title; * @phpcs:disable MediaWiki.NamingConventions.LowerCamelFunctionsName.FunctionName */ class Hooks implements - ArticleFromTitleHook, SpecialTrackingCategories__preprocessHook, SpecialTrackingCategories__generateCatLinkHook, SkinBuildSidebarHook, ParserFirstCallInitHook, - OutputPageMakeCategoryLinksHook + OutputPageMakeCategoryLinksHook, + CategoryViewer__doCategoryQueryHook, + CategoryViewer__generateLinkHook { /** @var CategoryCache */ @@ -208,20 +210,6 @@ class Hooks implements return $ct->getTag( $parser, $cat, $hideroot, $attr, $depth, $allowMissing ); } - /** - * ArticleFromTitle hook, override category page handling - * - * @param Title $title - * @param Article|null &$article Article (object) that will be returned - * @param IContextSource $context - * @return bool|void True or no return value to continue or false to abort - */ - public function onArticleFromTitle( $title, &$article, $context ) { - if ( $title->inNamespace( NS_CATEGORY ) ) { - $article = new CategoryTreeCategoryPage( $title ); - } - } - /** * OutputPageMakeCategoryLinks hook, override category links * @param OutputPage $out @@ -293,4 +281,45 @@ class Hooks implements $html .= CategoryTree::createCountString( $specialPage->getContext(), $cat, 0 ); } + + /** + * @param string $type + * @param IResultWrapper $res + */ + public function onCategoryViewer__doCategoryQuery( $type, $res ) { + if ( $type === 'subcat' && $res ) { + $this->categoryCache->fillFromQuery( $res ); + CategoryTree::setHeaders( RequestContext::getMain()->getOutput() ); + } + } + + /** + * @param string $type + * @param Title $title + * @param string $html + * @param string &$link + * @return bool + */ + public function onCategoryViewer__generateLink( $type, $title, $html, &$link ) { + if ( $type !== 'subcat' || $link !== null ) { + return true; + } + + $request = RequestContext::getMain()->getRequest(); + if ( $request->getCheck( 'notree' ) ) { + return true; + } + + $options = $this->config->get( 'CategoryTreeCategoryPageOptions' ); + $mode = $request->getRawVal( 'mode' ); + if ( $mode !== null ) { + $options['mode'] = $mode; + } + $tree = new CategoryTree( $options ); + + $cat = $this->categoryCache->getCategory( $title ); + + $link = $tree->renderNodeInfo( $title, $cat ); + return false; + } }