summaryrefslogtreecommitdiff
path: root/www/wiki/extensions/UploadWizard/UploadWizardHooks.php
blob: 1d8f2520bf9fa1fd4604950eb8b4eabede159295 (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
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
<?php
class UploadWizardHooks {

	/**
	 * Schema update to set up the needed database tables.
	 *
	 * @since 1.2
	 *
	 * @param DatabaseUpdater $updater
	 *
	 * @return true
	 */
	public static function onSchemaUpdate( /* DatabaseUpdater */ $updater = null ) {
		$dbfile = __DIR__ . '/UploadWizard.' . $updater->getDB()->getType() . '.sql';
		if ( !file_exists( $dbfile ) ) {
			$dbfile = __DIR__ . '/UploadWizard.sql';
		}
		$updater->addExtensionTable( 'uw_campaigns', $dbfile );
		$updater->addExtensionUpdate( [
			'addIndex',
			'uw_campaigns',
			'uw_campaigns_name',
			__DIR__ . '/sql/UW_IndexCampaignsName.sql',
			true
		] );
		$updater->addExtensionUpdate( [
			'addIndex',
			'uw_campaigns',
			'uw_campaigns_enabled',
			__DIR__ . '/sql/UW_IndexCampaignsEnabled.sql',
			true
		] );

		return true;
	}

	/**
	 * Adds the preferences of UploadWizard to the list of available ones.
	 * @see https://www.mediawiki.org/wiki/Manual:Hooks/GetPreferences
	 *
	 * @since 1.2
	 *
	 * @param User $user
	 * @param array &$preferences
	 *
	 * @return true
	 */
	public static function onGetPreferences( User $user, array &$preferences ) {
		$config = UploadWizardConfig::getConfig();

		// User preference to skip the licensing tutorial, provided it's not globally disabled
		if ( UploadWizardConfig::getSetting( 'tutorial' ) != [] ) {
			$preferences['upwiz_skiptutorial'] = [
				'type' => 'check',
				'label-message' => 'mwe-upwiz-prefs-skiptutorial',
				'section' => 'uploads/upwiz-interface'
			];
		}

		$preferences['upwiz_licensename'] = [
			'type' => 'text',
			'label-message' => 'mwe-upwiz-prefs-license-name',
			'help-message' => 'mwe-upwiz-prefs-license-name-help',
			'section' => 'uploads/upwiz-licensing'
		];

		if ( UploadWizardConfig::getSetting( 'enableLicensePreference' ) ) {
			$licenseConfig = UploadWizardConfig::getSetting( 'licenses' );

			$licenses = [];

			$licensingOptions = UploadWizardConfig::getSetting( 'licensing' );

			$ownWork = $licensingOptions['ownWork'];
			foreach ( $ownWork['licenses'] as $license ) {
				$licenseMessage = self::getLicenseMessage( $license, $licenseConfig );
				$licenseKey = wfMessage( 'mwe-upwiz-prefs-license-own', $licenseMessage )->text();
				$licenses[$licenseKey] = 'ownwork-' . $license;
			}

			$thirdParty = UploadWizardConfig::getThirdPartyLicenses();
			$hasCustom = false;
			foreach ( $thirdParty as $license ) {
				if ( $license !== 'custom' ) {
					$licenseMessage = self::getLicenseMessage( $license, $licenseConfig );
					$licenseKey = wfMessage( 'mwe-upwiz-prefs-license-thirdparty', $licenseMessage )->text();
					$licenses[$licenseKey] = 'thirdparty-' . $license;
				} else {
					$hasCustom = true;
				}
			}

			$licenses = array_merge(
				[
					wfMessage( 'mwe-upwiz-prefs-def-license-def' )->text() => 'default'
				],
				$licenses
			);

			if ( $hasCustom ) {
				// The "custom license" option must be last, otherwise the text referring to "following
				// wikitext" and "last option above" makes no sense.
				$licenseMessage = self::getLicenseMessage( 'custom', $licenseConfig );
				$licenseKey = wfMessage( 'mwe-upwiz-prefs-license-thirdparty', $licenseMessage )->text();
				$licenses[$licenseKey] = 'thirdparty-custom';
			};

			$preferences['upwiz_deflicense'] = [
				'type' => 'radio',
				'label-message' => 'mwe-upwiz-prefs-def-license',
				'section' => 'uploads/upwiz-licensing',
				'options' => $licenses
			];

			if ( $hasCustom ) {
				$preferences['upwiz_deflicense_custom'] = [
					'type' => 'text',
					'label-message' => 'mwe-upwiz-prefs-def-license-custom',
					'help-message' => 'mwe-upwiz-prefs-def-license-custom-help',
					'section' => 'uploads/upwiz-licensing',
				];
			}
		}

		// Setting for maximum number of simultaneous uploads (always lower than the server-side config)
		if ( $config[ 'maxSimultaneousConnections' ] > 1 ) {
			// Hack to make the key and value the same otherwise options are added wrongly.
			$range = range( 0, $config[ 'maxSimultaneousConnections' ] );
			$range[0] = 'default';

			$preferences['upwiz_maxsimultaneous'] = [
				'type' => 'select',
				'label-message' => 'mwe-upwiz-prefs-maxsimultaneous-upload',
				'section' => 'uploads/upwiz-experimental',
				'options' => $range
			];
		}

		return true;
	}

	/**
	 * Hook to blacklist flickr images by intercepting upload from url
	 * @param string $url
	 * @param bool &$allowed
	 * @return true
	 */
	public static function onIsUploadAllowedFromUrl( $url, &$allowed ) {
		if ( $allowed ) {
			$flickrBlacklist = new UploadWizardFlickrBlacklist(
				UploadWizardConfig::getConfig(),
				RequestContext::getMain()
			);
			if ( $flickrBlacklist->isBlacklisted( $url ) ) {
				$allowed = false;
			}
		}
		return true;
	}

	/**
	 * Get JavaScript test modules
	 * @param array &$testModules
	 * @param ResourceLoader &$resourceLoader
	 */
	public static function onResourceLoaderTestModules(
		array &$testModules,
		ResourceLoader &$resourceLoader
	) {
		$dependencies = [ 'ext.uploadWizard' ];

		// Add our EventLogging schemas (normally lazy-loaded) to dependencies to avoid issues with
		// tests failing intermittently due to "Pending AJAX requests".
		$dependencies[] = 'ext.eventLogging';
		$schemas = array_keys( ExtensionRegistry::getInstance()->getAttribute( 'EventLoggingSchemas' ) );
		$ourSchemas = array_filter( $schemas, function ( $schema ) {
			return substr( $schema, 0, 12 ) === 'UploadWizard';
		} );
		foreach ( $ourSchemas as $schema ) {
			$dependencies[] = "schema.$schema";
		}

		$testModules['qunit']['ext.uploadWizard.unit.tests'] = [
			'scripts' => [
				'tests/qunit/controller/uw.controller.Deed.test.js',
				'tests/qunit/controller/uw.controller.Details.test.js',
				'tests/qunit/controller/uw.controller.Step.test.js',
				'tests/qunit/controller/uw.controller.Thanks.test.js',
				'tests/qunit/controller/uw.controller.Tutorial.test.js',
				'tests/qunit/controller/uw.controller.Upload.test.js',
				'tests/qunit/transports/mw.FormDataTransport.test.js',
				'tests/qunit/uw.EventFlowLogger.test.js',
				'tests/qunit/uw.ConcurrentQueue.test.js',
				'tests/qunit/mw.UploadWizardUpload.test.js',
				'tests/qunit/mw.UploadWizardLicenseInput.test.js',
				'tests/qunit/mw.FlickrChecker.test.js',
				'tests/qunit/uw.TitleDetailsWidget.test.js',
				'tests/qunit/mw.fileApi.test.js',
			],
			'dependencies' => $dependencies,
			'localBasePath' => __DIR__,
			'remoteExtPath' => 'UploadWizard',
		];
	}

	/**
	 * Helper function to get the message for a license.
	 *
	 * @since 1.2
	 *
	 * @param string $licenseName
	 * @param array $licenseConfig
	 *
	 * @return string
	 */
	public static function getLicenseMessage( $licenseName, array $licenseConfig ) {
		if ( array_key_exists( 'url', $licenseConfig[$licenseName] ) ) {
			return wfMessage(
				$licenseConfig[$licenseName]['msg'],
				'',
				$licenseConfig[$licenseName]['url']
			)->parse();
		} else {
			return wfMessage( $licenseConfig[$licenseName]['msg'] )->text();
		}
	}

	/**
	 * Lists tags used by UploadWizard (via ListDefinedTags,
	 * ListExplicitlyDefinedTags & ChangeTagsListActive hooks)
	 *
	 * @param array &$tags
	 * @return bool true
	 */
	public static function onListDefinedTags( &$tags ) {
		$tags[] = 'uploadwizard';
		$tags[] = 'uploadwizard-flickr';
		return true;
	}
}