Code Coverage |
||||||||||
Lines |
Functions and Methods |
Classes and Traits |
||||||||
| Total | |
53.60% |
67 / 125 |
|
16.67% |
2 / 12 |
CRAP | |
0.00% |
0 / 1 |
| OptionManager | |
53.60% |
67 / 125 |
|
16.67% |
2 / 12 |
771.19 | |
0.00% |
0 / 1 |
| __construct | |
60.00% |
9 / 15 |
|
0.00% |
0 / 1 |
10.14 | |||
| getOptions | |
0.00% |
0 / 1 |
|
0.00% |
0 / 1 |
2 | |||
| getOption | |
100.00% |
1 / 1 |
|
100.00% |
1 / 1 |
1 | |||
| isInverse | |
0.00% |
0 / 1 |
|
0.00% |
0 / 1 |
2 | |||
| decodeNamespaces | |
9.09% |
2 / 22 |
|
0.00% |
0 / 1 |
139.97 | |||
| decodeMode | |
100.00% |
16 / 16 |
|
100.00% |
1 / 1 |
13 | |||
| decodeBoolean | |
16.67% |
2 / 12 |
|
0.00% |
0 / 1 |
55.88 | |||
| decodeHidePrefix | |
90.00% |
27 / 30 |
|
0.00% |
0 / 1 |
24.58 | |||
| encodeOptions | |
0.00% |
0 / 6 |
|
0.00% |
0 / 1 |
20 | |||
| getOptionsAsCacheKey | |
0.00% |
0 / 8 |
|
0.00% |
0 / 1 |
20 | |||
| getOptionsAsJsStructure | |
0.00% |
0 / 1 |
|
0.00% |
0 / 1 |
2 | |||
| capDepth | |
83.33% |
10 / 12 |
|
0.00% |
0 / 1 |
5.12 | |||
| 1 | <?php |
| 2 | /** |
| 3 | * © 2006-2007 Daniel Kinzler |
| 4 | * |
| 5 | * This program is free software; you can redistribute it and/or modify |
| 6 | * it under the terms of the GNU General Public License as published by |
| 7 | * the Free Software Foundation; either version 2 of the License, or |
| 8 | * (at your option) any later version. |
| 9 | * |
| 10 | * This program is distributed in the hope that it will be useful, |
| 11 | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 12 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 13 | * GNU General Public License for more details. |
| 14 | * |
| 15 | * You should have received a copy of the GNU General Public License along |
| 16 | * with this program; if not, write to the Free Software Foundation, Inc., |
| 17 | * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. |
| 18 | * http://www.gnu.org/copyleft/gpl.html |
| 19 | * |
| 20 | * @file |
| 21 | * @ingroup Extensions |
| 22 | * @author Daniel Kinzler, brightbyte.de |
| 23 | */ |
| 24 | |
| 25 | namespace MediaWiki\Extension\CategoryTree; |
| 26 | |
| 27 | use InvalidArgumentException; |
| 28 | use MediaWiki\Config\Config; |
| 29 | use MediaWiki\Json\FormatJson; |
| 30 | use MediaWiki\MediaWikiServices; |
| 31 | |
| 32 | /** |
| 33 | * Core functions to handle the options |
| 34 | */ |
| 35 | class OptionManager { |
| 36 | /** |
| 37 | * Maps the numeric mode values used before MediaWiki 1.47 to the current |
| 38 | * string constants. They may still occur in site configuration and in the |
| 39 | * options attribute of parser cached HTML. |
| 40 | */ |
| 41 | private const LEGACY_MODE_VALUES = [ |
| 42 | 0 => CategoryTreeMode::CATEGORIES, |
| 43 | 10 => CategoryTreeMode::PAGES, |
| 44 | 20 => CategoryTreeMode::ALL, |
| 45 | 100 => CategoryTreeMode::PARENTS, |
| 46 | ]; |
| 47 | |
| 48 | /** |
| 49 | * Maps the numeric hideprefix values used before MediaWiki 1.47 to the |
| 50 | * current string constants. |
| 51 | */ |
| 52 | private const LEGACY_HIDE_PREFIX_VALUES = [ |
| 53 | 0 => CategoryTreeHidePrefix::NEVER, |
| 54 | 10 => CategoryTreeHidePrefix::ALWAYS, |
| 55 | 20 => CategoryTreeHidePrefix::CATEGORIES, |
| 56 | 30 => CategoryTreeHidePrefix::AUTO, |
| 57 | ]; |
| 58 | |
| 59 | private array $mOptions = []; |
| 60 | |
| 61 | public function __construct( |
| 62 | array $options, |
| 63 | private readonly Config $config, |
| 64 | ) { |
| 65 | // ensure default values and order of options. |
| 66 | // Order may become important, it may influence the cache key! |
| 67 | foreach ( $config->get( 'CategoryTreeDefaultOptions' ) as $option => $default ) { |
| 68 | $this->mOptions[$option] = $options[$option] ?? $default; |
| 69 | } |
| 70 | |
| 71 | $this->mOptions['mode'] = $this->decodeMode( $this->mOptions['mode'] ); |
| 72 | |
| 73 | if ( $this->mOptions['mode'] === CategoryTreeMode::PARENTS ) { |
| 74 | // namespace filter makes no sense with CategoryTreeMode::PARENTS |
| 75 | $this->mOptions['namespaces'] = false; |
| 76 | } |
| 77 | |
| 78 | $this->mOptions['hideprefix'] = $this->decodeHidePrefix( $this->mOptions['hideprefix'] ); |
| 79 | $this->mOptions['showcount'] = self::decodeBoolean( $this->mOptions['showcount'] ); |
| 80 | $this->mOptions['namespaces'] = self::decodeNamespaces( $this->mOptions['namespaces'] ); |
| 81 | |
| 82 | if ( $this->mOptions['namespaces'] ) { |
| 83 | # automatically adjust mode to match namespace filter |
| 84 | if ( count( $this->mOptions['namespaces'] ) === 1 |
| 85 | && $this->mOptions['namespaces'][0] === NS_CATEGORY ) { |
| 86 | $this->mOptions['mode'] = CategoryTreeMode::CATEGORIES; |
| 87 | } elseif ( !in_array( NS_FILE, $this->mOptions['namespaces'] ) ) { |
| 88 | $this->mOptions['mode'] = CategoryTreeMode::PAGES; |
| 89 | } else { |
| 90 | $this->mOptions['mode'] = CategoryTreeMode::ALL; |
| 91 | } |
| 92 | } |
| 93 | } |
| 94 | |
| 95 | public function getOptions(): array { |
| 96 | return $this->mOptions; |
| 97 | } |
| 98 | |
| 99 | /** |
| 100 | * @param string $name |
| 101 | * @return mixed |
| 102 | */ |
| 103 | public function getOption( string $name ) { |
| 104 | return $this->mOptions[$name]; |
| 105 | } |
| 106 | |
| 107 | public function isInverse(): bool { |
| 108 | return $this->getOption( 'mode' ) === CategoryTreeMode::PARENTS; |
| 109 | } |
| 110 | |
| 111 | /** |
| 112 | * @param mixed $nn |
| 113 | * @return array|bool |
| 114 | */ |
| 115 | private static function decodeNamespaces( $nn ) { |
| 116 | if ( $nn === false || $nn === null ) { |
| 117 | return false; |
| 118 | } |
| 119 | |
| 120 | if ( !is_array( $nn ) ) { |
| 121 | $nn = preg_split( '![\s#:|]+!', $nn ); |
| 122 | } |
| 123 | |
| 124 | $namespaces = []; |
| 125 | $contLang = MediaWikiServices::getInstance()->getContentLanguage(); |
| 126 | foreach ( $nn as $n ) { |
| 127 | if ( is_int( $n ) ) { |
| 128 | $ns = $n; |
| 129 | } else { |
| 130 | $n = trim( $n ); |
| 131 | if ( $n === '' ) { |
| 132 | continue; |
| 133 | } |
| 134 | |
| 135 | $lower = strtolower( $n ); |
| 136 | |
| 137 | if ( is_numeric( $n ) ) { |
| 138 | $ns = (int)$n; |
| 139 | } elseif ( $n === '-' || $n === '_' || $n === '*' || $lower === 'main' ) { |
| 140 | $ns = NS_MAIN; |
| 141 | } else { |
| 142 | $ns = $contLang->getNsIndex( $n ); |
| 143 | } |
| 144 | } |
| 145 | |
| 146 | if ( is_int( $ns ) ) { |
| 147 | $namespaces[] = $ns; |
| 148 | } |
| 149 | } |
| 150 | |
| 151 | # get elements into canonical order |
| 152 | sort( $namespaces ); |
| 153 | return $namespaces; |
| 154 | } |
| 155 | |
| 156 | /** |
| 157 | * @param mixed $mode |
| 158 | * @return string One of the CategoryTreeMode constants |
| 159 | */ |
| 160 | private function decodeMode( $mode ): string { |
| 161 | $defaultOptions = $this->config->get( 'CategoryTreeDefaultOptions' ); |
| 162 | |
| 163 | if ( is_string( $mode ) ) { |
| 164 | $mode = trim( strtolower( $mode ) ); |
| 165 | } |
| 166 | |
| 167 | if ( $mode === null || $mode === 'default' ) { |
| 168 | $mode = $defaultOptions['mode']; |
| 169 | if ( is_string( $mode ) ) { |
| 170 | $mode = trim( strtolower( $mode ) ); |
| 171 | } |
| 172 | } |
| 173 | |
| 174 | if ( is_numeric( $mode ) ) { |
| 175 | return self::LEGACY_MODE_VALUES[(int)$mode] ?? CategoryTreeMode::CATEGORIES; |
| 176 | } |
| 177 | |
| 178 | if ( $mode === CategoryTreeMode::ALL || $mode === CategoryTreeMode::PAGES ) { |
| 179 | // already normalized |
| 180 | } elseif ( $mode === 'categories' || $mode === 'sub' ) { |
| 181 | $mode = CategoryTreeMode::CATEGORIES; |
| 182 | } elseif ( $mode === 'parents' || $mode === 'super' || $mode === 'inverse' ) { |
| 183 | $mode = CategoryTreeMode::PARENTS; |
| 184 | } else { |
| 185 | $mode = CategoryTreeMode::CATEGORIES; |
| 186 | } |
| 187 | |
| 188 | return $mode; |
| 189 | } |
| 190 | |
| 191 | /** |
| 192 | * Helper function to convert a string to a boolean value. |
| 193 | * Perhaps make this a global function in MediaWiki proper |
| 194 | * @param mixed $value |
| 195 | * @return bool |
| 196 | */ |
| 197 | public static function decodeBoolean( $value ): bool { |
| 198 | if ( $value === null || is_bool( $value ) ) { |
| 199 | return (bool)$value; |
| 200 | } |
| 201 | if ( is_int( $value ) ) { |
| 202 | return ( $value > 0 ); |
| 203 | } |
| 204 | |
| 205 | $value = trim( strtolower( $value ) ); |
| 206 | if ( is_numeric( $value ) ) { |
| 207 | return ( (int)$value > 0 ); |
| 208 | } |
| 209 | |
| 210 | return $value === 'yes' || |
| 211 | $value === 'y' || |
| 212 | $value === 'true' || |
| 213 | $value === 't' || |
| 214 | $value === 'on'; |
| 215 | } |
| 216 | |
| 217 | /** |
| 218 | * @param mixed $value |
| 219 | * @return string One of the CategoryTreeHidePrefix constants |
| 220 | */ |
| 221 | private function decodeHidePrefix( $value ): string { |
| 222 | $defaultOptions = $this->config->get( 'CategoryTreeDefaultOptions' ); |
| 223 | |
| 224 | if ( $value === null ) { |
| 225 | $value = $defaultOptions['hideprefix']; |
| 226 | } |
| 227 | if ( $value === true ) { |
| 228 | return CategoryTreeHidePrefix::ALWAYS; |
| 229 | } |
| 230 | if ( $value === false ) { |
| 231 | return CategoryTreeHidePrefix::NEVER; |
| 232 | } |
| 233 | if ( is_numeric( $value ) ) { |
| 234 | return self::LEGACY_HIDE_PREFIX_VALUES[(int)$value] ?? CategoryTreeHidePrefix::NEVER; |
| 235 | } |
| 236 | if ( !is_string( $value ) ) { |
| 237 | return CategoryTreeHidePrefix::NEVER; |
| 238 | } |
| 239 | |
| 240 | $value = trim( strtolower( $value ) ); |
| 241 | |
| 242 | if ( $value === 'yes' || $value === 'y' |
| 243 | || $value === 'true' || $value === 't' || $value === 'on' |
| 244 | ) { |
| 245 | return CategoryTreeHidePrefix::ALWAYS; |
| 246 | } elseif ( $value === 'no' || $value === 'n' |
| 247 | || $value === 'false' || $value === 'f' || $value === 'off' |
| 248 | ) { |
| 249 | return CategoryTreeHidePrefix::NEVER; |
| 250 | } elseif ( $value === 'always' ) { |
| 251 | return CategoryTreeHidePrefix::ALWAYS; |
| 252 | } elseif ( $value === 'never' ) { |
| 253 | return CategoryTreeHidePrefix::NEVER; |
| 254 | } elseif ( $value === 'auto' ) { |
| 255 | return CategoryTreeHidePrefix::AUTO; |
| 256 | } elseif ( $value === 'categories' || $value === 'category' || $value === 'smart' ) { |
| 257 | return CategoryTreeHidePrefix::CATEGORIES; |
| 258 | } else { |
| 259 | $default = $defaultOptions['hideprefix']; |
| 260 | if ( is_string( $default ) && trim( strtolower( $default ) ) === $value ) { |
| 261 | // The configured default itself is invalid |
| 262 | return CategoryTreeHidePrefix::NEVER; |
| 263 | } |
| 264 | return $this->decodeHidePrefix( $default ); |
| 265 | } |
| 266 | } |
| 267 | |
| 268 | /** |
| 269 | * @param array $options |
| 270 | * @param string $enc |
| 271 | * @return mixed |
| 272 | */ |
| 273 | private static function encodeOptions( array $options, string $enc ) { |
| 274 | if ( $enc === 'mode' || $enc === '' ) { |
| 275 | $opt = $options['mode']; |
| 276 | } elseif ( $enc === 'json' ) { |
| 277 | $opt = FormatJson::encode( $options ); |
| 278 | } else { |
| 279 | throw new InvalidArgumentException( 'Unknown encoding for CategoryTree options: ' . $enc ); |
| 280 | } |
| 281 | |
| 282 | return $opt; |
| 283 | } |
| 284 | |
| 285 | /** |
| 286 | * @param int|null $depth |
| 287 | * @return string |
| 288 | */ |
| 289 | public function getOptionsAsCacheKey( ?int $depth = null ): string { |
| 290 | $key = ''; |
| 291 | |
| 292 | foreach ( $this->mOptions as $k => $v ) { |
| 293 | if ( is_array( $v ) ) { |
| 294 | $v = implode( '|', $v ); |
| 295 | } |
| 296 | $key .= $k . ':' . $v . ';'; |
| 297 | } |
| 298 | |
| 299 | if ( $depth !== null ) { |
| 300 | $key .= ';depth=' . $depth; |
| 301 | } |
| 302 | return $key; |
| 303 | } |
| 304 | |
| 305 | /** |
| 306 | * @return mixed |
| 307 | */ |
| 308 | public function getOptionsAsJsStructure() { |
| 309 | return self::encodeOptions( $this->mOptions, 'json' ); |
| 310 | } |
| 311 | |
| 312 | /** |
| 313 | * Internal function to cap depth |
| 314 | * @param int $depth |
| 315 | * @return int |
| 316 | */ |
| 317 | public function capDepth( int $depth ): int { |
| 318 | $mode = $this->getOption( 'mode' ); |
| 319 | $maxDepth = $this->config->get( 'CategoryTreeMaxDepth' ); |
| 320 | |
| 321 | if ( is_array( $maxDepth ) ) { |
| 322 | $max = $maxDepth[$mode] ?? 1; |
| 323 | $legacyMode = array_search( $mode, self::LEGACY_MODE_VALUES, true ); |
| 324 | if ( $legacyMode !== false && isset( $maxDepth[$legacyMode] ) ) { |
| 325 | // Site config may still use the legacy numeric mode values as keys. |
| 326 | $max = $maxDepth[$legacyMode]; |
| 327 | } |
| 328 | } elseif ( is_numeric( $maxDepth ) ) { |
| 329 | $max = $maxDepth; |
| 330 | } else { |
| 331 | wfDebug( __METHOD__ . ': $wgCategoryTreeMaxDepth is invalid.' ); |
| 332 | $max = 1; |
| 333 | } |
| 334 | |
| 335 | return min( $depth, $max ); |
| 336 | } |
| 337 | } |