aboutsummaryrefslogtreecommitdiffstats
path: root/maintenance/migrateFileTables.php
blob: d4020592734422882b739a76dcfec851a35a6a11 (plain) (blame)
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
289
290
291
292
293
294
<?php
/**
 * Maintenance script to refresh image metadata fields.
 *
 * 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
 */

// @codeCoverageIgnoreStart
require_once __DIR__ . '/Maintenance.php';
// @codeCoverageIgnoreEnd

use MediaWiki\Maintenance\Maintenance;
use Wikimedia\Rdbms\IMaintainableDatabase;
use Wikimedia\Rdbms\SelectQueryBuilder;

/**
 * Maintenance script to refresh image metadata fields.
 *
 * @ingroup Maintenance
 */
class MigrateFileTables extends Maintenance {

	/**
	 * @var IMaintainableDatabase
	 */
	protected $dbw;

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

		$this->addDescription( 'Script to migrate from image/oldimage tables to file/filerevision' );
		$this->setBatchSize( 200 );

		$this->addOption( 'start', 'Name of file to start with', false, true );
		$this->addOption( 'end', 'Name of file to end with', false, true );
		$this->addOption(
			'sleep',
			'Time to sleep between each batch (in seconds). Default: 0',
			false,
			true
		);
	}

	public function execute() {
		$verbose = $this->hasOption( 'verbose' );
		$start = $this->getOption( 'start', false );
		$sleep = (int)$this->getOption( 'sleep', 0 );

		$dbw = $this->getPrimaryDB();
		$queryBuilderTemplate = $dbw->newSelectQueryBuilder()
			->select(
				[
					'img_name',
					'img_size',
					'img_width',
					'img_height',
					'img_metadata',
					'img_bits',
					'img_media_type',
					'img_major_mime',
					'img_minor_mime',
					'img_timestamp',
					'img_sha1',
					'img_actor',
					'img_metadata',
					'img_description_id',
					'img_description_text' => 'comment_img_description.comment_text',
					'img_description_data' => 'comment_img_description.comment_data',
					'img_description_cid' => 'comment_img_description.comment_id'
				]
			)
			->from( 'image' )
			->join(
				'comment',
				'comment_img_description',
				'comment_img_description.comment_id = img_description_id'
			);
		$totalRowsInserted = 0;
		$filesHandled = 0;
		$batchSize = intval( $this->getBatchSize() );
		if ( $batchSize <= 0 ) {
			$this->fatalError( "Batch size is too low...", 12 );
		}
		$end = $this->getOption( 'end', false );
		if ( $end !== false ) {
			$queryBuilderTemplate->andWhere( $dbw->expr( 'img_name', '<=', $end ) );
		}
		$queryBuilderTemplate
			->orderBy( 'img_name', SelectQueryBuilder::SORT_ASC )
			->limit( $batchSize );

		$batchCondition = [];
		// For the WHERE img_name > 'foo' condition that comes after doing a batch
		if ( $start !== false ) {
			$batchCondition[] = $dbw->expr( 'img_name', '>=', $start );
		}
		do {
			$queryBuilder = clone $queryBuilderTemplate;
			$res = $queryBuilder->andWhere( $batchCondition )
				->caller( __METHOD__ )->fetchResultSet();
			if ( $res->numRows() > 0 ) {
				$row1 = $res->current();
				$this->output( "Processing next {$res->numRows()} row(s) starting with {$row1->img_name}.\n" );
				$res->rewind();
			}

			foreach ( $res as $row ) {
				$rowsInserted = $this->handleFile( $row );
				$filesHandled += 1;
				$totalRowsInserted += $rowsInserted;

				$this->output( "Migrated File:{$row->img_name}. Inserted $rowsInserted rows.\n" );
			}
			if ( $res->numRows() > 0 ) {
				// @phan-suppress-next-line PhanPossiblyUndeclaredVariable rows contains at least one item
				$batchCondition = [ $dbw->expr( 'img_name', '>', $row->img_name ) ];
			}
			$this->waitForReplication();
			if ( $sleep ) {
				sleep( $sleep );
			}
		} while ( $res->numRows() === $batchSize );

		$this->output( "\nFinished migration for $filesHandled files. "
			. "$totalRowsInserted rows have been inserted into filerevision table.\n" );
	}

	private function handleFile( stdClass $row ) {
		$repo = $this->getServiceContainer()->getRepoGroup()
			->newCustomLocalRepo();
		$dbw = $this->getPrimaryDB();
		$rowsInserted = 0;

		// LocalFile doesn't like it when the row holds img_description_id
		$imgDescriptionId = $row->img_description_id;
		unset( $row->img_description_id );

		$file = $repo->newFileFromRow( $row );

		// Lock everything we can
		$file->acquireFileLock();
		$dbw->startAtomic( __METHOD__ );
		$dbw->newSelectQueryBuilder()
			->select( '*' )
			->forUpdate()
			->from( 'image' )
			->where( [ 'img_name' => $row->img_name ] )
			->caller( __METHOD__ )->fetchRow();
		$oldimageRows = $dbw->newSelectQueryBuilder()
			->select( '*' )
			->forUpdate()
			->from( 'oldimage' )
			->where( [ 'oi_name' => $row->img_name ] )
			->orderBy( 'oi_timestamp', 'ASC' )
			->caller( __METHOD__ )->fetchResultSet();
		$dbw->newSelectQueryBuilder()
			->select( '*' )
			->forUpdate()
			->from( 'file' )
			->where( [ 'file_name' => $row->img_name ] )
			->caller( __METHOD__ )->fetchRow();

		// Make sure the row exists in file table
		$fileId = $file->acquireFileIdFromName();
		$fileRevisionRows = $dbw->newSelectQueryBuilder()
			->select( '*' )
			->forUpdate()
			->from( 'filerevision' )
			->where( [ 'fr_file' => $fileId ] )
			->caller( __METHOD__ )->fetchResultSet();

		// Make sure the filerevision rows exist
		foreach ( $oldimageRows as $oldimageRow ) {
			$timestamp = $oldimageRow->oi_timestamp;
			$sha1 = $oldimageRow->oi_sha1;

			$alreadyDone = false;
			foreach ( $fileRevisionRows as $fileRevisionRow ) {
				if (
					$timestamp === $fileRevisionRow->fr_timestamp &&
					$sha1 === $fileRevisionRow->fr_sha1
				) {
					// This assume the combination of oi_timestamp and oi_sha1
					// will be always unique which is not the case in production
					// but also all of them were duplicate old uploads and we are
					// willing to simply insert one row only. See T67264
					$alreadyDone = true;
					break;
				}
			}

			if ( $alreadyDone ) {
				continue;
			}

			$dbw->newInsertQueryBuilder()
				->insertInto( 'filerevision' )
				->row(
					[
						'fr_file' => $fileId,
						'fr_size' => $oldimageRow->oi_size,
						'fr_width' => $oldimageRow->oi_width,
						'fr_height' => $oldimageRow->oi_height,
						'fr_metadata' => $oldimageRow->oi_metadata,
						'fr_bits' => $oldimageRow->oi_bits,
						'fr_description_id' => $oldimageRow->oi_description_id,
						'fr_actor' => $oldimageRow->oi_actor,
						'fr_timestamp' => $oldimageRow->oi_timestamp,
						'fr_sha1' => $oldimageRow->oi_sha1,
						'fr_archive_name' => $oldimageRow->oi_archive_name,
						'fr_deleted' => $oldimageRow->oi_deleted,
					]
				)
				->caller( __METHOD__ )->execute();
			$rowsInserted += 1;
		}

		// Make sure the image row (most current version) is there
		$timestamp = $row->img_timestamp;
		$sha1 = $row->img_sha1;

		$alreadyDone = false;
		foreach ( $fileRevisionRows as $fileRevisionRow ) {
			if (
				$timestamp === $fileRevisionRow->fr_timestamp &&
				$sha1 === $fileRevisionRow->fr_sha1
			) {
				$alreadyDone = true;
				break;
			}
		}

		if ( !$alreadyDone ) {
			$dbw->newInsertQueryBuilder()
				->insertInto( 'filerevision' )
				->row(
					[
						'fr_file' => $fileId,
						'fr_size' => $row->img_size,
						'fr_width' => $row->img_width,
						'fr_height' => $row->img_height,
						'fr_metadata' => $row->img_metadata,
						'fr_bits' => $row->img_bits,
						'fr_description_id' => $imgDescriptionId,
						'fr_actor' => $row->img_actor,
						'fr_timestamp' => $row->img_timestamp,
						'fr_sha1' => $row->img_sha1,
						'fr_archive_name' => '',
						'fr_deleted' => 0,
					]
				)
				->caller( __METHOD__ )->execute();
			$rowsInserted += 1;
		}

		// Make sure file has the latest filerevision
		$latestFrId = $dbw->newSelectQueryBuilder()
			->select( 'fr_id' )
			->from( 'filerevision' )
			->where( [ 'fr_file' => $fileId ] )
			->orderBy( 'fr_timestamp', 'DESC' )
			->fetchField();
		$dbw->newUpdateQueryBuilder()
			->update( 'file' )
			->set( [ 'file_latest' => $latestFrId ] )
			->where( [ 'file_id' => $fileId ] )
			->caller( __METHOD__ )->execute();

		$dbw->endAtomic( __METHOD__ );
		$file->releaseFileLock();
		return $rowsInserted;
	}
}

// @codeCoverageIgnoreStart
$maintClass = MigrateFileTables::class;
require_once RUN_MAINTENANCE_IF_MAIN;
// @codeCoverageIgnoreEnd