51 private $resolvablePages = 0;
57 private $totalPages = 0;
63 private $resolvableLinks = 0;
69 private $totalLinks = 0;
76 private $deletedLinks = 0;
79 parent::__construct();
80 $this->
addDescription(
'Find and fix pages affected by namespace addition/removal' );
81 $this->
addOption(
'fix',
'Attempt to automatically fix errors and delete broken links' );
82 $this->
addOption(
'merge',
"Instead of renaming conflicts, do a history merge with " .
83 "the correct title" );
84 $this->
addOption(
'add-suffix',
"Dupes will be renamed with correct namespace with " .
85 "<text> appended after the article name",
false,
true );
86 $this->
addOption(
'add-prefix',
"Dupes will be renamed with correct namespace with " .
87 "<text> prepended before the article name",
false,
true );
88 $this->
addOption(
'source-pseudo-namespace',
"Move all pages with the given source " .
89 "prefix (with an implied colon following it). If --dest-namespace is not specified, " .
90 "the colon will be replaced with a hyphen.",
92 $this->
addOption(
'dest-namespace',
"In combination with --source-pseudo-namespace, " .
93 "specify the namespace ID of the destination.",
false,
true );
94 $this->
addOption(
'move-talk',
"If this is specified, pages in the Talk namespace that " .
95 "begin with a conflicting prefix will be renamed, for example " .
96 "Talk:File:Foo -> File_Talk:Foo" );
103 'add-suffix' => $this->
getOption(
'add-suffix',
'' ),
104 'add-prefix' => $this->
getOption(
'add-prefix',
'' ),
105 'move-talk' => $this->
hasOption(
'move-talk' ),
106 'source-pseudo-namespace' => $this->
getOption(
'source-pseudo-namespace',
'' ),
107 'dest-namespace' => intval( $this->
getOption(
'dest-namespace', 0 ) )
110 if ( $options[
'source-pseudo-namespace'] !==
'' ) {
111 $retval = $this->checkPrefix( $options );
113 $retval = $this->checkAll( $options );
117 $this->
output(
"\nLooks good!\n" );
119 $this->
output(
"\nOh noeees\n" );
130 private function checkAll( $options ) {
136 foreach ( $this->getInterwikiList() as $prefix ) {
137 $name = $contLang->ucfirst( $prefix );
147 if ( $name !==
'' ) {
148 $spaces[$name] = $ns;
151 foreach ( $contLang->getNamespaces() as $ns => $name ) {
152 if ( $name !==
'' ) {
153 $spaces[$name] = $ns;
156 foreach ( $contLang->getNamespaceAliases() as $name => $ns ) {
157 $spaces[$name] = $ns;
162 $capitalLinks = $this->
getConfig()->get( MainConfigNames::CapitalLinks );
163 foreach ( $spaces as $name => $ns ) {
165 $moreNames[] = $contLang->uc( $name );
166 $moreNames[] = $contLang->ucfirst( $contLang->lc( $name ) );
167 $moreNames[] = $contLang->ucwords( $name );
168 $moreNames[] = $contLang->ucwords( $contLang->lc( $name ) );
169 $moreNames[] = $contLang->ucwordbreaks( $name );
170 $moreNames[] = $contLang->ucwordbreaks( $contLang->lc( $name ) );
171 if ( !$capitalLinks ) {
172 foreach ( $moreNames as $altName ) {
173 $moreNames[] = $contLang->lcfirst( $altName );
175 $moreNames[] = $contLang->lcfirst( $name );
177 foreach ( array_unique( $moreNames ) as $altName ) {
178 if ( $altName !== $name ) {
179 $spaces[$altName] = $ns;
186 $origSpaces = $spaces;
187 uksort( $spaces,
static function ( $a, $b ) use ( $origSpaces ) {
188 return $origSpaces[$a] <=> $origSpaces[$b]
193 foreach ( $spaces as $name => $ns ) {
194 $ok = $this->checkNamespace( $ns, $name, $options ) && $ok;
198 "{$this->totalPages} pages to fix, " .
199 "{$this->resolvablePages} were resolvable.\n\n"
202 foreach ( $spaces as $name => $ns ) {
221 $this->checkLinkTable(
'pagelinks',
'pl', $ns, $name, $options );
222 $this->checkLinkTable(
'templatelinks',
'tl', $ns, $name, $options );
228 $this->checkLinkTable(
'redirect',
'rd', $ns, $name, $options,
229 [
'rd_interwiki' =>
'' ] );
234 "{$this->totalLinks} links to fix, " .
235 "{$this->resolvableLinks} were resolvable, " .
236 "{$this->deletedLinks} were deleted.\n"
245 private function getInterwikiList() {
247 return array_column( $result,
'iw_prefix' );
258 private function checkNamespace( $ns, $name, $options ) {
259 $targets = $this->getTargetList( $ns, $name, $options );
260 $count = $targets->numRows();
261 $this->totalPages += $count;
266 $dryRunNote = $options[
'fix'] ?
'' :
' DRY RUN ONLY';
269 foreach ( $targets as $row ) {
272 $newTitle = $this->getDestinationTitle(
273 $ns, $name, $row->page_namespace, $row->page_title );
276 if ( $options[
'add-prefix'] ==
'' && $options[
'add-suffix'] ==
'' ) {
277 $logStatus =
'invalid title and --add-prefix not specified';
280 $action =
'alternate';
282 } elseif ( $newTitle->
exists() ) {
283 if ( $options[
'merge'] ) {
284 if ( $this->canMerge( $row->page_id, $newTitle, $logStatus ) ) {
289 } elseif ( $options[
'add-prefix'] ==
'' && $options[
'add-suffix'] ==
'' ) {
291 $logStatus =
'dest title exists and --add-prefix not specified';
293 $action =
'alternate';
297 $logStatus =
'no conflict';
299 if ( $action ===
'alternate' ) {
300 [ $ns, $dbk ] = $this->getDestination( $ns, $name, $row->page_namespace,
302 $newTitle = $this->getAlternateTitle( $ns, $dbk, $options );
305 $logStatus =
'alternate title is invalid';
306 } elseif ( $newTitle->
exists() ) {
308 $logStatus =
'alternate title conflicts';
311 $logStatus =
'alternate';
317 $logTitle =
"id={$row->page_id} ns={$row->page_namespace} dbk={$row->page_title}";
322 $this->
output(
"$logTitle *** $logStatus\n" );
326 $this->
output(
"$logTitle -> " .
329 if ( $options[
'fix'] ) {
330 $pageOK = $this->movePage( $row->page_id, $newTitle );
334 $this->
output(
"$logTitle => " .
337 if ( $options[
'fix'] ) {
338 $pageOK = $this->mergePage( $row, $newTitle );
344 $this->resolvablePages++;
362 private function checkLinkTable( $table, $fieldPrefix, $ns, $name, $options,
368 $fromField =
"{$fieldPrefix}_from";
370 $sqb = $dbw->newSelectQueryBuilder()
371 ->select( $fromField )
372 ->where( $extraConds )
373 ->limit( $batchSize );
376 if ( isset( $linksMigration::$mapping[$table] ) ) {
377 $sqb->queryInfo( $linksMigration->getQueryInfo( $table ) );
378 [ $namespaceField, $titleField ] = $linksMigration->getTitleFields( $table );
379 $schemaMigrationStage = $this->
getConfig()->get( $linksMigration::$mapping[$table][
'config'] );
381 $targetIdField = $linksMigration::$mapping[$table][
'target_id'];
383 $sqb->table( $table );
384 $namespaceField =
"{$fieldPrefix}_namespace";
385 $titleField =
"{$fieldPrefix}_title";
386 $sqb->fields( [ $namespaceField, $titleField ] );
388 $schemaMigrationStage = -1;
389 $linkTargetLookup =
null;
393 $namespaceField => 0,
394 $dbw->expr( $titleField, IExpression::LIKE,
new LikeValue(
"$name:", $dbw->anyString() ) ),
396 ->orderBy( [ $titleField, $fromField ] )
397 ->caller( __METHOD__ );
399 $updateRowsPerQuery = $this->
getConfig()->get( MainConfigNames::UpdateRowsPerQuery );
401 $res = ( clone $sqb )
402 ->andWhere( $batchConds )
404 if ( $res->numRows() == 0 ) {
408 $rowsToDeleteIfStillExists = [];
410 foreach ( $res as $row ) {
411 $logTitle =
"from={$row->$fromField} ns={$row->$namespaceField} " .
412 "dbk={$row->$titleField}";
413 $destTitle = $this->getDestinationTitle(
414 $ns, $name, $row->$namespaceField, $row->$titleField );
417 $this->
output(
"$table $logTitle *** INVALID\n" );
420 $this->resolvableLinks++;
421 if ( !$options[
'fix'] ) {
422 $this->
output(
"$table $logTitle -> " .
423 $destTitle->getPrefixedDBkey() .
" DRY RUN\n" );
427 if ( isset( $linksMigration::$mapping[$table] ) ) {
430 $setValue[$targetIdField] = $linkTargetLookup->acquireLinkTargetId( $destTitle, $dbw );
433 $setValue[
"{$fieldPrefix}_namespace"] = $destTitle->getNamespace();
434 $setValue[
"{$fieldPrefix}_title"] = $destTitle->getDBkey();
436 $whereCondition = $linksMigration->getLinksConditions(
440 $deleteCondition = $linksMigration->getLinksConditions(
442 new TitleValue( (
int)$row->$namespaceField, $row->$titleField )
446 $namespaceField => $destTitle->getNamespace(),
447 $titleField => $destTitle->getDBkey()
450 $namespaceField => 0,
451 $titleField => $row->$titleField
454 $namespaceField => $row->$namespaceField,
455 $titleField => $row->$titleField,
459 $dbw->newUpdateQueryBuilder()
463 ->where( [ $fromField => $row->$fromField ] )
464 ->andWhere( $whereCondition )
465 ->caller( __METHOD__ )
469 $rowsToDeleteIfStillExists[] = array_merge( [ $fromField => $row->$fromField ], $deleteCondition );
471 $this->
output(
"$table $logTitle -> " .
472 $destTitle->getPrefixedDBkey() .
"\n"
476 if ( $options[
'fix'] && count( $rowsToDeleteIfStillExists ) > 0 ) {
478 $deleteBatches = array_chunk( $rowsToDeleteIfStillExists, $updateRowsPerQuery );
479 foreach ( $deleteBatches as $deleteBatch ) {
480 $dbw->newDeleteQueryBuilder()
481 ->deleteFrom( $table )
482 ->where( $dbw->factorConds( $deleteBatch ) )
483 ->caller( __METHOD__ )
485 $affectedRows += $dbw->affectedRows();
486 if ( count( $deleteBatches ) > 1 ) {
491 $this->deletedLinks += $affectedRows;
492 $this->resolvableLinks -= $affectedRows;
496 $dbw->buildComparison(
'>', [
498 $titleField => $row->$titleField,
500 $fromField => $row->$fromField,
515 private function checkPrefix( $options ) {
516 $prefix = $options[
'source-pseudo-namespace'];
517 $ns = $options[
'dest-namespace'];
518 $this->
output(
"Checking prefix \"$prefix\" vs namespace $ns\n" );
520 return $this->checkNamespace( $ns, $prefix, $options );
533 private function getTargetList( $ns, $name, $options ) {
537 $options[
'move-talk'] &&
545 return $dbw->newSelectQueryBuilder()
546 ->select( [
'page_id',
'page_title',
'page_namespace' ] )
549 'page_namespace' => $checkNamespaces,
550 $dbw->expr(
'page_title', IExpression::LIKE,
new LikeValue(
"$name:", $dbw->anyString() ) ),
552 ->caller( __METHOD__ )->fetchResultSet();
563 private function getDestination( $ns, $name, $sourceNs, $sourceDbk ) {
564 $dbk = substr( $sourceDbk, strlen(
"$name:" ) );
567 $dbk =
"$name-" . $dbk;
571 if ( $sourceNs ==
NS_TALK && $nsInfo->isSubject( $ns ) ) {
573 $destNS = $nsInfo->getTalk( $destNS );
575 return [ $destNS, $dbk ];
586 private function getDestinationTitle( $ns, $name, $sourceNs, $sourceDbk ) {
587 [ $destNS, $dbk ] = $this->getDestination( $ns, $name, $sourceNs, $sourceDbk );
588 $newTitle = Title::makeTitleSafe( $destNS, $dbk );
589 if ( !$newTitle || !$newTitle->
canExist() ) {
604 private function getAlternateTitle( $ns, $dbk, $options ) {
605 $prefix = $options[
'add-prefix'];
606 $suffix = $options[
'add-suffix'];
607 if ( $prefix ==
'' && $suffix ==
'' ) {
610 $newDbk = $prefix . $dbk . $suffix;
611 return Title::makeTitleSafe( $ns, $newDbk );
621 private function movePage( $id,
LinkTarget $newLinkTarget ) {
624 $dbw->newUpdateQueryBuilder()
628 "page_title" => $newLinkTarget->
getDBkey(),
633 ->caller( __METHOD__ )
637 $fromNamespaceTables = [
638 [
'pagelinks',
'pl', [
'pl_namespace',
'pl_title' ] ],
639 [
'templatelinks',
'tl', [
'tl_target_id' ] ],
640 [
'imagelinks',
'il', [
'il_to' ] ]
642 $updateRowsPerQuery = $this->
getConfig()->get( MainConfigNames::UpdateRowsPerQuery );
643 foreach ( $fromNamespaceTables as [ $table, $fieldPrefix, $additionalPrimaryKeyFields ] ) {
644 $fromField =
"{$fieldPrefix}_from";
645 $fromNamespaceField =
"{$fieldPrefix}_from_namespace";
647 $res = $dbw->newSelectQueryBuilder()
648 ->select( $additionalPrimaryKeyFields )
650 ->where( [ $fromField => $id ] )
651 ->andWhere( $dbw->expr( $fromNamespaceField,
'!=', $newLinkTarget->
getNamespace() ) )
652 ->caller( __METHOD__ )
659 foreach ( $res as $row ) {
660 $updateConds[] = array_merge( [ $fromField => $id ], (array)$row );
662 $updateBatches = array_chunk( $updateConds, $updateRowsPerQuery );
663 foreach ( $updateBatches as $updateBatch ) {
664 $dbw->newUpdateQueryBuilder()
666 ->set( [ $fromNamespaceField => $newLinkTarget->
getNamespace() ] )
667 ->where( $dbw->factorConds( $updateBatch ) )
668 ->caller( __METHOD__ )
670 if ( count( $updateBatches ) > 1 ) {
691 private function canMerge( $id,
LinkTarget $linkTarget, &$logStatus ) {
693 $latestDest = $revisionLookup->getRevisionByTitle( $linkTarget, 0,
694 IDBAccessObject::READ_LATEST );
695 $latestSource = $revisionLookup->getRevisionByPageId( $id, 0,
696 IDBAccessObject::READ_LATEST );
697 if ( $latestSource->getTimestamp() > $latestDest->getTimestamp() ) {
698 $logStatus =
'cannot merge since source is later';
712 private function mergePage( $row,
Title $newTitle ) {
714 $updateRowsPerQuery = $this->
getConfig()->get( MainConfigNames::UpdateRowsPerQuery );
721 $sourceTitle = Title::makeTitle( $row->page_namespace, $row->page_title );
722 $sourceTitle->resetArticleID( $id );
723 $wikiPage = $this->
getServiceContainer()->getWikiPageFactory()->newFromTitle( $sourceTitle );
724 $wikiPage->loadPageData( IDBAccessObject::READ_LATEST );
728 $revIds = $dbw->newSelectQueryBuilder()
731 ->where( [
'rev_page' => $id ] )
732 ->caller( __METHOD__ )
733 ->fetchFieldValues();
734 $updateBatches = array_chunk( array_map(
'intval', $revIds ), $updateRowsPerQuery );
735 foreach ( $updateBatches as $updateBatch ) {
736 $dbw->newUpdateQueryBuilder()
737 ->update(
'revision' )
738 ->set( [
'rev_page' => $destId ] )
739 ->where( [
'rev_id' => $updateBatch ] )
740 ->caller( __METHOD__ )
742 if ( count( $updateBatches ) > 1 ) {
747 $dbw->newDeleteQueryBuilder()
748 ->deleteFrom(
'page' )
749 ->where( [
'page_id' => $id ] )
750 ->caller( __METHOD__ )
765 DeferredUpdates::doUpdates();