2006-07-26 17:12:30 +00:00
|
|
|
<?php
|
|
|
|
/**
|
2018-04-16 06:50:28 +00:00
|
|
|
* © 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
|
2006-07-26 17:12:30 +00:00
|
|
|
*
|
2010-06-06 15:12:22 +00:00
|
|
|
* @file
|
|
|
|
* @ingroup Extensions
|
2007-03-13 11:39:12 +00:00
|
|
|
* @author Daniel Kinzler, brightbyte.de
|
2006-07-26 17:12:30 +00:00
|
|
|
*/
|
2021-04-07 22:35:52 +00:00
|
|
|
|
|
|
|
namespace MediaWiki\Extension\CategoryTree;
|
|
|
|
|
|
|
|
use ExtensionRegistry;
|
|
|
|
use IContextSource;
|
2023-05-12 14:33:05 +00:00
|
|
|
use MediaWiki\Category\Category;
|
2023-10-28 19:40:51 +00:00
|
|
|
use MediaWiki\Config\Config;
|
2023-10-03 09:30:38 +00:00
|
|
|
use MediaWiki\Html\Html;
|
2016-05-30 10:01:38 +00:00
|
|
|
use MediaWiki\Linker\LinkRenderer;
|
2019-07-31 05:46:31 +00:00
|
|
|
use MediaWiki\MediaWikiServices;
|
2023-10-03 09:30:38 +00:00
|
|
|
use MediaWiki\Output\OutputPage;
|
|
|
|
use MediaWiki\SpecialPage\SpecialPage;
|
2023-08-16 01:54:29 +00:00
|
|
|
use MediaWiki\Title\Title;
|
2021-04-07 22:35:52 +00:00
|
|
|
use Parser;
|
|
|
|
use RequestContext;
|
2023-11-01 14:11:34 +00:00
|
|
|
use Wikimedia\Rdbms\IConnectionProvider;
|
2019-07-31 05:46:31 +00:00
|
|
|
|
2018-04-16 06:50:28 +00:00
|
|
|
/**
|
|
|
|
* Core functions for the CategoryTree extension, an AJAX based gadget
|
|
|
|
* to display the category structure of a wiki
|
|
|
|
*/
|
2006-08-24 17:12:13 +00:00
|
|
|
class CategoryTree {
|
2023-10-28 18:11:36 +00:00
|
|
|
/** @var OptionManager */
|
|
|
|
public $optionManager;
|
2016-05-30 10:01:38 +00:00
|
|
|
|
2023-10-28 19:40:51 +00:00
|
|
|
/** @var Config */
|
|
|
|
private $config;
|
|
|
|
|
2023-11-01 14:11:34 +00:00
|
|
|
/** @var IConnectionProvider */
|
|
|
|
private $dbProvider;
|
|
|
|
|
2022-11-10 19:49:27 +00:00
|
|
|
/** @var LinkRenderer */
|
2019-07-31 05:46:31 +00:00
|
|
|
private $linkRenderer;
|
2008-06-28 20:13:20 +00:00
|
|
|
|
2012-02-09 01:23:31 +00:00
|
|
|
/**
|
2017-10-11 18:25:13 +00:00
|
|
|
* @param array $options
|
2023-10-28 19:40:51 +00:00
|
|
|
* @param Config $config
|
2023-11-01 14:11:34 +00:00
|
|
|
* @param IConnectionProvider $dbProvider
|
2023-10-26 20:22:10 +00:00
|
|
|
* @param LinkRenderer $linkRenderer
|
2012-02-09 01:23:31 +00:00
|
|
|
*/
|
2023-10-26 20:22:10 +00:00
|
|
|
public function __construct(
|
|
|
|
array $options,
|
2023-10-28 19:40:51 +00:00
|
|
|
Config $config,
|
2023-11-01 14:11:34 +00:00
|
|
|
IConnectionProvider $dbProvider,
|
2023-10-26 20:22:10 +00:00
|
|
|
LinkRenderer $linkRenderer
|
|
|
|
) {
|
2023-10-28 19:40:51 +00:00
|
|
|
$this->optionManager = new OptionManager( $options, $config );
|
|
|
|
$this->config = $config;
|
2023-11-01 14:11:34 +00:00
|
|
|
$this->dbProvider = $dbProvider;
|
2023-10-26 20:22:10 +00:00
|
|
|
$this->linkRenderer = $linkRenderer;
|
2008-06-30 21:22:03 +00:00
|
|
|
}
|
|
|
|
|
2006-08-24 17:12:13 +00:00
|
|
|
/**
|
2014-10-31 05:40:56 +00:00
|
|
|
* Add ResourceLoader modules to the OutputPage object
|
2008-02-04 09:22:12 +00:00
|
|
|
* @param OutputPage $outputPage
|
2006-08-24 17:12:13 +00:00
|
|
|
*/
|
2018-12-16 09:47:07 +00:00
|
|
|
public static function setHeaders( OutputPage $outputPage ) {
|
2012-02-08 14:07:10 +00:00
|
|
|
# Add the modules
|
2018-12-08 23:12:18 +00:00
|
|
|
$outputPage->addModuleStyles( 'ext.categoryTree.styles' );
|
2011-09-30 02:51:53 +00:00
|
|
|
$outputPage->addModules( 'ext.categoryTree' );
|
2008-02-04 09:22:12 +00:00
|
|
|
}
|
2006-07-29 09:18:34 +00:00
|
|
|
|
2006-08-24 17:12:13 +00:00
|
|
|
/**
|
2021-04-07 22:35:52 +00:00
|
|
|
* Custom tag implementation. This is called by Hooks::parserHook, which is used to
|
2012-01-14 15:27:30 +00:00
|
|
|
* load CategoryTreeFunctions.php on demand.
|
2019-10-12 20:14:41 +00:00
|
|
|
* @param ?Parser $parser
|
2017-10-11 18:25:13 +00:00
|
|
|
* @param string $category
|
|
|
|
* @param bool $hideroot
|
2018-04-09 01:50:31 +00:00
|
|
|
* @param array $attr
|
2017-10-11 18:25:13 +00:00
|
|
|
* @param int $depth
|
|
|
|
* @param bool $allowMissing
|
2012-01-14 15:27:30 +00:00
|
|
|
* @return bool|string
|
|
|
|
*/
|
2019-10-12 20:14:41 +00:00
|
|
|
public function getTag( ?Parser $parser, $category, $hideroot = false, array $attr = [],
|
2018-12-16 09:47:07 +00:00
|
|
|
$depth = 1, $allowMissing = false
|
2017-05-30 18:21:54 +00:00
|
|
|
) {
|
2023-10-28 19:40:51 +00:00
|
|
|
$disableCache = $this->config->get( 'CategoryTreeDisableCache' );
|
2006-08-24 17:12:13 +00:00
|
|
|
|
|
|
|
$category = trim( $category );
|
|
|
|
if ( $category === '' ) {
|
|
|
|
return false;
|
|
|
|
}
|
2010-04-20 22:00:34 +00:00
|
|
|
|
2014-02-19 13:10:24 +00:00
|
|
|
if ( $parser ) {
|
2023-10-28 19:40:51 +00:00
|
|
|
if ( $disableCache === true ) {
|
2019-04-20 15:00:53 +00:00
|
|
|
$parser->getOutput()->updateCacheExpiry( 0 );
|
2023-10-28 19:40:51 +00:00
|
|
|
} elseif ( is_int( $disableCache ) ) {
|
|
|
|
$parser->getOutput()->updateCacheExpiry( $disableCache );
|
2014-02-19 13:10:24 +00:00
|
|
|
}
|
2006-08-24 17:12:13 +00:00
|
|
|
}
|
2008-02-04 09:22:12 +00:00
|
|
|
|
2010-04-20 22:00:34 +00:00
|
|
|
$title = self::makeTitle( $category );
|
2008-02-04 09:22:12 +00:00
|
|
|
|
2021-09-02 09:35:57 +00:00
|
|
|
if ( $title === null ) {
|
2010-04-20 22:00:34 +00:00
|
|
|
return false;
|
|
|
|
}
|
2008-06-30 21:35:01 +00:00
|
|
|
|
2010-04-20 22:00:34 +00:00
|
|
|
if ( isset( $attr['class'] ) ) {
|
|
|
|
$attr['class'] .= ' CategoryTreeTag';
|
|
|
|
} else {
|
2021-09-02 09:35:57 +00:00
|
|
|
$attr['class'] = 'CategoryTreeTag';
|
2010-04-20 22:00:34 +00:00
|
|
|
}
|
2008-07-04 20:05:29 +00:00
|
|
|
|
2023-10-28 18:11:36 +00:00
|
|
|
$attr['data-ct-mode'] = $this->optionManager->getOption( 'mode' );
|
|
|
|
$attr['data-ct-options'] = $this->optionManager->getOptionsAsJsStructure();
|
2011-09-30 21:08:28 +00:00
|
|
|
|
2008-07-02 20:19:54 +00:00
|
|
|
if ( !$allowMissing && !$title->getArticleID() ) {
|
2021-08-31 11:18:30 +00:00
|
|
|
$html = Html::rawElement( 'span', [ 'class' => 'CategoryTreeNotice' ],
|
|
|
|
wfMessage( 'categorytree-not-found' )
|
|
|
|
->plaintextParams( $category )
|
|
|
|
->parse()
|
|
|
|
);
|
2017-05-30 18:21:54 +00:00
|
|
|
} else {
|
2010-04-20 22:00:34 +00:00
|
|
|
if ( !$hideroot ) {
|
2021-08-31 11:18:30 +00:00
|
|
|
$html = $this->renderNode( $title, $depth );
|
2010-04-20 22:00:34 +00:00
|
|
|
} else {
|
2021-08-31 11:18:30 +00:00
|
|
|
$html = $this->renderChildren( $title, $depth );
|
2006-08-24 17:12:13 +00:00
|
|
|
}
|
2006-07-29 09:18:34 +00:00
|
|
|
}
|
2008-02-04 09:22:12 +00:00
|
|
|
|
2021-08-31 11:18:30 +00:00
|
|
|
return Html::rawElement( 'div', $attr, $html );
|
2006-07-26 17:12:30 +00:00
|
|
|
}
|
2006-08-24 17:12:13 +00:00
|
|
|
|
|
|
|
/**
|
2012-02-09 01:23:31 +00:00
|
|
|
* Returns a string with an HTML representation of the children of the given category.
|
2017-10-11 18:25:13 +00:00
|
|
|
* @param Title $title
|
|
|
|
* @param int $depth
|
2019-07-29 19:19:36 +00:00
|
|
|
* @suppress PhanUndeclaredClassMethod,PhanUndeclaredClassInstanceof
|
2012-02-09 01:23:31 +00:00
|
|
|
* @return string
|
|
|
|
*/
|
2018-12-16 09:47:07 +00:00
|
|
|
public function renderChildren( Title $title, $depth = 1 ) {
|
2021-09-02 09:35:57 +00:00
|
|
|
if ( !$title->inNamespace( NS_CATEGORY ) ) {
|
2007-09-18 15:52:30 +00:00
|
|
|
// Non-categories can't have children. :)
|
|
|
|
return '';
|
|
|
|
}
|
2008-02-04 09:22:12 +00:00
|
|
|
|
2023-11-01 14:11:34 +00:00
|
|
|
$dbr = $this->dbProvider->getReplicaDatabase();
|
2006-08-24 17:12:13 +00:00
|
|
|
|
2023-10-28 18:11:36 +00:00
|
|
|
$inverse = $this->optionManager->isInverse();
|
|
|
|
$mode = $this->optionManager->getOption( 'mode' );
|
|
|
|
$namespaces = $this->optionManager->getOption( 'namespaces' );
|
2008-02-04 09:22:12 +00:00
|
|
|
|
2024-04-25 18:21:57 +00:00
|
|
|
$queryBuilder = $dbr->newSelectQueryBuilder()
|
|
|
|
->select( [
|
|
|
|
'page_id', 'page_namespace', 'page_title',
|
|
|
|
'page_is_redirect', 'page_len', 'page_latest', 'cl_to', 'cl_from'
|
|
|
|
] )
|
|
|
|
->orderBy( [ 'cl_type', 'cl_sortkey' ] )
|
|
|
|
->limit( $this->config->get( 'CategoryTreeMaxChildren' ) )
|
|
|
|
->caller( __METHOD__ );
|
2010-08-13 22:38:00 +00:00
|
|
|
|
2008-07-02 09:49:28 +00:00
|
|
|
if ( $inverse ) {
|
2024-04-25 18:21:57 +00:00
|
|
|
$queryBuilder
|
|
|
|
->from( 'categorylinks' )
|
|
|
|
->leftJoin( 'page', null, [
|
|
|
|
'cl_to = page_title', 'page_namespace' => NS_CATEGORY
|
|
|
|
] )
|
|
|
|
->where( [ 'cl_from' => $title->getArticleID() ] );
|
2010-08-13 22:38:00 +00:00
|
|
|
} else {
|
2024-04-25 18:21:57 +00:00
|
|
|
$queryBuilder
|
|
|
|
->from( 'page' )
|
|
|
|
->join( 'categorylinks', null, 'cl_from = page_id' )
|
|
|
|
->where( [ 'cl_to' => $title->getDBkey() ] )
|
|
|
|
->useIndex( 'cl_sortkey' );
|
2008-07-02 09:49:28 +00:00
|
|
|
|
2010-04-20 22:00:34 +00:00
|
|
|
# namespace filter.
|
2008-07-02 09:49:28 +00:00
|
|
|
if ( $namespaces ) {
|
2017-05-30 18:21:54 +00:00
|
|
|
// NOTE: we assume that the $namespaces array contains only integers!
|
|
|
|
// decodeNamepsaces makes it so.
|
2024-04-25 18:21:57 +00:00
|
|
|
$queryBuilder->where( [ 'page_namespace' => $namespaces ] );
|
2021-09-02 08:33:30 +00:00
|
|
|
} elseif ( $mode !== CategoryTreeMode::ALL ) {
|
|
|
|
if ( $mode === CategoryTreeMode::PAGES ) {
|
2024-04-25 18:21:57 +00:00
|
|
|
$queryBuilder->where( [ 'cl_type' => [ 'page', 'subcat' ] ] );
|
2010-04-20 22:00:34 +00:00
|
|
|
} else {
|
2024-04-25 18:21:57 +00:00
|
|
|
$queryBuilder->where( [ 'cl_type' => 'subcat' ] );
|
2010-04-20 22:00:34 +00:00
|
|
|
}
|
2008-07-02 09:49:28 +00:00
|
|
|
}
|
2008-07-01 21:40:42 +00:00
|
|
|
}
|
2008-02-04 09:22:12 +00:00
|
|
|
|
2008-06-30 14:09:47 +00:00
|
|
|
# fetch member count if possible
|
2023-10-28 19:40:51 +00:00
|
|
|
$doCount = !$inverse && $this->config->get( 'CategoryTreeUseCategoryTable' );
|
2008-06-30 14:09:47 +00:00
|
|
|
|
|
|
|
if ( $doCount ) {
|
2024-04-25 18:21:57 +00:00
|
|
|
$queryBuilder
|
|
|
|
->leftJoin( 'category', null, [ 'cat_title = page_title', 'page_namespace' => NS_CATEGORY ] )
|
|
|
|
->fields( [ 'cat_id', 'cat_title', 'cat_subcats', 'cat_pages', 'cat_files' ] );
|
2008-06-30 14:09:47 +00:00
|
|
|
}
|
|
|
|
|
2024-04-25 18:21:57 +00:00
|
|
|
$res = $queryBuilder->fetchResultSet();
|
2008-02-04 09:22:12 +00:00
|
|
|
|
2010-04-20 22:00:34 +00:00
|
|
|
# collect categories separately from other pages
|
|
|
|
$categories = '';
|
|
|
|
$other = '';
|
2023-10-28 18:11:36 +00:00
|
|
|
$suppressTranslations = OptionManager::decodeBoolean(
|
|
|
|
$this->optionManager->getOption( 'notranslations' )
|
2019-07-29 19:19:36 +00:00
|
|
|
) && ExtensionRegistry::getInstance()->isLoaded( 'Translate' );
|
|
|
|
|
|
|
|
if ( $suppressTranslations ) {
|
2022-12-19 20:34:02 +00:00
|
|
|
$lb = MediaWikiServices::getInstance()->getLinkBatchFactory()->newLinkBatch();
|
2019-07-29 19:19:36 +00:00
|
|
|
foreach ( $res as $row ) {
|
|
|
|
$title = Title::newFromText( $row->page_title, $row->page_namespace );
|
|
|
|
// Page name could have slashes, check the subpage for valid language built-in codes
|
2022-11-10 19:27:04 +00:00
|
|
|
if ( $title !== null && $title->getSubpageText() ) {
|
2019-07-29 19:19:36 +00:00
|
|
|
$lb->addObj( $title->getBaseTitle() );
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
$lb->execute();
|
|
|
|
}
|
2008-02-04 09:22:12 +00:00
|
|
|
|
2010-08-13 22:38:00 +00:00
|
|
|
foreach ( $res as $row ) {
|
2019-07-29 19:19:36 +00:00
|
|
|
if ( $suppressTranslations ) {
|
|
|
|
$title = Title::newFromRow( $row );
|
|
|
|
$baseTitle = $title->getBaseTitle();
|
|
|
|
$page = \TranslatablePage::isTranslationPage( $title );
|
|
|
|
|
|
|
|
if ( ( $page instanceof \TranslatablePage ) && $baseTitle->exists() ) {
|
|
|
|
// T229265: Render only the default pages created and ignore their
|
|
|
|
// translations.
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2010-04-20 22:00:34 +00:00
|
|
|
# NOTE: in inverse mode, the page record may be null, because we use a right join.
|
2008-07-02 20:19:54 +00:00
|
|
|
# happens for categories with no category page (red cat links)
|
2010-01-06 21:24:10 +00:00
|
|
|
if ( $inverse && $row->page_title === null ) {
|
2008-07-02 20:19:54 +00:00
|
|
|
$t = Title::makeTitle( NS_CATEGORY, $row->cl_to );
|
2010-04-20 22:00:34 +00:00
|
|
|
} else {
|
|
|
|
# TODO: translation support; ideally added to Title object
|
2008-07-02 20:19:54 +00:00
|
|
|
$t = Title::newFromRow( $row );
|
|
|
|
}
|
2007-09-01 11:26:00 +00:00
|
|
|
|
2010-01-06 21:24:10 +00:00
|
|
|
$cat = null;
|
2008-06-30 14:09:47 +00:00
|
|
|
|
2021-09-02 08:33:30 +00:00
|
|
|
if ( $doCount && (int)$row->page_namespace === NS_CATEGORY ) {
|
2008-06-30 14:09:47 +00:00
|
|
|
$cat = Category::newFromRow( $row, $t );
|
|
|
|
}
|
|
|
|
|
2014-02-19 13:10:24 +00:00
|
|
|
$s = $this->renderNodeInfo( $t, $cat, $depth - 1 );
|
2007-09-01 11:26:00 +00:00
|
|
|
|
2021-09-02 08:33:30 +00:00
|
|
|
if ( (int)$row->page_namespace === NS_CATEGORY ) {
|
2010-04-20 22:00:34 +00:00
|
|
|
$categories .= $s;
|
|
|
|
} else {
|
|
|
|
$other .= $s;
|
|
|
|
}
|
2006-08-24 17:12:13 +00:00
|
|
|
}
|
2008-02-04 09:22:12 +00:00
|
|
|
|
2007-09-01 11:26:00 +00:00
|
|
|
return $categories . $other;
|
2006-08-24 17:12:13 +00:00
|
|
|
}
|
2006-07-26 17:12:30 +00:00
|
|
|
|
2006-08-24 17:12:13 +00:00
|
|
|
/**
|
2012-02-09 01:23:31 +00:00
|
|
|
* Returns a string with an HTML representation of the parents of the given category.
|
2017-10-11 18:25:13 +00:00
|
|
|
* @param Title $title
|
2012-02-09 01:23:31 +00:00
|
|
|
* @return string
|
|
|
|
*/
|
2018-12-16 09:47:07 +00:00
|
|
|
public function renderParents( Title $title ) {
|
2023-11-01 14:11:34 +00:00
|
|
|
$dbr = $this->dbProvider->getReplicaDatabase();
|
2008-02-04 09:22:12 +00:00
|
|
|
|
2024-04-25 18:21:57 +00:00
|
|
|
$res = $dbr->newSelectQueryBuilder()
|
|
|
|
->select( 'cl_to' )
|
|
|
|
->from( 'categorylinks' )
|
|
|
|
->where( [ 'cl_from' => $title->getArticleID() ] )
|
|
|
|
->limit( $this->config->get( 'CategoryTreeMaxChildren' ) )
|
|
|
|
->orderBy( 'cl_to' )
|
|
|
|
->caller( __METHOD__ )
|
|
|
|
->fetchResultSet();
|
2008-02-04 09:22:12 +00:00
|
|
|
|
2010-06-08 19:30:48 +00:00
|
|
|
$special = SpecialPage::getTitleFor( 'CategoryTree' );
|
2008-02-04 09:22:12 +00:00
|
|
|
|
2021-08-31 11:18:30 +00:00
|
|
|
$s = [];
|
2008-02-04 09:22:12 +00:00
|
|
|
|
2010-10-29 21:30:20 +00:00
|
|
|
foreach ( $res as $row ) {
|
2016-05-30 10:01:38 +00:00
|
|
|
$t = Title::makeTitle( NS_CATEGORY, $row->cl_to );
|
2008-02-04 09:22:12 +00:00
|
|
|
|
2021-08-31 11:18:30 +00:00
|
|
|
$s[] = Html::rawElement( 'span', [ 'class' => 'CategoryTreeItem' ],
|
|
|
|
$this->linkRenderer->makeLink(
|
|
|
|
$special,
|
|
|
|
$t->getText(),
|
|
|
|
[ 'class' => 'CategoryTreeLabel' ],
|
2023-10-28 18:11:36 +00:00
|
|
|
[ 'target' => $t->getDBkey() ] + $this->optionManager->getOptions()
|
2021-08-31 11:18:30 +00:00
|
|
|
)
|
2016-05-30 10:01:38 +00:00
|
|
|
);
|
2006-08-24 17:12:13 +00:00
|
|
|
}
|
2008-02-04 09:22:12 +00:00
|
|
|
|
2021-08-31 11:18:30 +00:00
|
|
|
return implode( wfMessage( 'pipe-separator' )->escaped(), $s );
|
2006-08-24 17:12:13 +00:00
|
|
|
}
|
2006-07-26 17:12:30 +00:00
|
|
|
|
2006-08-24 17:12:13 +00:00
|
|
|
/**
|
2012-02-09 01:23:31 +00:00
|
|
|
* Returns a string with a HTML represenation of the given page.
|
2017-10-11 18:25:13 +00:00
|
|
|
* @param Title $title
|
2012-02-09 01:23:31 +00:00
|
|
|
* @param int $children
|
|
|
|
* @return string
|
|
|
|
*/
|
2018-12-16 09:47:07 +00:00
|
|
|
public function renderNode( Title $title, $children = 0 ) {
|
2023-10-28 19:40:51 +00:00
|
|
|
if ( $this->config->get( 'CategoryTreeUseCategoryTable' )
|
|
|
|
&& $title->inNamespace( NS_CATEGORY )
|
2023-10-28 18:11:36 +00:00
|
|
|
&& !$this->optionManager->isInverse()
|
2017-05-30 18:21:54 +00:00
|
|
|
) {
|
2008-07-02 09:49:28 +00:00
|
|
|
$cat = Category::newFromTitle( $title );
|
2010-04-20 22:00:34 +00:00
|
|
|
} else {
|
|
|
|
$cat = null;
|
2008-07-02 09:49:28 +00:00
|
|
|
}
|
2008-06-30 14:09:47 +00:00
|
|
|
|
2014-02-19 13:10:24 +00:00
|
|
|
return $this->renderNodeInfo( $title, $cat, $children );
|
2008-06-30 14:09:47 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2012-02-09 01:23:31 +00:00
|
|
|
* 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.
|
2017-10-11 18:25:13 +00:00
|
|
|
* @param Title $title
|
2018-12-16 09:47:07 +00:00
|
|
|
* @param Category|null $cat
|
2017-10-11 18:25:13 +00:00
|
|
|
* @param int $children
|
2012-02-09 01:23:31 +00:00
|
|
|
* @return string
|
|
|
|
*/
|
2018-12-16 09:47:07 +00:00
|
|
|
public function renderNodeInfo( Title $title, Category $cat = null, $children = 0 ) {
|
2023-10-28 18:11:36 +00:00
|
|
|
$mode = $this->optionManager->getOption( 'mode' );
|
2008-02-04 09:22:12 +00:00
|
|
|
|
2021-09-02 09:35:57 +00:00
|
|
|
$isInCatNS = $title->inNamespace( NS_CATEGORY );
|
2008-01-14 10:09:08 +00:00
|
|
|
$key = $title->getDBkey();
|
2008-02-04 09:22:12 +00:00
|
|
|
|
2023-10-28 18:11:36 +00:00
|
|
|
$hideprefix = $this->optionManager->getOption( 'hideprefix' );
|
2008-06-30 21:22:03 +00:00
|
|
|
|
2021-09-02 08:33:30 +00:00
|
|
|
if ( $hideprefix === CategoryTreeHidePrefix::ALWAYS ) {
|
2010-04-20 22:00:34 +00:00
|
|
|
$hideprefix = true;
|
2021-09-02 08:33:30 +00:00
|
|
|
} elseif ( $hideprefix === CategoryTreeHidePrefix::AUTO ) {
|
|
|
|
$hideprefix = ( $mode === CategoryTreeMode::CATEGORIES );
|
|
|
|
} elseif ( $hideprefix === CategoryTreeHidePrefix::CATEGORIES ) {
|
2021-09-02 09:35:57 +00:00
|
|
|
$hideprefix = $isInCatNS;
|
2010-04-20 22:00:34 +00:00
|
|
|
} else {
|
|
|
|
$hideprefix = true;
|
|
|
|
}
|
2008-06-30 21:22:03 +00:00
|
|
|
|
2017-05-30 18:21:54 +00:00
|
|
|
// 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
|
2010-04-20 22:00:34 +00:00
|
|
|
if ( $hideprefix ) {
|
2018-04-18 06:23:38 +00:00
|
|
|
$label = $title->getText();
|
2010-04-20 22:00:34 +00:00
|
|
|
} else {
|
2018-04-18 06:23:38 +00:00
|
|
|
$label = $title->getPrefixedText();
|
2010-04-20 22:00:34 +00:00
|
|
|
}
|
2009-05-27 06:17:56 +00:00
|
|
|
|
2019-07-31 05:46:31 +00:00
|
|
|
$link = $this->linkRenderer->makeLink( $title, $label );
|
2008-02-04 09:22:12 +00:00
|
|
|
|
2008-06-30 14:09:47 +00:00
|
|
|
$count = false;
|
2006-08-24 17:12:13 +00:00
|
|
|
$s = '';
|
2008-02-04 09:22:12 +00:00
|
|
|
|
2010-04-20 22:00:34 +00:00
|
|
|
# NOTE: things in CategoryTree.js rely on the exact order of tags!
|
2006-08-24 17:12:13 +00:00
|
|
|
# Specifically, the CategoryTreeChildren div must be the first
|
|
|
|
# sibling with nodeName = DIV of the grandparent of the expland link.
|
2008-02-04 09:22:12 +00:00
|
|
|
|
2021-08-31 11:18:30 +00:00
|
|
|
$s .= Html::openElement( 'div', [ 'class' => 'CategoryTreeSection' ] );
|
|
|
|
$s .= Html::openElement( 'div', [ 'class' => 'CategoryTreeItem' ] );
|
2008-02-04 09:22:12 +00:00
|
|
|
|
2016-10-14 16:11:52 +00:00
|
|
|
$attr = [ 'class' => 'CategoryTreeBullet' ];
|
2008-06-28 20:13:20 +00:00
|
|
|
|
2021-09-02 09:35:57 +00:00
|
|
|
if ( $isInCatNS ) {
|
2008-06-30 14:09:47 +00:00
|
|
|
if ( $cat ) {
|
2021-09-02 08:33:30 +00:00
|
|
|
if ( $mode === CategoryTreeMode::CATEGORIES ) {
|
2022-02-08 05:38:58 +00:00
|
|
|
$count = $cat->getSubcatCount();
|
2021-09-02 08:33:30 +00:00
|
|
|
} elseif ( $mode === CategoryTreeMode::PAGES ) {
|
2022-02-08 05:38:58 +00:00
|
|
|
$count = $cat->getMemberCount() - $cat->getFileCount();
|
2010-04-20 22:00:34 +00:00
|
|
|
} else {
|
2022-02-08 05:38:58 +00:00
|
|
|
$count = $cat->getMemberCount();
|
2010-04-20 22:00:34 +00:00
|
|
|
}
|
|
|
|
}
|
Adapt CategoryTree to the new schema
This should obsolete $wgCategoryTreeMaxScanRows, added in r67179, so I
removed it. Note that I only tested with very basic usage, since I
don't quite understand all the complicated things this extension can do,
and some code paths are certainly going to remain inefficient, since
arbitrary namespace filtering seems possible here (at least
renderChildren() has support for it). However, clicking the little plus
sign on category pages should now scan only as many rows as are actually
used, so no limit should be necessary.
Sorting is now by cl_type, cl_sortkey instead of cl_sortkey. This
change has to be made to all users for efficiency, since the old index
was dropped. It means the sort order might be somewhat unexpected in
some cases, but for basic CategoryTree use it makes no difference, since
all the results have cl_type = 'subcat' anyway.
Fixes bug 23682, I think.
2010-08-16 21:57:49 +00:00
|
|
|
if ( $count === 0 ) {
|
2019-02-06 11:00:16 +00:00
|
|
|
$bullet = '';
|
2010-05-28 02:15:44 +00:00
|
|
|
$attr['class'] = 'CategoryTreeEmptyBullet';
|
|
|
|
} else {
|
2021-09-02 08:33:30 +00:00
|
|
|
$linkattr = [
|
2024-01-23 05:03:40 +00:00
|
|
|
// href and role will be added client-side
|
2021-09-02 08:33:30 +00:00
|
|
|
'class' => 'CategoryTreeToggle',
|
|
|
|
'data-ct-title' => $key,
|
|
|
|
];
|
2008-06-28 20:13:20 +00:00
|
|
|
|
2021-09-02 08:33:30 +00:00
|
|
|
if ( $children === 0 ) {
|
2024-01-23 05:03:40 +00:00
|
|
|
$linkattr['aria-expanded'] = 'false';
|
2010-05-28 02:15:44 +00:00
|
|
|
} else {
|
2021-09-02 08:33:30 +00:00
|
|
|
$linkattr['data-ct-loaded'] = true;
|
2024-01-23 05:03:40 +00:00
|
|
|
$linkattr['aria-expanded'] = 'true';
|
2010-04-20 22:00:34 +00:00
|
|
|
}
|
2008-06-28 20:13:20 +00:00
|
|
|
|
2024-01-23 05:03:40 +00:00
|
|
|
$bullet = Html::element( 'a', $linkattr ) . ' ';
|
2010-04-20 22:00:34 +00:00
|
|
|
}
|
2006-08-24 17:12:13 +00:00
|
|
|
} else {
|
2019-02-06 11:00:16 +00:00
|
|
|
$bullet = '';
|
|
|
|
$attr['class'] = 'CategoryTreePageBullet';
|
2006-08-24 17:12:13 +00:00
|
|
|
}
|
2021-08-31 11:18:30 +00:00
|
|
|
$s .= Html::rawElement( 'span', $attr, $bullet ) . ' ';
|
2008-06-28 20:13:20 +00:00
|
|
|
|
2019-07-31 05:46:31 +00:00
|
|
|
$s .= $link;
|
2008-06-30 14:09:47 +00:00
|
|
|
|
2023-10-28 18:11:36 +00:00
|
|
|
if ( $count !== false && $this->optionManager->getOption( 'showcount' ) ) {
|
2017-07-23 07:08:05 +00:00
|
|
|
$s .= self::createCountString( RequestContext::getMain(), $cat, $count );
|
2008-06-30 14:09:47 +00:00
|
|
|
}
|
|
|
|
|
2021-08-31 11:18:30 +00:00
|
|
|
$s .= Html::closeElement( 'div' );
|
|
|
|
$s .= Html::openElement(
|
2012-08-28 22:35:39 +00:00
|
|
|
'div',
|
2016-10-14 16:11:52 +00:00
|
|
|
[
|
2012-08-28 22:35:39 +00:00
|
|
|
'class' => 'CategoryTreeChildren',
|
2021-09-02 09:35:57 +00:00
|
|
|
'style' => $children === 0 ? 'display:none' : null
|
2016-10-14 16:11:52 +00:00
|
|
|
]
|
2012-08-28 22:35:39 +00:00
|
|
|
);
|
2010-04-20 22:00:34 +00:00
|
|
|
|
2021-09-02 09:35:57 +00:00
|
|
|
if ( $isInCatNS && $children > 0 ) {
|
2008-07-01 21:40:42 +00:00
|
|
|
$children = $this->renderChildren( $title, $children );
|
2021-09-02 08:33:30 +00:00
|
|
|
if ( $children === '' ) {
|
2021-08-31 11:18:30 +00:00
|
|
|
switch ( $mode ) {
|
|
|
|
case CategoryTreeMode::CATEGORIES:
|
|
|
|
$msg = 'categorytree-no-subcategories';
|
|
|
|
break;
|
|
|
|
case CategoryTreeMode::PAGES:
|
|
|
|
$msg = 'categorytree-no-pages';
|
|
|
|
break;
|
|
|
|
case CategoryTreeMode::PARENTS:
|
|
|
|
$msg = 'categorytree-no-parent-categories';
|
|
|
|
break;
|
|
|
|
default:
|
|
|
|
$msg = 'categorytree-nothing-found';
|
|
|
|
break;
|
2010-04-20 22:00:34 +00:00
|
|
|
}
|
2021-08-31 11:18:30 +00:00
|
|
|
$children = Html::element( 'i', [ 'class' => 'CategoryTreeNotice' ],
|
|
|
|
wfMessage( $msg )->text()
|
|
|
|
);
|
2008-07-01 21:40:42 +00:00
|
|
|
}
|
2021-08-31 11:18:30 +00:00
|
|
|
$s .= $children;
|
2008-07-01 21:40:42 +00:00
|
|
|
}
|
|
|
|
|
2021-08-31 11:18:30 +00:00
|
|
|
$s .= Html::closeElement( 'div' ) . Html::closeElement( 'div' );
|
2008-02-04 09:22:12 +00:00
|
|
|
|
2006-08-24 17:12:13 +00:00
|
|
|
return $s;
|
|
|
|
}
|
|
|
|
|
2016-11-13 14:57:16 +00:00
|
|
|
/**
|
|
|
|
* Create a string which format the page, subcat and file counts of a category
|
|
|
|
* @param IContextSource $context
|
2019-10-12 20:14:41 +00:00
|
|
|
* @param ?Category $cat
|
2016-11-13 14:57:16 +00:00
|
|
|
* @param int $countMode
|
|
|
|
* @return string
|
|
|
|
*/
|
2019-10-12 20:14:41 +00:00
|
|
|
public static function createCountString( IContextSource $context, ?Category $cat,
|
2018-12-16 09:47:07 +00:00
|
|
|
$countMode
|
|
|
|
) {
|
2022-02-08 05:38:58 +00:00
|
|
|
$allCount = $cat ? $cat->getMemberCount() : 0;
|
|
|
|
$subcatCount = $cat ? $cat->getSubcatCount() : 0;
|
|
|
|
$fileCount = $cat ? $cat->getFileCount() : 0;
|
|
|
|
$pages = $cat ? $cat->getPageCount( Category::COUNT_CONTENT_PAGES ) : 0;
|
2016-11-13 14:57:16 +00:00
|
|
|
|
|
|
|
$attr = [
|
|
|
|
'title' => $context->msg( 'categorytree-member-counts' )
|
2017-05-30 18:21:54 +00:00
|
|
|
->numParams( $subcatCount, $pages, $fileCount, $allCount, $countMode )->text(),
|
2019-02-05 14:57:56 +00:00
|
|
|
# numbers and commas get messed up in a mixed dir env
|
|
|
|
'dir' => $context->getLanguage()->getDir()
|
2016-11-13 14:57:16 +00:00
|
|
|
];
|
2019-09-01 13:59:59 +00:00
|
|
|
$contLang = MediaWikiServices::getInstance()->getContentLanguage();
|
|
|
|
$s = $contLang->getDirMark() . ' ';
|
2016-11-13 14:57:16 +00:00
|
|
|
|
|
|
|
# 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.
|
2021-08-31 11:18:30 +00:00
|
|
|
$s .= Html::rawElement(
|
2016-11-13 14:57:16 +00:00
|
|
|
'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;
|
|
|
|
}
|
|
|
|
|
2006-08-24 17:12:13 +00:00
|
|
|
/**
|
2012-02-09 01:23:31 +00:00
|
|
|
* Creates a Title object from a user provided (and thus unsafe) string
|
2017-10-11 18:25:13 +00:00
|
|
|
* @param string $title
|
2012-02-09 01:23:31 +00:00
|
|
|
* @return null|Title
|
|
|
|
*/
|
2018-04-11 03:47:26 +00:00
|
|
|
public static function makeTitle( $title ) {
|
2021-09-02 19:29:25 +00:00
|
|
|
$title = trim( strval( $title ) );
|
2008-02-04 09:22:12 +00:00
|
|
|
|
2021-09-02 19:29:25 +00:00
|
|
|
if ( $title === '' ) {
|
2010-01-06 21:24:10 +00:00
|
|
|
return null;
|
2006-08-24 17:12:13 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
# The title must be in the category namespace
|
|
|
|
# Ignore a leading Category: if there is one
|
|
|
|
$t = Title::newFromText( $title, NS_CATEGORY );
|
2021-09-02 09:35:57 +00:00
|
|
|
if ( !$t || !$t->inNamespace( NS_CATEGORY ) || $t->isExternal() ) {
|
2011-02-13 22:19:18 +00:00
|
|
|
// If we were given something like "Wikipedia:Foo" or "Template:",
|
|
|
|
// try it again but forced.
|
2006-08-24 17:12:13 +00:00
|
|
|
$title = "Category:$title";
|
|
|
|
$t = Title::newFromText( $title );
|
|
|
|
}
|
|
|
|
return $t;
|
|
|
|
}
|
|
|
|
}
|