Code Coverage |
||||||||||
Lines |
Functions and Methods |
Classes and Traits |
||||||||
Total | |
100.00% |
18 / 18 |
|
100.00% |
4 / 4 |
CRAP | |
100.00% |
1 / 1 |
ConfigBase | |
100.00% |
18 / 18 |
|
100.00% |
4 / 4 |
11 | |
100.00% |
1 / 1 |
getConfigArray | n/a |
0 / 0 |
n/a |
0 / 0 |
0 | |||||
getSetting | |
100.00% |
1 / 1 |
|
100.00% |
1 / 1 |
1 | |||
getAvailableLicenses | |
100.00% |
5 / 5 |
|
100.00% |
1 / 1 |
2 | |||
isAssoc | |
100.00% |
1 / 1 |
|
100.00% |
1 / 1 |
1 | |||
arrayReplaceSanely | |
100.00% |
11 / 11 |
|
100.00% |
1 / 1 |
7 |
1 | <?php |
2 | |
3 | namespace MediaWiki\Extension\MediaUploader\Config; |
4 | |
5 | /** |
6 | * Abstract base for all config classes. Provides a config retrieval interface |
7 | * and protected utility functions. |
8 | */ |
9 | abstract class ConfigBase { |
10 | public const LIC_OWN_WORK = 'ownWork'; |
11 | public const LIC_THIRD_PARTY = 'thirdParty'; |
12 | |
13 | /** |
14 | * Returns the entire configuration array. |
15 | * |
16 | * @return array |
17 | */ |
18 | abstract public function getConfigArray(): array; |
19 | |
20 | /** |
21 | * Returns a specific setting of the configuration array. |
22 | * |
23 | * @param string $key |
24 | * @param mixed $default Default value if $key is not found in the array |
25 | * |
26 | * @return mixed $default if the key does not exist. |
27 | */ |
28 | public function getSetting( string $key, $default = null ) { |
29 | return $this->getConfigArray()[$key] ?? $default; |
30 | } |
31 | |
32 | /** |
33 | * Get a list of available licenses for a given deed type (own work or third party) |
34 | * from the config. |
35 | * |
36 | * @param string $type one of ConfigBase::LIC_* constants |
37 | * |
38 | * @return array |
39 | */ |
40 | public function getAvailableLicenses( string $type ): array { |
41 | $licensing = $this->getSetting( 'licensing', [] )[$type] ?? []; |
42 | $licenses = $licensing['licenses'] ?? []; |
43 | |
44 | foreach ( $licensing['licenseGroups'] ?? [] as $group ) { |
45 | $licenses = array_merge( $licenses, $group['licenses'] ); |
46 | } |
47 | |
48 | return array_unique( $licenses ); |
49 | } |
50 | |
51 | /** |
52 | * Returns true if any of the keys of an array is a string |
53 | * |
54 | * @param array $array |
55 | * |
56 | * @return bool |
57 | */ |
58 | private function isAssoc( array $array ): bool { |
59 | return (bool)count( array_filter( array_keys( $array ), 'is_string' ) ); |
60 | } |
61 | |
62 | /** |
63 | * Same functionality as array_merge_recursive, but sanely |
64 | * It treats 'normal' integer indexed arrays as scalars, and does |
65 | * not recurse into them. Associative arrays are recursed into. |
66 | * |
67 | * Null values in the second array will result in unset keys. |
68 | * |
69 | * @param array $array |
70 | * @param array $array1 |
71 | * |
72 | * @return array Yet another array, sanely replacing contents of $array with $array1 |
73 | */ |
74 | final protected function arrayReplaceSanely( array $array, array $array1 ): array { |
75 | $newArray = []; |
76 | |
77 | foreach ( $array as $key => $value ) { |
78 | if ( array_key_exists( $key, $array1 ) ) { |
79 | $value1 = $array1[$key]; |
80 | if ( $value1 === null ) { |
81 | // Special case: if the new array has this value as null, unset it entirely. |
82 | // This is useful for removing parts of the config in campaigns. |
83 | continue; |
84 | } if ( is_array( $value ) && is_array( $value1 ) && $this->isAssoc( $value ) ) { |
85 | $newArray[$key] = $this->arrayReplaceSanely( $value, $value1 ); |
86 | } else { |
87 | $newArray[$key] = $value1; |
88 | } |
89 | } else { |
90 | $newArray[$key] = $value; |
91 | } |
92 | } |
93 | |
94 | return array_merge( $newArray, array_diff_key( $array1, $array ) ); |
95 | } |
96 | } |