2023-10-28 18:11:36 +00:00
|
|
|
<?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
|
|
|
|
*/
|
|
|
|
|
|
|
|
namespace MediaWiki\Extension\CategoryTree;
|
|
|
|
|
2024-02-09 13:25:05 +00:00
|
|
|
use InvalidArgumentException;
|
2023-10-28 19:40:51 +00:00
|
|
|
use MediaWiki\Config\Config;
|
2024-08-22 12:53:23 +00:00
|
|
|
use MediaWiki\Json\FormatJson;
|
2023-10-28 18:11:36 +00:00
|
|
|
use MediaWiki\MediaWikiServices;
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Core functions to handle the options
|
|
|
|
*/
|
|
|
|
class OptionManager {
|
2024-08-17 21:49:42 +00:00
|
|
|
private array $mOptions = [];
|
|
|
|
private Config $config;
|
2023-10-28 18:11:36 +00:00
|
|
|
|
2023-10-28 19:40:51 +00:00
|
|
|
public function __construct( array $options, Config $config ) {
|
|
|
|
$this->config = $config;
|
2023-10-28 18:11:36 +00:00
|
|
|
|
|
|
|
// ensure default values and order of options.
|
|
|
|
// Order may become important, it may influence the cache key!
|
2023-10-28 19:40:51 +00:00
|
|
|
foreach ( $config->get( 'CategoryTreeDefaultOptions' ) as $option => $default ) {
|
2023-10-28 18:11:36 +00:00
|
|
|
$this->mOptions[$option] = $options[$option] ?? $default;
|
|
|
|
}
|
|
|
|
|
2023-10-28 19:40:51 +00:00
|
|
|
$this->mOptions['mode'] = $this->decodeMode( $this->mOptions['mode'] );
|
2023-10-28 18:11:36 +00:00
|
|
|
|
|
|
|
if ( $this->mOptions['mode'] === CategoryTreeMode::PARENTS ) {
|
|
|
|
// namespace filter makes no sense with CategoryTreeMode::PARENTS
|
|
|
|
$this->mOptions['namespaces'] = false;
|
|
|
|
}
|
|
|
|
|
2023-10-28 19:40:51 +00:00
|
|
|
$this->mOptions['hideprefix'] = $this->decodeHidePrefix( $this->mOptions['hideprefix'] );
|
2023-10-28 18:11:36 +00:00
|
|
|
$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;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2024-08-17 21:49:42 +00:00
|
|
|
public function getOptions(): array {
|
2023-10-28 18:11:36 +00:00
|
|
|
return $this->mOptions;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @param string $name
|
|
|
|
* @return mixed
|
|
|
|
*/
|
2024-08-17 21:49:42 +00:00
|
|
|
public function getOption( string $name ) {
|
2023-10-28 18:11:36 +00:00
|
|
|
return $this->mOptions[$name];
|
|
|
|
}
|
|
|
|
|
2024-08-17 21:49:42 +00:00
|
|
|
public function isInverse(): bool {
|
2023-10-28 18:11:36 +00:00
|
|
|
return $this->getOption( 'mode' ) === CategoryTreeMode::PARENTS;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @param mixed $nn
|
|
|
|
* @return array|bool
|
|
|
|
*/
|
|
|
|
private static function decodeNamespaces( $nn ) {
|
|
|
|
if ( $nn === false || $nn === null ) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
if ( !is_array( $nn ) ) {
|
|
|
|
$nn = preg_split( '![\s#:|]+!', $nn );
|
|
|
|
}
|
|
|
|
|
|
|
|
$namespaces = [];
|
|
|
|
$contLang = MediaWikiServices::getInstance()->getContentLanguage();
|
|
|
|
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 = $contLang->getNsIndex( $n );
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if ( is_int( $ns ) ) {
|
|
|
|
$namespaces[] = $ns;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
# get elements into canonical order
|
|
|
|
sort( $namespaces );
|
|
|
|
return $namespaces;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @param mixed $mode
|
|
|
|
* @return int|string
|
|
|
|
*/
|
2023-10-28 19:40:51 +00:00
|
|
|
private function decodeMode( $mode ) {
|
|
|
|
$defaultOptions = $this->config->get( 'CategoryTreeDefaultOptions' );
|
2023-10-28 18:11:36 +00:00
|
|
|
|
|
|
|
if ( $mode === null ) {
|
2023-10-28 19:40:51 +00:00
|
|
|
return $defaultOptions['mode'];
|
2023-10-28 18:11:36 +00:00
|
|
|
}
|
|
|
|
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' ) {
|
2023-10-28 19:40:51 +00:00
|
|
|
$mode = $defaultOptions['mode'];
|
2023-10-28 18:11:36 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
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
|
2024-08-17 21:49:42 +00:00
|
|
|
* @return bool
|
2023-10-28 18:11:36 +00:00
|
|
|
*/
|
2024-08-17 21:49:42 +00:00
|
|
|
public static function decodeBoolean( $value ): bool {
|
2023-10-28 18:11:36 +00:00
|
|
|
if ( $value === null ) {
|
2024-08-17 21:49:42 +00:00
|
|
|
return false;
|
2023-10-28 18:11:36 +00:00
|
|
|
}
|
|
|
|
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;
|
|
|
|
} else {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @param mixed $value
|
|
|
|
* @return int|string
|
|
|
|
*/
|
2023-10-28 19:40:51 +00:00
|
|
|
private function decodeHidePrefix( $value ) {
|
|
|
|
$defaultOptions = $this->config->get( 'CategoryTreeDefaultOptions' );
|
2023-10-28 18:11:36 +00:00
|
|
|
|
|
|
|
if ( $value === null ) {
|
2023-10-28 19:40:51 +00:00
|
|
|
return $defaultOptions['hideprefix'];
|
2023-10-28 18:11:36 +00:00
|
|
|
}
|
|
|
|
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 {
|
2023-10-28 19:40:51 +00:00
|
|
|
return $defaultOptions['hideprefix'];
|
2023-10-28 18:11:36 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @param array $options
|
|
|
|
* @param string $enc
|
|
|
|
* @return mixed
|
|
|
|
*/
|
2024-08-17 21:49:42 +00:00
|
|
|
private static function encodeOptions( array $options, string $enc ) {
|
2023-10-28 18:11:36 +00:00
|
|
|
if ( $enc === 'mode' || $enc === '' ) {
|
|
|
|
$opt = $options['mode'];
|
|
|
|
} elseif ( $enc === 'json' ) {
|
|
|
|
$opt = FormatJson::encode( $options );
|
|
|
|
} else {
|
2024-02-09 13:25:05 +00:00
|
|
|
throw new InvalidArgumentException( 'Unknown encoding for CategoryTree options: ' . $enc );
|
2023-10-28 18:11:36 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
return $opt;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @param int|null $depth
|
|
|
|
* @return string
|
|
|
|
*/
|
2024-08-17 21:49:42 +00:00
|
|
|
public function getOptionsAsCacheKey( int $depth = null ): string {
|
2023-10-28 18:11:36 +00:00
|
|
|
$key = '';
|
|
|
|
|
|
|
|
foreach ( $this->mOptions as $k => $v ) {
|
|
|
|
if ( is_array( $v ) ) {
|
|
|
|
$v = implode( '|', $v );
|
|
|
|
}
|
|
|
|
$key .= $k . ':' . $v . ';';
|
|
|
|
}
|
|
|
|
|
|
|
|
if ( $depth !== null ) {
|
|
|
|
$key .= ';depth=' . $depth;
|
|
|
|
}
|
|
|
|
return $key;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @param int|null $depth
|
|
|
|
* @return mixed
|
|
|
|
*/
|
2024-08-17 21:49:42 +00:00
|
|
|
public function getOptionsAsJsStructure( int $depth = null ) {
|
2023-10-28 18:11:36 +00:00
|
|
|
$opt = $this->mOptions;
|
|
|
|
if ( $depth !== null ) {
|
|
|
|
$opt['depth'] = $depth;
|
|
|
|
}
|
|
|
|
|
|
|
|
return self::encodeOptions( $opt, 'json' );
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Internal function to cap depth
|
|
|
|
* @param int $depth
|
2024-08-17 21:49:42 +00:00
|
|
|
* @return int
|
2023-10-28 18:11:36 +00:00
|
|
|
*/
|
2024-08-17 21:49:42 +00:00
|
|
|
public function capDepth( int $depth ): int {
|
2023-10-28 19:40:51 +00:00
|
|
|
$mode = $this->getOption( 'mode' );
|
|
|
|
$maxDepth = $this->config->get( 'CategoryTreeMaxDepth' );
|
2023-10-28 18:11:36 +00:00
|
|
|
|
2023-10-28 19:40:51 +00:00
|
|
|
if ( is_array( $maxDepth ) ) {
|
|
|
|
$max = $maxDepth[$mode] ?? 1;
|
|
|
|
} elseif ( is_numeric( $maxDepth ) ) {
|
|
|
|
$max = $maxDepth;
|
2023-10-28 18:11:36 +00:00
|
|
|
} else {
|
|
|
|
wfDebug( __METHOD__ . ': $wgCategoryTreeMaxDepth is invalid.' );
|
|
|
|
$max = 1;
|
|
|
|
}
|
|
|
|
|
|
|
|
return min( $depth, $max );
|
|
|
|
}
|
|
|
|
}
|