| 1 |
<?php |
| 2 |
|
| 3 |
r |
| 4 |
|
| 5 |
|
| 6 |
|
| 7 |
|
| 8 |
|
| 9 |
|
| 10 |
|
| 11 |
|
| 12 |
|
| 13 |
|
| 14 |
|
| 15 |
|
| 16 |
|
| 17 |
|
| 18 |
|
| 19 |
|
| 20 |
|
| 21 |
|
| 22 |
|
| 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 |
|
| 34 |
|
| 35 |
class OptionManager { |
| 36 |
private array $mOptions = []; |
| 37 |
private Config $config; |
| 38 |
|
| 39 |
public function __construct( array $options, Config $config ) { |
| 40 |
$this->config = $config; |
| 41 |
|
| 42 |
|
| 43 |
|
| 44 |
foreach ( $config->get( 'CategoryTreeDefaultOptions' ) as $option => $default ) { |
| 45 |
$this->mOptions[$option] = $options[$option] ?? $default; |
| 46 |
} |
| 47 |
|
| 48 |
$this->mOptions['mode'] = $this->decodeMode( $this->mOptions['mode'] ); |
| 49 |
|
| 50 |
if ( $this->mOptions['mode'] === CategoryTreeMode::PARENTS ) { |
| 51 |
|
| 52 |
$this->mOptions['namespaces'] = false; |
| 53 |
} |
| 54 |
|
| 55 |
$this->mOptions['hideprefix'] = $this->decodeHidePrefix( $this->mOptions['hideprefix'] ); |
| 56 |
$this->mOptions['showcount'] = self::decodeBoolean( $this->mOptions['showcount'] ); |
| 57 |
$this->mOptions['namespaces'] = self::decodeNamespaces( $this->mOptions['namespaces'] ); |
| 58 |
|
| 59 |
if ( $this->mOptions['namespaces'] ) { |
| 60 |
|
| 61 |
if ( count( $this->mOptions['namespaces'] ) === 1 |
| 62 |
&& $this->mOptions['namespaces'][0] === NS_CATEGORY ) { |
| 63 |
$this->mOptions['mode'] = CategoryTreeMode::CATEGORIES; |
| 64 |
} elseif ( !in_array( NS_FILE, $this->mOptions['namespaces'] ) ) { |
| 65 |
$this->mOptions['mode'] = CategoryTreeMode::PAGES; |
| 66 |
} else { |
| 67 |
$this->mOptions['mode'] = CategoryTreeMode::ALL; |
| 68 |
} |
| 69 |
} |
| 70 |
} |
| 71 |
|
| 72 |
public function getOptions(): array { |
| 73 |
return $this->mOptions; |
| 74 |
} |
| 75 |
|
| 76 |
|
| 77 |
|
| 78 |
|
| 79 |
|
| 80 |
public function getOption( string $name ) { |
| 81 |
return $this->mOptions[$name]; |
| 82 |
} |
| 83 |
|
| 84 |
public function isInverse(): bool { |
| 85 |
return $this->getOption( 'mode' ) === CategoryTreeMode::PARENTS; |
| 86 |
} |
| 87 |
|
| 88 |
|
| 89 |
|
| 90 |
|
| 91 |
|
| 92 |
private static function decodeNamespaces( $nn ) { |
| 93 |
if ( $nn === false || $nn === null ) { |
| 94 |
return false; |
| 95 |
} |
| 96 |
|
| 97 |
if ( !is_array( $nn ) ) { |
| 98 |
$nn = preg_split( '![\s#:|]+!', $nn ); |
| 99 |
} |
| 100 |
|
| 101 |
$namespaces = []; |
| 102 |
$contLang = MediaWikiServices::getInstance()->getContentLanguage(); |
| 103 |
foreach ( $nn as $n ) { |
| 104 |
if ( is_int( $n ) ) { |
| 105 |
$ns = $n; |
| 106 |
} else { |
| 107 |
$n = trim( $n ); |
| 108 |
if ( $n === '' ) { |
| 109 |
continue; |
| 110 |
} |
| 111 |
|
| 112 |
$lower = strtolower( $n ); |
| 113 |
|
| 114 |
if ( is_numeric( $n ) ) { |
| 115 |
$ns = (int)$n; |
| 116 |
} elseif ( $n === '-' || $n === '_' || $n === '*' || $lower === 'main' ) { |
| 117 |
$ns = NS_MAIN; |
| 118 |
} else { |
| 119 |
$ns = $contLang->getNsIndex( $n ); |
| 120 |
} |
| 121 |
} |
| 122 |
|
| 123 |
if ( is_int( $ns ) ) { |
| 124 |
$namespaces[] = $ns; |
| 125 |
} |
| 126 |
} |
| 127 |
|
| 128 |
|
| 129 |
sort( $namespaces ); |
| 130 |
return $namespaces; |
| 131 |
} |
| 132 |
|
| 133 |
|
| 134 |
|
| 135 |
|
| 136 |
|
| 137 |
private function decodeMode( $mode ) { |
| 138 |
$defaultOptions = $this->config->get( 'CategoryTreeDefaultOptions' ); |
| 139 |
|
| 140 |
if ( $mode === null ) { |
| 141 |
return $defaultOptions['mode']; |
| 142 |
} |
| 143 |
if ( is_int( $mode ) ) { |
| 144 |
return $mode; |
| 145 |
} |
| 146 |
|
| 147 |
$mode = trim( strtolower( $mode ) ); |
| 148 |
|
| 149 |
if ( is_numeric( $mode ) ) { |
| 150 |
return (int)$mode; |
| 151 |
} |
| 152 |
|
| 153 |
if ( $mode === 'all' ) { |
| 154 |
$mode = CategoryTreeMode::ALL; |
| 155 |
} elseif ( $mode === 'pages' ) { |
| 156 |
$mode = CategoryTreeMode::PAGES; |
| 157 |
} elseif ( $mode === 'categories' || $mode === 'sub' ) { |
| 158 |
$mode = CategoryTreeMode::CATEGORIES; |
| 159 |
} elseif ( $mode === 'parents' || $mode === 'super' || $mode === 'inverse' ) { |
| 160 |
$mode = CategoryTreeMode::PARENTS; |
| 161 |
} elseif ( $mode === 'default' ) { |
| 162 |
$mode = $defaultOptions['mode']; |
| 163 |
} |
| 164 |
|
| 165 |
return (int)$mode; |
| 166 |
} |
| 167 |
|
| 168 |
|
| 169 |
|
| 170 |
|
| 171 |
|
| 172 |
|
| 173 |
|
| 174 |
public static function decodeBoolean( $value ): bool { |
| 175 |
if ( $value === null ) { |
| 176 |
return false; |
| 177 |
} |
| 178 |
if ( is_bool( $value ) ) { |
| 179 |
return $value; |
| 180 |
} |
| 181 |
if ( is_int( $value ) ) { |
| 182 |
return ( $value > 0 ); |
| 183 |
} |
| 184 |
|
| 185 |
$value = trim( strtolower( $value ) ); |
| 186 |
if ( is_numeric( $value ) ) { |
| 187 |
return ( (int)$value > 0 ); |
| 188 |
} |
| 189 |
|
| 190 |
if ( $value === 'yes' || $value === 'y' |
| 191 |
|| $value === 'true' || $value === 't' || $value === 'on' |
| 192 |
) { |
| 193 |
return true; |
| 194 |
} else { |
| 195 |
return false; |
| 196 |
} |
| 197 |
} |
| 198 |
|
| 199 |
|
| 200 |
|
| 201 |
|
| 202 |
|
| 203 |
private function decodeHidePrefix( $value ) { |
| 204 |
$defaultOptions = $this->config->get( 'CategoryTreeDefaultOptions' ); |
| 205 |
|
| 206 |
if ( $value === null ) { |
| 207 |
return $defaultOptions['hideprefix']; |
| 208 |
} |
| 209 |
if ( is_int( $value ) ) { |
| 210 |
return $value; |
| 211 |
} |
| 212 |
if ( $value === true ) { |
| 213 |
return CategoryTreeHidePrefix::ALWAYS; |
| 214 |
} |
| 215 |
if ( $value === false ) { |
| 216 |
return CategoryTreeHidePrefix::NEVER; |
| 217 |
} |
| 218 |
|
| 219 |
$value = trim( strtolower( $value ) ); |
| 220 |
|
| 221 |
if ( $value === 'yes' || $value === 'y' |
| 222 |
|| $value === 'true' || $value === 't' || $value === 'on' |
| 223 |
) { |
| 224 |
return CategoryTreeHidePrefix::ALWAYS; |
| 225 |
} elseif ( $value === 'no' || $value === 'n' |
| 226 |
|| $value === 'false' || $value === 'f' || $value === 'off' |
| 227 |
) { |
| 228 |
return CategoryTreeHidePrefix::NEVER; |
| 229 |
} elseif ( $value === 'always' ) { |
| 230 |
return CategoryTreeHidePrefix::ALWAYS; |
| 231 |
} elseif ( $value === 'never' ) { |
| 232 |
return CategoryTreeHidePrefix::NEVER; |
| 233 |
} elseif ( $value === 'auto' ) { |
| 234 |
return CategoryTreeHidePrefix::AUTO; |
| 235 |
} elseif ( $value === 'categories' || $value === 'category' || $value === 'smart' ) { |
| 236 |
return CategoryTreeHidePrefix::CATEGORIES; |
| 237 |
} else { |
| 238 |
return $defaultOptions['hideprefix']; |
| 239 |
} |
| 240 |
} |
| 241 |
|
| 242 |
|
| 243 |
|
| 244 |
|
| 245 |
|
| 246 |
|
| 247 |
private static function encodeOptions( array $options, string $enc ) { |
| 248 |
if ( $enc === 'mode' || $enc === '' ) { |
| 249 |
$opt = $options['mode']; |
| 250 |
} elseif ( $enc === 'json' ) { |
| 251 |
$opt = FormatJson::encode( $options ); |
| 252 |
} else { |
| 253 |
throw new InvalidArgumentException( 'Unknown encoding for CategoryTree options: ' . $enc ); |
| 254 |
} |
| 255 |
|
| 256 |
return $opt; |
| 257 |
} |
| 258 |
|
| 259 |
|
| 260 |
|
| 261 |
|
| 262 |
|
| 263 |
public function getOptionsAsCacheKey( ?int $depth = null ): string { |
| 264 |
$key = ''; |
| 265 |
|
| 266 |
foreach ( $this->mOptions as $k => $v ) { |
| 267 |
if ( is_array( $v ) ) { |
| 268 |
$v = implode( '|', $v ); |
| 269 |
} |
| 270 |
$key .= $k . ':' . $v . ';'; |
| 271 |
} |
| 272 |
|
| 273 |
if ( $depth !== null ) { |
| 274 |
$key .= ';depth=' . $depth; |
| 275 |
} |
| 276 |
return $key; |
| 277 |
} |
| 278 |
|
| 279 |
|
| 280 |
|
| 281 |
|
| 282 |
|
| 283 |
public function getOptionsAsJsStructure( ?int $depth = null ) { |
| 284 |
$opt = $this->mOptions; |
| 285 |
if ( $depth !== null ) { |
| 286 |
$opt['depth'] = $depth; |
| 287 |
} |
| 288 |
|
| 289 |
return self::encodeOptions( $opt, 'json' ); |
| 290 |
} |
| 291 |
|
| 292 |
|
| 293 |
|
| 294 |
|
| 295 |
|
| 296 |
|
| 297 |
public function capDepth( int $depth ): int { |
| 298 |
$mode = $this->getOption( 'mode' ); |
| 299 |
$maxDepth = $this->config->get( 'CategoryTreeMaxDepth' ); |
| 300 |
|
| 301 |
if ( is_array( $maxDepth ) ) { |
| 302 |
$max = $maxDepth[$mode] ?? 1; |
| 303 |
} elseif ( is_numeric( $maxDepth ) ) { |
| 304 |
$max = $maxDepth; |
| 305 |
} else { |
| 306 |
wfDebug( __METHOD__ . ': $wgCategoryTreeMaxDepth is invalid.' ); |
| 307 |
$max = 1; |
| 308 |
} |
| 309 |
|
| 310 |
return min( $depth, $max ); |
| 311 |
} |
| 312 |
} |