47 private $resolvablePages = 0;
53 private $totalPages = 0;
59 private $resolvableLinks = 0;
65 private $totalLinks = 0;
72 private $deletedLinks = 0;
75 parent::__construct();
76 $this->
addDescription(
'Find and fix pages affected by namespace addition/removal' );
77 $this->
addOption(
'fix',
'Attempt to automatically fix errors and delete broken links' );
78 $this->
addOption(
'merge',
"Instead of renaming conflicts, do a history merge with " .
79 "the correct title" );
80 $this->
addOption(
'add-suffix',
"Dupes will be renamed with correct namespace with " .
81 "<text> appended after the article name",
false,
true );
82 $this->
addOption(
'add-prefix',
"Dupes will be renamed with correct namespace with " .
83 "<text> prepended before the article name",
false,
true );
84 $this->
addOption(
'source-pseudo-namespace',
"Move all pages with the given source " .
85 "prefix (with an implied colon following it). If --dest-namespace is not specified, " .
86 "the colon will be replaced with a hyphen.",
88 $this->
addOption(
'dest-namespace',
"In combination with --source-pseudo-namespace, " .
89 "specify the namespace ID of the destination.",
false,
true );
90 $this->
addOption(
'move-talk',
"If this is specified, pages in the Talk namespace that " .
91 "begin with a conflicting prefix will be renamed, for example " .
92 "Talk:File:Foo -> File_Talk:Foo" );
99 'add-suffix' => $this->
getOption(
'add-suffix',
'' ),
100 'add-prefix' => $this->
getOption(
'add-prefix',
'' ),
101 'move-talk' => $this->
hasOption(
'move-talk' ),
102 'source-pseudo-namespace' => $this->
getOption(
'source-pseudo-namespace',
'' ),
103 'dest-namespace' => intval( $this->
getOption(
'dest-namespace', 0 ) )
106 if ( $options[
'source-pseudo-namespace'] !==
'' ) {
107 $retval = $this->checkPrefix( $options );
109 $retval = $this->checkAll( $options );
113 $this->
output(
"\nLooks good!\n" );
115 $this->
output(
"\nOh noeees\n" );
126 private function checkAll( $options ) {
132 foreach ( $this->getInterwikiList() as $prefix ) {
133 $name = $contLang->ucfirst( $prefix );
143 if ( $name !==
'' ) {
144 $spaces[$name] = $ns;
147 foreach ( $contLang->getNamespaces() as $ns => $name ) {
148 if ( $name !==
'' ) {
149 $spaces[$name] = $ns;
152 foreach ( $contLang->getNamespaceAliases() as $name => $ns ) {
153 $spaces[$name] = $ns;
158 $capitalLinks = $this->
getConfig()->get( MainConfigNames::CapitalLinks );
159 foreach ( $spaces as $name => $ns ) {
161 $contLang->uc( $name ),
162 $contLang->ucfirst( $contLang->lc( $name ) ),
163 $contLang->ucwords( $name ),
164 $contLang->ucwords( $contLang->lc( $name ) ),
165 $contLang->ucwordbreaks( $name ),
166 $contLang->ucwordbreaks( $contLang->lc( $name ) ),
168 if ( !$capitalLinks ) {
169 foreach ( $moreNames as $altName ) {
170 $moreNames[] = $contLang->lcfirst( $altName );
172 $moreNames[] = $contLang->lcfirst( $name );
174 foreach ( array_unique( $moreNames ) as $altName ) {
175 if ( $altName !== $name ) {
176 $spaces[$altName] = $ns;
183 $origSpaces = $spaces;
184 uksort( $spaces,
static function ( $a, $b ) use ( $origSpaces ) {
185 return $origSpaces[$a] <=> $origSpaces[$b]
190 foreach ( $spaces as $name => $ns ) {
191 $ok = $this->checkNamespace( $ns, $name, $options ) && $ok;
195 "{$this->totalPages} pages to fix, " .
196 "{$this->resolvablePages} were resolvable.\n\n"
199 foreach ( $spaces as $name => $ns ) {
218 $this->checkLinkTable(
'pagelinks',
'pl', $ns, $name, $options );
219 $this->checkLinkTable(
'templatelinks',
'tl', $ns, $name, $options );
225 $this->checkLinkTable(
'redirect',
'rd', $ns, $name, $options,
226 [
'rd_interwiki' =>
'' ] );
231 "{$this->totalLinks} links to fix, " .
232 "{$this->resolvableLinks} were resolvable, " .
233 "{$this->deletedLinks} were deleted.\n"
242 private function getInterwikiList() {
244 return array_column( $result,
'iw_prefix' );
247 private function isSingleRevRedirectTo(
Title $oldTitle,
Title $newTitle ): bool {
248 if ( !$oldTitle->isSingleRevRedirect() ) {
252 $rev = $revStore->getRevisionByTitle( $oldTitle, 0, IDBAccessObject::READ_LATEST );
256 $content = $rev->getContent( SlotRecord::MAIN );
260 $target = $content->getRedirectTarget();
261 return $target && $target->equals( $newTitle );
264 private function deletePage(
Title $pageToDelete,
string $reason ):
Status {
265 $services = $this->getServiceContainer();
266 $page = $services->getWikiPageFactory()->newFromTitle( $pageToDelete );
267 $user = User::newSystemUser(
"Maintenance script" );
268 $deletePage = $services->getDeletePageFactory()->newDeletePage( $page, $user );
269 return $deletePage->deleteUnsafe( $reason );
280 private function checkNamespace( $ns, $name, $options ) {
281 $targets = $this->getTargetList( $ns, $name, $options );
282 $count = $targets->numRows();
283 $this->totalPages += $count;
288 $dryRunNote = $options[
'fix'] ?
'' :
' DRY RUN ONLY';
291 foreach ( $targets as $row ) {
294 $newTitle = $this->getDestinationTitle(
295 $ns, $name, $row->page_namespace, $row->page_title );
299 $oldTitle = Title::makeTitle( $row->page_namespace, $row->page_title );
301 if ( $options[
'add-prefix'] ==
'' && $options[
'add-suffix'] ==
'' ) {
302 $logStatus =
'invalid title and --add-prefix not specified';
305 $action =
'alternate';
307 } elseif ( $newTitle->
exists( IDBAccessObject::READ_LATEST ) ) {
308 if ( $this->isSingleRevRedirectTo( $newTitle, $newTitle ) ) {
312 $action =
'delete-new';
313 } elseif ( $options[
'merge'] ) {
314 if ( $this->canMerge( $row->page_id, $newTitle, $logStatus ) ) {
319 } elseif ( $options[
'add-prefix'] ==
'' && $options[
'add-suffix'] ==
'' ) {
321 $logStatus =
'dest title exists and --add-prefix not specified';
323 $action =
'alternate';
327 $logStatus =
'no conflict';
329 if ( $action ===
'alternate' ) {
330 [ $ns, $dbk ] = $this->getDestination( $ns, $name, $row->page_namespace,
332 $altTitle = $this->getAlternateTitle( $ns, $dbk, $options );
335 $logStatus =
'alternate title is invalid';
336 } elseif ( $altTitle->exists() ) {
338 $logStatus =
'alternate title conflicts';
339 } elseif ( $this->isSingleRevRedirectTo( $oldTitle, $newTitle ) ) {
340 $action =
'delete-old';
341 $newTitle = $altTitle;
344 $logStatus =
'alternate';
345 $newTitle = $altTitle;
351 $logTitle =
"id={$row->page_id} ns={$row->page_namespace} dbk={$row->page_title}";
356 $this->output(
"$logTitle move to " . $newTitle->getPrefixedDBKey() .
357 " then delete as single-revision redirect to new home$dryRunNote\n" );
358 if ( $options[
'fix'] ) {
360 $pageOK = $this->movePage( $row->page_id, $newTitle );
362 $status = $this->deletePage(
364 "Non-normalized title already redirects to new form"
366 if ( !$status->isOK() ) {
367 $this->error( $status );
374 $this->output(
"$logTitle -> " .
376 if ( $options[
'fix'] ) {
377 $status = $this->deletePage( $newTitle,
"Delete circular redirect to make way for move" );
378 $pageOK = $status->isOK();
380 $pageOK = $this->movePage( $row->page_id, $newTitle );
382 $this->error( $status );
387 $this->output(
"$logTitle *** $logStatus\n" );
391 $this->output(
"$logTitle -> " .
394 if ( $options[
'fix'] ) {
395 $pageOK = $this->movePage( $row->page_id, $newTitle );
399 $this->output(
"$logTitle => " .
402 if ( $options[
'fix'] ) {
403 $pageOK = $this->mergePage( $row, $newTitle );
409 $this->resolvablePages++;
427 private function checkLinkTable( $table, $fieldPrefix, $ns, $name, $options,
431 'templatelinks' => TemplateLinksTable::VIRTUAL_DOMAIN,
432 'imagelinks' => ImageLinksTable::VIRTUAL_DOMAIN,
433 'pagelinks' => PageLinksTable::VIRTUAL_DOMAIN,
436 if ( isset( $domainMap[$table] ) ) {
437 $dbw = $this->getServiceContainer()->getConnectionProvider()->getPrimaryDatabase( $domainMap[$table] );
439 $dbw = $this->getPrimaryDB();
443 $fromField =
"{$fieldPrefix}_from";
445 $sqb = $dbw->newSelectQueryBuilder()
446 ->select( $fromField )
447 ->where( $extraConds )
448 ->limit( $batchSize );
450 $linksMigration = $this->getServiceContainer()->getLinksMigration();
451 if ( isset( $linksMigration::$mapping[$table] ) ) {
452 $sqb->queryInfo( $linksMigration->getQueryInfo( $table ) );
453 [ $namespaceField, $titleField ] = $linksMigration->getTitleFields( $table );
454 $schemaMigrationStage = $linksMigration::$mapping[$table][
'config'] === -1
456 : $this->
getConfig()->get( $linksMigration::$mapping[$table][
'config'] );
457 $linkTargetLookup = $this->getServiceContainer()->getLinkTargetLookup();
458 $targetIdField = $linksMigration::$mapping[$table][
'target_id'];
460 $sqb->table( $table );
461 $namespaceField =
"{$fieldPrefix}_namespace";
462 $titleField =
"{$fieldPrefix}_title";
463 $sqb->fields( [ $namespaceField, $titleField ] );
465 $schemaMigrationStage = -1;
466 $linkTargetLookup =
null;
470 $namespaceField => 0,
471 $dbw->expr( $titleField, IExpression::LIKE,
new LikeValue(
"$name:", $dbw->anyString() ) ),
473 ->orderBy( [ $titleField, $fromField ] )
474 ->caller( __METHOD__ );
476 $updateRowsPerQuery = $this->
getConfig()->get( MainConfigNames::UpdateRowsPerQuery );
478 $res = ( clone $sqb )
479 ->andWhere( $batchConds )
481 if ( $res->numRows() == 0 ) {
485 $rowsToDeleteIfStillExists = [];
487 foreach ( $res as $row ) {
488 $logTitle =
"from={$row->$fromField} ns={$row->$namespaceField} " .
489 "dbk={$row->$titleField}";
490 $destTitle = $this->getDestinationTitle(
491 $ns, $name, $row->$namespaceField, $row->$titleField );
494 $this->output(
"$table $logTitle *** INVALID\n" );
497 $this->resolvableLinks++;
498 if ( !$options[
'fix'] ) {
499 $this->output(
"$table $logTitle -> " .
500 $destTitle->getPrefixedDBkey() .
" DRY RUN\n" );
504 if ( isset( $linksMigration::$mapping[$table] ) ) {
507 $setValue[$targetIdField] = $linkTargetLookup->acquireLinkTargetId( $destTitle, $dbw );
510 $setValue[
"{$fieldPrefix}_namespace"] = $destTitle->getNamespace();
511 $setValue[
"{$fieldPrefix}_title"] = $destTitle->getDBkey();
513 $whereCondition = $linksMigration->getLinksConditions(
517 $deleteCondition = $linksMigration->getLinksConditions(
519 new TitleValue( (
int)$row->$namespaceField, $row->$titleField )
523 $namespaceField => $destTitle->getNamespace(),
524 $titleField => $destTitle->getDBkey()
527 $namespaceField => 0,
528 $titleField => $row->$titleField
531 $namespaceField => $row->$namespaceField,
532 $titleField => $row->$titleField,
536 $dbw->newUpdateQueryBuilder()
540 ->where( [ $fromField => $row->$fromField ] )
541 ->andWhere( $whereCondition )
542 ->caller( __METHOD__ )
546 $rowsToDeleteIfStillExists[] = array_merge( [ $fromField => $row->$fromField ], $deleteCondition );
548 $this->output(
"$table $logTitle -> " .
549 $destTitle->getPrefixedDBkey() .
"\n"
553 if ( $options[
'fix'] && count( $rowsToDeleteIfStillExists ) > 0 ) {
555 $deleteBatches = array_chunk( $rowsToDeleteIfStillExists, $updateRowsPerQuery );
556 foreach ( $deleteBatches as $deleteBatch ) {
557 $dbw->newDeleteQueryBuilder()
558 ->deleteFrom( $table )
559 ->where( $dbw->factorConds( $deleteBatch ) )
560 ->caller( __METHOD__ )
562 $affectedRows += $dbw->affectedRows();
563 if ( count( $deleteBatches ) > 1 ) {
564 $this->waitForReplication();
568 $this->deletedLinks += $affectedRows;
569 $this->resolvableLinks -= $affectedRows;
573 $dbw->buildComparison(
'>', [
575 $titleField => $row->$titleField,
577 $fromField => $row->$fromField,
581 $this->waitForReplication();
592 private function checkPrefix( $options ) {
593 $prefix = $options[
'source-pseudo-namespace'];
594 $ns = $options[
'dest-namespace'];
595 $this->output(
"Checking prefix \"$prefix\" vs namespace $ns\n" );
597 return $this->checkNamespace( $ns, $prefix, $options );
610 private function getTargetList( $ns, $name, $options ) {
611 $dbw = $this->getPrimaryDB();
614 $options[
'move-talk'] &&
615 $this->getServiceContainer()->getNamespaceInfo()->isSubject( $ns )
622 return $dbw->newSelectQueryBuilder()
623 ->select( [
'page_id',
'page_title',
'page_namespace' ] )
626 'page_namespace' => $checkNamespaces,
627 $dbw->expr(
'page_title', IExpression::LIKE,
new LikeValue(
"$name:", $dbw->anyString() ) ),
629 ->caller( __METHOD__ )->fetchResultSet();
640 private function getDestination( $ns, $name, $sourceNs, $sourceDbk ) {
641 $dbk = substr( $sourceDbk, strlen(
"$name:" ) );
645 $dbk =
"$name-" . $dbk;
649 $nsInfo = $this->getServiceContainer()->getNamespaceInfo();
650 if ( $sourceNs ==
NS_TALK && $nsInfo->isSubject( $ns ) ) {
652 $destNS = $nsInfo->getTalk( $destNS );
654 return [ $destNS, $dbk ];
665 private function getDestinationTitle( $ns, $name, $sourceNs, $sourceDbk ) {
666 [ $destNS, $dbk ] = $this->getDestination( $ns, $name, $sourceNs, $sourceDbk );
667 $newTitle = Title::makeTitleSafe( $destNS, $dbk );
668 if ( !$newTitle || !$newTitle->
canExist() ) {
683 private function getAlternateTitle( $ns, $dbk, $options ) {
684 $prefix = $options[
'add-prefix'];
685 $suffix = $options[
'add-suffix'];
686 if ( $prefix ==
'' && $suffix ==
'' ) {
689 $newDbk = $prefix . $dbk . $suffix;
690 return Title::makeTitleSafe( $ns, $newDbk );
700 private function movePage( $id,
LinkTarget $newLinkTarget ) {
701 $dbw = $this->getPrimaryDB();
703 $update = $dbw->newUpdateQueryBuilder()
707 "page_title" => $newLinkTarget->
getDBkey(),
712 ->caller( __METHOD__ );
714 $this->getServiceContainer()->getLinkWriteDuplicator()->duplicate( $update );
717 $fromNamespaceTables = [
718 [
'templatelinks',
'tl', [
'tl_target_id' ] ],
719 [
'pagelinks',
'pl', [
'pl_target_id' ] ],
722 $fromNamespaceTables[] = [
'imagelinks',
'il', [
'il_to' ] ];
724 $fromNamespaceTables[] = [
'imagelinks',
'il', [
'il_target_id' ] ];
727 $updateRowsPerQuery = $this->
getConfig()->get( MainConfigNames::UpdateRowsPerQuery );
729 foreach ( $fromNamespaceTables as [ $table, $fieldPrefix, $additionalPrimaryKeyFields ] ) {
731 'templatelinks' => TemplateLinksTable::VIRTUAL_DOMAIN,
732 'imagelinks' => ImageLinksTable::VIRTUAL_DOMAIN,
733 'pagelinks' => PageLinksTable::VIRTUAL_DOMAIN,
736 if ( isset( $domainMap[$table] ) ) {
737 $dbw = $this->getServiceContainer()->getConnectionProvider()->getPrimaryDatabase( $domainMap[$table] );
739 $dbw = $this->getPrimaryDB();
742 $fromField =
"{$fieldPrefix}_from";
743 $fromNamespaceField =
"{$fieldPrefix}_from_namespace";
745 $res = $dbw->newSelectQueryBuilder()
746 ->select( $additionalPrimaryKeyFields )
748 ->where( [ $fromField => $id ] )
749 ->andWhere( $dbw->expr( $fromNamespaceField,
'!=', $newLinkTarget->
getNamespace() ) )
750 ->caller( __METHOD__ )
757 foreach ( $res as $row ) {
758 $updateConds[] = array_merge( [ $fromField => $id ], (array)$row );
760 $updateBatches = array_chunk( $updateConds, $updateRowsPerQuery );
761 foreach ( $updateBatches as $updateBatch ) {
762 $this->beginTransactionRound( __METHOD__ );
763 $dbw->newUpdateQueryBuilder()
765 ->set( [ $fromNamespaceField => $newLinkTarget->
getNamespace() ] )
766 ->where( $dbw->factorConds( $updateBatch ) )
767 ->caller( __METHOD__ )
769 $this->commitTransactionRound( __METHOD__ );
788 private function canMerge( $id,
PageIdentity $page, &$logStatus ) {
789 $revisionLookup = $this->getServiceContainer()->getRevisionLookup();
790 $latestDest = $revisionLookup->getRevisionByTitle( $page, 0,
791 IDBAccessObject::READ_LATEST );
792 $latestSource = $revisionLookup->getRevisionByPageId( $id, 0,
793 IDBAccessObject::READ_LATEST );
794 if ( $latestSource->getTimestamp() > $latestDest->getTimestamp() ) {
795 $logStatus =
'cannot merge since source is later';
809 private function mergePage( $row,
Title $newTitle ) {
810 $updateRowsPerQuery = $this->
getConfig()->get( MainConfigNames::UpdateRowsPerQuery );
817 $sourceTitle = Title::makeTitle( $row->page_namespace, $row->page_title );
818 $sourceTitle->resetArticleID( $id );
819 $wikiPage = $this->getServiceContainer()->getWikiPageFactory()->newFromTitle( $sourceTitle );
820 $wikiPage->loadPageData( IDBAccessObject::READ_LATEST );
823 $dbw = $this->getPrimaryDB();
824 $this->beginTransactionRound( __METHOD__ );
825 $revIds = $dbw->newSelectQueryBuilder()
828 ->where( [
'rev_page' => $id ] )
829 ->caller( __METHOD__ )
830 ->fetchFieldValues();
831 $updateBatches = array_chunk( array_map(
'intval', $revIds ), $updateRowsPerQuery );
832 foreach ( $updateBatches as $updateBatch ) {
833 $dbw->newUpdateQueryBuilder()
834 ->update(
'revision' )
835 ->set( [
'rev_page' => $destId ] )
836 ->where( [
'rev_id' => $updateBatch ] )
837 ->caller( __METHOD__ )
839 if ( count( $updateBatches ) > 1 ) {
840 $this->commitTransactionRound( __METHOD__ );
841 $this->beginTransactionRound( __METHOD__ );
845 $delete = $dbw->newDeleteQueryBuilder()
846 ->deleteFrom(
'page' )
847 ->where( [
'page_id' => $id ] )
848 ->caller( __METHOD__ );
850 $this->getServiceContainer()->getLinkWriteDuplicator()->duplicate( $delete );
851 $this->commitTransactionRound( __METHOD__ );
863 DeferredUpdates::doUpdates();