forked from wikimedia/mediawiki-tools-grabbers
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathgrabNewText.php
831 lines (763 loc) · 26.1 KB
/
grabNewText.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
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
<?php
/**
* Maintenance script to grab text from a wiki and import it to another wiki.
* Translated from Edward Chernenko's Perl version (text.pl).
*
* @file
* @ingroup Maintenance
* @author Jack Phoenix <[email protected]>
* @author Calimonious the Estrange
* @author Jesús Martínez <[email protected]>
* @version 1.1
* @date 5 August 2019
*/
use MediaWiki\MediaWikiServices;
require_once 'includes/TextGrabber.php';
class GrabNewText extends TextGrabber {
/**
* Start date
*
* @var string
*/
protected $startDate;
/**
* Last revision in the current db
*
* @var int
*/
protected $lastRevision = 0;
/**
* Array of namespaces to grab changes
*
* @var Array
*/
protected $namespaces = null;
/**
* A list of page ids already processed. Don't get new edits for those
*
* @var array
*/
protected $pagesProcessed = [];
/**
* A list of page ids already processed for protection
*
* @var array
*/
protected $pagesProtected = [];
/**
* Used to know if the current user can see deleted revisions on remote wiki,
* to also gather them to be available on local wiki
*
* @var bool
*/
protected $canSeeDeletedRevs = true;
public function __construct() {
parent::__construct();
$this->mDescription = "Grab new changes from an external wiki and add it over an imported dump.\nFor use when the available dump is slightly out of date.";
$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. If a start date is not provided, the last revision timestamp in the database is used.', false, true );
$this->addOption( 'namespaces', 'A pipe-separated list of namespaces (ID) to grab changes from. Defaults to all namespaces', false, true );
}
public function execute() {
parent::execute();
$this->startDate = $this->getOption( 'startdate' );
if ( $this->startDate ) {
if ( !wfTimestamp( TS_ISO_8601, $this->startDate ) ) {
$this->fatalError( 'Invalid startdate format.' );
}
} else {
// If a start date was not provided, use the latest timestamp from the database.
$this->startDate = $this->dbw->selectField(
'revision',
'rev_timestamp',
[],
__METHOD__,
[ 'ORDER BY' => 'rev_timestamp DESC' ]
);
if ( !$this->startDate ) {
$this->fatalError( 'No startdate provided, and no revisions found in our DB.' );
}
$this->output( "Using the latest revision timestamp in our database: $this->startDate\n" );
}
if ( $this->hasOption( 'namespaces' ) ) {
$this->namespaces = explode( '|', $this->getOption( 'namespaces' ) );
}
# Get last revision id to avoid duplicates
$this->lastRevision = (int)$this->dbw->selectField(
'revision',
'rev_id',
[],
__METHOD__,
[ 'ORDER BY' => 'rev_id DESC' ]
);
$this->output( "\n" );
# Get page changes from recentchanges and crap
$this->processRecentLogs();
$this->processRecentChanges();
$this->output( "\nDone.\n" );
# Done.
}
/**
* Get page edits and creations
*/
function processRecentChanges() {
$more = true;
$count = 0;
# Get edits
$params = [
'list' => 'recentchanges',
'rcdir' => 'newer',
'rctype' => 'edit|new',
'rclimit' => 'max',
'rcprop' => 'title|sizes|redirect|ids',
'rcstart' => $this->startDate,
'rcend' => $this->endDate
];
$count = 0;
$more = true;
if ( !is_null( $this->namespaces ) ) {
$params['rcnamespace'] = implode( '|', $this->namespaces );
}
$this->output( "Retreiving list of changed pages...\n" );
while ( $more ) {
$result = $this->bot->query( $params );
if ( empty( $result['query']['recentchanges'] ) ) {
$this->output( 'No changes found...' );
}
foreach ( $result['query']['recentchanges'] as $entry ) {
# new pages, new uploads, edited pages
# while more, parse into $pagesList
if ( ( $count % 500 ) == 0 ) {
$this->output( "$count\n" );
}
$title = $entry['title'];
$ns = $entry['ns'];
$title = $this->sanitiseTitle( $ns, $title );
if ( in_array( $entry['pageid'], $this->pagesProcessed ) ) {
# Already done; continue
continue;
}
$this->pagesProcessed[] = $entry['pageid'];
$pageInfo = [
'pageid' => $entry['pageid'],
'title' => $entry['title'],
'ns' => $ns,
'protection' => null,
];
if ( in_array( $entry['pageid'], $this->pagesProtected ) ) {
$pageInfo['protection'] = true;
# Remove from the array so we don't attempt to insert restrictions again
array_slice( $this->pagesProtected, array_search( $entry['pageid'], $this->pagesProtected ), 1 );
}
$this->processPage( $pageInfo, $this->startDate );
$count++;
}
if ( isset( $result['query-continue'] ) && isset( $result['query-continue']['recentchanges'] ) ) {
$params = array_merge( $params, $result['query-continue']['recentchanges'] );
} elseif ( isset( $result['continue'] ) ) {
$params = array_merge( $params, $result['continue'] );
} else {
$more = false;
}
}
$this->output( "\n" );
}
/**
* Get delete/move/import changes
*/
function processRecentLogs() {
$params = [
'list' => 'logevents',
'ledir' => 'newer',
'lelimit' => 'max',
'lestart' => $this->startDate,
'leend' => $this->endDate
];
$count = 0;
$more = true;
$this->output( "Updating deleted and moved items...\n" );
while ( $more ) {
$result = $this->bot->query( $params );
if ( empty( $result['query']['logevents'] ) ) {
$this->output( "No changes found...\n" );
} else {
foreach ( $result['query']['logevents'] as $logEntry ) {
if ( ( $count % 500 ) == 0 ) {
$this->output( "$count\n" );
}
$pageID = $logEntry['pageid'];
$title = $logEntry['title'];
$ns = $logEntry['ns'];
$title = $this->sanitiseTitle( $ns, $title );
$sourceTitle = Title::makeTitle( $ns, $title );
$newns = -1;
if ( $logEntry['type'] == 'move' ) {
if ( isset( $logEntry['move'] ) ) {
$newns = $logEntry['move']['new_ns'];
} else {
$newns = $logEntry['params']['target_ns'];
}
}
if ( !is_null( $this->namespaces ) && !in_array( $ns, $this->namespaces ) && !in_array( $newns, $this->namespaces ) ) {
continue;
}
if ( $logEntry['type'] == 'move' ) {
# Move our copy
# New title
if ( isset( $logEntry['move'] ) ) {
$newTitle = $this->sanitiseTitle( $newns, $logEntry['move']['new_title'] );
} else {
$newTitle = $this->sanitiseTitle( $newns, $logEntry['params']['target_title'] );
}
$destTitle = Title::makeTitle( $newns, $newTitle );
$this->output( "$sourceTitle was moved to $destTitle; updating...\n" );
$this->processMove( $ns, $title );
$this->processMove( $newns, $newTitle );
} elseif ( $logEntry['type'] == 'delete' && $logEntry['action'] == 'delete' ) {
if ( !in_array( (string)$sourceTitle, $this->movedTitles ) ) {
$this->output( "$sourceTitle was deleted; updating...\n" );
# Delete our copy, move revisions -> archive
$pageID = $this->getPageID( $ns, $title );
if ( !$pageID ) {
# Page may be created and then deleted before we processed recentchanges
$this->output( "Page $sourceTitle not found in database, nothing to delete.\n" );
# Update deleted revisions from remote wiki anyway
$this->updateDeletedRevs( $ns, $title );
} else {
$this->archiveAndDeletePage( $pageID, $ns, $title );
}
} else {
$this->output( "$sourceTitle was deleted; updating only archived revisions...\n" );
# we've already processed this title as part of a page move.
# It may not be the current page anymore, so just update the archived revisions
$this->updateDeletedRevs( $ns, $title );
}
} elseif ( $logEntry['type'] == 'delete' && $logEntry['action'] == 'restore' ) {
$this->output( "$sourceTitle was undeleted; updating....\n" );
# Remove any revisions from archive, and process as new
$this->updateRestored( $ns, $title );
$pageInfo = [
'pageid' => $pageID,
'title' => $title,
'ns' => $ns,
'protection' => true,
];
$this->processPage( $pageInfo, null, false );
if ( !in_array( $pageID, $this->pagesProcessed ) ) {
$this->pagesProcessed[] = $pageID;
}
$this->output( "$sourceTitle processed.\n" );
} elseif ( $logEntry['type'] == 'upload' ) { # action can be upload or reupload
$this->output( "$sourceTitle was imported; updating....\n" );
# Process as new
if ( !$pageID ) {
$pageID = null;
}
$pageInfo = [
'pageid' => $pageID,
'title' => $title,
'ns' => $ns,
'protection' => true,
];
$this->processPage( $pageInfo );
if ( !in_array( $pageID, $this->pagesProcessed ) ) {
$this->pagesProcessed[] = $pageID;
}
} elseif ( $logEntry['type'] == 'protect' ) {
# Don't bother if there's no pageID
if ( $pageID ) {
$pageInfo = [
'pageid' => $pageID,
'title' => $title,
'ns' => $ns,
'protection' => null,
];
if ( $logEntry['action'] == 'unprotect' ) {
# Remove protection info
$this->dbw->delete(
'page_restrictions',
[ 'pr_page' => $pageID ],
__METHOD__
);
} elseif ( !in_array( $pageID, $this->pagesProtected ) ) {
$pageInfo['protection'] = true;
}
$this->processPage( $pageInfo, $this->startDate );
if ( !in_array( $pageID, $this->pagesProcessed ) ) {
$this->pagesProcessed[] = $pageID;
}
}
}
$count++;
}
}
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;
}
}
$this->output( "\n" );
}
/**
* Handle an individual page.
*
* @param array $page: Array retrieved from the API, containing pageid,
* page title, namespace, protection status and more...
* @param int|null $start: Timestamp from which to get revisions; if this is
* defined, protection stuff is skipped.
* @param bool|null $skipPrevious: Skip revision ids lower than the largest revision
* existing when the script started, a shortcut to not process old
* revisions that should be already in the database
*/
function processPage( $page, $start = null, $skipPrevious = true ) {
$pageID = $page['pageid'];
$pageTitle = null;
$pageDesignation = "id $pageID";
if ( !$pageID ) {
# We don't have page id... we need to use page title
$pageTitle = (string)Title::makeTitle( $page['ns'], $page['title'] );
$pageDesignation = $pageTitle;
}
$this->output( "Processing page $pageDesignation...\n" );
$params = [
'prop' => 'info|revisions',
'rvlimit' => 'max',
'rvprop' => 'ids|flags|timestamp|user|userid|comment|content|tags|contentmodel',
'rvdir' => 'newer',
'rvend' => wfTimestamp( TS_ISO_8601, $this->endDate )
];
if ( $pageID ) {
$params['pageids'] = $pageID;
} else {
# We don't have page id... we need to use page title
$params['titles'] = $pageTitle;
}
if ( $start ) {
$params['rvstart'] = wfTimestamp( TS_ISO_8601, $start );
}
if ( $page['protection'] ) {
$params['inprop'] = 'protection';
}
$result = $this->bot->query( $params );
if ( !$result || isset( $result['error'] ) ) {
$this->fatalError( "Error getting revision information from API for page $pageDesignation." );
return;
}
if ( isset( $params['inprop'] ) ) {
unset( $params['inprop'] );
}
if ( $start ) {
# start and the continuation parameter cannot be used together, so we remove it for next requests
unset( $params['rvstart'] );
}
$info_pages = array_values( $result['query']['pages'] );
if ( isset( $info_pages[0]['missing'] ) ) {
$this->output( "Page $pageDesignation not found.\n" );
return;
}
if ( !$pageID ) {
$pageID = $info_pages[0]['pageid'];
}
$page_e = [
'namespace' => null,
'title' => null,
'is_redirect' => 0,
'is_new' => 0,
'random' => wfRandom(),
'touched' => wfTimestampNow(),
'len' => 0,
'content_model' => null
];
# Trim and convert displayed title to database page title
# Get it from the returned value from api
$page_e['namespace'] = $info_pages[0]['ns'];
$page_e['title'] = $this->sanitiseTitle( $info_pages[0]['ns'], $info_pages[0]['title'] );
$title = Title::makeTitle( $page_e['namespace'], $page_e['title'] );
# Get other information from api info
$page_e['is_redirect'] = ( isset( $info_pages[0]['redirect'] ) ? 1 : 0 );
$page_e['is_new'] = ( isset( $info_pages[0]['new'] ) ? 1 : 0 );
$page_e['len'] = $info_pages[0]['length'];
$page_e['latest'] = $info_pages[0]['lastrevid'];
$defaultModel = null;
if ( isset( $info_pages[0]['contentmodel'] ) ) {
# This would be the most accurate way of getting the content model for a page.
# However it calls hooks and can be incredibly slow or cause errors
#$defaultModel = ContentHandler::getDefaultModelFor( $title );
$defaultModel = MediaWikiServices::getInstance()->getNamespaceInfo()->
getNamespaceContentModel( $info_pages[0]['ns'] ) || CONTENT_MODEL_WIKITEXT;
# Set only if not the default content model
if ( $defaultModel != $info_pages[0]['contentmodel'] ) {
$page_e['content_model'] = $info_pages[0]['contentmodel'];
}
}
# Check if page is present
$pageRow = $this->dbw->selectRow(
'page',
[ 'page_namespace', 'page_title' ],
[ 'page_id' => $pageID ],
__METHOD__
);
# If page is not present, check if title is present, because we can't insert
# a duplicate title. That would mean the page was moved leaving a redirect but
# we haven't processed the move yet
# Also, check the destination title before moving the page
if ( $pageRow === false ||
$pageRow->page_namespace != $page_e['namespace'] ||
$pageRow->page_title != $page_e['title']
) {
$conflictingPageID = $this->getPageID( $page_e['namespace'], $page_e['title'] );
if ( $conflictingPageID ) {
# Whoops...
$this->resolveConflictingTitle( $page_e['namespace'], $page_e['title'], $pageID, $conflictingPageID );
}
}
# Update page_restrictions (only if requested)
if ( isset( $info_pages[0]['protection'] ) ) {
$this->output( "Setting page_restrictions changes on page_id $pageID.\n" );
# Delete first any existing protection
$this->dbw->delete(
'page_restrictions',
[ 'pr_page' => $pageID ],
__METHOD__
);
# insert current restrictions
foreach ( $info_pages[0]['protection'] as $prot ) {
# Skip protections inherited from cascade protections
if ( !isset( $prot['source'] ) ) {
$e = [
'page' => $pageID,
'type' => $prot['type'],
'level' => $prot['level'],
'cascade' => (int)isset( $prot['cascade'] ),
'user' => null,
'expiry' => ( $prot['expiry'] == 'infinity' ? 'infinity' : wfTimestamp( TS_MW, $prot['expiry'] ) )
];
$this->dbw->insert(
'page_restrictions',
[
'pr_page' => $e['page'],
'pr_type' => $e['type'],
'pr_level' => $e['level'],
'pr_cascade' => $e['cascade'],
'pr_user' => $e['user'],
'pr_expiry' => $e['expiry']
],
__METHOD__
);
}
}
}
$revisionsProcessed = false;
while ( true ) {
foreach ( $info_pages[0]['revisions'] as $revision ) {
if ( !$skipPrevious || $revision['revid'] > $this->lastRevision) {
$revisionsProcessed = $this->processRevision( $revision, $pageID, $title ) || $revisionsProcessed;
} else {
$this->output( sprintf( "Skipping the processRevision of revision %d minor or equal to the last revision of the database (%d).\n",
$revision['revid'], $this->lastRevision ) );
}
}
# Add continuation parameters
if ( isset( $result['query-continue'] ) && isset( $result['query-continue']['revisions'] ) ) {
$params = array_merge( $params, $result['query-continue']['revisions'] );
} elseif ( isset( $result['continue'] ) ) {
$params = array_merge( $params, $result['continue'] );
} else {
break;
}
$result = $this->bot->query( $params );
if ( !$result || isset( $result['error'] ) ) {
$this->fatalError( "Error getting revision information from API for page $pageDesignation." );
return;
}
$info_pages = array_values( $result['query']['pages'] );
}
if ( !$revisionsProcessed ) {
# We already processed the page before? page doesn't need updating, then
return;
}
$insert_fields = [
'page_namespace' => $page_e['namespace'],
'page_title' => $page_e['title'],
'page_is_redirect' => $page_e['is_redirect'],
'page_is_new' => $page_e['is_new'],
'page_random' => $page_e['random'],
'page_touched' => $page_e['touched'],
'page_latest' => $page_e['latest'],
'page_len' => $page_e['len'],
'page_content_model' => $page_e['content_model']
];
if ( $pageRow === false ) {
# insert if not present
$this->output( "Inserting page entry $pageID\n" );
$insert_fields['page_id'] = $pageID;
$this->dbw->insert(
'page',
$insert_fields,
__METHOD__
);
} else {
# update existing
$this->output( "Updating page entry $pageID\n" );
$this->dbw->update(
'page',
$insert_fields,
[ 'page_id' => $pageID ],
__METHOD__
);
}
$this->dbw->commit();
}
function updateRestored( $ns, $title ) {
# We need to delete content from the slots table too, otherwise
# when we get the revisions from the remote wiki that already
# exist here, we'll get duplicate errors
$result = $this->dbw->select(
'archive',
[ 'ar_rev_id' ],
[
'ar_title' => $title,
'ar_namespace' => $ns
],
__METHOD__
);
$revids = [];
foreach ( $result as $row ) {
$revids[] = $row->ar_rev_id;
}
if ( $revids ) {
$this->dbw->delete(
'slots',
[ 'slot_revision_id' => $revids ],
__METHOD__
);
}
# Delete existing deleted revisions for page
$this->dbw->delete(
'archive',
[
'ar_title' => $title,
'ar_namespace' => $ns
],
__METHOD__
);
$this->updateDeletedRevs( $ns, $title );
}
/**
* Get deleted revisions of a particular title on remote wiki, and inserts
* them on the archive table if they don't exist already
*
* @param int $ns Namespace of the restored page
* @param string $title Title of the restored page
**/
function updateDeletedRevs( $ns, $title ) {
$pageTitle = Title::makeTitle( $ns, $title );
if ( !$this->canSeeDeletedRevs ) {
$this->output( "Unable to see deleted revisions for title $pageTitle\n" );
return;
}
# TODO: list=deletedrevs is deprecated in recent MediaWiki versions.
# should try to use list=alldeletedrevisions first and fallback to deletedrevs
$params = [
'list' => 'deletedrevs',
'titles' => (string)$pageTitle,
'drprop' => 'revid|parentid|user|userid|comment|minor|len|content|tags',
'drlimit' => 'max',
'drdir' => 'newer'
];
$result = $this->bot->query( $params );
if ( !$result || isset( $result['error'] ) ) {
if ( isset( $result['error'] ) && $result['error']['code'] == 'drpermissiondenied' ) {
$this->output( "Warning: Current user can't see deleted revisions.\n" .
"Unable to see deleted revisions for title $pageTitle\n" );
$this->canSeeDeletedRevs = false;
return;
}
$this->fatalError( "Error getting deleted revision information from API for page $pageTitle." );
return;
}
if ( count( $result['query']['deletedrevs'] ) === 0 ) {
# No deleted revisions for that title, nothing to do
return;
}
$info_deleted = $result['query']['deletedrevs'][0];
while ( true ) {
foreach ( $info_deleted['revisions'] as $revision ) {
$revisionId = $revision['revid'];
if ( !$revisionId ) {
# Revision ID is mandatory with the new content tables and things will fail if not provided.
$this->output( sprintf( "WARNING: Got revision without revision id, " .
"with timestamp %s. Skipping!\n", $revision['timestamp'] ) );
continue;
}
# Check if archived revision is already there to prevent duplicate entries
$count = $this->dbw->selectRowCount(
'archive',
'1',
[ 'ar_rev_id' => $revisionId ],
__METHOD__
);
if ( !$count ) {
$this->insertArchivedRevision( $revision, $pageTitle );
}
}
# Add continuation parameters
if ( isset( $result['query-continue'] ) && isset( $result['query-continue']['deletedrevs'] ) ) {
$params = array_merge( $params, $result['query-continue']['deletedrevs'] );
} elseif ( isset( $result['continue'] ) ) {
$params = array_merge( $params, $result['continue'] );
} else {
break;
}
$result = $this->bot->query( $params );
if ( !$result || isset( $result['error'] ) ) {
$this->fatalError( "Error getting deleted revision information from API for page $pageTitle." );
return;
}
$info_deleted = $result['query']['deletedrevs'][0];
}
}
function processMove( $ns, $title ) {
$sourceTitle = Title::makeTitle( $ns, $title );
if ( !in_array( (string)$sourceTitle, $this->movedTitles ) ) {
$this->movedTitles[] = (string)$sourceTitle;
}
$this->output( "Check whether $sourceTitle refers to the same page on both wikis...\n" );
$pageID = $this->getPageID( $ns, $title );
if ( $pageID ) {
# There's a local page at the given title
# Check if page exists on remote wiki
$params = [
'prop' => 'info',
'pageids' => $pageID
];
$result = $this->bot->query( $params );
if ( !$result || isset( $result['error'] ) ) {
$this->fatalError( "Error getting information from API for page ID $pageID" );
return;
}
$info_pages = array_values( $result['query']['pages'] );
if ( isset( $info_pages[0]['missing'] ) ) {
# Local page doesn't exist on remote wiki. It must have been deleted
# NOTE: When overwritting empty redirects on move, they're deleted
# without being archived, but here we're archiving everything
$this->output( "Page ID $pageID for title $sourceTitle on local wiki doesn't exist on remote. Archiving...\n" );
$this->archiveAndDeletePage( $pageID, $ns, $title );
} else {
$remotePageNs = $info_pages[0]['ns'];
$remotePageTitle = $this->sanitiseTitle( $info_pages[0]['ns'], $info_pages[0]['title'] );
if ( $remotePageNs == $ns && $remotePageTitle == $title ) {
$this->output( "$sourceTitle refer to the same page on both wikis. Nothing to do.\n" );
# If the existing page has the same title, nothing more to do
# If it was moved, processPage should have been called already
return;
}
# Existing page is on a different title on remote wiki
# Move it, but first check that there's not a conflicting title!
$conflictingPageID = $this->getPageID( $remotePageNs, $remotePageTitle );
if ( $conflictingPageID ) {
# Whoops...
$this->resolveConflictingTitle( $remotePageNs, $remotePageTitle, $pageID, $conflictingPageID );
}
$this->output( sprintf( "Page ID $pageID has been moved on remote wiki. Moving $sourceTitle to %s...\n",
Title::makeTitle( $remotePageNs, $remotePageTitle ) ) );
$this->dbw->update(
'page',
[
'page_namespace' => $remotePageNs,
'page_title' => $remotePageTitle,
],
[ 'page_id' => $pageID ],
__METHOD__
);
# Update revisions on the moved page
$pageInfo = [
'pageid' => $pageID,
'title' => $remotePageTitle,
'ns' => $remotePageNs,
'protection' => true,
];
# Need to process also old revisions in case there were page restores
$this->processPage( $pageInfo, null, false );
if ( !in_array( $pageID, $this->pagesProcessed ) ) {
$this->pagesProcessed[] = $pageID;
}
}
# Now process the original title. If it exists on remote wiki, the
# corresponding page will be created, otherwise nothing will be done
$pageInfo = [
'pageid' => null,
'title' => $title,
'ns' => $ns,
'protection' => true,
];
# Need to process also old revisions in case there were page restores
$this->processPage( $pageInfo, null, false );
} else {
# Local title doesn't exist. Should have been created after,
# the move but we haven't processed it yet in recentchanges.
# Or it's under another title. See if title exists on remote wiki
$params = [
'prop' => 'info',
'titles' => (string)$sourceTitle
];
$result = $this->bot->query( $params );
if ( !$result || isset( $result['error'] ) ) {
$this->fatalError( "Error getting information from API for page $sourceTitle" );
return;
}
$info_pages = array_values( $result['query']['pages'] );
# Check if title exists on remote wiki
if ( isset( $info_pages[0]['missing'] ) ) {
# Title doesn't exist on local nor remote wiki
# Nothing to do
$this->output( "Title $sourceTitle doesn't exist on both wikis. Nothing to do.\n" );
return;
}
$remoteID = $info_pages[0]['pageid'];
# Check if the page on the remote wiki exists on local database.
# If it exists, it'll be under a different title, because we
# already know that the original local title doesn't exist
$row = $this->dbw->selectRow(
'page',
[
'page_namespace',
'page_title'
],
[ 'page_id' => $remoteID ],
__METHOD__
);
if ( $row ) {
# Page exists under a different title, move it
$this->output( sprintf( "Page ID $remoteID has been moved on remote wiki. Moving %s to $sourceTitle...\n",
Title::makeTitle( $row->page_namespace, $row->page_title ) ) );
$this->dbw->update(
'page',
[
'page_namespace' => $ns,
'page_title' => $title,
],
[ 'page_id' => $remoteID ],
__METHOD__
);
}
# Do processPage. If we had the page and we've moved it, it'll add the
# revisions of the move, otherwise it will create the page if needed
$pageInfo = [
'pageid' => $remoteID,
'title' => $title,
'ns' => $ns,
'protection' => true,
];
# Need to process also old revisions in case there were page restores
$this->processPage( $pageInfo, null, false );
if ( !in_array( $remoteID, $this->pagesProcessed ) ) {
$this->pagesProcessed[] = $remoteID;
}
}
}
}
$maintClass = 'GrabNewText';
require_once RUN_MAINTENANCE_IF_MAIN;