diff options
Diffstat (limited to 'www/wiki/extensions/CategoryTree/includes')
8 files changed, 1678 insertions, 0 deletions
diff --git a/www/wiki/extensions/CategoryTree/includes/ApiCategoryTree.php b/www/wiki/extensions/CategoryTree/includes/ApiCategoryTree.php new file mode 100644 index 00000000..f512ea4c --- /dev/null +++ b/www/wiki/extensions/CategoryTree/includes/ApiCategoryTree.php @@ -0,0 +1,143 @@ +<?php +/** + * 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 + */ + +class ApiCategoryTree extends ApiBase { + public function execute() { + $params = $this->extractRequestParams(); + $options = []; + if ( isset( $params['options'] ) ) { + $options = FormatJson::decode( $params['options'] ); + if ( !is_object( $options ) ) { + if ( is_callable( [ $this, 'dieWithError' ] ) ) { + $this->dieWithError( 'apierror-categorytree-invalidjson', 'invalidjson' ); + } else { + $this->dieUsage( 'Options must be valid a JSON object', 'invalidjson' ); + } + return; + } + $options = get_object_vars( $options ); + } + $depth = isset( $options['depth'] ) ? (int)$options['depth'] : 1; + + $ct = new CategoryTree( $options ); + $depth = CategoryTree::capDepth( $ct->getOption( 'mode' ), $depth ); + $title = CategoryTree::makeTitle( $params['category'] ); + $config = $this->getConfig(); + $ctConfig = ConfigFactory::getDefaultInstance()->makeConfig( 'categorytree' ); + $html = $this->getHTML( $ct, $title, $depth, $ctConfig ); + + if ( + $ctConfig->get( 'CategoryTreeHTTPCache' ) && + $config->get( 'SquidMaxage' ) && + $config->get( 'UseSquid' ) + ) { + if ( $config->get( 'UseESI' ) ) { + $this->getRequest()->response()->header( + 'Surrogate-Control: max-age=' . $config->get( 'SquidMaxage' ) . ', content="ESI/1.0"' + ); + $this->getMain()->setCacheMaxAge( 0 ); + } else { + $this->getMain()->setCacheMaxAge( $config->get( 'SquidMaxage' ) ); + } + // cache for anons only + $this->getRequest()->response()->header( 'Vary: Accept-Encoding, Cookie' ); + // TODO: purge the squid cache when a category page is invalidated + } + + $this->getResult()->addContentValue( $this->getModuleName(), 'html', $html ); + } + + /** + * @param string $condition + * + * @return bool|null|string + */ + public function getConditionalRequestData( $condition ) { + if ( $condition === 'last-modified' ) { + $params = $this->extractRequestParams(); + $title = CategoryTree::makeTitle( $params['category'] ); + return wfGetDB( DB_REPLICA )->selectField( 'page', 'page_touched', + [ + 'page_namespace' => NS_CATEGORY, + 'page_title' => $title->getDBkey(), + ], + __METHOD__ + ); + } + } + + /** + * Get category tree HTML for the given tree, title, depth and config + * + * @param CategoryTree $ct + * @param Title $title + * @param int $depth + * @param Config $ctConfig Config for CategoryTree + * @return string HTML + */ + private function getHTML( $ct, $title, $depth, $ctConfig ) { + global $wgContLang, $wgMemc; + + $mckey = wfMemcKey( + 'ajax-categorytree', + md5( $title->getDBkey() ), + md5( $ct->getOptionsAsCacheKey( $depth ) ), + $this->getLanguage()->getCode(), + $wgContLang->getExtraHashOptions(), + $ctConfig->get( 'RenderHashAppend' ) + ); + + $touched = $this->getConditionalRequestData( 'last-modified' ); + if ( $touched ) { + $mcvalue = $wgMemc->get( $mckey ); + if ( $mcvalue && $touched <= $mcvalue['timestamp'] ) { + $html = $mcvalue['value']; + } + } + + if ( !isset( $html ) ) { + $html = $ct->renderChildren( $title, $depth ); + + $wgMemc->set( + $mckey, + [ + 'timestamp' => wfTimestampNow(), + 'value' => $html + ], + 86400 + ); + } + return trim( $html ); + } + + public function getAllowedParams() { + return [ + 'category' => [ + ApiBase::PARAM_TYPE => 'string', + ApiBase::PARAM_REQUIRED => true, + ], + 'options' => [ + ApiBase::PARAM_TYPE => 'string', + ], + ]; + } + + public function isInternal() { + return true; + } +} diff --git a/www/wiki/extensions/CategoryTree/includes/CategoryTree.php b/www/wiki/extensions/CategoryTree/includes/CategoryTree.php new file mode 100644 index 00000000..139012fd --- /dev/null +++ b/www/wiki/extensions/CategoryTree/includes/CategoryTree.php @@ -0,0 +1,822 @@ +<?php +/** + * © 2006-2007 Daniel Kinzler + * + * 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 + * + * @file + * @ingroup Extensions + * @author Daniel Kinzler, brightbyte.de + */ + +/** + * Core functions for the CategoryTree extension, an AJAX based gadget + * to display the category structure of a wiki + */ +class CategoryTree { + public $mOptions = []; + + /** + * @param array $options + */ + public function __construct( $options ) { + global $wgCategoryTreeDefaultOptions; + + // ensure default values and order of options. + // Order may become important, it may influence the cache key! + foreach ( $wgCategoryTreeDefaultOptions as $option => $default ) { + if ( isset( $options[$option] ) ) { + $this->mOptions[$option] = $options[$option]; + } else { + $this->mOptions[$option] = $default; + } + } + + $this->mOptions['mode'] = self::decodeMode( $this->mOptions['mode'] ); + + if ( $this->mOptions['mode'] == CategoryTreeMode::PARENTS ) { + // namespace filter makes no sense with CategoryTreeMode::PARENTS + $this->mOptions['namespaces'] = false; + } + + $this->mOptions['hideprefix'] = self::decodeHidePrefix( $this->mOptions['hideprefix'] ); + $this->mOptions['showcount'] = self::decodeBoolean( $this->mOptions['showcount'] ); + $this->mOptions['namespaces'] = self::decodeNamespaces( $this->mOptions['namespaces'] ); + + if ( $this->mOptions['namespaces'] ) { + # automatically adjust mode to match namespace filter + if ( count( $this->mOptions['namespaces'] ) === 1 + && $this->mOptions['namespaces'][0] == NS_CATEGORY ) { + $this->mOptions['mode'] = CategoryTreeMode::CATEGORIES; + } elseif ( !in_array( NS_FILE, $this->mOptions['namespaces'] ) ) { + $this->mOptions['mode'] = CategoryTreeMode::PAGES; + } else { + $this->mOptions['mode'] = CategoryTreeMode::ALL; + } + } + } + + /** + * @param string $name + * @return mixed + */ + public function getOption( $name ) { + return $this->mOptions[$name]; + } + + /** + * @return bool + */ + private function isInverse() { + return $this->getOption( 'mode' ) == CategoryTreeMode::PARENTS; + } + + /** + * @param mixed $nn + * @return array|bool + */ + private static function decodeNamespaces( $nn ) { + global $wgContLang; + + if ( $nn === false || is_null( $nn ) ) { + return false; + } + + if ( !is_array( $nn ) ) { + $nn = preg_split( '![\s#:|]+!', $nn ); + } + + $namespaces = []; + + foreach ( $nn as $n ) { + if ( is_int( $n ) ) { + $ns = $n; + } else { + $n = trim( $n ); + if ( $n === '' ) { + continue; + } + + $lower = strtolower( $n ); + + if ( is_numeric( $n ) ) { + $ns = (int)$n; + } elseif ( $n == '-' || $n == '_' || $n == '*' || $lower == 'main' ) { + $ns = NS_MAIN; + } else { + $ns = $wgContLang->getNsIndex( $n ); + } + } + + if ( is_int( $ns ) ) { + $namespaces[] = $ns; + } + } + + sort( $namespaces ); # get elements into canonical order + return $namespaces; + } + + /** + * @param mixed $mode + * @return int|string + */ + public static function decodeMode( $mode ) { + global $wgCategoryTreeDefaultOptions; + + if ( is_null( $mode ) ) { + return $wgCategoryTreeDefaultOptions['mode']; + } + if ( is_int( $mode ) ) { + return $mode; + } + + $mode = trim( strtolower( $mode ) ); + + if ( is_numeric( $mode ) ) { + return (int)$mode; + } + + if ( $mode == 'all' ) { + $mode = CategoryTreeMode::ALL; + } elseif ( $mode == 'pages' ) { + $mode = CategoryTreeMode::PAGES; + } elseif ( $mode == 'categories' || $mode == 'sub' ) { + $mode = CategoryTreeMode::CATEGORIES; + } elseif ( $mode == 'parents' || $mode == 'super' || $mode == 'inverse' ) { + $mode = CategoryTreeMode::PARENTS; + } elseif ( $mode == 'default' ) { + $mode = $wgCategoryTreeDefaultOptions['mode']; + } + + return (int)$mode; + } + + /** + * Helper function to convert a string to a boolean value. + * Perhaps make this a global function in MediaWiki proper + * @param mixed $value + * @return bool|null|string + */ + public static function decodeBoolean( $value ) { + if ( is_null( $value ) ) { + return null; + } + if ( is_bool( $value ) ) { + return $value; + } + if ( is_int( $value ) ) { + return ( $value > 0 ); + } + + $value = trim( strtolower( $value ) ); + if ( is_numeric( $value ) ) { + return ( (int)$value > 0 ); + } + + if ( $value == 'yes' || $value == 'y' + || $value == 'true' || $value == 't' || $value == 'on' + ) { + return true; + } elseif ( $value == 'no' || $value == 'n' + || $value == 'false' || $value == 'f' || $value == 'off' + ) { + return false; + } elseif ( $value == 'null' || $value == 'default' || $value == 'none' || $value == 'x' ) { + return null; + } else { + return false; + } + } + + /** + * @param mixed $value + * @return int|string + */ + public static function decodeHidePrefix( $value ) { + global $wgCategoryTreeDefaultOptions; + + if ( is_null( $value ) ) { + return $wgCategoryTreeDefaultOptions['hideprefix']; + } + if ( is_int( $value ) ) { + return $value; + } + if ( $value === true ) { + return CategoryTreeHidePrefix::ALWAYS; + } + if ( $value === false ) { + return CategoryTreeHidePrefix::NEVER; + } + + $value = trim( strtolower( $value ) ); + + if ( $value == 'yes' || $value == 'y' + || $value == 'true' || $value == 't' || $value == 'on' + ) { + return CategoryTreeHidePrefix::ALWAYS; + } elseif ( $value == 'no' || $value == 'n' + || $value == 'false' || $value == 'f' || $value == 'off' + ) { + return CategoryTreeHidePrefix::NEVER; + } elseif ( $value == 'always' ) { + return CategoryTreeHidePrefix::ALWAYS; + } elseif ( $value == 'never' ) { + return CategoryTreeHidePrefix::NEVER; + } elseif ( $value == 'auto' ) { + return CategoryTreeHidePrefix::AUTO; + } elseif ( $value == 'categories' || $value == 'category' || $value == 'smart' ) { + return CategoryTreeHidePrefix::CATEGORIES; + } else { + return $wgCategoryTreeDefaultOptions['hideprefix']; + } + } + + /** + * Add ResourceLoader modules to the OutputPage object + * @param OutputPage $outputPage + */ + public static function setHeaders( $outputPage ) { + # Add the modules + $outputPage->addModuleStyles( 'ext.categoryTree.css' ); + $outputPage->addModules( 'ext.categoryTree' ); + } + + /** + * @param array $options + * @param string $enc + * @return mixed + * @throws Exception + */ + protected static function encodeOptions( $options, $enc ) { + if ( $enc == 'mode' || $enc == '' ) { + $opt = $options['mode']; + } elseif ( $enc == 'json' ) { + $opt = FormatJson::encode( $options ); + } else { + throw new Exception( 'Unknown encoding for CategoryTree options: ' . $enc ); + } + + return $opt; + } + + /** + * @param string|null $depth + * @return string + */ + public function getOptionsAsCacheKey( $depth = null ) { + $key = ""; + + foreach ( $this->mOptions as $k => $v ) { + if ( is_array( $v ) ) { + $v = implode( '|', $v ); + } + $key .= $k . ':' . $v . ';'; + } + + if ( !is_null( $depth ) ) { + $key .= ";depth=" . $depth; + } + return $key; + } + + /** + * @param int|null $depth + * @return mixed + */ + public function getOptionsAsJsStructure( $depth = null ) { + if ( $depth !== null ) { + $opt = $this->mOptions; + $opt['depth'] = $depth; + $s = self::encodeOptions( $opt, 'json' ); + } else { + $s = self::encodeOptions( $this->mOptions, 'json' ); + } + + return $s; + } + + /** + * @return string + */ + private function getOptionsAsUrlParameters() { + return http_build_query( $this->mOptions ); + } + + /** + * Custom tag implementation. This is called by CategoryTreeHooks::parserHook, which is used to + * load CategoryTreeFunctions.php on demand. + * @param Parser $parser + * @param string $category + * @param bool $hideroot + * @param array $attr + * @param int $depth + * @param bool $allowMissing + * @return bool|string + */ + public function getTag( $parser, $category, $hideroot = false, $attr = [], $depth = 1, + $allowMissing = false + ) { + global $wgCategoryTreeDisableCache; + + $category = trim( $category ); + if ( $category === '' ) { + return false; + } + + if ( $parser ) { + if ( $wgCategoryTreeDisableCache === true ) { + $parser->disableCache(); + } elseif ( is_int( $wgCategoryTreeDisableCache ) ) { + $parser->getOutput()->updateCacheExpiry( $wgCategoryTreeDisableCache ); + } + } + + $title = self::makeTitle( $category ); + + if ( $title === false || $title === null ) { + return false; + } + + if ( isset( $attr['class'] ) ) { + $attr['class'] .= ' CategoryTreeTag'; + } else { + $attr['class'] = ' CategoryTreeTag'; + } + + $attr['data-ct-mode'] = $this->mOptions['mode']; + $attr['data-ct-options'] = $this->getOptionsAsJsStructure(); + + $html = ''; + $html .= Html::openElement( 'div', $attr ); + + if ( !$allowMissing && !$title->getArticleID() ) { + $html .= Html::openElement( 'span', [ 'class' => 'CategoryTreeNotice' ] ); + if ( $parser ) { + $html .= $parser->recursiveTagParse( + wfMessage( 'categorytree-not-found', $category )->plain() ); + } else { + $html .= wfMessage( 'categorytree-not-found', $category )->parse(); + } + $html .= Html::closeElement( 'span' ); + } else { + if ( !$hideroot ) { + $html .= $this->renderNode( $title, $depth ); + } else { + $html .= $this->renderChildren( $title, $depth ); + } + } + + $html .= Xml::closeElement( 'div' ); + $html .= "\n\t\t"; + + return $html; + } + + /** + * Returns a string with an HTML representation of the children of the given category. + * @param Title $title + * @param int $depth + * @return string + */ + public function renderChildren( $title, $depth = 1 ) { + global $wgCategoryTreeMaxChildren, $wgCategoryTreeUseCategoryTable; + + if ( $title->getNamespace() != NS_CATEGORY ) { + // Non-categories can't have children. :) + return ''; + } + + $dbr = wfGetDB( DB_REPLICA ); + + $inverse = $this->isInverse(); + $mode = $this->getOption( 'mode' ); + $namespaces = $this->getOption( 'namespaces' ); + + $tables = [ 'page', 'categorylinks' ]; + $fields = [ 'page_id', 'page_namespace', 'page_title', + 'page_is_redirect', 'page_len', 'page_latest', 'cl_to', + 'cl_from' ]; + $where = []; + $joins = []; + $options = [ 'ORDER BY' => 'cl_type, cl_sortkey', 'LIMIT' => $wgCategoryTreeMaxChildren ]; + + if ( $inverse ) { + $joins['categorylinks'] = [ 'RIGHT JOIN', [ + 'cl_to = page_title', 'page_namespace' => NS_CATEGORY + ] ]; + $where['cl_from'] = $title->getArticleID(); + } else { + $joins['categorylinks'] = [ 'JOIN', 'cl_from = page_id' ]; + $where['cl_to'] = $title->getDBkey(); + $options['USE INDEX']['categorylinks'] = 'cl_sortkey'; + + # namespace filter. + if ( $namespaces ) { + // NOTE: we assume that the $namespaces array contains only integers! + // decodeNamepsaces makes it so. + $where['page_namespace'] = $namespaces; + } elseif ( $mode != CategoryTreeMode::ALL ) { + if ( $mode == CategoryTreeMode::PAGES ) { + $where['cl_type'] = [ 'page', 'subcat' ]; + } else { + $where['cl_type'] = 'subcat'; + } + } + } + + # fetch member count if possible + $doCount = !$inverse && $wgCategoryTreeUseCategoryTable; + + if ( $doCount ) { + $tables = array_merge( $tables, [ 'category' ] ); + $fields = array_merge( $fields, [ + 'cat_id', 'cat_title', 'cat_subcats', 'cat_pages', 'cat_files' + ] ); + $joins['category'] = [ 'LEFT JOIN', [ + 'cat_title = page_title', 'page_namespace' => NS_CATEGORY ] + ]; + } + + $res = $dbr->select( $tables, $fields, $where, __METHOD__, $options, $joins ); + + # collect categories separately from other pages + $categories = ''; + $other = ''; + + foreach ( $res as $row ) { + # NOTE: in inverse mode, the page record may be null, because we use a right join. + # happens for categories with no category page (red cat links) + if ( $inverse && $row->page_title === null ) { + $t = Title::makeTitle( NS_CATEGORY, $row->cl_to ); + } else { + # TODO: translation support; ideally added to Title object + $t = Title::newFromRow( $row ); + } + + $cat = null; + + if ( $doCount && $row->page_namespace == NS_CATEGORY ) { + $cat = Category::newFromRow( $row, $t ); + } + + $s = $this->renderNodeInfo( $t, $cat, $depth - 1 ); + $s .= "\n\t\t"; + + if ( $row->page_namespace == NS_CATEGORY ) { + $categories .= $s; + } else { + $other .= $s; + } + } + + return $categories . $other; + } + + /** + * Returns a string with an HTML representation of the parents of the given category. + * @param Title $title + * @return string + */ + public function renderParents( $title ) { + global $wgCategoryTreeMaxChildren; + + $dbr = wfGetDB( DB_REPLICA ); + + $res = $dbr->select( + 'categorylinks', + [ + 'page_namespace' => NS_CATEGORY, + 'page_title' => 'cl_to', + ], + [ 'cl_from' => $title->getArticleID() ], + __METHOD__, + [ + 'LIMIT' => $wgCategoryTreeMaxChildren, + 'ORDER BY' => 'cl_to' + ] + ); + + $special = SpecialPage::getTitleFor( 'CategoryTree' ); + + $s = ''; + + foreach ( $res as $row ) { + $t = Title::newFromRow( $row ); + + $label = $t->getText(); + + $wikiLink = $special->getLocalURL( 'target=' . $t->getPartialURL() . + '&' . $this->getOptionsAsUrlParameters() ); + + if ( $s !== '' ) { + $s .= wfMessage( 'pipe-separator' )->escaped(); + } + + $s .= Xml::openElement( 'span', [ 'class' => 'CategoryTreeItem' ] ); + $s .= Xml::element( 'a', [ 'class' => 'CategoryTreeLabel', 'href' => $wikiLink ], $label ); + $s .= Xml::closeElement( 'span' ); + + $s .= "\n\t\t"; + } + + return $s; + } + + /** + * Returns a string with a HTML represenation of the given page. + * @param Title $title + * @param int $children + * @return string + */ + public function renderNode( $title, $children = 0 ) { + global $wgCategoryTreeUseCategoryTable; + + if ( $wgCategoryTreeUseCategoryTable && $title->getNamespace() == NS_CATEGORY + && !$this->isInverse() + ) { + $cat = Category::newFromTitle( $title ); + } else { + $cat = null; + } + + return $this->renderNodeInfo( $title, $cat, $children ); + } + + /** + * Returns a string with a HTML represenation of the given page. + * $info must be an associative array, containing at least a Title object under the 'title' key. + * @param Title $title + * @param Category $cat + * @param int $children + * @return string + */ + public function renderNodeInfo( $title, $cat, $children = 0 ) { + $mode = $this->getOption( 'mode' ); + + $ns = $title->getNamespace(); + $key = $title->getDBkey(); + + $hideprefix = $this->getOption( 'hideprefix' ); + + if ( $hideprefix == CategoryTreeHidePrefix::ALWAYS ) { + $hideprefix = true; + } elseif ( $hideprefix == CategoryTreeHidePrefix::AUTO ) { + $hideprefix = ( $mode == CategoryTreeMode::CATEGORIES ); + } elseif ( $hideprefix == CategoryTreeHidePrefix::CATEGORIES ) { + $hideprefix = ( $ns == NS_CATEGORY ); + } else { + $hideprefix = true; + } + + // when showing only categories, omit namespace in label unless we explicitely defined the + // configuration setting + // patch contributed by Manuel Schneider <manuel.schneider@wikimedia.ch>, Bug 8011 + if ( $hideprefix ) { + $label = $title->getText(); + } else { + $label = $title->getPrefixedText(); + } + + $labelClass = 'CategoryTreeLabel ' . ' CategoryTreeLabelNs' . $ns; + + if ( !$title->getArticleID() ) { + $labelClass .= ' new'; + $wikiLink = $title->getLocalURL( 'action=edit&redlink=1' ); + } else { + $wikiLink = $title->getLocalURL(); + } + + if ( $ns == NS_CATEGORY ) { + $labelClass .= ' CategoryTreeLabelCategory'; + } else { + $labelClass .= ' CategoryTreeLabelPage'; + } + + if ( ( $ns % 2 ) > 0 ) { + $labelClass .= ' CategoryTreeLabelTalk'; + } + + $count = false; + $s = ''; + + # NOTE: things in CategoryTree.js rely on the exact order of tags! + # Specifically, the CategoryTreeChildren div must be the first + # sibling with nodeName = DIV of the grandparent of the expland link. + + $s .= Xml::openElement( 'div', [ 'class' => 'CategoryTreeSection' ] ); + $s .= Xml::openElement( 'div', [ 'class' => 'CategoryTreeItem' ] ); + + $attr = [ 'class' => 'CategoryTreeBullet' ]; + + if ( $ns == NS_CATEGORY ) { + if ( $cat ) { + if ( $mode == CategoryTreeMode::CATEGORIES ) { + $count = intval( $cat->getSubcatCount() ); + } elseif ( $mode == CategoryTreeMode::PAGES ) { + $count = intval( $cat->getPageCount() ) - intval( $cat->getFileCount() ); + } else { + $count = intval( $cat->getPageCount() ); + } + } + if ( $count === 0 ) { + $bullet = wfMessage( 'categorytree-empty-bullet' )->escaped() . ' '; + $attr['class'] = 'CategoryTreeEmptyBullet'; + } else { + $linkattr = []; + + $linkattr[ 'class' ] = "CategoryTreeToggle"; + $linkattr['data-ct-title'] = $key; + + $tag = 'span'; + if ( $children == 0 ) { + // Use ->plain() and htmlspecialchars() to ensure + // identical to what is done by JS, which does: + // $link.text( mw.msg( 'categorytree-expand-bullet' ) ) + $txt = htmlspecialchars( wfMessage( 'categorytree-expand-bullet' )->plain() ); + $linkattr[ 'data-ct-state' ] = 'collapsed'; + } else { + $txt = htmlspecialchars( wfMessage( 'categorytree-collapse-bullet' )->plain() ); + $linkattr[ 'data-ct-loaded' ] = true; + $linkattr[ 'data-ct-state' ] = 'expanded'; + } + + $bullet = Xml::openElement( $tag, $linkattr ) . $txt . Xml::closeElement( $tag ) . ' '; + } + } else { + $bullet = wfMessage( 'categorytree-page-bullet' )->escaped(); + } + $s .= Xml::tags( 'span', $attr, $bullet ) . ' '; + + $s .= Xml::element( + 'a', + [ + 'class' => $labelClass, + 'href' => $wikiLink, + 'title' => $title->getPrefixedText() + ], + $label + ); + + if ( $count !== false && $this->getOption( 'showcount' ) ) { + $s .= self::createCountString( RequestContext::getMain(), $cat, $count ); + } + + $s .= Xml::closeElement( 'div' ); + $s .= "\n\t\t"; + $s .= Xml::openElement( + 'div', + [ + 'class' => 'CategoryTreeChildren', + 'style' => $children > 0 ? "display:block" : "display:none" + ] + ); + + if ( $ns == NS_CATEGORY && $children > 0 ) { + $children = $this->renderChildren( $title, $children ); + if ( $children == '' ) { + $s .= Xml::openElement( 'i', [ 'class' => 'CategoryTreeNotice' ] ); + if ( $mode == CategoryTreeMode::CATEGORIES ) { + $s .= wfMessage( 'categorytree-no-subcategories' )->escaped(); + } elseif ( $mode == CategoryTreeMode::PAGES ) { + $s .= wfMessage( 'categorytree-no-pages' )->escaped(); + } elseif ( $mode == CategoryTreeMode::PARENTS ) { + $s .= wfMessage( 'categorytree-no-parent-categories' )->escaped(); + } else { + $s .= wfMessage( 'categorytree-nothing-found' )->escaped(); + } + $s .= Xml::closeElement( 'i' ); + } else { + $s .= $children; + } + } + + $s .= Xml::closeElement( 'div' ); + $s .= Xml::closeElement( 'div' ); + + $s .= "\n\t\t"; + + return $s; + } + + /** + * Create a string which format the page, subcat and file counts of a category + * @param IContextSource $context + * @param Category|null $cat + * @param int $countMode + * @return string + */ + public static function createCountString( IContextSource $context, $cat, $countMode ) { + global $wgContLang; + + # Get counts, with conversion to integer so === works + # Note: $allCount is the total number of cat members, + # not the count of how many members are normal pages. + $allCount = $cat ? intval( $cat->getPageCount() ) : 0; + $subcatCount = $cat ? intval( $cat->getSubcatCount() ) : 0; + $fileCount = $cat ? intval( $cat->getFileCount() ) : 0; + $pages = $allCount - $subcatCount - $fileCount; + + $attr = [ + 'title' => $context->msg( 'categorytree-member-counts' ) + ->numParams( $subcatCount, $pages, $fileCount, $allCount, $countMode )->text(), + 'dir' => $context->getLanguage()->getDir() # numbers and commas get messed up in a mixed dir env + ]; + + $s = $wgContLang->getDirMark() . ' '; + + # Create a list of category members with only non-zero member counts + $memberNums = []; + if ( $subcatCount ) { + $memberNums[] = $context->msg( 'categorytree-num-categories' ) + ->numParams( $subcatCount )->text(); + } + if ( $pages ) { + $memberNums[] = $context->msg( 'categorytree-num-pages' )->numParams( $pages )->text(); + } + if ( $fileCount ) { + $memberNums[] = $context->msg( 'categorytree-num-files' ) + ->numParams( $fileCount )->text(); + } + $memberNumsShort = $memberNums + ? $context->getLanguage()->commaList( $memberNums ) + : $context->msg( 'categorytree-num-empty' )->text(); + + # Only $5 is actually used in the default message. + # Other arguments can be used in a customized message. + $s .= Xml::tags( + 'span', + $attr, + $context->msg( 'categorytree-member-num' ) + // Do not use numParams on params 1-4, as they are only used for customisation. + ->params( $subcatCount, $pages, $fileCount, $allCount, $memberNumsShort ) + ->escaped() + ); + + return $s; + } + + /** + * Creates a Title object from a user provided (and thus unsafe) string + * @param string $title + * @return null|Title + */ + public static function makeTitle( $title ) { + $title = trim( $title ); + + if ( strval( $title ) === '' ) { + return null; + } + + # The title must be in the category namespace + # Ignore a leading Category: if there is one + $t = Title::newFromText( $title, NS_CATEGORY ); + if ( !$t || $t->getNamespace() != NS_CATEGORY || $t->getInterwiki() != '' ) { + // If we were given something like "Wikipedia:Foo" or "Template:", + // try it again but forced. + $title = "Category:$title"; + $t = Title::newFromText( $title ); + } + return $t; + } + + /** + * Internal function to cap depth + * @param string $mode + * @param int $depth + * @return int|mixed + */ + public static function capDepth( $mode, $depth ) { + global $wgCategoryTreeMaxDepth; + + if ( is_numeric( $depth ) ) { + $depth = intval( $depth ); + } else { + return 1; + } + + if ( is_array( $wgCategoryTreeMaxDepth ) ) { + $max = isset( $wgCategoryTreeMaxDepth[$mode] ) ? $wgCategoryTreeMaxDepth[$mode] : 1; + } elseif ( is_numeric( $wgCategoryTreeMaxDepth ) ) { + $max = $wgCategoryTreeMaxDepth; + } else { + wfDebug( 'CategoryTree::capDepth: $wgCategoryTreeMaxDepth is invalid.' ); + $max = 1; + } + + return min( $depth, $max ); + } +} diff --git a/www/wiki/extensions/CategoryTree/includes/CategoryTreeCategoryPage.php b/www/wiki/extensions/CategoryTree/includes/CategoryTreeCategoryPage.php new file mode 100644 index 00000000..77ab51a0 --- /dev/null +++ b/www/wiki/extensions/CategoryTree/includes/CategoryTreeCategoryPage.php @@ -0,0 +1,23 @@ +<?php +/** + * 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 + * + * @file + */ + +class CategoryTreeCategoryPage extends CategoryPage { + public $mCategoryViewerClass = CategoryTreeCategoryViewer::class; +} diff --git a/www/wiki/extensions/CategoryTree/includes/CategoryTreeCategoryViewer.php b/www/wiki/extensions/CategoryTree/includes/CategoryTreeCategoryViewer.php new file mode 100644 index 00000000..434d7222 --- /dev/null +++ b/www/wiki/extensions/CategoryTree/includes/CategoryTreeCategoryViewer.php @@ -0,0 +1,78 @@ +<?php +/** + * 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 + * + * @file + */ + +class CategoryTreeCategoryViewer extends CategoryViewer { + public $child_cats; + + /** + * @var CategoryTree + */ + public $categorytree; + + /** + * @return CategoryTree + */ + private function getCategoryTree() { + global $wgCategoryTreeCategoryPageOptions; + + if ( !isset( $this->categorytree ) ) { + if ( !CategoryTreeHooks::shouldForceHeaders() ) { + CategoryTree::setHeaders( $this->getOutput() ); + } + + $this->categorytree = new CategoryTree( $wgCategoryTreeCategoryPageOptions ); + } + + 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 ); + } + + public function clearCategoryState() { + $this->child_cats = []; + parent::clearCategoryState(); + } + + public function finaliseCategoryState() { + if ( $this->flip ) { + $this->child_cats = array_reverse( $this->child_cats ); + } + parent::finaliseCategoryState(); + } +} diff --git a/www/wiki/extensions/CategoryTree/includes/CategoryTreeHidePrefix.php b/www/wiki/extensions/CategoryTree/includes/CategoryTreeHidePrefix.php new file mode 100644 index 00000000..29d69b70 --- /dev/null +++ b/www/wiki/extensions/CategoryTree/includes/CategoryTreeHidePrefix.php @@ -0,0 +1,39 @@ +<?php +/** + * © 2006 Daniel Kinzler + * + * 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 + * + * @file + * @ingroup Extensions + * @author Daniel Kinzler, brightbyte.de + */ + +/** + * Constants for use with the hideprefix option, + * defining when the namespace prefix should be hidden. + */ +class CategoryTreeHidePrefix { + + const NEVER = 0; + + const ALWAYS = 10; + + const CATEGORIES = 20; + + const AUTO = 30; + +} diff --git a/www/wiki/extensions/CategoryTree/includes/CategoryTreeHooks.php b/www/wiki/extensions/CategoryTree/includes/CategoryTreeHooks.php new file mode 100644 index 00000000..a30d4932 --- /dev/null +++ b/www/wiki/extensions/CategoryTree/includes/CategoryTreeHooks.php @@ -0,0 +1,321 @@ +<?php +/** + * © 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 + * + * @file + * @ingroup Extensions + * @author Daniel Kinzler, brightbyte.de + */ + +/** + * Hooks for the CategoryTree extension, an AJAX based gadget + * to display the category structure of a wiki + */ +class CategoryTreeHooks { + + public static function shouldForceHeaders() { + global $wgCategoryTreeSidebarRoot, $wgCategoryTreeHijackPageCategories, + $wgCategoryTreeForceHeaders; + return $wgCategoryTreeForceHeaders || $wgCategoryTreeSidebarRoot + || $wgCategoryTreeHijackPageCategories; + } + + /** + * Adjusts config once MediaWiki is fully initialised + * TODO: Don't do this, lazy initialize the config + */ + public static function initialize() { + global $wgRequest; + global $wgCategoryTreeDefaultOptions, $wgCategoryTreeDefaultMode; + global $wgCategoryTreeCategoryPageOptions, $wgCategoryTreeCategoryPageMode; + global $wgCategoryTreeOmitNamespace; + + if ( !isset( $wgCategoryTreeDefaultOptions['mode'] ) + || is_null( $wgCategoryTreeDefaultOptions['mode'] ) + ) { + $wgCategoryTreeDefaultOptions['mode'] = $wgCategoryTreeDefaultMode; + } + + if ( !isset( $wgCategoryTreeDefaultOptions['hideprefix'] ) + || is_null( $wgCategoryTreeDefaultOptions['hideprefix'] ) + ) { + $wgCategoryTreeDefaultOptions['hideprefix'] = $wgCategoryTreeOmitNamespace; + } + + if ( !isset( $wgCategoryTreeCategoryPageOptions['mode'] ) + || is_null( $wgCategoryTreeCategoryPageOptions['mode'] ) + ) { + $mode = $wgRequest->getVal( 'mode' ); + $wgCategoryTreeCategoryPageOptions['mode'] = ( $mode ) + ? CategoryTree::decodeMode( $mode ) : $wgCategoryTreeCategoryPageMode; + } + } + + /** + * @param Parser $parser + */ + public static function setHooks( $parser ) { + global $wgCategoryTreeAllowTag; + if ( !$wgCategoryTreeAllowTag ) { + return; + } + $parser->setHook( 'categorytree', 'CategoryTreeHooks::parserHook' ); + $parser->setFunctionHook( 'categorytree', 'CategoryTreeHooks::parserFunction' ); + } + + /** + * Entry point for the {{#categorytree}} tag parser function. + * This is a wrapper around CategoryTreeHooks::parserHook + * @param Parser $parser + * @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 + $argv = []; + 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 ); + return [ $html, 'noparse' => true, 'isHTML' => true ]; + } + + /** + * Hook implementation for injecting a category tree into the sidebar. + * Only does anything if $wgCategoryTreeSidebarRoot is set to a category name. + * @param Skin $skin + * @param SkinTemplate $tpl + */ + public static function skinTemplateOutputPageBeforeExec( $skin, $tpl ) { + global $wgCategoryTreeSidebarRoot, $wgCategoryTreeSidebarOptions; + + if ( !$wgCategoryTreeSidebarRoot ) { + return; + } + + $html = self::parserHook( $wgCategoryTreeSidebarRoot, $wgCategoryTreeSidebarOptions ); + if ( $html ) { + $tpl->data['sidebar']['categorytree-portlet'] = $html; + } + } + + /** + * Entry point for the <categorytree> tag parser hook. + * This loads CategoryTreeFunctions.php and calls CategoryTree::getTag() + * @param string $cat + * @param array $argv + * @param Parser $parser + * @param bool $allowMissing + * @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' ); + + $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; + + $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 + * @param OutputPage $outputPage + * @param ParserOutput $parserOutput + */ + public static function parserOutput( $outputPage, $parserOutput ) { + if ( self::shouldForceHeaders() ) { + // Skip, we've already set the headers unconditionally + return; + } + if ( !empty( $parserOutput->mCategoryTreeTag ) ) { + CategoryTree::setHeaders( $outputPage ); + } + } + + /** + * BeforePageDisplay and BeforePageDisplayMobile hooks. + * These hooks are used when $wgCategoryTreeForceHeaders is set. + * Otherwise similar to CategoryTreeHooks::parserOutput. + * @param OutputPage $out + */ + public static function addHeaders( OutputPage $out ) { + if ( !self::shouldForceHeaders() ) { + return; + } + CategoryTree::setHeaders( $out ); + } + + /** + * ArticleFromTitle hook, override category page handling + * + * @param Title $title + * @param Article &$article + * @return bool + */ + public static function articleFromTitle( $title, &$article ) { + if ( $title->getNamespace() == NS_CATEGORY ) { + $article = new CategoryTreeCategoryPage( $title ); + } + return true; + } + + /** + * OutputPageMakeCategoryLinks hook, override category links + * @param OutputPage &$out + * @param array $categories + * @param array &$links + * @return bool + */ + public static function outputPageMakeCategoryLinks( &$out, $categories, &$links ) { + global $wgCategoryTreePageCategoryOptions, $wgCategoryTreeHijackPageCategories; + + if ( !$wgCategoryTreeHijackPageCategories ) { + // Not enabled, don't do anything + return true; + } + + foreach ( $categories as $category => $type ) { + $links[$type][] = self::parserHook( $category, $wgCategoryTreePageCategoryOptions, null, true ); + } + + return false; + } + + /** + * @param Skin $skin + * @param array &$links + * @param string &$result + * @return bool + */ + public static function skinJoinCategoryLinks( $skin, &$links, &$result ) { + global $wgCategoryTreeHijackPageCategories; + if ( !$wgCategoryTreeHijackPageCategories ) { + // Not enabled, don't do anything. + return true; + } + $embed = '<div class="CategoryTreeCategoryBarItem">'; + $pop = '</div>'; + $sep = ' '; + + $result = $embed . implode( "{$pop} {$sep} {$embed}", $links ) . $pop; + + return false; + } + + /** + * @param array &$vars + * @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; + } + + /** + * 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 ); + } +} diff --git a/www/wiki/extensions/CategoryTree/includes/CategoryTreeMode.php b/www/wiki/extensions/CategoryTree/includes/CategoryTreeMode.php new file mode 100644 index 00000000..48ff6133 --- /dev/null +++ b/www/wiki/extensions/CategoryTree/includes/CategoryTreeMode.php @@ -0,0 +1,38 @@ +<?php +/** + * © 2006 Daniel Kinzler + * + * 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 + * + * @file + * @ingroup Extensions + * @author Daniel Kinzler, brightbyte.de + */ + +/** + * Constants for use with the mode, defining what should be shown in the tree. + */ +class CategoryTreeMode { + + const CATEGORIES = 0; + + const PAGES = 10; + + const ALL = 20; + + const PARENTS = 100; + +} diff --git a/www/wiki/extensions/CategoryTree/includes/CategoryTreePage.php b/www/wiki/extensions/CategoryTree/includes/CategoryTreePage.php new file mode 100644 index 00000000..202e84e8 --- /dev/null +++ b/www/wiki/extensions/CategoryTree/includes/CategoryTreePage.php @@ -0,0 +1,214 @@ +<?php +/** + * © 2006 Daniel Kinzler + * + * 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 + * + * @file + * @ingroup Extensions + * @author Daniel Kinzler, brightbyte.de + */ + +/** + * Special page for the CategoryTree extension, an AJAX based gadget + * to display the category structure of a wiki + */ +class CategoryTreePage extends SpecialPage { + public $target = ''; + + /** + * @var CategoryTree + */ + public $tree = null; + + public function __construct() { + parent::__construct( 'CategoryTree' ); + } + + /** + * @param string $name + * @return mixed + */ + private function getOption( $name ) { + global $wgCategoryTreeDefaultOptions; + + if ( $this->tree ) { + return $this->tree->getOption( $name ); + } else { + return $wgCategoryTreeDefaultOptions[$name]; + } + } + + /** + * Main execution function + * @param string|null $par Parameters passed to the page + */ + public function execute( $par ) { + global $wgCategoryTreeDefaultOptions, $wgCategoryTreeSpecialPageOptions; + + $this->setHeaders(); + $request = $this->getRequest(); + if ( $par ) { + $this->target = $par; + } else { + $this->target = $request->getVal( 'target' ); + if ( $this->target === null ) { + $rootcategory = $this->msg( 'rootcategory' ); + if ( $rootcategory->exists() ) { + $this->target = $rootcategory->text(); + } + } + } + + $this->target = trim( $this->target ); + + $options = []; + + # grab all known options from the request. Normalization is done by the CategoryTree class + foreach ( $wgCategoryTreeDefaultOptions as $option => $default ) { + if ( isset( $wgCategoryTreeSpecialPageOptions[$option] ) ) { + $default = $wgCategoryTreeSpecialPageOptions[$option]; + } + + $options[$option] = $request->getVal( $option, $default ); + } + + $this->tree = new CategoryTree( $options ); + + $output = $this->getOutput(); + $output->addWikiMsg( 'categorytree-header' ); + + $this->executeInputForm(); + + if ( $this->target !== '' && $this->target !== null ) { + if ( !CategoryTreeHooks::shouldForceHeaders() ) { + CategoryTree::setHeaders( $output ); + } + + $title = CategoryTree::makeTitle( $this->target ); + + if ( $title && $title->getArticleID() ) { + $output->addHTML( Xml::openElement( 'div', [ 'class' => 'CategoryTreeParents' ] ) ); + $output->addHTML( $this->msg( 'categorytree-parents' )->parse() ); + $output->addHTML( $this->msg( 'colon-separator' )->escaped() ); + + $parents = $this->tree->renderParents( $title ); + + if ( $parents == '' ) { + $output->addHTML( $this->msg( 'categorytree-no-parent-categories' )->parse() ); + } else { + $output->addHTML( $parents ); + } + + $output->addHTML( Xml::closeElement( 'div' ) ); + + $output->addHTML( Xml::openElement( 'div', [ 'class' => 'CategoryTreeResult' ] ) ); + $output->addHTML( $this->tree->renderNode( $title, 1 ) ); + $output->addHTML( Xml::closeElement( 'div' ) ); + } else { + $output->addHTML( Xml::openElement( 'div', [ 'class' => 'CategoryTreeNotice' ] ) ); + $output->addHTML( $this->msg( 'categorytree-not-found', $this->target )->parse() ); + $output->addHTML( Xml::closeElement( 'div' ) ); + } + } + } + + /** + * Input form for entering a category + */ + private function executeInputForm() { + $namespaces = $this->getRequest()->getVal( 'namespaces', '' ); + // mode may be overriden by namespaces option + $mode = ( $namespaces == '' ? $this->getOption( 'mode' ) : CategoryTreeMode::ALL ); + if ( $mode == CategoryTreeMode::CATEGORIES ) { + $modeDefault = 'categories'; + } elseif ( $mode == CategoryTreeMode::PAGES ) { + $modeDefault = 'pages'; + } else { + $modeDefault = 'all'; + } + + $formDescriptor = [ + 'category' => [ + 'type' => 'title', + 'name' => 'target', + 'label-message' => 'categorytree-category', + 'namespace' => NS_CATEGORY, + ], + + 'mode' => [ + 'type' => 'select', + 'name' => 'mode', + 'label-message' => 'categorytree-mode-label', + 'options-messages' => [ + 'categorytree-mode-categories' => 'categories', + 'categorytree-mode-pages' => 'pages', + 'categorytree-mode-all' => 'all', + ], + 'default' => $modeDefault, + 'nodata' => true, + ], + + 'namespace' => [ + 'type' => 'namespaceselect', + 'name' => 'namespaces', + 'label-message' => 'namespace', + 'all' => '', + ], + ]; + + HTMLForm::factory( 'ooui', $formDescriptor, $this->getContext() ) + ->addHiddenFields( [ 'title' => $this->getPageTitle()->getPrefixedDbKey() ] ) + ->setWrapperLegendMsg( 'categorytree-legend' ) + ->setSubmitTextMsg( 'categorytree-go' ) + ->setMethod( 'get' ) + ->prepareForm() + ->displayForm( false ); + } + + /** + * Return an array of subpages beginning with $search that this special page will accept. + * + * @param string $search Prefix to search for + * @param int $limit Maximum number of results to return (usually 10) + * @param int $offset Number of results to skip (usually 0) + * @return string[] Matching subpages + */ + public function prefixSearchSubpages( $search, $limit, $offset ) { + $title = Title::newFromText( $search, NS_CATEGORY ); + if ( $title && $title->getNamespace() !== NS_CATEGORY ) { + // Someone searching for something like "Wikipedia:Foo" + $title = Title::makeTitleSafe( NS_CATEGORY, $search ); + } + if ( !$title ) { + // No prefix suggestion outside of category namespace + return []; + } + // Autocomplete subpage the same as a normal search, but just for categories + $prefixSearcher = new TitlePrefixSearch; + $result = $prefixSearcher->search( $title->getPrefixedText(), $limit, [ NS_CATEGORY ], $offset ); + + return array_map( function ( Title $t ) { + // Remove namespace in search suggestion + return $t->getText(); + }, $result ); + } + + protected function getGroupName() { + return 'pages'; + } + +} |