54 private $resolvablePages = 0;
60 private $totalPages = 0;
66 private $resolvableLinks = 0;
72 private $totalLinks = 0;
79 private $deletedLinks = 0;
82 parent::__construct();
83 $this->
addDescription(
'Find and fix pages affected by namespace addition/removal' );
84 $this->
addOption(
'fix',
'Attempt to automatically fix errors and delete broken links' );
85 $this->
addOption(
'merge',
"Instead of renaming conflicts, do a history merge with " .
86 "the correct title" );
87 $this->
addOption(
'add-suffix',
"Dupes will be renamed with correct namespace with " .
88 "<text> appended after the article name",
false,
true );
89 $this->
addOption(
'add-prefix',
"Dupes will be renamed with correct namespace with " .
90 "<text> prepended before the article name",
false,
true );
91 $this->
addOption(
'source-pseudo-namespace',
"Move all pages with the given source " .
92 "prefix (with an implied colon following it). If --dest-namespace is not specified, " .
93 "the colon will be replaced with a hyphen.",
95 $this->
addOption(
'dest-namespace',
"In combination with --source-pseudo-namespace, " .
96 "specify the namespace ID of the destination.",
false,
true );
97 $this->
addOption(
'move-talk',
"If this is specified, pages in the Talk namespace that " .
98 "begin with a conflicting prefix will be renamed, for example " .
99 "Talk:File:Foo -> File_Talk:Foo" );
106 'add-suffix' => $this->
getOption(
'add-suffix',
'' ),
107 'add-prefix' => $this->
getOption(
'add-prefix',
'' ),
108 'move-talk' => $this->
hasOption(
'move-talk' ),
109 'source-pseudo-namespace' => $this->
getOption(
'source-pseudo-namespace',
'' ),
110 'dest-namespace' => intval( $this->
getOption(
'dest-namespace', 0 ) )
113 if ( $options[
'source-pseudo-namespace'] !==
'' ) {
114 $retval = $this->checkPrefix( $options );
116 $retval = $this->checkAll( $options );
120 $this->
output(
"\nLooks good!\n" );
122 $this->
output(
"\nOh noeees\n" );
133 private function checkAll( $options ) {
134 $contLang = MediaWikiServices::getInstance()->getContentLanguage();
139 foreach ( $this->getInterwikiList() as $prefix ) {
140 $name = $contLang->ucfirst( $prefix );
146 MediaWikiServices::getInstance()->getNamespaceInfo()->getCanonicalNamespaces()
150 if ( $name !==
'' ) {
151 $spaces[$name] = $ns;
154 foreach ( $contLang->getNamespaces() as $ns => $name ) {
155 if ( $name !==
'' ) {
156 $spaces[$name] = $ns;
159 foreach ( $contLang->getNamespaceAliases() as $name => $ns ) {
160 $spaces[$name] = $ns;
165 $capitalLinks = $this->
getConfig()->get( MainConfigNames::CapitalLinks );
166 foreach ( $spaces as $name => $ns ) {
168 $moreNames[] = $contLang->uc( $name );
169 $moreNames[] = $contLang->ucfirst( $contLang->lc( $name ) );
170 $moreNames[] = $contLang->ucwords( $name );
171 $moreNames[] = $contLang->ucwords( $contLang->lc( $name ) );
172 $moreNames[] = $contLang->ucwordbreaks( $name );
173 $moreNames[] = $contLang->ucwordbreaks( $contLang->lc( $name ) );
174 if ( !$capitalLinks ) {
175 foreach ( $moreNames as $altName ) {
176 $moreNames[] = $contLang->lcfirst( $altName );
178 $moreNames[] = $contLang->lcfirst( $name );
180 foreach ( array_unique( $moreNames ) as $altName ) {
181 if ( $altName !== $name ) {
182 $spaces[$altName] = $ns;
189 $origSpaces = $spaces;
190 uksort( $spaces,
static function ( $a, $b ) use ( $origSpaces ) {
191 return $origSpaces[$a] <=> $origSpaces[$b]
196 foreach ( $spaces as $name => $ns ) {
197 $ok = $this->checkNamespace( $ns, $name, $options ) && $ok;
201 "{$this->totalPages} pages to fix, " .
202 "{$this->resolvablePages} were resolvable.\n\n"
205 foreach ( $spaces as $name => $ns ) {
224 $this->checkLinkTable(
'pagelinks',
'pl', $ns, $name, $options );
225 $this->checkLinkTable(
'templatelinks',
'tl', $ns, $name, $options );
231 $this->checkLinkTable(
'redirect',
'rd', $ns, $name, $options,
232 [
'rd_interwiki' =>
null ] );
233 $this->checkLinkTable(
'redirect',
'rd', $ns, $name, $options,
234 [
'rd_interwiki' =>
'' ] );
239 "{$this->totalLinks} links to fix, " .
240 "{$this->resolvableLinks} were resolvable, " .
241 "{$this->deletedLinks} were deleted.\n"
250 private function getInterwikiList() {
251 $result = MediaWikiServices::getInstance()->getInterwikiLookup()->getAllPrefixes();
252 return array_column( $result,
'iw_prefix' );
263 private function checkNamespace( $ns, $name, $options ) {
264 $targets = $this->getTargetList( $ns, $name, $options );
265 $count = $targets->numRows();
266 $this->totalPages += $count;
271 $dryRunNote = $options[
'fix'] ?
'' :
' DRY RUN ONLY';
274 foreach ( $targets as $row ) {
277 $newTitle = $this->getDestinationTitle(
278 $ns, $name, $row->page_namespace, $row->page_title );
281 if ( $options[
'add-prefix'] ==
'' && $options[
'add-suffix'] ==
'' ) {
282 $logStatus =
'invalid title and --add-prefix not specified';
285 $action =
'alternate';
287 } elseif ( $newTitle->
exists() ) {
288 if ( $options[
'merge'] ) {
289 if ( $this->canMerge( $row->page_id, $newTitle, $logStatus ) ) {
294 } elseif ( $options[
'add-prefix'] ==
'' && $options[
'add-suffix'] ==
'' ) {
296 $logStatus =
'dest title exists and --add-prefix not specified';
298 $action =
'alternate';
302 $logStatus =
'no conflict';
304 if ( $action ===
'alternate' ) {
305 [ $ns, $dbk ] = $this->getDestination( $ns, $name, $row->page_namespace,
307 $newTitle = $this->getAlternateTitle( $ns, $dbk, $options );
310 $logStatus =
'alternate title is invalid';
311 } elseif ( $newTitle->
exists() ) {
313 $logStatus =
'alternate title conflicts';
316 $logStatus =
'alternate';
322 $logTitle =
"id={$row->page_id} ns={$row->page_namespace} dbk={$row->page_title}";
327 $this->
output(
"$logTitle *** $logStatus\n" );
331 $this->
output(
"$logTitle -> " .
334 if ( $options[
'fix'] ) {
335 $pageOK = $this->movePage( $row->page_id, $newTitle );
339 $this->
output(
"$logTitle => " .
342 if ( $options[
'fix'] ) {
343 $pageOK = $this->mergePage( $row, $newTitle );
349 $this->resolvablePages++;
367 private function checkLinkTable( $table, $fieldPrefix, $ns, $name, $options,
373 $fromField =
"{$fieldPrefix}_from";
375 $lbFactory = MediaWikiServices::getInstance()->getDBLoadBalancerFactory();
376 $linksMigration = MediaWikiServices::getInstance()->getLinksMigration();
377 if ( isset( $linksMigration::$mapping[$table] ) ) {
378 $queryInfo = $linksMigration->getQueryInfo( $table );
379 list( $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 $encLastTitle = $dbw->addQuotes( $row->$titleField );
494 $encLastFrom = $dbw->addQuotes( $row->$fromField );
497 "$titleField > $encLastTitle " .
498 "OR ($titleField = $encLastTitle AND $fromField > $encLastFrom)"
501 $lbFactory->waitForReplication();
512 private function checkPrefix( $options ) {
513 $prefix = $options[
'source-pseudo-namespace'];
514 $ns = $options[
'dest-namespace'];
515 $this->
output(
"Checking prefix \"$prefix\" vs namespace $ns\n" );
517 return $this->checkNamespace( $ns, $prefix, $options );
530 private function getTargetList( $ns, $name, $options ) {
534 $options[
'move-talk'] &&
535 MediaWikiServices::getInstance()->getNamespaceInfo()->isSubject( $ns )
542 return $dbw->select(
'page',
549 'page_namespace' => $checkNamespaces,
550 'page_title' . $dbw->buildLike(
"$name:", $dbw->anyString() ),
564 private function getDestination( $ns, $name, $sourceNs, $sourceDbk ) {
565 $dbk = substr( $sourceDbk, strlen(
"$name:" ) );
568 $dbk =
"$name-" . $dbk;
571 $nsInfo = MediaWikiServices::getInstance()->getNamespaceInfo();
572 if ( $sourceNs ==
NS_TALK && $nsInfo->isSubject( $ns ) ) {
574 $destNS = $nsInfo->getTalk( $destNS );
576 return [ $destNS, $dbk ];
587 private function getDestinationTitle( $ns, $name, $sourceNs, $sourceDbk ) {
588 [ $destNS, $dbk ] = $this->getDestination( $ns, $name, $sourceNs, $sourceDbk );
590 if ( !$newTitle || !$newTitle->
canExist() ) {
605 private function getAlternateTitle( $ns, $dbk, $options ) {
606 $prefix = $options[
'add-prefix'];
607 $suffix = $options[
'add-suffix'];
608 if ( $prefix ==
'' && $suffix ==
'' ) {
611 $newDbk = $prefix . $dbk . $suffix;
622 private function movePage( $id,
LinkTarget $newLinkTarget ) {
625 $dbw->update(
'page',
628 "page_title" => $newLinkTarget->
getDBkey(),
637 $fromNamespaceTables = [
638 [
'pagelinks',
'pl' ],
639 [
'templatelinks',
'tl' ],
640 [
'imagelinks',
'il' ]
642 foreach ( $fromNamespaceTables as [ $table, $fieldPrefix ] ) {
643 $dbw->update( $table,
645 [
"{$fieldPrefix}_from_namespace" => $newLinkTarget->
getNamespace() ],
647 [
"{$fieldPrefix}_from" => $id ],
667 private function canMerge( $id,
LinkTarget $linkTarget, &$logStatus ) {
668 $revisionLookup = MediaWikiServices::getInstance()->getRevisionLookup();
669 $latestDest = $revisionLookup->getRevisionByTitle( $linkTarget, 0,
670 IDBAccessObject::READ_LATEST );
671 $latestSource = $revisionLookup->getRevisionByPageId( $id, 0,
672 IDBAccessObject::READ_LATEST );
673 if ( $latestSource->getTimestamp() > $latestDest->getTimestamp() ) {
674 $logStatus =
'cannot merge since source is later';
688 private function mergePage( $row,
Title $newTitle ) {
697 $sourceTitle->resetArticleID( $id );
698 $wikiPage = MediaWikiServices::getInstance()->getWikiPageFactory()->newFromTitle( $sourceTitle );
699 $wikiPage->loadPageData( WikiPage::READ_LATEST );
703 $dbw->update(
'revision',
705 [
'rev_page' => $destId ],
707 [
'rev_page' => $id ],
711 $dbw->delete(
'page', [
'page_id' => $id ], __METHOD__ );
addOption( $name, $description, $required=false, $withArg=false, $shortName=false, $multiOccurrence=false)
Add a parameter to the script.