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