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
|
|
|
|
*/
|
|
|
|
|
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
|
|
|
|
*/
|
2016-02-05 05:31:34 +00:00
|
|
|
class CategoryTreeHooks {
|
|
|
|
|
2018-04-16 06:41:38 +00:00
|
|
|
public static function shouldForceHeaders() {
|
|
|
|
global $wgCategoryTreeSidebarRoot, $wgCategoryTreeHijackPageCategories,
|
|
|
|
$wgCategoryTreeForceHeaders;
|
|
|
|
return $wgCategoryTreeForceHeaders || $wgCategoryTreeSidebarRoot
|
|
|
|
|| $wgCategoryTreeHijackPageCategories;
|
|
|
|
}
|
|
|
|
|
2016-02-05 05:31:34 +00:00
|
|
|
/**
|
2018-04-16 06:41:38 +00:00
|
|
|
* Adjusts config once MediaWiki is fully initialised
|
|
|
|
* TODO: Don't do this, lazy initialize the config
|
2016-02-05 05:31:34 +00:00
|
|
|
*/
|
|
|
|
public static function initialize() {
|
2018-04-16 06:41:38 +00:00
|
|
|
global $wgRequest;
|
|
|
|
global $wgCategoryTreeDefaultOptions, $wgCategoryTreeDefaultMode;
|
2017-05-30 18:21:54 +00:00
|
|
|
global $wgCategoryTreeCategoryPageOptions, $wgCategoryTreeCategoryPageMode;
|
2018-04-16 06:41:38 +00:00
|
|
|
global $wgCategoryTreeOmitNamespace;
|
2016-02-05 05:31:34 +00:00
|
|
|
|
2017-05-30 18:21:54 +00:00
|
|
|
if ( !isset( $wgCategoryTreeDefaultOptions['mode'] )
|
|
|
|
|| is_null( $wgCategoryTreeDefaultOptions['mode'] )
|
|
|
|
) {
|
2016-02-05 05:31:34 +00:00
|
|
|
$wgCategoryTreeDefaultOptions['mode'] = $wgCategoryTreeDefaultMode;
|
|
|
|
}
|
|
|
|
|
2017-05-30 18:21:54 +00:00
|
|
|
if ( !isset( $wgCategoryTreeDefaultOptions['hideprefix'] )
|
|
|
|
|| is_null( $wgCategoryTreeDefaultOptions['hideprefix'] )
|
|
|
|
) {
|
2016-02-05 05:31:34 +00:00
|
|
|
$wgCategoryTreeDefaultOptions['hideprefix'] = $wgCategoryTreeOmitNamespace;
|
|
|
|
}
|
|
|
|
|
2017-05-30 18:21:54 +00:00
|
|
|
if ( !isset( $wgCategoryTreeCategoryPageOptions['mode'] )
|
|
|
|
|| is_null( $wgCategoryTreeCategoryPageOptions['mode'] )
|
|
|
|
) {
|
2018-02-17 03:29:26 +00:00
|
|
|
$mode = $wgRequest->getVal( 'mode' );
|
|
|
|
$wgCategoryTreeCategoryPageOptions['mode'] = ( $mode )
|
2017-05-30 18:21:54 +00:00
|
|
|
? CategoryTree::decodeMode( $mode ) : $wgCategoryTreeCategoryPageMode;
|
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
|
|
|
*/
|
|
|
|
public static function setHooks( $parser ) {
|
2018-04-16 06:41:38 +00:00
|
|
|
global $wgCategoryTreeAllowTag;
|
|
|
|
if ( !$wgCategoryTreeAllowTag ) {
|
|
|
|
return;
|
|
|
|
}
|
2017-05-30 18:21:54 +00:00
|
|
|
$parser->setHook( 'categorytree', 'CategoryTreeHooks::parserHook' );
|
|
|
|
$parser->setFunctionHook( 'categorytree', 'CategoryTreeHooks::parserFunction' );
|
2016-02-05 05:31:34 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Entry point for the {{#categorytree}} tag parser function.
|
|
|
|
* This is a wrapper around CategoryTreeHooks::parserHook
|
2017-10-11 18:25:13 +00:00
|
|
|
* @param Parser $parser
|
2016-02-05 05:31:34 +00:00
|
|
|
* @return array|string
|
|
|
|
*/
|
|
|
|
public static function parserFunction( $parser ) {
|
|
|
|
$params = func_get_args();
|
|
|
|
array_shift( $params ); // first is $parser, strip it
|
|
|
|
|
|
|
|
// first user-supplied parameter must be category name
|
|
|
|
if ( !$params ) {
|
|
|
|
return ''; // no category specified, return nothing
|
|
|
|
}
|
|
|
|
$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];
|
|
|
|
$v = preg_replace( '/^"\s*(.*?)\s*"$/', '$1', $m[2] ); // strip any quotes enclusing the value
|
|
|
|
} else {
|
|
|
|
$k = trim( $p );
|
|
|
|
$v = true;
|
|
|
|
}
|
|
|
|
|
|
|
|
$argv[$k] = $v;
|
|
|
|
}
|
|
|
|
|
|
|
|
// now handle just like a <categorytree> tag
|
|
|
|
$html = self::parserHook( $cat, $argv, $parser );
|
2016-10-14 16:11:52 +00:00
|
|
|
return [ $html, 'noparse' => true, 'isHTML' => true ];
|
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
|
|
|
*/
|
2019-01-02 21:54:34 +00:00
|
|
|
public static function onSkinBuildSidebar( Skin $skin, array &$sidebar ) {
|
2016-02-05 05:31:34 +00:00
|
|
|
global $wgCategoryTreeSidebarRoot, $wgCategoryTreeSidebarOptions;
|
|
|
|
|
2018-04-16 06:41:38 +00:00
|
|
|
if ( !$wgCategoryTreeSidebarRoot ) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2016-02-05 05:31:34 +00:00
|
|
|
$html = self::parserHook( $wgCategoryTreeSidebarRoot, $wgCategoryTreeSidebarOptions );
|
|
|
|
if ( $html ) {
|
2019-01-02 21:54:34 +00:00
|
|
|
$sidebar['categorytree-portlet'] = $html;
|
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
|
2017-10-11 18:25:13 +00:00
|
|
|
* @param bool $allowMissing
|
2016-02-05 05:31:34 +00:00
|
|
|
* @return bool|string
|
|
|
|
*/
|
|
|
|
public static function parserHook( $cat, $argv, $parser = null, $allowMissing = false ) {
|
|
|
|
global $wgOut;
|
|
|
|
|
|
|
|
if ( $parser ) {
|
|
|
|
$parser->mOutput->mCategoryTreeTag = true; # flag for use by CategoryTreeHooks::parserOutput
|
|
|
|
} else {
|
|
|
|
CategoryTree::setHeaders( $wgOut );
|
|
|
|
}
|
|
|
|
|
|
|
|
$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.
|
|
|
|
* Does nothing if $parserOutput->mCategoryTreeTag is not set
|
2017-10-11 18:25:13 +00:00
|
|
|
* @param OutputPage $outputPage
|
|
|
|
* @param ParserOutput $parserOutput
|
2016-02-05 05:31:34 +00:00
|
|
|
*/
|
2017-04-17 13:11:25 +00:00
|
|
|
public static function parserOutput( $outputPage, $parserOutput ) {
|
2018-04-16 06:41:38 +00:00
|
|
|
if ( self::shouldForceHeaders() ) {
|
|
|
|
// Skip, we've already set the headers unconditionally
|
|
|
|
return;
|
|
|
|
}
|
2016-02-05 05:31:34 +00:00
|
|
|
if ( !empty( $parserOutput->mCategoryTreeTag ) ) {
|
|
|
|
CategoryTree::setHeaders( $outputPage );
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2017-07-26 19:16:24 +00:00
|
|
|
* BeforePageDisplay and BeforePageDisplayMobile hooks.
|
|
|
|
* These hooks are used when $wgCategoryTreeForceHeaders is set.
|
2016-02-05 05:31:34 +00:00
|
|
|
* Otherwise similar to CategoryTreeHooks::parserOutput.
|
2017-10-11 18:25:13 +00:00
|
|
|
* @param OutputPage $out
|
2016-02-05 05:31:34 +00:00
|
|
|
*/
|
2017-07-26 19:16:24 +00:00
|
|
|
public static function addHeaders( OutputPage $out ) {
|
2018-04-16 06:41:38 +00:00
|
|
|
if ( !self::shouldForceHeaders() ) {
|
|
|
|
return;
|
|
|
|
}
|
2016-02-05 05:31:34 +00:00
|
|
|
CategoryTree::setHeaders( $out );
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* ArticleFromTitle hook, override category page handling
|
|
|
|
*
|
2017-10-11 18:25:13 +00:00
|
|
|
* @param Title $title
|
|
|
|
* @param Article &$article
|
2016-02-05 05:31:34 +00:00
|
|
|
* @return bool
|
|
|
|
*/
|
|
|
|
public static function articleFromTitle( $title, &$article ) {
|
|
|
|
if ( $title->getNamespace() == NS_CATEGORY ) {
|
|
|
|
$article = new CategoryTreeCategoryPage( $title );
|
|
|
|
}
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* OutputPageMakeCategoryLinks hook, override category links
|
2018-04-16 06:41:38 +00:00
|
|
|
* @param OutputPage &$out
|
|
|
|
* @param array $categories
|
2017-10-11 18:25:13 +00:00
|
|
|
* @param array &$links
|
2016-02-05 05:31:34 +00:00
|
|
|
* @return bool
|
|
|
|
*/
|
2018-04-16 06:41:38 +00:00
|
|
|
public static function outputPageMakeCategoryLinks( &$out, $categories, &$links ) {
|
|
|
|
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 ) {
|
|
|
|
$links[$type][] = self::parserHook( $category, $wgCategoryTreePageCategoryOptions, null, true );
|
|
|
|
}
|
|
|
|
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2017-10-11 18:25:13 +00:00
|
|
|
* @param array &$vars
|
2016-02-05 05:31:34 +00:00
|
|
|
* @return bool
|
|
|
|
*/
|
|
|
|
public static function getConfigVars( &$vars ) {
|
|
|
|
global $wgCategoryTreeCategoryPageOptions;
|
|
|
|
|
|
|
|
// Look this is pretty bad but Category tree is just whacky, it needs to be rewritten
|
|
|
|
$ct = new CategoryTree( $wgCategoryTreeCategoryPageOptions );
|
|
|
|
$vars['wgCategoryTreePageCategoryOptions'] = $ct->getOptionsAsJsStructure();
|
|
|
|
return true;
|
|
|
|
}
|
2016-11-13 14:57:16 +00:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Hook handler for the SpecialTrackingCategories::preprocess hook
|
|
|
|
* @param SpecialPage $specialPage SpecialTrackingCategories object
|
|
|
|
* @param array $trackingCategories [ 'msg' => Title, 'cats' => Title[] ]
|
|
|
|
*/
|
|
|
|
public static function onSpecialTrackingCategoriesPreprocess(
|
|
|
|
$specialPage, $trackingCategories
|
|
|
|
) {
|
|
|
|
$categoryDbKeys = [];
|
|
|
|
foreach ( $trackingCategories as $catMsg => $data ) {
|
|
|
|
foreach ( $data['cats'] as $catTitle ) {
|
|
|
|
$categoryDbKeys[] = $catTitle->getDbKey();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
$categories = [];
|
|
|
|
if ( $categoryDbKeys ) {
|
|
|
|
$dbr = wfGetDB( DB_REPLICA );
|
|
|
|
$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
|
|
|
|
* @param SpecialPage $specialPage SpecialTrackingCategories object
|
|
|
|
* @param Title $catTitle Title object of the linked category
|
|
|
|
* @param string &$html Result html
|
|
|
|
*/
|
|
|
|
public static function onSpecialTrackingCategoriesGenerateCatLink(
|
|
|
|
$specialPage, $catTitle, &$html
|
|
|
|
) {
|
|
|
|
if ( !isset( $specialPage->categoryTreeCategories ) ) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
$cat = null;
|
|
|
|
if ( isset( $specialPage->categoryTreeCategories[$catTitle->getDbKey()] ) ) {
|
|
|
|
$cat = $specialPage->categoryTreeCategories[$catTitle->getDbKey()];
|
|
|
|
}
|
|
|
|
|
|
|
|
$html .= CategoryTree::createCountString( $specialPage->getContext(), $cat, 0 );
|
|
|
|
}
|
2016-02-05 05:31:34 +00:00
|
|
|
}
|