2016-02-05 05:31:34 +00:00
|
|
|
<?php
|
|
|
|
/**
|
2018-04-16 06:50:28 +00:00
|
|
|
* © 2006-2008 Daniel Kinzler and others
|
|
|
|
*
|
|
|
|
* This program is free software; you can redistribute it and/or modify
|
|
|
|
* it under the terms of the GNU General Public License as published by
|
|
|
|
* the Free Software Foundation; either version 2 of the License, or
|
|
|
|
* (at your option) any later version.
|
|
|
|
*
|
|
|
|
* This program is distributed in the hope that it will be useful,
|
|
|
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
* GNU General Public License for more details.
|
|
|
|
*
|
|
|
|
* You should have received a copy of the GNU General Public License along
|
|
|
|
* with this program; if not, write to the Free Software Foundation, Inc.,
|
|
|
|
* 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
|
|
|
|
* http://www.gnu.org/copyleft/gpl.html
|
2016-02-05 05:31:34 +00:00
|
|
|
*
|
|
|
|
* @file
|
|
|
|
* @ingroup Extensions
|
|
|
|
* @author Daniel Kinzler, brightbyte.de
|
|
|
|
*/
|
|
|
|
|
2021-04-07 22:35:52 +00:00
|
|
|
namespace MediaWiki\Extension\CategoryTree;
|
|
|
|
|
|
|
|
use Article;
|
|
|
|
use Category;
|
2021-05-22 13:43:11 +00:00
|
|
|
use Config;
|
2021-04-07 22:35:52 +00:00
|
|
|
use Html;
|
2021-05-22 13:43:11 +00:00
|
|
|
use IContextSource;
|
|
|
|
use MediaWiki\Hook\OutputPageMakeCategoryLinksHook;
|
|
|
|
use MediaWiki\Hook\OutputPageParserOutputHook;
|
|
|
|
use MediaWiki\Hook\ParserFirstCallInitHook;
|
|
|
|
use MediaWiki\Hook\SkinBuildSidebarHook;
|
|
|
|
use MediaWiki\Hook\SpecialTrackingCategories__generateCatLinkHook;
|
|
|
|
use MediaWiki\Hook\SpecialTrackingCategories__preprocessHook;
|
|
|
|
use MediaWiki\Page\Hook\ArticleFromTitleHook;
|
2021-04-07 22:35:52 +00:00
|
|
|
use OutputPage;
|
|
|
|
use Parser;
|
|
|
|
use ParserOutput;
|
|
|
|
use PPFrame;
|
|
|
|
use Sanitizer;
|
|
|
|
use Skin;
|
|
|
|
use SpecialPage;
|
|
|
|
use Title;
|
2021-05-22 13:43:11 +00:00
|
|
|
use Wikimedia\Rdbms\ILoadBalancer;
|
2021-04-07 22:35:52 +00:00
|
|
|
|
2018-04-16 06:50:28 +00:00
|
|
|
/**
|
|
|
|
* Hooks for the CategoryTree extension, an AJAX based gadget
|
|
|
|
* to display the category structure of a wiki
|
2021-05-22 13:43:11 +00:00
|
|
|
*
|
|
|
|
* @phpcs:disable MediaWiki.NamingConventions.LowerCamelFunctionsName.FunctionName
|
2018-04-16 06:50:28 +00:00
|
|
|
*/
|
2021-05-22 13:43:11 +00:00
|
|
|
class Hooks implements
|
|
|
|
ArticleFromTitleHook,
|
|
|
|
SpecialTrackingCategories__preprocessHook,
|
|
|
|
SpecialTrackingCategories__generateCatLinkHook,
|
|
|
|
OutputPageParserOutputHook,
|
|
|
|
SkinBuildSidebarHook,
|
|
|
|
ParserFirstCallInitHook,
|
|
|
|
OutputPageMakeCategoryLinksHook
|
|
|
|
{
|
2016-02-05 05:31:34 +00:00
|
|
|
|
2020-12-02 14:44:48 +00:00
|
|
|
private const EXTENSION_DATA_FLAG = 'CategoryTree';
|
|
|
|
|
2021-05-22 13:43:11 +00:00
|
|
|
/** @var ILoadBalancer */
|
|
|
|
private $loadBalancer;
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @var Config
|
|
|
|
*/
|
|
|
|
private $config;
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @param ILoadBalancer $loadBalancer
|
|
|
|
* @param Config $config
|
|
|
|
*/
|
|
|
|
public function __construct( ILoadBalancer $loadBalancer, Config $config ) {
|
|
|
|
$this->loadBalancer = $loadBalancer;
|
|
|
|
$this->config = $config;
|
|
|
|
}
|
|
|
|
|
2016-02-05 05:31:34 +00:00
|
|
|
/**
|
2017-10-11 18:25:13 +00:00
|
|
|
* @param Parser $parser
|
2016-02-05 05:31:34 +00:00
|
|
|
*/
|
2021-05-22 13:43:11 +00:00
|
|
|
public function onParserFirstCallInit( $parser ) {
|
|
|
|
if ( !$this->config->get( 'CategoryTreeAllowTag' ) ) {
|
2018-04-16 06:41:38 +00:00
|
|
|
return;
|
|
|
|
}
|
2021-05-22 13:43:11 +00:00
|
|
|
$parser->setHook( 'categorytree', [ $this, 'parserHook' ] );
|
|
|
|
$parser->setFunctionHook( 'categorytree', [ $this, 'parserFunction' ] );
|
2016-02-05 05:31:34 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Entry point for the {{#categorytree}} tag parser function.
|
2021-04-07 22:35:52 +00:00
|
|
|
* This is a wrapper around Hooks::parserHook
|
2017-10-11 18:25:13 +00:00
|
|
|
* @param Parser $parser
|
2019-03-08 21:03:38 +00:00
|
|
|
* @param string ...$params
|
2016-02-05 05:31:34 +00:00
|
|
|
* @return array|string
|
|
|
|
*/
|
2021-05-22 13:43:11 +00:00
|
|
|
public function parserFunction( Parser $parser, ...$params ) {
|
2016-02-05 05:31:34 +00:00
|
|
|
// first user-supplied parameter must be category name
|
|
|
|
if ( !$params ) {
|
2019-02-05 14:57:56 +00:00
|
|
|
// no category specified, return nothing
|
|
|
|
return '';
|
2016-02-05 05:31:34 +00:00
|
|
|
}
|
|
|
|
$cat = array_shift( $params );
|
|
|
|
|
|
|
|
// build associative arguments from flat parameter list
|
2016-10-14 16:11:52 +00:00
|
|
|
$argv = [];
|
2016-02-05 05:31:34 +00:00
|
|
|
foreach ( $params as $p ) {
|
|
|
|
if ( preg_match( '/^\s*(\S.*?)\s*=\s*(.*?)\s*$/', $p, $m ) ) {
|
|
|
|
$k = $m[1];
|
2021-04-07 22:35:52 +00:00
|
|
|
// strip any quotes enclosing the value
|
2019-02-05 14:57:56 +00:00
|
|
|
$v = preg_replace( '/^"\s*(.*?)\s*"$/', '$1', $m[2] );
|
2016-02-05 05:31:34 +00:00
|
|
|
} else {
|
|
|
|
$k = trim( $p );
|
|
|
|
$v = true;
|
|
|
|
}
|
|
|
|
|
|
|
|
$argv[$k] = $v;
|
|
|
|
}
|
|
|
|
|
2021-04-01 17:57:54 +00:00
|
|
|
if ( $parser->getOutputType() === Parser::OT_PREPROCESS ) {
|
2021-08-31 11:18:30 +00:00
|
|
|
return Html::rawElement( 'categorytree', $argv, $cat );
|
2021-04-01 17:57:54 +00:00
|
|
|
} else {
|
|
|
|
// now handle just like a <categorytree> tag
|
2021-05-22 13:43:11 +00:00
|
|
|
$html = $this->parserHook( $cat, $argv, $parser );
|
2021-04-01 17:57:54 +00:00
|
|
|
return [ $html, 'noparse' => true, 'isHTML' => true ];
|
|
|
|
}
|
2016-02-05 05:31:34 +00:00
|
|
|
}
|
|
|
|
|
2021-06-15 18:48:48 +00:00
|
|
|
/**
|
|
|
|
* Obtain a category sidebar link based on config
|
|
|
|
* @return bool|string of link
|
|
|
|
*/
|
|
|
|
private function getCategorySidebarBox() {
|
|
|
|
global $wgCategoryTreeSidebarRoot, $wgCategoryTreeSidebarOptions;
|
|
|
|
if ( !$wgCategoryTreeSidebarRoot ) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
return self::parserHook( $wgCategoryTreeSidebarRoot, $wgCategoryTreeSidebarOptions );
|
|
|
|
}
|
|
|
|
|
2016-02-05 05:31:34 +00:00
|
|
|
/**
|
|
|
|
* Hook implementation for injecting a category tree into the sidebar.
|
2018-04-16 06:41:38 +00:00
|
|
|
* Only does anything if $wgCategoryTreeSidebarRoot is set to a category name.
|
2017-10-11 18:25:13 +00:00
|
|
|
* @param Skin $skin
|
2019-01-02 21:54:34 +00:00
|
|
|
* @param array &$sidebar
|
2016-02-05 05:31:34 +00:00
|
|
|
*/
|
2021-05-22 13:43:11 +00:00
|
|
|
public function onSkinBuildSidebar( $skin, &$sidebar ) {
|
2021-06-15 18:48:48 +00:00
|
|
|
$html = $this->getCategorySidebarBox();
|
2016-02-05 05:31:34 +00:00
|
|
|
if ( $html ) {
|
2021-06-15 18:48:48 +00:00
|
|
|
$sidebar['categorytree-portlet'] = [];
|
2019-01-02 22:24:24 +00:00
|
|
|
CategoryTree::setHeaders( $skin->getOutput() );
|
2016-02-05 05:31:34 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-06-15 18:48:48 +00:00
|
|
|
/**
|
|
|
|
* Hook implementation for injecting a category tree link into the sidebar.
|
|
|
|
* Only does anything if $wgCategoryTreeSidebarRoot is set to a category name.
|
|
|
|
* @param Skin $skin
|
|
|
|
* @param string $portlet
|
|
|
|
* @param string &$html
|
|
|
|
*/
|
|
|
|
public function onSkinAfterPortlet( $skin, $portlet, &$html ) {
|
|
|
|
if ( $portlet === 'categorytree-portlet' ) {
|
|
|
|
$box = $this->getCategorySidebarBox();
|
|
|
|
if ( $box ) {
|
|
|
|
$html .= $box;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-02-05 05:31:34 +00:00
|
|
|
/**
|
|
|
|
* Entry point for the <categorytree> tag parser hook.
|
|
|
|
* This loads CategoryTreeFunctions.php and calls CategoryTree::getTag()
|
2017-10-11 18:25:13 +00:00
|
|
|
* @param string $cat
|
|
|
|
* @param array $argv
|
2018-05-26 01:01:12 +00:00
|
|
|
* @param Parser|null $parser
|
2020-05-29 17:05:25 +00:00
|
|
|
* @param PPFrame|null $frame
|
2017-10-11 18:25:13 +00:00
|
|
|
* @param bool $allowMissing
|
2016-02-05 05:31:34 +00:00
|
|
|
* @return bool|string
|
|
|
|
*/
|
2021-05-22 13:43:11 +00:00
|
|
|
public function parserHook(
|
2018-12-16 09:47:07 +00:00
|
|
|
$cat,
|
|
|
|
array $argv,
|
|
|
|
Parser $parser = null,
|
2020-05-29 17:05:25 +00:00
|
|
|
PPFrame $frame = null,
|
2018-12-16 09:47:07 +00:00
|
|
|
$allowMissing = false
|
|
|
|
) {
|
2016-02-05 05:31:34 +00:00
|
|
|
if ( $parser ) {
|
2021-04-07 22:35:52 +00:00
|
|
|
# flag for use by Hooks::parserOutput
|
2020-12-02 14:44:48 +00:00
|
|
|
$parser->getOutput()->setExtensionData( self::EXTENSION_DATA_FLAG, true );
|
2016-02-05 05:31:34 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
$ct = new CategoryTree( $argv );
|
|
|
|
|
|
|
|
$attr = Sanitizer::validateTagAttributes( $argv, 'div' );
|
|
|
|
|
2017-05-30 18:21:54 +00:00
|
|
|
$hideroot = isset( $argv['hideroot'] )
|
|
|
|
? CategoryTree::decodeBoolean( $argv['hideroot'] ) : null;
|
|
|
|
$onlyroot = isset( $argv['onlyroot'] )
|
|
|
|
? CategoryTree::decodeBoolean( $argv['onlyroot'] ) : null;
|
|
|
|
$depthArg = isset( $argv['depth'] ) ? (int)$argv['depth'] : null;
|
2016-02-05 05:31:34 +00:00
|
|
|
|
|
|
|
$depth = CategoryTree::capDepth( $ct->getOption( 'mode' ), $depthArg );
|
|
|
|
if ( $onlyroot ) {
|
|
|
|
$depth = 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
return $ct->getTag( $parser, $cat, $hideroot, $attr, $depth, $allowMissing );
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Hook callback that injects messages and things into the <head> tag,
|
|
|
|
* if needed in the current page.
|
2020-12-02 14:44:48 +00:00
|
|
|
* Does nothing if self::EXTENSION_DATA_FLAG is not set on $parserOutput extension data.
|
2017-10-11 18:25:13 +00:00
|
|
|
* @param OutputPage $outputPage
|
|
|
|
* @param ParserOutput $parserOutput
|
2016-02-05 05:31:34 +00:00
|
|
|
*/
|
2021-07-22 05:16:52 +00:00
|
|
|
public function onOutputPageParserOutput( $outputPage, $parserOutput ): void {
|
2021-01-01 18:16:42 +00:00
|
|
|
if ( $parserOutput->getExtensionData( self::EXTENSION_DATA_FLAG ) ) {
|
2020-12-02 14:44:48 +00:00
|
|
|
CategoryTree::setHeaders( $outputPage );
|
|
|
|
}
|
2016-02-05 05:31:34 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* ArticleFromTitle hook, override category page handling
|
|
|
|
*
|
2017-10-11 18:25:13 +00:00
|
|
|
* @param Title $title
|
2018-12-16 09:47:07 +00:00
|
|
|
* @param Article|null &$article Article (object) that will be returned
|
2021-05-22 13:43:11 +00:00
|
|
|
* @param IContextSource $context
|
|
|
|
* @return bool|void True or no return value to continue or false to abort
|
2016-02-05 05:31:34 +00:00
|
|
|
*/
|
2021-05-22 13:43:11 +00:00
|
|
|
public function onArticleFromTitle( $title, &$article, $context ) {
|
2021-09-02 08:33:30 +00:00
|
|
|
if ( $title->getNamespace() === NS_CATEGORY ) {
|
2016-02-05 05:31:34 +00:00
|
|
|
$article = new CategoryTreeCategoryPage( $title );
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* OutputPageMakeCategoryLinks hook, override category links
|
2021-04-07 22:35:52 +00:00
|
|
|
* @param OutputPage $out
|
2018-04-16 06:41:38 +00:00
|
|
|
* @param array $categories
|
2017-10-11 18:25:13 +00:00
|
|
|
* @param array &$links
|
2016-02-05 05:31:34 +00:00
|
|
|
* @return bool
|
|
|
|
*/
|
2021-05-22 13:43:11 +00:00
|
|
|
public function onOutputPageMakeCategoryLinks( $out, $categories, &$links ) {
|
2018-04-16 06:41:38 +00:00
|
|
|
global $wgCategoryTreePageCategoryOptions, $wgCategoryTreeHijackPageCategories;
|
|
|
|
|
|
|
|
if ( !$wgCategoryTreeHijackPageCategories ) {
|
|
|
|
// Not enabled, don't do anything
|
|
|
|
return true;
|
|
|
|
}
|
2016-02-05 05:31:34 +00:00
|
|
|
|
|
|
|
foreach ( $categories as $category => $type ) {
|
2021-05-22 13:43:11 +00:00
|
|
|
$links[$type][] = $this->parserHook( $category, $wgCategoryTreePageCategoryOptions, null, null, true );
|
2016-02-05 05:31:34 +00:00
|
|
|
}
|
2021-04-07 22:34:03 +00:00
|
|
|
CategoryTree::setHeaders( $out );
|
2016-02-05 05:31:34 +00:00
|
|
|
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2019-04-04 19:45:27 +00:00
|
|
|
* Get exported data for the "ext.categoryTree" ResourceLoader module.
|
|
|
|
*
|
|
|
|
* @internal For use in extension.json only.
|
|
|
|
* @return array Data to be serialised as data.json
|
2016-02-05 05:31:34 +00:00
|
|
|
*/
|
2019-04-04 19:45:27 +00:00
|
|
|
public static function getDataForJs() {
|
2016-02-05 05:31:34 +00:00
|
|
|
global $wgCategoryTreeCategoryPageOptions;
|
|
|
|
|
2019-04-04 19:45:27 +00:00
|
|
|
// Look, this is pretty bad but CategoryTree is just whacky, it needs to be rewritten
|
2016-02-05 05:31:34 +00:00
|
|
|
$ct = new CategoryTree( $wgCategoryTreeCategoryPageOptions );
|
2019-04-04 19:45:27 +00:00
|
|
|
|
|
|
|
return [
|
|
|
|
'defaultCtOptions' => $ct->getOptionsAsJsStructure(),
|
|
|
|
];
|
2016-02-05 05:31:34 +00:00
|
|
|
}
|
2016-11-13 14:57:16 +00:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Hook handler for the SpecialTrackingCategories::preprocess hook
|
2019-03-17 15:42:15 +00:00
|
|
|
* @suppress PhanUndeclaredProperty SpecialPage->categoryTreeCategories
|
2016-11-13 14:57:16 +00:00
|
|
|
* @param SpecialPage $specialPage SpecialTrackingCategories object
|
|
|
|
* @param array $trackingCategories [ 'msg' => Title, 'cats' => Title[] ]
|
2019-11-01 20:23:17 +00:00
|
|
|
* @phan-param array<string,array{msg:Title,cats:Title[]}> $trackingCategories
|
2016-11-13 14:57:16 +00:00
|
|
|
*/
|
2021-05-22 13:43:11 +00:00
|
|
|
public function onSpecialTrackingCategories__preprocess(
|
|
|
|
$specialPage,
|
|
|
|
$trackingCategories
|
2016-11-13 14:57:16 +00:00
|
|
|
) {
|
|
|
|
$categoryDbKeys = [];
|
|
|
|
foreach ( $trackingCategories as $catMsg => $data ) {
|
|
|
|
foreach ( $data['cats'] as $catTitle ) {
|
|
|
|
$categoryDbKeys[] = $catTitle->getDbKey();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
$categories = [];
|
|
|
|
if ( $categoryDbKeys ) {
|
2021-05-22 13:43:11 +00:00
|
|
|
$dbr = $this->loadBalancer->getConnectionRef( DB_REPLICA );
|
2016-11-13 14:57:16 +00:00
|
|
|
$res = $dbr->select(
|
|
|
|
'category',
|
|
|
|
[ 'cat_id', 'cat_title', 'cat_pages', 'cat_subcats', 'cat_files' ],
|
|
|
|
[ 'cat_title' => array_unique( $categoryDbKeys ) ],
|
|
|
|
__METHOD__
|
|
|
|
);
|
|
|
|
foreach ( $res as $row ) {
|
|
|
|
$categories[$row->cat_title] = Category::newFromRow( $row );
|
|
|
|
}
|
|
|
|
}
|
|
|
|
$specialPage->categoryTreeCategories = $categories;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Hook handler for the SpecialTrackingCategories::generateCatLink hook
|
2019-03-17 15:42:15 +00:00
|
|
|
* @suppress PhanUndeclaredProperty SpecialPage->categoryTreeCategories
|
2016-11-13 14:57:16 +00:00
|
|
|
* @param SpecialPage $specialPage SpecialTrackingCategories object
|
|
|
|
* @param Title $catTitle Title object of the linked category
|
|
|
|
* @param string &$html Result html
|
|
|
|
*/
|
2021-05-22 13:43:11 +00:00
|
|
|
public function onSpecialTrackingCategories__generateCatLink( $specialPage,
|
|
|
|
$catTitle, &$html
|
2016-11-13 14:57:16 +00:00
|
|
|
) {
|
|
|
|
if ( !isset( $specialPage->categoryTreeCategories ) ) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2021-09-02 08:33:30 +00:00
|
|
|
$cat = $specialPage->categoryTreeCategories[$catTitle->getDBkey()] ?? null;
|
2016-11-13 14:57:16 +00:00
|
|
|
|
|
|
|
$html .= CategoryTree::createCountString( $specialPage->getContext(), $cat, 0 );
|
|
|
|
}
|
2016-02-05 05:31:34 +00:00
|
|
|
}
|