55 private $resolvablePages = 0;
61 private $totalPages = 0;
67 private $resolvableLinks = 0;
73 private $totalLinks = 0;
80 private $deletedLinks = 0;
83 parent::__construct();
84 $this->
addDescription(
'Find and fix pages affected by namespace addition/removal' );
85 $this->
addOption(
'fix',
'Attempt to automatically fix errors and delete broken links' );
86 $this->
addOption(
'merge',
"Instead of renaming conflicts, do a history merge with " .
87 "the correct title" );
88 $this->
addOption(
'add-suffix',
"Dupes will be renamed with correct namespace with " .
89 "<text> appended after the article name",
false,
true );
90 $this->
addOption(
'add-prefix',
"Dupes will be renamed with correct namespace with " .
91 "<text> prepended before the article name",
false,
true );
92 $this->
addOption(
'source-pseudo-namespace',
"Move all pages with the given source " .
93 "prefix (with an implied colon following it). If --dest-namespace is not specified, " .
94 "the colon will be replaced with a hyphen.",
96 $this->
addOption(
'dest-namespace',
"In combination with --source-pseudo-namespace, " .
97 "specify the namespace ID of the destination.",
false,
true );
98 $this->
addOption(
'move-talk',
"If this is specified, pages in the Talk namespace that " .
99 "begin with a conflicting prefix will be renamed, for example " .
100 "Talk:File:Foo -> File_Talk:Foo" );
107 'add-suffix' => $this->
getOption(
'add-suffix',
'' ),
108 'add-prefix' => $this->
getOption(
'add-prefix',
'' ),
109 'move-talk' => $this->
hasOption(
'move-talk' ),
110 'source-pseudo-namespace' => $this->
getOption(
'source-pseudo-namespace',
'' ),
111 'dest-namespace' => intval( $this->
getOption(
'dest-namespace', 0 ) )
114 if ( $options[
'source-pseudo-namespace'] !==
'' ) {
115 $retval = $this->checkPrefix( $options );
117 $retval = $this->checkAll( $options );
121 $this->
output(
"\nLooks good!\n" );
123 $this->
output(
"\nOh noeees\n" );
134 private function checkAll( $options ) {
135 $contLang = MediaWikiServices::getInstance()->getContentLanguage();
140 foreach ( $this->getInterwikiList() as $prefix ) {
141 $name = $contLang->ucfirst( $prefix );
147 MediaWikiServices::getInstance()->getNamespaceInfo()->getCanonicalNamespaces()
151 if ( $name !==
'' ) {
152 $spaces[$name] = $ns;
155 foreach ( $contLang->getNamespaces() as $ns => $name ) {
156 if ( $name !==
'' ) {
157 $spaces[$name] = $ns;
160 foreach ( $contLang->getNamespaceAliases() as $name => $ns ) {
161 $spaces[$name] = $ns;
166 $capitalLinks = $this->
getConfig()->get( MainConfigNames::CapitalLinks );
167 foreach ( $spaces as $name => $ns ) {
169 $moreNames[] = $contLang->uc( $name );
170 $moreNames[] = $contLang->ucfirst( $contLang->lc( $name ) );
171 $moreNames[] = $contLang->ucwords( $name );
172 $moreNames[] = $contLang->ucwords( $contLang->lc( $name ) );
173 $moreNames[] = $contLang->ucwordbreaks( $name );
174 $moreNames[] = $contLang->ucwordbreaks( $contLang->lc( $name ) );
175 if ( !$capitalLinks ) {
176 foreach ( $moreNames as $altName ) {
177 $moreNames[] = $contLang->lcfirst( $altName );
179 $moreNames[] = $contLang->lcfirst( $name );
181 foreach ( array_unique( $moreNames ) as $altName ) {
182 if ( $altName !== $name ) {
183 $spaces[$altName] = $ns;
190 $origSpaces = $spaces;
191 uksort( $spaces,
static function ( $a, $b ) use ( $origSpaces ) {
192 return $origSpaces[$a] <=> $origSpaces[$b]
197 foreach ( $spaces as $name => $ns ) {
198 $ok = $this->checkNamespace( $ns, $name, $options ) && $ok;
202 "{$this->totalPages} pages to fix, " .
203 "{$this->resolvablePages} were resolvable.\n\n"
206 foreach ( $spaces as $name => $ns ) {
225 $this->checkLinkTable(
'pagelinks',
'pl', $ns, $name, $options );
226 $this->checkLinkTable(
'templatelinks',
'tl', $ns, $name, $options );
232 $this->checkLinkTable(
'redirect',
'rd', $ns, $name, $options,
233 [
'rd_interwiki' =>
null ] );
234 $this->checkLinkTable(
'redirect',
'rd', $ns, $name, $options,
235 [
'rd_interwiki' =>
'' ] );
240 "{$this->totalLinks} links to fix, " .
241 "{$this->resolvableLinks} were resolvable, " .
242 "{$this->deletedLinks} were deleted.\n"
251 private function getInterwikiList() {
252 $result = MediaWikiServices::getInstance()->getInterwikiLookup()->getAllPrefixes();
253 return array_column( $result,
'iw_prefix' );
264 private function checkNamespace( $ns, $name, $options ) {
265 $targets = $this->getTargetList( $ns, $name, $options );
266 $count = $targets->numRows();
267 $this->totalPages += $count;
272 $dryRunNote = $options[
'fix'] ?
'' :
' DRY RUN ONLY';
275 foreach ( $targets as $row ) {
278 $newTitle = $this->getDestinationTitle(
279 $ns, $name, $row->page_namespace, $row->page_title );
282 if ( $options[
'add-prefix'] ==
'' && $options[
'add-suffix'] ==
'' ) {
283 $logStatus =
'invalid title and --add-prefix not specified';
286 $action =
'alternate';
288 } elseif ( $newTitle->
exists() ) {
289 if ( $options[
'merge'] ) {
290 if ( $this->canMerge( $row->page_id, $newTitle, $logStatus ) ) {
295 } elseif ( $options[
'add-prefix'] ==
'' && $options[
'add-suffix'] ==
'' ) {
297 $logStatus =
'dest title exists and --add-prefix not specified';
299 $action =
'alternate';
303 $logStatus =
'no conflict';
305 if ( $action ===
'alternate' ) {
306 [ $ns, $dbk ] = $this->getDestination( $ns, $name, $row->page_namespace,
308 $newTitle = $this->getAlternateTitle( $ns, $dbk, $options );
311 $logStatus =
'alternate title is invalid';
312 } elseif ( $newTitle->
exists() ) {
314 $logStatus =
'alternate title conflicts';
317 $logStatus =
'alternate';
323 $logTitle =
"id={$row->page_id} ns={$row->page_namespace} dbk={$row->page_title}";
328 $this->
output(
"$logTitle *** $logStatus\n" );
332 $this->
output(
"$logTitle -> " .
335 if ( $options[
'fix'] ) {
336 $pageOK = $this->movePage( $row->page_id, $newTitle );
340 $this->
output(
"$logTitle => " .
343 if ( $options[
'fix'] ) {
344 $pageOK = $this->mergePage( $row, $newTitle );
350 $this->resolvablePages++;
368 private function checkLinkTable( $table, $fieldPrefix, $ns, $name, $options,
374 $fromField =
"{$fieldPrefix}_from";
376 $linksMigration = MediaWikiServices::getInstance()->getLinksMigration();
377 if ( isset( $linksMigration::$mapping[$table] ) ) {
378 $queryInfo = $linksMigration->getQueryInfo( $table );
379 [ $namespaceField, $titleField ] = $linksMigration->getTitleFields( $table );
382 'tables' => [ $table ],
384 "{$fieldPrefix}_namespace",
385 "{$fieldPrefix}_title"
389 $namespaceField =
"{$fieldPrefix}_namespace";
390 $titleField =
"{$fieldPrefix}_title";
395 $queryInfo[
'tables'],
396 array_merge( [ $fromField ], $queryInfo[
'fields'] ),
401 $namespaceField => 0,
402 $titleField . $dbw->buildLike(
"$name:", $dbw->anyString() )
407 'ORDER BY' => [ $titleField, $fromField ],
408 'LIMIT' => $batchSize
413 if (
$res->numRows() == 0 ) {
417 $rowsToDeleteIfStillExists = [];
419 foreach (
$res as $row ) {
420 $logTitle =
"from={$row->$fromField} ns={$row->$namespaceField} " .
421 "dbk={$row->$titleField}";
422 $destTitle = $this->getDestinationTitle(
423 $ns, $name, $row->$namespaceField, $row->$titleField );
426 $this->
output(
"$table $logTitle *** INVALID\n" );
429 $this->resolvableLinks++;
430 if ( !$options[
'fix'] ) {
431 $this->
output(
"$table $logTitle -> " .
432 $destTitle->getPrefixedDBkey() .
" DRY RUN\n" );
436 if ( isset( $linksMigration::$mapping[$table] ) ) {
437 $setValue = $linksMigration->getLinksConditions( $table, $destTitle );
438 $whereCondition = $linksMigration->getLinksConditions(
442 $deleteCondition = $linksMigration->getLinksConditions(
444 new TitleValue( (
int)$row->$namespaceField, $row->$titleField )
448 $namespaceField => $destTitle->getNamespace(),
449 $titleField => $destTitle->getDBkey()
452 $namespaceField => 0,
453 $titleField => $row->$titleField
456 $namespaceField => $row->$namespaceField,
457 $titleField => $row->$titleField,
461 $dbw->update( $table,
465 array_merge( [ $fromField => $row->$fromField ], $whereCondition ),
470 $rowsToDeleteIfStillExists[] = $dbw->makeList(
471 array_merge( [ $fromField => $row->$fromField ], $deleteCondition ),
475 $this->
output(
"$table $logTitle -> " .
476 $destTitle->getPrefixedDBkey() .
"\n"
480 if ( $options[
'fix'] && count( $rowsToDeleteIfStillExists ) > 0 ) {
483 $dbw->makeList( $rowsToDeleteIfStillExists, IDatabase::LIST_OR ),
487 $this->deletedLinks += $dbw->affectedRows();
488 $this->resolvableLinks -= $dbw->affectedRows();
492 $dbw->buildComparison(
'>', [
494 $titleField => $row->$titleField,
496 $fromField => $row->$fromField,
511 private function checkPrefix( $options ) {
512 $prefix = $options[
'source-pseudo-namespace'];
513 $ns = $options[
'dest-namespace'];
514 $this->
output(
"Checking prefix \"$prefix\" vs namespace $ns\n" );
516 return $this->checkNamespace( $ns, $prefix, $options );
529 private function getTargetList( $ns, $name, $options ) {
533 $options[
'move-talk'] &&
534 MediaWikiServices::getInstance()->getNamespaceInfo()->isSubject( $ns )
541 return $dbw->select(
'page',
548 'page_namespace' => $checkNamespaces,
549 'page_title' . $dbw->buildLike(
"$name:", $dbw->anyString() ),
563 private function getDestination( $ns, $name, $sourceNs, $sourceDbk ) {
564 $dbk = substr( $sourceDbk, strlen(
"$name:" ) );
567 $dbk =
"$name-" . $dbk;
570 $nsInfo = MediaWikiServices::getInstance()->getNamespaceInfo();
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->update(
'page',
627 "page_title" => $newLinkTarget->
getDBkey(),
636 $fromNamespaceTables = [
637 [
'pagelinks',
'pl' ],
638 [
'templatelinks',
'tl' ],
639 [
'imagelinks',
'il' ]
641 foreach ( $fromNamespaceTables as [ $table, $fieldPrefix ] ) {
642 $dbw->update( $table,
644 [
"{$fieldPrefix}_from_namespace" => $newLinkTarget->
getNamespace() ],
646 [
"{$fieldPrefix}_from" => $id ],
666 private function canMerge( $id,
LinkTarget $linkTarget, &$logStatus ) {
667 $revisionLookup = MediaWikiServices::getInstance()->getRevisionLookup();
668 $latestDest = $revisionLookup->getRevisionByTitle( $linkTarget, 0,
669 IDBAccessObject::READ_LATEST );
670 $latestSource = $revisionLookup->getRevisionByPageId( $id, 0,
671 IDBAccessObject::READ_LATEST );
672 if ( $latestSource->getTimestamp() > $latestDest->getTimestamp() ) {
673 $logStatus =
'cannot merge since source is later';
687 private function mergePage( $row,
Title $newTitle ) {
695 $sourceTitle = Title::makeTitle( $row->page_namespace, $row->page_title );
696 $sourceTitle->resetArticleID( $id );
697 $wikiPage = MediaWikiServices::getInstance()->getWikiPageFactory()->newFromTitle( $sourceTitle );
698 $wikiPage->loadPageData( WikiPage::READ_LATEST );
702 $dbw->update(
'revision',
704 [
'rev_page' => $destId ],
706 [
'rev_page' => $id ],
710 $dbw->delete(
'page', [
'page_id' => $id ], __METHOD__ );