48 private $resolvablePages = 0;
54 private $totalPages = 0;
60 private $resolvableLinks = 0;
66 private $totalLinks = 0;
73 private $deletedLinks = 0;
76 parent::__construct();
77 $this->
addDescription(
'Find and fix pages affected by namespace addition/removal' );
78 $this->
addOption(
'fix',
'Attempt to automatically fix errors and delete broken links' );
79 $this->
addOption(
'merge',
"Instead of renaming conflicts, do a history merge with " .
80 "the correct title" );
81 $this->
addOption(
'add-suffix',
"Dupes will be renamed with correct namespace with " .
82 "<text> appended after the article name",
false,
true );
83 $this->
addOption(
'add-prefix',
"Dupes will be renamed with correct namespace with " .
84 "<text> prepended before the article name",
false,
true );
85 $this->
addOption(
'source-pseudo-namespace',
"Move all pages with the given source " .
86 "prefix (with an implied colon following it). If --dest-namespace is not specified, " .
87 "the colon will be replaced with a hyphen.",
89 $this->
addOption(
'dest-namespace',
"In combination with --source-pseudo-namespace, " .
90 "specify the namespace ID of the destination.",
false,
true );
97 'add-suffix' => $this->
getOption(
'add-suffix',
'' ),
98 'add-prefix' => $this->
getOption(
'add-prefix',
'' ),
99 'source-pseudo-namespace' => $this->
getOption(
'source-pseudo-namespace',
'' ),
100 'dest-namespace' => intval( $this->
getOption(
'dest-namespace', 0 ) )
103 if ( $options[
'source-pseudo-namespace'] !==
'' ) {
104 $retval = $this->checkPrefix( $options );
106 $retval = $this->checkAll( $options );
110 $this->
output(
"\nLooks good!\n" );
112 $this->
output(
"\nOh noeees\n" );
123 private function checkAll( $options ) {
128 foreach ( $this->getInterwikiList() as $prefix ) {
129 $name = $contLang->ucfirst( $prefix );
130 $interwikis[$name] = 0;
139 if ( $name !==
'' ) {
140 $spaces[$name] = $ns;
143 foreach ( $contLang->getNamespaces() as $ns => $name ) {
144 if ( $name !==
'' ) {
145 $spaces[$name] = $ns;
148 foreach ( $contLang->getNamespaceAliases() as $name => $ns ) {
149 $spaces[$name] = $ns;
154 $capitalLinks = $this->
getConfig()->get( MainConfigNames::CapitalLinks );
155 foreach ( $spaces as $name => $ns ) {
157 $contLang->uc( $name ),
158 $contLang->ucfirst( $contLang->lc( $name ) ),
159 $contLang->ucwords( $name ),
160 $contLang->ucwords( $contLang->lc( $name ) ),
161 $contLang->ucwordbreaks( $name ),
162 $contLang->ucwordbreaks( $contLang->lc( $name ) ),
164 if ( !$capitalLinks ) {
165 foreach ( $moreNames as $altName ) {
166 $moreNames[] = $contLang->lcfirst( $altName );
168 $moreNames[] = $contLang->lcfirst( $name );
170 foreach ( array_unique( $moreNames ) as $altName ) {
171 if ( $altName !== $name ) {
172 $spaces[$altName] = $ns;
178 $spaces += $interwikis;
182 $origSpaces = $spaces;
183 uksort( $spaces,
static function ( $a, $b ) use ( $origSpaces ) {
184 return $origSpaces[$a] <=> $origSpaces[$b]
189 foreach ( $spaces as $name => $ns ) {
190 $ok = $this->checkNamespace( $ns, $name, $options ) && $ok;
194 "{$this->totalPages} pages to fix, " .
195 "{$this->resolvablePages} were resolvable.\n\n"
198 foreach ( $spaces as $name => $ns ) {
217 $this->checkLinkTable(
'pagelinks',
'pl', $ns, $name, $options );
218 $this->checkLinkTable(
'templatelinks',
'tl', $ns, $name, $options );
224 $this->checkLinkTable(
'redirect',
'rd', $ns, $name, $options,
225 [
'rd_interwiki' =>
'' ] );
230 "{$this->totalLinks} links to fix, " .
231 "{$this->resolvableLinks} were resolvable, " .
232 "{$this->deletedLinks} were deleted.\n"
241 private function getInterwikiList() {
243 return array_column( $result,
'iw_prefix' );
246 private function isSingleRevRedirectTo(
Title $oldTitle,
Title $newTitle ): bool {
247 if ( !$oldTitle->isSingleRevRedirect() ) {
251 $rev = $revStore->getRevisionByTitle( $oldTitle, 0, IDBAccessObject::READ_LATEST );
255 $content = $rev->getContent( SlotRecord::MAIN );
259 $target = $content->getRedirectTarget();
260 return $target && $target->equals( $newTitle );
263 private function deletePage(
Title $pageToDelete,
string $reason ):
Status {
264 $services = $this->getServiceContainer();
265 $page = $services->getWikiPageFactory()->newFromTitle( $pageToDelete );
266 $user = User::newSystemUser(
"Maintenance script" );
267 $deletePage = $services->getDeletePageFactory()->newDeletePage( $page, $user );
268 return $deletePage->deleteUnsafe( $reason );
279 private function checkNamespace( $ns, $name, $options ) {
280 $targets = $this->getTargetList( $ns, $name );
281 $count = $targets->numRows();
282 $this->totalPages += $count;
287 $dryRunNote = $options[
'fix'] ?
'' :
' DRY RUN ONLY';
290 foreach ( $targets as $row ) {
293 $newTitle = $this->getDestinationTitle(
294 $ns, $name, $row->page_namespace, $row->page_title );
298 $oldTitle = Title::makeTitle( $row->page_namespace, $row->page_title );
300 if ( $options[
'add-prefix'] ==
'' && $options[
'add-suffix'] ==
'' ) {
301 $logStatus =
'invalid title and --add-prefix not specified';
304 $action =
'alternate';
306 } elseif ( $newTitle->
exists( IDBAccessObject::READ_LATEST ) ) {
307 if ( $this->isSingleRevRedirectTo( $newTitle, $newTitle ) ) {
311 $action =
'delete-new';
312 } elseif ( $options[
'merge'] ) {
313 if ( $this->canMerge( $row->page_id, $newTitle, $logStatus ) ) {
318 } elseif ( $options[
'add-prefix'] ==
'' && $options[
'add-suffix'] ==
'' ) {
320 $logStatus =
'dest title exists and --add-prefix not specified';
322 $action =
'alternate';
326 $logStatus =
'no conflict';
328 if ( $action ===
'alternate' ) {
329 [ $ns, $dbk ] = $this->getDestination( $ns, $name, $row->page_namespace,
331 $altTitle = $this->getAlternateTitle( $ns, $dbk, $options );
334 $logStatus =
'alternate title is invalid';
335 } elseif ( $altTitle->exists() ) {
337 $logStatus =
'alternate title conflicts';
338 } elseif ( $this->isSingleRevRedirectTo( $oldTitle, $newTitle ) ) {
339 $action =
'delete-old';
340 $newTitle = $altTitle;
343 $logStatus =
'alternate';
344 $newTitle = $altTitle;
350 $logTitle =
"id={$row->page_id} ns={$row->page_namespace} dbk={$row->page_title}";
355 $this->output(
"$logTitle move to " . $newTitle->getPrefixedDBKey() .
356 " then delete as single-revision redirect to new home$dryRunNote\n" );
357 if ( $options[
'fix'] ) {
359 $pageOK = $this->movePage( $row->page_id, $newTitle );
361 $status = $this->deletePage(
363 "Non-normalized title already redirects to new form"
365 if ( !$status->isOK() ) {
366 $this->error( $status );
373 $this->output(
"$logTitle -> " .
375 if ( $options[
'fix'] ) {
376 $status = $this->deletePage( $newTitle,
"Delete circular redirect to make way for move" );
377 $pageOK = $status->isOK();
379 $pageOK = $this->movePage( $row->page_id, $newTitle );
381 $this->error( $status );
386 $this->output(
"$logTitle *** $logStatus\n" );
390 $this->output(
"$logTitle -> " .
393 if ( $options[
'fix'] ) {
394 $pageOK = $this->movePage( $row->page_id, $newTitle );
398 $this->output(
"$logTitle => " .
401 if ( $options[
'fix'] ) {
402 $pageOK = $this->mergePage( $row, $newTitle );
408 $this->resolvablePages++;
426 private function checkLinkTable( $table, $fieldPrefix, $ns, $name, $options,
430 'templatelinks' => TemplateLinksTable::VIRTUAL_DOMAIN,
431 'imagelinks' => ImageLinksTable::VIRTUAL_DOMAIN,
432 'pagelinks' => PageLinksTable::VIRTUAL_DOMAIN,
435 if ( isset( $domainMap[$table] ) ) {
436 $dbw = $this->getServiceContainer()->getConnectionProvider()->getPrimaryDatabase( $domainMap[$table] );
438 $dbw = $this->getPrimaryDB();
442 $fromField =
"{$fieldPrefix}_from";
444 $sqb = $dbw->newSelectQueryBuilder()
445 ->select( $fromField )
446 ->where( $extraConds )
447 ->limit( $batchSize );
449 $linksMigration = $this->getServiceContainer()->getLinksMigration();
450 if ( isset( $linksMigration::$mapping[$table] ) ) {
451 $sqb->queryInfo( $linksMigration->getQueryInfo( $table ) );
452 [ $namespaceField, $titleField ] = $linksMigration->getTitleFields( $table );
453 $linkTargetLookup = $this->getServiceContainer()->getLinkTargetLookup();
454 $targetIdField = $linksMigration::$mapping[$table][
'target_id'];
456 $sqb->table( $table );
457 $namespaceField =
"{$fieldPrefix}_namespace";
458 $titleField =
"{$fieldPrefix}_title";
459 $sqb->fields( [ $namespaceField, $titleField ] );
461 $linkTargetLookup =
null;
465 $namespaceField => 0,
466 $dbw->expr( $titleField, IExpression::LIKE,
new LikeValue(
"$name:", $dbw->anyString() ) ),
468 ->orderBy( [ $titleField, $fromField ] )
469 ->caller( __METHOD__ );
471 $updateRowsPerQuery = $this->
getConfig()->get( MainConfigNames::UpdateRowsPerQuery );
473 $res = ( clone $sqb )
474 ->andWhere( $batchConds )
476 if ( $res->numRows() == 0 ) {
480 $rowsToDeleteIfStillExists = [];
482 foreach ( $res as $row ) {
483 $logTitle =
"from={$row->$fromField} ns={$row->$namespaceField} " .
484 "dbk={$row->$titleField}";
485 $destTitle = $this->getDestinationTitle(
486 $ns, $name, $row->$namespaceField, $row->$titleField );
489 $this->output(
"$table $logTitle *** INVALID\n" );
492 $this->resolvableLinks++;
493 if ( !$options[
'fix'] ) {
494 $this->output(
"$table $logTitle -> " .
495 $destTitle->getPrefixedDBkey() .
" DRY RUN\n" );
499 if ( isset( $linksMigration::$mapping[$table] ) ) {
501 $targetIdField => $linkTargetLookup->acquireLinkTargetId( $destTitle, $dbw )
503 $whereCondition = $linksMigration->getLinksConditions(
507 $deleteCondition = $linksMigration->getLinksConditions(
509 new TitleValue( (
int)$row->$namespaceField, $row->$titleField )
513 $namespaceField => $destTitle->getNamespace(),
514 $titleField => $destTitle->getDBkey()
517 $namespaceField => 0,
518 $titleField => $row->$titleField
521 $namespaceField => $row->$namespaceField,
522 $titleField => $row->$titleField,
526 $dbw->newUpdateQueryBuilder()
530 ->where( [ $fromField => $row->$fromField ] )
531 ->andWhere( $whereCondition )
532 ->caller( __METHOD__ )
536 $rowsToDeleteIfStillExists[] = array_merge( [ $fromField => $row->$fromField ], $deleteCondition );
538 $this->output(
"$table $logTitle -> " .
539 $destTitle->getPrefixedDBkey() .
"\n"
543 if ( $options[
'fix'] && count( $rowsToDeleteIfStillExists ) > 0 ) {
545 $deleteBatches = array_chunk( $rowsToDeleteIfStillExists, $updateRowsPerQuery );
546 foreach ( $deleteBatches as $deleteBatch ) {
547 $dbw->newDeleteQueryBuilder()
548 ->deleteFrom( $table )
549 ->where( $dbw->factorConds( $deleteBatch ) )
550 ->caller( __METHOD__ )
552 $affectedRows += $dbw->affectedRows();
553 if ( count( $deleteBatches ) > 1 ) {
554 $this->waitForReplication();
558 $this->deletedLinks += $affectedRows;
559 $this->resolvableLinks -= $affectedRows;
563 $dbw->buildComparison(
'>', [
565 $titleField => $row->$titleField,
567 $fromField => $row->$fromField,
571 $this->waitForReplication();
582 private function checkPrefix( $options ) {
583 $prefix = $options[
'source-pseudo-namespace'];
584 $ns = $options[
'dest-namespace'];
585 $this->output(
"Checking prefix \"$prefix\" vs namespace $ns\n" );
587 return $this->checkNamespace( $ns, $prefix, $options );
599 private function getTargetList( $ns, $name ) {
600 $dbw = $this->getPrimaryDB();
603 $this->getServiceContainer()->getNamespaceInfo()->isSubject( $ns )
610 return $dbw->newSelectQueryBuilder()
611 ->select( [
'page_id',
'page_title',
'page_namespace' ] )
614 'page_namespace' => $checkNamespaces,
615 $dbw->expr(
'page_title', IExpression::LIKE,
new LikeValue(
"$name:", $dbw->anyString() ) ),
617 ->caller( __METHOD__ )->fetchResultSet();
628 private function getDestination( $ns, $name, $sourceNs, $sourceDbk ) {
629 $dbk = substr( $sourceDbk, strlen(
"$name:" ) );
633 $dbk =
"$name-" . $dbk;
637 $nsInfo = $this->getServiceContainer()->getNamespaceInfo();
638 if ( $sourceNs ==
NS_TALK && $nsInfo->isSubject( $ns ) ) {
640 $destNS = $nsInfo->getTalk( $destNS );
642 return [ $destNS, $dbk ];
653 private function getDestinationTitle( $ns, $name, $sourceNs, $sourceDbk ) {
654 [ $destNS, $dbk ] = $this->getDestination( $ns, $name, $sourceNs, $sourceDbk );
655 $newTitle = Title::makeTitleSafe( $destNS, $dbk );
656 if ( !$newTitle || !$newTitle->
canExist() ) {
671 private function getAlternateTitle( $ns, $dbk, $options ) {
672 $prefix = $options[
'add-prefix'];
673 $suffix = $options[
'add-suffix'];
674 if ( $prefix ==
'' && $suffix ==
'' ) {
677 $newDbk = $prefix . $dbk . $suffix;
678 return Title::makeTitleSafe( $ns, $newDbk );
688 private function movePage( $id,
LinkTarget $newLinkTarget ) {
689 $dbw = $this->getPrimaryDB();
691 $update = $dbw->newUpdateQueryBuilder()
695 "page_title" => $newLinkTarget->
getDBkey(),
700 ->caller( __METHOD__ );
702 $this->getServiceContainer()->getLinkWriteDuplicator()->duplicate( $update );
705 $fromNamespaceTables = [
706 [
'templatelinks',
'tl', [
'tl_target_id' ] ],
707 [
'pagelinks',
'pl', [
'pl_target_id' ] ],
708 [
'imagelinks',
'il', [
'il_target_id' ] ],
711 $updateRowsPerQuery = $this->
getConfig()->get( MainConfigNames::UpdateRowsPerQuery );
713 foreach ( $fromNamespaceTables as [ $table, $fieldPrefix, $additionalPrimaryKeyFields ] ) {
715 'templatelinks' => TemplateLinksTable::VIRTUAL_DOMAIN,
716 'imagelinks' => ImageLinksTable::VIRTUAL_DOMAIN,
717 'pagelinks' => PageLinksTable::VIRTUAL_DOMAIN,
720 $dbw = $this->getServiceContainer()->getConnectionProvider()->getPrimaryDatabase( $domainMap[$table] );
722 $fromField =
"{$fieldPrefix}_from";
723 $fromNamespaceField =
"{$fieldPrefix}_from_namespace";
725 $res = $dbw->newSelectQueryBuilder()
726 ->select( $additionalPrimaryKeyFields )
728 ->where( [ $fromField => $id ] )
729 ->andWhere( $dbw->expr( $fromNamespaceField,
'!=', $newLinkTarget->
getNamespace() ) )
730 ->caller( __METHOD__ )
737 foreach ( $res as $row ) {
738 $updateConds[] = array_merge( [ $fromField => $id ], (array)$row );
740 $updateBatches = array_chunk( $updateConds, $updateRowsPerQuery );
741 foreach ( $updateBatches as $updateBatch ) {
742 $this->beginTransactionRound( __METHOD__ );
743 $dbw->newUpdateQueryBuilder()
745 ->set( [ $fromNamespaceField => $newLinkTarget->
getNamespace() ] )
746 ->where( $dbw->factorConds( $updateBatch ) )
747 ->caller( __METHOD__ )
749 $this->commitTransactionRound( __METHOD__ );
768 private function canMerge( $id,
PageIdentity $page, &$logStatus ) {
769 $revisionLookup = $this->getServiceContainer()->getRevisionLookup();
770 $latestDest = $revisionLookup->getRevisionByTitle( $page, 0,
771 IDBAccessObject::READ_LATEST );
772 $latestSource = $revisionLookup->getRevisionByPageId( $id, 0,
773 IDBAccessObject::READ_LATEST );
774 if ( $latestSource->getTimestamp() > $latestDest->getTimestamp() ) {
775 $logStatus =
'cannot merge since source is later';
789 private function mergePage( $row,
Title $newTitle ) {
790 $updateRowsPerQuery = $this->
getConfig()->get( MainConfigNames::UpdateRowsPerQuery );
797 $sourceTitle = Title::makeTitle( $row->page_namespace, $row->page_title );
798 $sourceTitle->resetArticleID( $id );
799 $wikiPage = $this->getServiceContainer()->getWikiPageFactory()->newFromTitle( $sourceTitle );
800 $wikiPage->loadPageData( IDBAccessObject::READ_LATEST );
803 $dbw = $this->getPrimaryDB();
804 $this->beginTransactionRound( __METHOD__ );
805 $revIds = $dbw->newSelectQueryBuilder()
808 ->where( [
'rev_page' => $id ] )
809 ->caller( __METHOD__ )
810 ->fetchFieldValues();
811 $updateBatches = array_chunk( array_map(
'intval', $revIds ), $updateRowsPerQuery );
812 foreach ( $updateBatches as $updateBatch ) {
813 $dbw->newUpdateQueryBuilder()
814 ->update(
'revision' )
815 ->set( [
'rev_page' => $destId ] )
816 ->where( [
'rev_id' => $updateBatch ] )
817 ->caller( __METHOD__ )
819 if ( count( $updateBatches ) > 1 ) {
820 $this->commitTransactionRound( __METHOD__ );
821 $this->beginTransactionRound( __METHOD__ );
825 $delete = $dbw->newDeleteQueryBuilder()
826 ->deleteFrom(
'page' )
827 ->where( [
'page_id' => $id ] )
828 ->caller( __METHOD__ );
830 $this->getServiceContainer()->getLinkWriteDuplicator()->duplicate( $delete );
831 $this->commitTransactionRound( __METHOD__ );
843 DeferredUpdates::doUpdates();