summaryrefslogtreecommitdiff
path: root/www/wiki/extensions/UploadWizard/maintenance/migrateCampaigns.php
blob: de3135f6a5d16d37e2cfec170265d59d595a70c6 (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
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
<?php
/**
 * Fixes timestamp corruption caused by one or more webservers temporarily
 * being set to the wrong time.
 * The time offset must be known and consistent. Start and end times
 * (in 14-character format) restrict the search, and must bracket the damage.
 * There must be a majority of good timestamps in the search period.
 *
 * This program is free software; you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation; either version 2 of the License, or
 * (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License along
 * with this program; if not, write to the Free Software Foundation, Inc.,
 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
 * http://www.gnu.org/copyleft/gpl.html
 *
 * @file
 * @ingroup Maintenance
 * @author Yuvi Panda <yuvipanda@gmail.com>
 */

$IP = getenv( 'MW_INSTALL_PATH' );
if ( $IP === false ) {
	$IP = __DIR__ . '/../../..';
}
require_once "$IP/maintenance/Maintenance.php";

/**
 * Maintenance script to migrate campaigns from older, database table
 * to newer page based storage
 *
 * @ingroup Maintenance
 */
class MigrateCampaigns extends Maintenance {

	/**
	 * @var \Wikimedia\Rdbms\IDatabase
	 */
	private $dbr = null;

	public function __construct() {
		parent::__construct();

		$this->requireExtension( 'Upload Wizard' );
		$this->mDescription = "Migrate UploadCampaigns from database storage to pages";
		$this->addOption( 'user', 'The user to perform the migration as', false, true, 'u' );
	}

	private $oldKeyDefaults = [
		'headerLabelPage' => '',
		'thanksLabelPage' => '',

		'defaultLicenseType' => 'choice',
		'ownWorkOption' => 'choice',
		'licensesOwnWork' => '',
		'defaultOwnWorkLicense' => '',

		'skipTutorial' => false,
		'tutorialTemplate' => 'Licensing_tutorial_$1.svg',
		'tutorialWidth' => 720,
		'tutorialHelpdeskCoords' => '27, 1319, 691, 1384',

		'autoWikiText' => '',
		'autoCategories' => '',

		'defaultDescription' => '',
		'defaultLat' => '',
		'defaultLon' => '',
		'defaultAlt' => '',
		'defaultCategories' => '',

		'idField' => '',
		'idFieldLabel' => '',
		'idFieldLabelPage' => '',
		'idFieldMaxLength' => 25,
		'idFieldInitialValue' => '',

		'idField2' => '',
		'idField2Label' => '',
		'idField2LabelPage' => '',
		'idField2MaxLength' => 25,
		'idField2InitialValue' => ''
	];

	private $oldNumberConfigs = [
		'idFieldMaxLength',
		'idField2MaxLength',
		'tutorialWidth',
		'defaultLat',
		'defaultLon',
		'defaultAlt'
	];

	/**
	 * @param int|string $id
	 * @return array
	 */
	private function getConfigFromDB( $id ) {
		$config = [];

		$confProps = $this->dbr->select(
			'uw_campaign_conf',
			[ 'cc_property', 'cc_value' ],
			[ 'cc_campaign_id' => $id ],
			__METHOD__
		);

		foreach ( $confProps as $confProp ) {
			if ( in_array( $confProp->cc_property, $this->oldNumberConfigs ) ) {
				$config[$confProp->cc_property] = intval( $confProp->cc_value );
			} else {
				$config[$confProp->cc_property] = $confProp->cc_value;
			}
		}

		$mergedConfig = [];

		foreach ( $this->oldKeyDefaults as $key => $default ) {
			if ( array_key_exists( $key, $config ) && $config[$key] !== $default ) {
				$mergedConfig[$key] = $config[$key];
			} else {
				$mergedConfig[$key] = null;
			}
		}

		return $mergedConfig;
	}

	/**
	 * @param string $string
	 * @return array
	 */
	private function explodeStringToArray( $string ) {
		$parts = explode( '|', $string );
		$array = [];

		foreach ( $parts as $part ) {
			$part = trim( $part );

			if ( $part !== '' ) {
				$array[] = $part;
			}
		}
		return $array;
	}

	/**
	 * @param array $array
	 * @return array
	 */
	private function trimArray( $array ) {
		$newArray = [];
		foreach ( $array as $key => $value ) {
			if ( gettype( $value ) === 'array' ) {
				$trimmedValue = $this->trimArray( $value );
				if ( $trimmedValue !== [] ) {
					$newArray[$key] = $trimmedValue;
				}
			} else {
				if ( $value !== null ) {
					$newArray[$key] = $value;
				}
			}
		}
		return $newArray;
	}

	/**
	 * Ensure that the default license, if set, is the first
	 *
	 * @param array $licenses
	 * @param string $default
	 * @return array
	 */
	private function ensureDefaultLicense( $licenses, $default ) {
		if ( count( $licenses ) === 1 || ( $default === null || trim( $default ) === '' ) ) {
			return $licenses;
		}
		if ( in_array( $default, $licenses ) ) {
			array_splice( $licenses, array_search( $default, $licenses ), 1 );
		}
		array_unshift( $licenses, $default );
		// FIXME: No return value
	}

	/**
	 * @param object $campaign
	 * @param array $oldConfig
	 * @return array
	 */
	private function getConfigForJSON( $campaign, $oldConfig ) {
		$config = [
			'enabled' => $campaign->campaign_enabled === '1',
			'display' => [
				'headerLabelPage' => $oldConfig['headerLabelPage'],
				'thanksLabelPage' => $oldConfig['thanksLabelPage']
			],
			'defaults' => [
				'description' => $oldConfig['defaultDescription'],
				'lat' => $oldConfig['defaultLat'],
				'lon' => $oldConfig['defaultLon'],
				'categories' => $this->explodeStringToArray( $oldConfig['defaultCategories'] )
			],
			'autoAdd' => [
				'wikitext' => $oldConfig['autoWikiText'],
				'categories' => $this->explodeStringToArray( $oldConfig['autoCategories'] )
			],
			"licensing" => [
				'defaultType' => $oldConfig['defaultLicenseType'],
				'ownWorkDefault' => $oldConfig['ownWorkOption'],
				'ownWork' => [
					'licenses' => $this->ensureDefaultLicense(
						$this->explodeStringToArray( $oldConfig['licensesOwnWork'] ),
						$oldConfig['defaultOwnWorkLicense']
					)
				]
			],
			'fields' => [
				[
					'wikitext' => $oldConfig['idField'],
					'label' => $oldConfig['idFieldLabel'],
					# Migrated even though this is a nop.
					# People will have to migrate this manually
					'labelPage' => $oldConfig['idFieldLabelPage'],
					'maxLength' => $oldConfig['idFieldMaxLength'],
					'initialValue' => $oldConfig['idFieldInitialValue']
				],
				[
					'wikitext' => $oldConfig['idField2'],
					'label' => $oldConfig['idField2Label'],
					'labelPage' => $oldConfig['idField2LabelPage'],
					'maxLength' => $oldConfig['idField2MaxLength'],
					'initialValue' => $oldConfig['idField2InitialValue']
				]
			],
			'tutorial' => [
				'skip' => (bool)$oldConfig['skipTutorial'],
				'template' => $oldConfig['tutorialTemplate'],
				'helpdeskCoords' => $oldConfig['tutorialHelpdeskCoords'],
				'width' => $oldConfig['tutorialWidth']
			]
		];

		return $this->trimArray( $config );
	}

	public function execute() {
		$user = $this->getOption( 'user', 'Maintenance script' );

		$this->dbr = wfGetDB( DB_MASTER );
		$campaigns = $this->dbr->select(
			'uw_campaigns',
			'*'
		);

		if ( !$campaigns->numRows() ) {
			$this->output( "Nothing to migrate.\n" );
			return;
		}

		foreach ( $campaigns as $campaign ) {
			$oldConfig = $this->getConfigFromDB( $campaign->campaign_id );
			$newConfig = $this->getConfigForJSON( $campaign, $oldConfig );

			$title = Title::makeTitleSafe( NS_CAMPAIGN, $campaign->campaign_name );
			$page = Wikipage::factory( $title );

			$content = new CampaignContent( json_encode( $newConfig ) );
			$page->doEditContent(
				$content,
				"Migrating from old campaign tables",
				0, false,
				User::newFromName( $user )
			);
			$this->output( "Migrated {$campaign->campaign_name}\n" );
		}
	}
}

$maintClass = "MigrateCampaigns";
require_once RUN_MAINTENANCE_IF_MAIN;