Code Coverage
 
Lines
Functions and Methods
Classes and Traits
Total
16.67% covered (danger)
16.67%
7 / 42
25.00% covered (danger)
25.00%
2 / 8
CRAP
0.00% covered (danger)
0.00%
0 / 1
Config
16.67% covered (danger)
16.67%
7 / 42
25.00% covered (danger)
25.00%
2 / 8
251.48
0.00% covered (danger)
0.00%
0 / 1
 isAssoc
0.00% covered (danger)
0.00%
0 / 1
0.00% covered (danger)
0.00%
0 / 1
2
 arrayReplaceSanely
0.00% covered (danger)
0.00%
0 / 12
0.00% covered (danger)
0.00%
0 / 1
42
 getConfig
30.77% covered (danger)
30.77%
4 / 13
0.00% covered (danger)
0.00%
0 / 1
5.99
 getSetting
100.00% covered (success)
100.00%
2 / 2
100.00% covered (success)
100.00%
1 / 1
1
 setUrlSetting
100.00% covered (success)
100.00%
1 / 1
100.00% covered (success)
100.00%
1 / 1
1
 getDefaultConfig
0.00% covered (danger)
0.00%
0 / 2
0.00% covered (danger)
0.00%
0 / 1
6
 getCampaignConfig
0.00% covered (danger)
0.00%
0 / 5
0.00% covered (danger)
0.00%
0 / 1
20
 getThirdPartyLicenses
0.00% covered (danger)
0.00%
0 / 6
0.00% covered (danger)
0.00%
0 / 1
6
1<?php
2
3namespace MediaWiki\Extension\UploadWizard;
4
5/**
6 * Static class with methods for interacting with the Upload Wizards configuration.
7 *
8 * @file
9 * @ingroup Upload
10 *
11 * @since 1.2
12 *
13 * @license GPL-2.0-or-later
14 * @author Jeroen De Dauw < jeroendedauw@gmail.com >
15 */
16class Config {
17
18    /**
19     * Returns true if any of the keys of an array is a string
20     *
21     * @param array $array
22     *
23     * @return bool
24     */
25    private static function isAssoc( $array ) {
26        return (bool)count( array_filter( array_keys( $array ), 'is_string' ) );
27    }
28
29    /**
30     * Same functionality as array_merge_recursive, but sanely
31     * It treats 'normal' integer indexed arrays as scalars, and does
32     * not recurse into them. Associative arrays are recursed into
33     *
34     * @param array $array
35     * @param array $array1
36     *
37     * @return array Yet another array, sanely replacing contents of $array with $array1
38     */
39    public static function arrayReplaceSanely( $array, $array1 ) {
40        $newArray = [];
41
42        foreach ( $array as $key => $value ) {
43            if ( array_key_exists( $key, $array1 ) ) {
44                switch ( gettype( $value ) ) {
45                    case "array":
46                        if ( self::isAssoc( $array[$key] ) ) {
47                            $newArray[$key] = self::arrayReplaceSanely( $array[$key], $array1[$key] );
48                            break;
49                        }
50                        # fall through
51                    default:
52                        $newArray[$key] = $array1[$key];
53                        break;
54                }
55            } else {
56                $newArray[$key] = $array[$key];
57            }
58        }
59        return array_merge( $newArray, array_diff_key( $array1, $array ) );
60    }
61
62    /**
63     * Holder for configuration specified via url arguments.
64     * This will override other config when returned via getConfig.
65     *
66     * @since 1.2
67     * @var array
68     */
69    protected static $urlConfig = [];
70
71    /**
72     * Returns the globally configuration, optionally combined with campaign specific
73     * configuration.
74     *
75     * @since 1.2
76     *
77     * @param string|null $campaignName
78     *
79     * @return array
80     */
81    public static function getConfig( $campaignName = null ) {
82        global $wgUploadWizardConfig;
83        static $mergedConfig = false;
84
85        if ( !$mergedConfig ) {
86            $wgUploadWizardConfig = self::arrayReplaceSanely(
87                self::getDefaultConfig(),
88                $wgUploadWizardConfig
89            );
90            $mergedConfig = true;
91        }
92
93        if ( $campaignName !== null ) {
94            $wgUploadWizardConfig = self::arrayReplaceSanely(
95                $wgUploadWizardConfig,
96                self::getCampaignConfig( $campaignName )
97            );
98        }
99
100        return array_replace_recursive( $wgUploadWizardConfig, self::$urlConfig );
101    }
102
103    /**
104     * Returns the value of a single configuration setting.
105     *
106     * @since 1.2
107     *
108     * @param string $settingName
109     * @param string|null $campaignName
110     *
111     * @return mixed
112     */
113    public static function getSetting( $settingName, $campaignName = null ) {
114        $config = self::getConfig( $campaignName );
115        return $config[$settingName];
116    }
117
118    /**
119     * Sets a configuration setting provided by URL.
120     * This will override other config when returned via getConfig.
121     *
122     * @param string $name
123     * @param mixed $value
124     *
125     * @since 1.2
126     */
127    public static function setUrlSetting( $name, $value ) {
128        self::$urlConfig[$name] = $value;
129    }
130
131    /**
132     * Returns the default global config, from UploadWizard.config.php.
133     *
134     * @since 1.2
135     *
136     * @return array
137     */
138    protected static function getDefaultConfig() {
139        $configPath = dirname( __DIR__ ) . '/UploadWizard.config.php';
140        return is_file( $configPath ) ? include $configPath : [];
141    }
142
143    /**
144     * Returns the configuration of the specified campaign,
145     * or an empty array when the campaign is not found or not enabled.
146     *
147     * @since 1.2
148     *
149     * @param string $campaignName
150     *
151     * @return array
152     */
153    protected static function getCampaignConfig( $campaignName ) {
154        if ( $campaignName !== null ) {
155            $campaign = Campaign::newFromName( $campaignName );
156
157            if ( $campaign !== false && $campaign->getIsEnabled() ) {
158                return $campaign->getParsedConfig();
159            }
160        }
161
162        return [];
163    }
164
165    /**
166     * Get a list of available third party licenses from the config.
167     *
168     * @since 1.2
169     *
170     * @return array
171     */
172    public static function getThirdPartyLicenses() {
173        $licensing = self::getSetting( 'licensing' );
174        $thirdParty = $licensing['thirdParty'];
175        $licenses = [];
176
177        foreach ( $thirdParty['licenseGroups'] as $group ) {
178            $licenses = array_merge( $licenses, $group['licenses'] );
179        }
180
181        return $licenses;
182    }
183
184}