summaryrefslogtreecommitdiff
path: root/www/wiki/extensions/UploadWizard/includes/UploadWizardConfig.php
blob: 774e045d8f5917a3f94e24511c45d545451ee106 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
<?php

/**
 * Static class with methods for interacting with the Upload Wizards configuration.
 *
 * @file
 * @ingroup Upload
 *
 * @since 1.2
 *
 * @license GPL-2.0-or-later
 * @author Jeroen De Dauw < jeroendedauw@gmail.com >
 */
class UploadWizardConfig {

	/**
	 * Returns true if any of the keys of an array is a string
	 *
	 * @param array $array
	 *
	 * @return bool
	 */
	private static function is_assoc( $array ) {
		return (bool)count( array_filter( array_keys( $array ), 'is_string' ) );
	}

	/**
	 * Same functionality as array_merge_recursive, but sanely
	 * It treats 'normal' integer indexed arrays as scalars, and does
	 * not recurse into them. Associative arrays are recursed into
	 *
	 * @param array $array
	 * @param array $array1
	 *
	 * @return array Yet another array, sanely replacing contents of $array with $array1
	 */
	public static function array_replace_sanely( $array, $array1 ) {
		$newArray = [];

		foreach ( $array as $key => $value ) {
			if ( array_key_exists( $key, $array1 ) ) {
				switch ( gettype( $value ) ) {
				case "array":
					if ( self::is_assoc( $array[$key] ) ) {
						$newArray[$key] = self::array_replace_sanely( $array[$key], $array1[$key] );
						break;
					}
					# fall through
				default:
					$newArray[$key] = $array1[$key];
					break;
				}
			} else {
				$newArray[$key] = $array[$key];
			}
		}
		$newArray = array_merge( $newArray, array_diff_key( $array1, $array ) );
		return $newArray;
	}

	/**
	 * Holder for configuration specified via url arguments.
	 * This will override other config when returned via getConfig.
	 *
	 * @since 1.2
	 * @var array
	 */
	protected static $urlConfig = [];

	/**
	 * Returns the globally configuration, optionally combined with campaign specific
	 * configuration.
	 *
	 * @since 1.2
	 *
	 * @param string|null $campaignName
	 *
	 * @return array
	 */
	public static function getConfig( $campaignName = null ) {
		global $wgUploadWizardConfig;
		static $mergedConfig = false;

		if ( !$mergedConfig ) {
			$wgUploadWizardConfig = self::array_replace_sanely(
				self::getDefaultConfig(),
				$wgUploadWizardConfig
			);
			$mergedConfig = true;
		}

		if ( !is_null( $campaignName ) ) {
			$wgUploadWizardConfig = self::array_replace_sanely(
				$wgUploadWizardConfig,
				self::getCampaignConfig( $campaignName )
			);
		}

		return array_replace_recursive( $wgUploadWizardConfig, self::$urlConfig );
	}

	/**
	 * Returns the value of a single configuration setting.
	 *
	 * @since 1.2
	 *
	 * @param string $settingName
	 * @param string|null $campaignName
	 *
	 * @return mixed
	 */
	public static function getSetting( $settingName, $campaignName = null ) {
		$config = self::getConfig( $campaignName );
		return $config[$settingName];
	}

	/**
	 * Sets a configuration setting provided by URL.
	 * This will override other config when returned via getConfig.
	 *
	 * @param string $name
	 * @param mixed $value
	 *
	 * @since 1.2
	 */
	public static function setUrlSetting( $name, $value ) {
		self::$urlConfig[$name] = $value;
	}

	/**
	 * Returns the default global config, from UploadWizard.config.php.
	 *
	 * @since 1.2
	 *
	 * @return array
	 */
	protected static function getDefaultConfig() {
		$configPath = dirname( __DIR__ ) . '/UploadWizard.config.php';
		return is_file( $configPath ) ? include $configPath : [];
	}

	/**
	 * Returns the configuration of the specified campaign,
	 * or an empty array when the campaign is not found or not enabled.
	 *
	 * @since 1.2
	 *
	 * @param string $campaignName
	 *
	 * @return array
	 */
	protected static function getCampaignConfig( $campaignName ) {
		if ( !is_null( $campaignName ) ) {
			$campaign = UploadWizardCampaign::newFromName( $campaignName );

			if ( $campaign !== false && $campaign->getIsEnabled() ) {
				return $campaign->getParsedConfig();
			}
		}

		return [];
	}

	/**
	 * Get a list of available third party licenses from the config.
	 *
	 * @since 1.2
	 *
	 * @return array
	 */
	public static function getThirdPartyLicenses() {
		$licensing = self::getSetting( 'licensing' );
		$thirdParty = $licensing['thirdParty'];
		$licenses = [];

		foreach ( $thirdParty['licenseGroups'] as $group ) {
			$licenses = array_merge( $licenses, $group['licenses'] );
		}

		return $licenses;
	}

}