-
Notifications
You must be signed in to change notification settings - Fork 6
/
grabNewFiles.php
724 lines (676 loc) · 25.4 KB
/
grabNewFiles.php
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
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
<?php
/**
* Updates an existing database that has imported files from a pre-existing
* to its current state.
*
* @file
* @ingroup Maintenance
* @author Jesús Martínez <[email protected]>
* @date 5 August 2019
* @version 1.0
* @note Based on code by Calimonious the Estrange, Misza, Jack Phoenix and Edward Chernenko.
*/
require_once 'includes/FileGrabber.php';
class GrabNewFiles extends FileGrabber {
protected ?string $startDate;
protected ?string $endDate;
/**
* A list of page titles of images that have uploads pending
*
* @var array
*/
protected $pendingUploads = [];
/**
* A list of page titles of images that need checking for deletions and restores.
* Associative array with reason and user, necessary for deleting files
*
* @var array
*/
protected $pendingDeletions = [];
public function __construct() {
parent::__construct();
$this->addDescription(
"Grabs updates to files from an external wiki\nFor use when files have been imported already and want to keep track of new uploads."
);
$this->addOption( 'startdate', 'Start point (20121222142317, 2012-12-22T14:23:17Z, etc); note that this cannot go back further than 1-3 months on most projects.', true /* required? */, true /* withArg */ );
$this->addOption( 'enddate', 'Date after which to ignore new files (20121222142317, 2012-12-22T14:23:17Z, etc); note that the process may fail to process existing files that have been moved after this date', false, true );
}
public function execute() {
parent::execute();
$this->startDate = $this->getOption( 'startdate' );
if ( $this->startDate ) {
$this->startDate = wfTimestamp( TS_MW, $this->startDate );
if ( !$this->startDate ) {
$this->fatalError( 'Invalid startdate format.' );
}
} else {
$this->fatalError( 'A timestamp to start from is required.' );
}
$this->endDate = $this->getOption( 'enddate' );
if ( $this->endDate ) {
$this->endDate = wfTimestamp( TS_MW, $this->endDate );
if ( !$this->endDate ) {
$this->fatalError( 'Invalid enddate format.' );
}
} else {
$this->endDate = wfTimestampNow();
}
$params = [
'list' => 'logevents',
'leprop' => 'ids|title|type|timestamp|details|comment|user|userid',
'leend' => (string)$this->endDate,
'ledir' => 'newer',
'lestart' => (string)$this->startDate,
'lelimit' => 'max'
];
$more = true;
$count = 0;
$this->output( "Processing and downloading changes from files...\n" );
while ( $more ) {
$result = $this->bot->query( $params );
if ( empty( $result['query']['logevents'] ) ) {
$this->output( "No changes found...\n" );
break;
}
# This is very fragile if people start to mess with moves,
# deletions and restores.
# Our strategy: Do it step by step to minimize inconsistencies.
# Remote wiki can have file renames applied that conflict with our
# titles, so delay uploads of affected titles to the end, while we
# can keep track of moves of existing files from our database.
foreach ( $result['query']['logevents'] as $logEntry ) {
if ( $logEntry['ns'] == NS_FILE ) {
$title = $logEntry['title'];
if ( $logEntry['type'] == 'upload' ) {
# This is for uploads, reuploads and reverts
$this->processNewUpload( $title, $logEntry['timestamp'] );
} elseif ( $logEntry['type'] == 'delete' ) {
# This affects deletions of the entire page but also
# deleting a file version, or restores
$this->processDeletion( $title, $logEntry );
} elseif ( $logEntry['type'] == 'move' ) {
if ( isset( $logEntry['move'] ) ) {
$newTitle = $logEntry['move']['new_title'];
} else {
$newTitle = $logEntry['params']['target_title'];
}
$user = $this->getUserIdentity( (int)$logEntry['userid'], $logEntry['user'] );
$this->processMove( $title, $newTitle, $user );
}
}
}
if ( isset( $result['query-continue'] ) && isset( $result['query-continue']['logevents'] ) ) {
$params = array_merge( $params, $result['query-continue']['logevents'] );
} elseif ( isset( $result['continue'] ) ) {
$params = array_merge( $params, $result['continue'] );
} else {
$more = false;
}
}
# Process pending queues now
foreach ( $this->pendingUploads as $title ) {
$this->processPendingUploads( $title );
}
foreach ( $this->pendingDeletions as $title => $deleteParams ) {
$this->processPendingDeletions( $title, $deleteParams['reason'], $deleteParams['user'] );
}
$this->output( "Done.\n" );
}
/**
* Process a new upload from the log. If it's a reupload, moves our
* current file to oldimage
*
* @param string $title Title of the file page to process
* @param string $timestamp Timestamp of the upload returned by API
*/
function processNewUpload( $title, $timestamp ) {
# Return early if it has been queued
if ( in_array( $title, $this->pendingUploads ) || array_key_exists( $title, $this->pendingDeletions ) ) {
return;
}
$name = $this->sanitiseTitle( NS_FILE, $title );
# Check current timestamp of the file on local wiki
$img_timestamp = $this->dbw->selectField(
'image',
'img_timestamp',
[ 'img_name' => $name ],
__METHOD__
);
$endtimestamp = $timestamp;
$moveOldImage = false;
if ( $img_timestamp ) {
# File exists on local wiki, need to move local file to oldimage,
# and for that we need the timestamp of the archived version
# Ensure we don't have a more recent version, this should never happen
if ( wfTimestamp( TS_MW, $timestamp ) < $img_timestamp ) {
$this->output( "Current file $name is more recent than remote wiki\n" );
return;
}
$endtimestamp = wfTimestamp( TS_ISO_8601, $img_timestamp );
$moveOldImage = true;
}
$params = [
'titles' => $title,
'prop' => 'imageinfo',
'iiprop' => 'timestamp|user|userid|comment|url|size|sha1|mime|metadata|archivename|bitdepth|mediatype',
# Limit information to the exact time the upload happened
'iistart' => $timestamp,
'iiend' => $endtimestamp,
# We don't care about continuation, it shouldn't be more than 2 files here
'iilimit' => 'max'
];
$result = $this->bot->query( $params );
# Api always returns an entry for title
$page = array_values( $result['query']['pages'] )[0];
if ( !isset( $page['imageinfo'] ) || empty( $page['imageinfo'] ) ) {
# Image does not exist, or there's no revision from the time range
# This may happen if the file has been renamed after this change
$this->output( "File $name with timestamp $timestamp not found.\n" );
$this->pendingUploads[] = $title;
return;
}
# Find file revision at the timestamp of the log, and also
# what we have as our current file
$currentEntry = null;
$oldArchiveName = null;
foreach ( $page['imageinfo'] as $fileVersion ) {
if ( is_null( $currentEntry ) ) {
if ( $fileVersion['timestamp'] == $timestamp ) {
$currentEntry = $fileVersion;
# Check for Wikia's videos
if ( $this->isWikiaVideo( $fileVersion ) ) {
$this->output( "...this appears to be a video, skipping it.\n" );
return 0;
}
} else {
$this->output( "File $name with timestamp $timestamp not found.\n" );
$this->pendingUploads[] = $title;
return;
}
}
if ( $moveOldImage && $fileVersion['timestamp'] == $endtimestamp && isset( $fileVersion['archivename'] ) ) {
# Our current revision is found, move to archive to leave room for the upload
$oldArchiveName = $fileVersion['archivename'];
$ok = $this->moveToOldFile( $name, $oldArchiveName, $img_timestamp );
if ( !$ok ) {
$this->output( "Skipping file update of $name from $timestamp\n" );
return;
}
break;
}
}
if ( $moveOldImage && is_null( $oldArchiveName ) ) {
# Local file doesn't seem to exist on remote wiki (anymore)
# May be deleted after this change
# Simply kill it from database before importing. The physical
# file will be overwritten/moved on upload
$moveOldImage = false;
$this->dbw->delete(
'image',
[ 'img_name' => $name ],
__METHOD__
);
}
$this->newUpload( $name, $currentEntry );
}
/**
* Archives the current version of a file to oldimage in the
* database and also moves the physical file to the archive
*
* @param string $name Name of the file
* @param string $oldArchiveName Archive name
* @param string $oldTimestamp Archive timestamp in TS_MW format
* @return bool Whether succeeded or not
*/
function moveToOldFile( $name, $oldArchiveName, $oldTimestamp ) {
$this->output( "Moving current file $name to archive $oldArchiveName\n" );
$fields = [
'oi_name' => 'img_name',
'oi_archive_name' => $this->dbw->addQuotes( $oldArchiveName ),
'oi_size' => 'img_size',
'oi_width' => 'img_width',
'oi_height' => 'img_height',
'oi_bits' => 'img_bits',
'oi_timestamp' => 'img_timestamp',
#'oi_description' => 'img_description',
#'oi_user' => 'img_user',
#'oi_user_text' => 'img_user_text',
'oi_metadata' => 'img_metadata',
'oi_media_type' => 'img_media_type',
'oi_major_mime' => 'img_major_mime',
'oi_minor_mime' => 'img_minor_mime',
'oi_sha1' => 'img_sha1',
'oi_actor' => 'img_actor'
];
$this->dbw->begin();
$this->dbw->insertSelect( 'oldimage', 'image',
$fields,
[ 'img_name' => $name ],
__METHOD__
);
$this->dbw->delete(
'image',
[ 'img_name' => $name ],
__METHOD__
);
$file = $this->localRepo->newFile( $name );
$oldFile = $this->localRepo->newFile( $name, $oldTimestamp );
$status = $oldFile->publish( $file->getPath(), File::DELETE_SOURCE );
if ( !$status->isOK() ) {
$this->output( sprintf( "Error moving current file %s to archive: %s\n",
$name, $status->getWikiText() ) );
$this->dbw->rollback();
return false;
}
# Remove thumbnails (if any)
$file->purgeThumbnails();
$this->dbw->commit();
return true;
}
/**
* Moves a file from one title to another. If target exists, deletes it
*
* @param $title string Old title of the file
* @param $newTitle string New title of the file
* @param $user UserIdentity user which performed the move, used only when
* the target title needs to be deleted
*/
function processMove( $title, $newTitle, $user ) {
$this->output( "Processing move of $title to $newTitle... " );
# If the target page existed, it was deleted
# If we have the new title queued, remove it
if ( in_array( $newTitle, $this->pendingUploads ) ) {
array_splice( $this->pendingUploads, array_search( $newTitle, $this->pendingUploads ), 1 );
}
# Reflect the rename in pendingUploads
if ( in_array( $title, $this->pendingUploads ) ) {
if ( !in_array( $newTitle, $this->pendingUploads ) ) {
$this->pendingUploads[] = $newTitle;
}
array_splice( $this->pendingUploads, array_search( $title, $this->pendingUploads ), 1 );
}
# Same in pendingDeletions
if ( array_key_exists( $title, $this->pendingDeletions ) ) {
if ( !array_key_exists( $newTitle, $this->pendingDeletions ) ) {
$this->pendingDeletions[$newTitle] = $this->pendingDeletions[$title];
}
unset( $this->pendingDeletions[$title] );
}
# Check if we have a file with the new name and delete it
$newName = $this->sanitiseTitle( NS_FILE, $newTitle );
$file = $this->localRepo->newFile( $newName );
if ( $file->exists() ) {
$this->output( "$newTitle aleady exists. Deleting to make room for the move. " );
$reason = wfMessage( 'delete_and_move_reason', $newTitle );
# NOTE: File::deleteFile takes care to do the related changes
# in the database. For instance, move rows to filearchive
# Use the user that performed the move for the deletion
$status = $file->deleteFile( $reason, $user );
if ( !$status->isOK() ) {
$this->fatalError( sprintf( "Failed to delete %s on move: %s",
$newName, implode( '. ', $status->getWikiText() ) ) );
}
}
# Perform the move if the file with old name exists
$name = $this->sanitiseTitle( NS_FILE, $title );
$file = $this->localRepo->newFile( $name );
if ( !$file->exists() ) {
# File may be uploaded and moved since startDate. Nothing to do
# If it has been deleted after the move, we'll catch it later
$this->output( "$title doesn't exist on our wiki, will process it later.\n" );
return;
}
# NOTE: File::move() takes care to do the related changes in the
# database. For instance, rename the file in image and oldimage
$status = $file->move( Title::makeTitle( NS_FILE, $newName ) );
if ( !$status->isOK() ) {
$this->fatalError( sprintf( "Failed to move %s: %s",
$name, implode( '. ', $status->getWikiText() ) ) );
}
$this->output( "Done\n" );
}
/**
* Adds to the queue of pending deletions, removing from the
* pending uploads if necessary
*
* @param $title string title of the file
* @param $logEntry Array log entry
*/
function processDeletion( $title, $logEntry ) {
$this->output( "$title has been deleted or restored. We'll take care of it later.\n" );
# Remove it from pending uploads since we're going to recheck everything
if ( in_array( $title, $this->pendingUploads ) ) {
array_splice( $this->pendingUploads, array_search( $title, $this->pendingUploads ), 1 );
}
# We add it to the array even if it exists already
if ( $logEntry['action'] == 'delete' ) {
# Update deletion reason and user, in case we had a restore action
$this->pendingDeletions[$title] = [
'reason' => $logEntry['comment'],
'user' => $this->getUserIdentity( (int)$logEntry['userid'], $logEntry['user'] )
];
} elseif ( !array_key_exists( $title, $this->pendingDeletions ) ) {
# Check to avoid overwritting the deletion reason or user on restore,
# which we don't need on file restore
$this->pendingDeletions[$title] = [
'reason' => '',
'user' => null
];
}
}
/**
* Process pending uploads once we're sorted out file move operations
* Uploads file revisions between startDate and endDate including current
* version of the file
*
* @param $title string Title of the file to process
*/
function processPendingUploads( $title ) {
$name = $this->sanitiseTitle( NS_FILE, $title );
$this->output( "Processing pending uploads for $name..." );
# We'll need to move our current image to oldimage, but we still
# don't know the archive name. Store current values in memory
$imageObj = $this->dbw->selectRow(
'image',
'*',
[ 'img_name' => $name ],
__METHOD__
);
# NOTE: imageinfo returns revisions from newer to older. startDate is older
$timestamp = wfTimestamp( TS_ISO_8601, $this->endDate );
$endtimestamp = wfTimestamp( TS_ISO_8601, $this->startDate );
$moveOldImage = false;
if ( $imageObj ) {
# File exists on local wiki, need to move local file to oldimage,
# and for that we need the timestamp of the archived version
$endtimestamp = wfTimestamp( TS_ISO_8601, $imageObj->img_timestamp );
$moveOldImage = true;
}
$overwrittenArchiveName = null;
$params = [
'titles' => $title,
'prop' => 'imageinfo',
'iiprop' => 'timestamp|user|userid|comment|url|size|sha1|mime|metadata|archivename|bitdepth|mediatype',
'iiend' => $endtimestamp,
'iilimit' => 'max'
];
$iistart = $timestamp;
$more = true;
$count = 0;
while ( $more ) {
$params['iistart'] = $iistart;
$result = $this->bot->query( $params );
# Api always returns an entry for title
$page = array_values( $result['query']['pages'] )[0];
if ( !isset( $page['imageinfo'] ) || empty( $page['imageinfo'] ) ) {
# Image does not exist, or there's no revision from the time range
$this->output( "File $name does not have uploads for the time range.\n" );
return;
}
foreach ( $page['imageinfo'] as $fileVersion ) {
# Api returns file revisions from new to old.
# WARNING: If a new version of a file is uploaded after the start of the script
# (or endDate), the file and all its previous revisions would be skipped,
# potentially leaving pages that were using the old image with redlinks.
# To prevent this, we'll skip only more recent versions, and mark the first
# one before the end date as the latest
if ( !$count && isset( $fileVersion['archivename'] ) ) {
unset( $fileVersion['archivename'] );
}
# Check for Wikia's videos
if ( $this->isWikiaVideo( $fileVersion ) ) {
$this->output( "...this appears to be a video, skipping it.\n" );
return;
}
# The code will enter this conditional only on the last iteration,
# or before if it happens that there are multiple files with
# the same timestamp than our latest version
if ( $moveOldImage && $fileVersion['timestamp'] == $endtimestamp ) {
# Our current revision is found.
# It should've been archived somewhere after being replaced.
# Move the archived file to where it belongs
if ( $count > 0 && !is_null( $overwrittenArchiveName ) ) {
$this->output( sprintf( 'Moving overwritten archive %s to timestamp %s... ',
$overwrittenArchiveName, $imageObj->img_timestamp ) );
$zombieFile = $this->localRepo->newFromArchiveName( $name, $overwrittenArchiveName );
$archivedFile = $this->localRepo->newFile( $name, $imageObj->img_timestamp );
$archivedFile->publish( $zombieFile->getPath(), File::DELETE_SOURCE );
$this->output( "Done\n" );
}
break;
}
if ( $count > 0 ) {
# Old upload
$status = $this->oldUpload( $name, $fileVersion );
} else {
# New upload
$this->dbw->begin();
if ( $moveOldImage ) {
# Delete existing row from image before importing
$this->dbw->delete(
'image',
[ 'img_name' => $name ],
__METHOD__
);
}
$status = $this->newUpload( $name, $fileVersion );
if ( !$status->isOK() ) {
$this->dbw->rollback();
return;
} else {
$this->dbw->commit();
# When overwritting a file, it returns the generated
# archive name in the status value
if ( $moveOldImage && (string)$status->getValue() != '' ) {
$overwrittenArchiveName = (string)$status->getValue();
$this->output( "Upload resulted in an overwrite. Marking $overwrittenArchiveName for restore.\n" );
}
}
}
$count++;
}
if ( isset( $result['query-continue'] ) && isset( $result['query-continue']['imageinfo'] ) ) {
$params = array_merge( $params, $result['query-continue']['imageinfo'] );
} elseif ( isset( $result['continue'] ) ) {
$params = array_merge( $params, $result['continue'] );
} else {
$more = false;
}
$count++;
}
}
/**
* Process files where a deletion or restore has been involved.
* Checks the entire remote file history against ours, updating as needed
*
* @param $title string Title of the file to process
* @param $reason string Last deletion reason
* @param $user User User which performed the last deletion
*/
function processPendingDeletions( $title, $reason, $user ) {
$name = $this->sanitiseTitle( NS_FILE, $title );
$this->output( "Processing pending deletions for $name..." );
# We'll need to move our current image to oldimage, but we still
# don't know the archive name. Store current values in memory
$imageObj = $this->dbw->selectRow(
'image',
'*',
[ 'img_name' => $name ],
__METHOD__
);
$moveOldImage = false;
$fileHistoryResult = null;
$currentHistoryEntry = false;
$idsToRestore = []; # filearchive IDs
if ( $imageObj ) {
$ourtimestamp = wfTimestamp( TS_ISO_8601, $imageObj->img_timestamp );
$moveOldImage = true;
# Get file history
$fileHistoryResult = $this->dbw->select(
'oldimage',
[ 'oi_timestamp' ],
[ 'oi_name' => $name ],
__METHOD__,
[ 'ORDER BY' => 'oi_timestamp DESC' ]
);
$currentHistoryEntry = $fileHistoryResult ? $fileHistoryResult->fetchRow() : false;
}
$overwrittenArchiveName = null;
$params = [
'titles' => $title,
'prop' => 'imageinfo',
'iiprop' => 'timestamp|user|userid|comment|url|size|sha1|mime|metadata|archivename|bitdepth|mediatype',
'iilimit' => 'max'
];
$iistart = null;
$more = true;
$count = 0;
# NOTE: imageinfo returns revisions from newer to older
while ( $more ) {
$result = $this->bot->query( $params );
# Api always returns an entry for title
$page = array_values( $result['query']['pages'] )[0];
if ( !isset( $page['imageinfo'] ) || empty( $page['imageinfo'] ) ) {
# Image does not exist, or there's no revision
# If we have the image, delete it
if ( $moveOldImage ) {
$this->output( "File $name does not have uploads. Deleting our file... " );
$file = $this->localRepo->newFile( $name );
$file->deleteFile( $reason, $user );
$this->output( "Done\n" );
} else {
$this->output( "File $name does not have uploads and doesn't exist in our wiki.\n" );
}
return;
}
foreach ( $page['imageinfo'] as $fileVersion ) {
$timestamp = wfTimestamp( TS_MW, $fileVersion['timestamp'] );
# Check for Wikia's videos
if ( $this->isWikiaVideo( $fileVersion ) ) {
$this->output( "...this appears to be a video, skipping it.\n" );
return;
}
if ( $moveOldImage && $fileVersion['timestamp'] == $ourtimestamp ) {
# Our current revision is found.
# It should've been archived somewhere after being replaced.
# Move the archived file to where it belongs if it's not the top version
if ( $count > 0 && !is_null( $overwrittenArchiveName ) ) {
$this->output( sprintf( 'Moving overwritten archive %s to timestamp %s... ',
$overwrittenArchiveName, $imageObj->img_timestamp ) );
$zombieFile = $this->localRepo->newFromArchiveName( $name, $overwrittenArchiveName );
$archivedFile = $this->localRepo->newFile( $name, $imageObj->img_timestamp );
$archivedFile->publish( $zombieFile->getPath(), File::DELETE_SOURCE );
$this->output( "Done\n" );
}
# Mark it as already processed, even if we haven't changed anything.
# For example if both wikis have the same top file version
$count++;
$moveOldImage = false;
continue;
}
if ( $count > 0 ) {
# Old upload
# Advance the iterator until the timestamp of this revision to see if we already have it
# We're going from newer to older, so oi_timestamp is decreasing
while ( $currentHistoryEntry && $currentHistoryEntry['oi_timestamp'] > $timestamp ) {
# $count = 0 is for image, $count = 1 would be the
# first version of oldimage. We need to stay on the
# current row the first time to avoid skipping the
# first oldimage version
if ( $count > 1 ) {
$currentHistoryEntry = $fileHistoryResult->fetchRow();
}
if ( $currentHistoryEntry['oi_timestamp'] > $timestamp ) {
# Still newer? It means this revision is not on the remote wiki
# Delete it
$this->output( "Deleting old version with timestamp {$currentHistoryEntry['oi_timestamp']}..." );
$file = $this->localRepo->newFile( $name, $currentHistoryEntry['oi_timestamp'] );
$file->deleteOldFile( $file->getArchiveName(), $reason, $user );
$this->output( "Done\n" );
# Increase $count so we don't get stuck in an infinite loop on first oldimage version
$count++;
}
}
if ( $currentHistoryEntry && $currentHistoryEntry['oi_timestamp'] == $timestamp ) {
# We already have this revision, skip it
continue;
}
# The revision isn't on our database
# See if it's deleted
$id = $this->dbw->selectField(
'filearchive',
'fa_id',
[ 'fa_name' => $name, 'fa_timestamp' => $timestamp ],
__METHOD__
);
if ( $id ) {
$this->output( "Marking version {$fileVersion['timestamp']} for restore.\n" );
$idsToRestore[] = $id;
$status = Status::newGood();
} else {
$status = $this->oldUpload( $name, $fileVersion );
}
} else {
# New upload
$this->dbw->begin();
if ( $moveOldImage ) {
if ( $fileVersion['timestamp'] == $ourtimestamp ) {
# If the most recent image is already our latest
# one, nothing to overwrite
$moveOldImage = false;
continue;
}
# Delete existing row from image before importing
$this->dbw->delete(
'image',
[ 'img_name' => $name ],
__METHOD__
);
}
# See if it's deleted
$id = $this->dbw->selectField(
'filearchive',
'fa_id',
[ 'fa_name' => $name, 'fa_timestamp' => $timestamp ],
__METHOD__
);
if ( $id ) {
$this->output( "Marking version {$fileVersion['timestamp']} for restore.\n" );
$idsToRestore[] = $id;
$status = Status::newGood();
} else {
$status = $this->newUpload( $name, $fileVersion );
}
if ( !$status->isOK() ) {
$this->dbw->rollback();
return;
} else {
$this->dbw->commit();
# When overwritting a file, it returns the generated
# archive name in the status value
if ( $moveOldImage && (string)$status->getValue() != '' ) {
$overwrittenArchiveName = (string)$status->getValue();
$this->output( "Upload resulted in an overwrite. Marking $overwrittenArchiveName for restore.\n" );
}
}
}
$count++;
}
if ( isset( $result['query-continue'] ) && isset( $result['query-continue']['imageinfo'] ) ) {
$params = array_merge( $params, $result['query-continue']['imageinfo'] );
} elseif ( isset( $result['continue'] ) ) {
$params = array_merge( $params, $result['continue'] );
} else {
$more = false;
}
$count++;
}
if ( count( $idsToRestore ) > 0 ) {
$this->output( sprintf( 'Restoring filearchive IDs %s... ', implode( ',', $idsToRestore ) ) );
$file->restore( $idsToRestore, true );
$this->output( "Done\n" );
}
}
}
$maintClass = 'GrabNewFiles';
require_once RUN_MAINTENANCE_IF_MAIN;