mirror of
https://gerrit.wikimedia.org/r/mediawiki/extensions/CategoryTree
synced 2024-11-27 09:43:06 +00:00
Replace global variables by injected Config
Convert some functions to non-static function to access the Config object. Change-Id: Iac3d6a6c00eef169aec91b8c367cbbb7359a9b02
This commit is contained in:
parent
e492044e44
commit
c63d5da014
|
@ -88,8 +88,8 @@ class ApiCategoryTree extends ApiBase {
|
|||
|
||||
$depth = isset( $options['depth'] ) ? (int)$options['depth'] : 1;
|
||||
|
||||
$ct = new CategoryTree( $options, $this->linkRenderer );
|
||||
$depth = OptionManager::capDepth( $ct->optionManager->getOption( 'mode' ), $depth );
|
||||
$ct = new CategoryTree( $options, $this->getConfig(), $this->linkRenderer );
|
||||
$depth = $ct->optionManager->capDepth( $depth );
|
||||
$ctConfig = $this->configFactory->makeConfig( 'categorytree' );
|
||||
$html = $this->getHTML( $ct, $title, $depth, $ctConfig );
|
||||
|
||||
|
|
|
@ -27,6 +27,7 @@ namespace MediaWiki\Extension\CategoryTree;
|
|||
use ExtensionRegistry;
|
||||
use IContextSource;
|
||||
use MediaWiki\Category\Category;
|
||||
use MediaWiki\Config\Config;
|
||||
use MediaWiki\Html\Html;
|
||||
use MediaWiki\Linker\LinkRenderer;
|
||||
use MediaWiki\MediaWikiServices;
|
||||
|
@ -44,18 +45,24 @@ class CategoryTree {
|
|||
/** @var OptionManager */
|
||||
public $optionManager;
|
||||
|
||||
/** @var Config */
|
||||
private $config;
|
||||
|
||||
/** @var LinkRenderer */
|
||||
private $linkRenderer;
|
||||
|
||||
/**
|
||||
* @param array $options
|
||||
* @param Config $config
|
||||
* @param LinkRenderer $linkRenderer
|
||||
*/
|
||||
public function __construct(
|
||||
array $options,
|
||||
Config $config,
|
||||
LinkRenderer $linkRenderer
|
||||
) {
|
||||
$this->optionManager = new OptionManager( $options );
|
||||
$this->optionManager = new OptionManager( $options, $config );
|
||||
$this->config = $config;
|
||||
$this->linkRenderer = $linkRenderer;
|
||||
}
|
||||
|
||||
|
@ -83,7 +90,7 @@ class CategoryTree {
|
|||
public function getTag( ?Parser $parser, $category, $hideroot = false, array $attr = [],
|
||||
$depth = 1, $allowMissing = false
|
||||
) {
|
||||
global $wgCategoryTreeDisableCache;
|
||||
$disableCache = $this->config->get( 'CategoryTreeDisableCache' );
|
||||
|
||||
$category = trim( $category );
|
||||
if ( $category === '' ) {
|
||||
|
@ -91,10 +98,10 @@ class CategoryTree {
|
|||
}
|
||||
|
||||
if ( $parser ) {
|
||||
if ( $wgCategoryTreeDisableCache === true ) {
|
||||
if ( $disableCache === true ) {
|
||||
$parser->getOutput()->updateCacheExpiry( 0 );
|
||||
} elseif ( is_int( $wgCategoryTreeDisableCache ) ) {
|
||||
$parser->getOutput()->updateCacheExpiry( $wgCategoryTreeDisableCache );
|
||||
} elseif ( is_int( $disableCache ) ) {
|
||||
$parser->getOutput()->updateCacheExpiry( $disableCache );
|
||||
}
|
||||
}
|
||||
|
||||
|
@ -138,8 +145,6 @@ class CategoryTree {
|
|||
* @return string
|
||||
*/
|
||||
public function renderChildren( Title $title, $depth = 1 ) {
|
||||
global $wgCategoryTreeMaxChildren, $wgCategoryTreeUseCategoryTable;
|
||||
|
||||
if ( !$title->inNamespace( NS_CATEGORY ) ) {
|
||||
// Non-categories can't have children. :)
|
||||
return '';
|
||||
|
@ -157,7 +162,10 @@ class CategoryTree {
|
|||
'cl_from' ];
|
||||
$where = [];
|
||||
$joins = [];
|
||||
$options = [ 'ORDER BY' => 'cl_type, cl_sortkey', 'LIMIT' => $wgCategoryTreeMaxChildren ];
|
||||
$options = [
|
||||
'ORDER BY' => 'cl_type, cl_sortkey',
|
||||
'LIMIT' => $this->config->get( 'CategoryTreeMaxChildren' )
|
||||
];
|
||||
|
||||
if ( $inverse ) {
|
||||
$joins['categorylinks'] = [ 'RIGHT JOIN', [
|
||||
|
@ -184,7 +192,7 @@ class CategoryTree {
|
|||
}
|
||||
|
||||
# fetch member count if possible
|
||||
$doCount = !$inverse && $wgCategoryTreeUseCategoryTable;
|
||||
$doCount = !$inverse && $this->config->get( 'CategoryTreeUseCategoryTable' );
|
||||
|
||||
if ( $doCount ) {
|
||||
$tables = array_merge( $tables, [ 'category' ] );
|
||||
|
@ -264,8 +272,6 @@ class CategoryTree {
|
|||
* @return string
|
||||
*/
|
||||
public function renderParents( Title $title ) {
|
||||
global $wgCategoryTreeMaxChildren;
|
||||
|
||||
$dbr = MediaWikiServices::getInstance()->getDBLoadBalancerFactory()->getReplicaDatabase();
|
||||
|
||||
$res = $dbr->select(
|
||||
|
@ -274,7 +280,7 @@ class CategoryTree {
|
|||
[ 'cl_from' => $title->getArticleID() ],
|
||||
__METHOD__,
|
||||
[
|
||||
'LIMIT' => $wgCategoryTreeMaxChildren,
|
||||
'LIMIT' => $this->config->get( 'CategoryTreeMaxChildren' ),
|
||||
'ORDER BY' => 'cl_to'
|
||||
]
|
||||
);
|
||||
|
@ -306,9 +312,8 @@ class CategoryTree {
|
|||
* @return string
|
||||
*/
|
||||
public function renderNode( Title $title, $children = 0 ) {
|
||||
global $wgCategoryTreeUseCategoryTable;
|
||||
|
||||
if ( $wgCategoryTreeUseCategoryTable && $title->inNamespace( NS_CATEGORY )
|
||||
if ( $this->config->get( 'CategoryTreeUseCategoryTable' )
|
||||
&& $title->inNamespace( NS_CATEGORY )
|
||||
&& !$this->optionManager->isInverse()
|
||||
) {
|
||||
$cat = Category::newFromTitle( $title );
|
||||
|
|
|
@ -100,7 +100,7 @@ class CategoryTreePage extends SpecialPage {
|
|||
$options[$option] = $request->getVal( $option, $default );
|
||||
}
|
||||
|
||||
$this->tree = new CategoryTree( $options, $this->getLinkRenderer() );
|
||||
$this->tree = new CategoryTree( $options, $config, $this->getLinkRenderer() );
|
||||
|
||||
$this->getOutput()->addWikiMsg( 'categorytree-header' );
|
||||
|
||||
|
|
|
@ -203,7 +203,7 @@ class Hooks implements
|
|||
$parserOutput->addModules( [ 'ext.categoryTree' ] );
|
||||
}
|
||||
|
||||
$ct = new CategoryTree( $argv, $this->linkRenderer );
|
||||
$ct = new CategoryTree( $argv, $this->config, $this->linkRenderer );
|
||||
|
||||
$attr = Sanitizer::validateTagAttributes( $argv, 'div' );
|
||||
|
||||
|
@ -213,7 +213,7 @@ class Hooks implements
|
|||
? OptionManager::decodeBoolean( $argv['onlyroot'] ) : null;
|
||||
$depthArg = isset( $argv['depth'] ) ? (int)$argv['depth'] : null;
|
||||
|
||||
$depth = OptionManager::capDepth( $ct->optionManager->getOption( 'mode' ), $depthArg );
|
||||
$depth = $ct->optionManager->capDepth( $depthArg );
|
||||
if ( $onlyroot ) {
|
||||
$depth = 0;
|
||||
}
|
||||
|
@ -253,7 +253,7 @@ class Hooks implements
|
|||
*/
|
||||
public static function getDataForJs( RL\Context $context, Config $config ) {
|
||||
// Look, this is pretty bad but CategoryTree is just whacky, it needs to be rewritten
|
||||
$optionManager = new OptionManager( $config->get( 'CategoryTreeCategoryPageOptions' ) );
|
||||
$optionManager = new OptionManager( $config->get( 'CategoryTreeCategoryPageOptions' ), $config );
|
||||
|
||||
return [
|
||||
'defaultCtOptions' => $optionManager->getOptionsAsJsStructure(),
|
||||
|
@ -326,7 +326,7 @@ class Hooks implements
|
|||
if ( $mode !== null ) {
|
||||
$options['mode'] = $mode;
|
||||
}
|
||||
$tree = new CategoryTree( $options, $this->linkRenderer );
|
||||
$tree = new CategoryTree( $options, $this->config, $this->linkRenderer );
|
||||
|
||||
$cat = $this->categoryCache->getCategory( $title );
|
||||
|
||||
|
|
|
@ -26,6 +26,7 @@ namespace MediaWiki\Extension\CategoryTree;
|
|||
|
||||
use Exception;
|
||||
use FormatJson;
|
||||
use MediaWiki\Config\Config;
|
||||
use MediaWiki\MediaWikiServices;
|
||||
|
||||
/**
|
||||
|
@ -35,26 +36,30 @@ class OptionManager {
|
|||
/** @var array */
|
||||
private $mOptions = [];
|
||||
|
||||
/** @var Config */
|
||||
private $config;
|
||||
|
||||
/**
|
||||
* @param array $options
|
||||
* @param Config $config
|
||||
*/
|
||||
public function __construct( array $options ) {
|
||||
global $wgCategoryTreeDefaultOptions;
|
||||
public function __construct( array $options, Config $config ) {
|
||||
$this->config = $config;
|
||||
|
||||
// ensure default values and order of options.
|
||||
// Order may become important, it may influence the cache key!
|
||||
foreach ( $wgCategoryTreeDefaultOptions as $option => $default ) {
|
||||
foreach ( $config->get( 'CategoryTreeDefaultOptions' ) as $option => $default ) {
|
||||
$this->mOptions[$option] = $options[$option] ?? $default;
|
||||
}
|
||||
|
||||
$this->mOptions['mode'] = self::decodeMode( $this->mOptions['mode'] );
|
||||
$this->mOptions['mode'] = $this->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['hideprefix'] = $this->decodeHidePrefix( $this->mOptions['hideprefix'] );
|
||||
$this->mOptions['showcount'] = self::decodeBoolean( $this->mOptions['showcount'] );
|
||||
$this->mOptions['namespaces'] = self::decodeNamespaces( $this->mOptions['namespaces'] );
|
||||
|
||||
|
@ -142,11 +147,11 @@ class OptionManager {
|
|||
* @param mixed $mode
|
||||
* @return int|string
|
||||
*/
|
||||
private static function decodeMode( $mode ) {
|
||||
global $wgCategoryTreeDefaultOptions;
|
||||
private function decodeMode( $mode ) {
|
||||
$defaultOptions = $this->config->get( 'CategoryTreeDefaultOptions' );
|
||||
|
||||
if ( $mode === null ) {
|
||||
return $wgCategoryTreeDefaultOptions['mode'];
|
||||
return $defaultOptions['mode'];
|
||||
}
|
||||
if ( is_int( $mode ) ) {
|
||||
return $mode;
|
||||
|
@ -167,7 +172,7 @@ class OptionManager {
|
|||
} elseif ( $mode === 'parents' || $mode === 'super' || $mode === 'inverse' ) {
|
||||
$mode = CategoryTreeMode::PARENTS;
|
||||
} elseif ( $mode === 'default' ) {
|
||||
$mode = $wgCategoryTreeDefaultOptions['mode'];
|
||||
$mode = $defaultOptions['mode'];
|
||||
}
|
||||
|
||||
return (int)$mode;
|
||||
|
@ -214,11 +219,11 @@ class OptionManager {
|
|||
* @param mixed $value
|
||||
* @return int|string
|
||||
*/
|
||||
private static function decodeHidePrefix( $value ) {
|
||||
global $wgCategoryTreeDefaultOptions;
|
||||
private function decodeHidePrefix( $value ) {
|
||||
$defaultOptions = $this->config->get( 'CategoryTreeDefaultOptions' );
|
||||
|
||||
if ( $value === null ) {
|
||||
return $wgCategoryTreeDefaultOptions['hideprefix'];
|
||||
return $defaultOptions['hideprefix'];
|
||||
}
|
||||
if ( is_int( $value ) ) {
|
||||
return $value;
|
||||
|
@ -249,7 +254,7 @@ class OptionManager {
|
|||
} elseif ( $value === 'categories' || $value === 'category' || $value === 'smart' ) {
|
||||
return CategoryTreeHidePrefix::CATEGORIES;
|
||||
} else {
|
||||
return $wgCategoryTreeDefaultOptions['hideprefix'];
|
||||
return $defaultOptions['hideprefix'];
|
||||
}
|
||||
}
|
||||
|
||||
|
@ -306,23 +311,22 @@ class OptionManager {
|
|||
|
||||
/**
|
||||
* Internal function to cap depth
|
||||
* @param string $mode
|
||||
* @param int $depth
|
||||
* @return int|mixed
|
||||
*/
|
||||
public static function capDepth( $mode, $depth ) {
|
||||
global $wgCategoryTreeMaxDepth;
|
||||
|
||||
public function capDepth( $depth ) {
|
||||
if ( !is_numeric( $depth ) ) {
|
||||
return 1;
|
||||
}
|
||||
|
||||
$mode = $this->getOption( 'mode' );
|
||||
$depth = intval( $depth );
|
||||
$maxDepth = $this->config->get( 'CategoryTreeMaxDepth' );
|
||||
|
||||
if ( is_array( $wgCategoryTreeMaxDepth ) ) {
|
||||
$max = $wgCategoryTreeMaxDepth[$mode] ?? 1;
|
||||
} elseif ( is_numeric( $wgCategoryTreeMaxDepth ) ) {
|
||||
$max = $wgCategoryTreeMaxDepth;
|
||||
if ( is_array( $maxDepth ) ) {
|
||||
$max = $maxDepth[$mode] ?? 1;
|
||||
} elseif ( is_numeric( $maxDepth ) ) {
|
||||
$max = $maxDepth;
|
||||
} else {
|
||||
wfDebug( __METHOD__ . ': $wgCategoryTreeMaxDepth is invalid.' );
|
||||
$max = 1;
|
||||
|
|
Loading…
Reference in a new issue