27require_once __DIR__ .
'/Maintenance.php';
80 parent::__construct();
81 $this->
addDescription(
'Find and fix pages affected by namespace addition/removal' );
82 $this->
addOption(
'fix',
'Attempt to automatically fix errors and delete broken links' );
83 $this->
addOption(
'merge',
"Instead of renaming conflicts, do a history merge with " .
84 "the correct title" );
85 $this->
addOption(
'add-suffix',
"Dupes will be renamed with correct namespace with " .
86 "<text> appended after the article name",
false,
true );
87 $this->
addOption(
'add-prefix',
"Dupes will be renamed with correct namespace with " .
88 "<text> prepended before the article name",
false,
true );
89 $this->
addOption(
'source-pseudo-namespace',
"Move all pages with the given source " .
90 "prefix (with an implied colon following it). If --dest-namespace is not specified, " .
91 "the colon will be replaced with a hyphen.",
93 $this->
addOption(
'dest-namespace',
"In combination with --source-pseudo-namespace, " .
94 "specify the namespace ID of the destination.",
false,
true );
95 $this->
addOption(
'move-talk',
"If this is specified, pages in the Talk namespace that " .
96 "begin with a conflicting prefix will be renamed, for example " .
97 "Talk:File:Foo -> File_Talk:Foo" );
104 'add-suffix' => $this->
getOption(
'add-suffix',
'' ),
105 'add-prefix' => $this->
getOption(
'add-prefix',
'' ),
106 'move-talk' => $this->
hasOption(
'move-talk' ),
107 'source-pseudo-namespace' => $this->
getOption(
'source-pseudo-namespace',
'' ),
108 'dest-namespace' => intval( $this->
getOption(
'dest-namespace', 0 ) )
111 if ( $options[
'source-pseudo-namespace'] !==
'' ) {
114 $retval = $this->
checkAll( $options );
118 $this->
output(
"\nLooks good!\n" );
120 $this->
output(
"\nOh noeees\n" );
132 $contLang = MediaWikiServices::getInstance()->getContentLanguage();
138 $name = $contLang->ucfirst( $prefix );
144 MediaWikiServices::getInstance()->getNamespaceInfo()->getCanonicalNamespaces()
148 if ( $name !==
'' ) {
149 $spaces[$name] = $ns;
152 foreach ( $contLang->getNamespaces() as $ns => $name ) {
153 if ( $name !==
'' ) {
154 $spaces[$name] = $ns;
157 foreach ( $contLang->getNamespaceAliases() as $name => $ns ) {
158 $spaces[$name] = $ns;
163 $capitalLinks = $this->
getConfig()->get(
'CapitalLinks' );
164 foreach ( $spaces as $name => $ns ) {
166 $moreNames[] = $contLang->uc( $name );
167 $moreNames[] = $contLang->ucfirst( $contLang->lc( $name ) );
168 $moreNames[] = $contLang->ucwords( $name );
169 $moreNames[] = $contLang->ucwords( $contLang->lc( $name ) );
170 $moreNames[] = $contLang->ucwordbreaks( $name );
171 $moreNames[] = $contLang->ucwordbreaks( $contLang->lc( $name ) );
172 if ( !$capitalLinks ) {
173 foreach ( $moreNames as $altName ) {
174 $moreNames[] = $contLang->lcfirst( $altName );
176 $moreNames[] = $contLang->lcfirst( $name );
178 foreach ( array_unique( $moreNames ) as $altName ) {
179 if ( $altName !== $name ) {
180 $spaces[$altName] = $ns;
187 $origSpaces = $spaces;
188 uksort( $spaces,
static function ( $a, $b ) use ( $origSpaces ) {
189 return $origSpaces[$a] <=> $origSpaces[$b]
194 foreach ( $spaces as $name => $ns ) {
199 "{$this->totalPages} pages to fix, " .
200 "{$this->resolvablePages} were resolvable.\n\n"
203 foreach ( $spaces as $name => $ns ) {
223 $this->
checkLinkTable(
'templatelinks',
'tl', $ns, $name, $options );
230 [
'rd_interwiki' =>
null ] );
232 [
'rd_interwiki' =>
'' ] );
237 "{$this->totalLinks} links to fix, " .
238 "{$this->resolvableLinks} were resolvable, " .
239 "{$this->deletedLinks} were deleted.\n"
249 $result = MediaWikiServices::getInstance()->getInterwikiLookup()->getAllPrefixes();
250 return array_column( $result,
'iw_prefix' );
263 $count = $targets->numRows();
264 $this->totalPages += $count;
269 $dryRunNote = $options[
'fix'] ?
'' :
' DRY RUN ONLY';
272 foreach ( $targets as $row ) {
276 $ns, $name, $row->page_namespace, $row->page_title );
279 $logStatus =
'invalid title';
281 } elseif ( $newTitle->exists() ) {
282 if ( $options[
'merge'] ) {
283 if ( $this->
canMerge( $row->page_id, $newTitle, $logStatus ) ) {
288 } elseif ( $options[
'add-prefix'] ==
'' && $options[
'add-suffix'] ==
'' ) {
290 $logStatus =
'dest title exists and --add-prefix not specified';
295 $logStatus =
'alternate title is invalid';
296 } elseif ( $newTitle->exists() ) {
298 $logStatus =
'title conflict';
301 $logStatus =
'alternate';
306 $logStatus =
'no conflict';
311 $logTitle =
"id={$row->page_id} ns={$row->page_namespace} dbk={$row->page_title}";
316 $this->
output(
"$logTitle *** $logStatus\n" );
320 $this->
output(
"$logTitle -> " .
321 $newTitle->getPrefixedDBkey() .
" ($logStatus)$dryRunNote\n" );
323 if ( $options[
'fix'] ) {
324 $pageOK = $this->
movePage( $row->page_id, $newTitle );
328 $this->
output(
"$logTitle => " .
329 $newTitle->getPrefixedDBkey() .
" (merge)$dryRunNote\n" );
331 if ( $options[
'fix'] ) {
332 $pageOK = $this->
mergePage( $row, $newTitle );
338 $this->resolvablePages++;
362 $fromField =
"{$fieldPrefix}_from";
363 $namespaceField =
"{$fieldPrefix}_namespace";
364 $titleField =
"{$fieldPrefix}_title";
366 $lbFactory = MediaWikiServices::getInstance()->getDBLoadBalancerFactory();
370 [ $fromField, $namespaceField, $titleField ],
375 $namespaceField => 0,
376 $titleField . $dbw->buildLike(
"$name:", $dbw->anyString() )
381 'ORDER BY' => [ $titleField, $fromField ],
382 'LIMIT' => $batchSize
386 if (
$res->numRows() == 0 ) {
390 $rowsToDeleteIfStillExists = [];
392 foreach (
$res as $row ) {
393 $logTitle =
"from={$row->$fromField} ns={$row->$namespaceField} " .
394 "dbk={$row->$titleField}";
396 $ns, $name, $row->$namespaceField, $row->$titleField );
399 $this->
output(
"$table $logTitle *** INVALID\n" );
402 $this->resolvableLinks++;
403 if ( !$options[
'fix'] ) {
404 $this->
output(
"$table $logTitle -> " .
405 $destTitle->getPrefixedDBkey() .
" DRY RUN\n" );
409 $dbw->update( $table,
412 $namespaceField => $destTitle->getNamespace(),
413 $titleField => $destTitle->getDBkey()
417 $namespaceField => 0,
418 $titleField => $row->$titleField,
419 $fromField => $row->$fromField
425 $rowsToDeleteIfStillExists[] = $dbw->makeList(
427 $fromField => $row->$fromField,
428 $namespaceField => $row->$namespaceField,
429 $titleField => $row->$titleField,
434 $this->
output(
"$table $logTitle -> " .
435 $destTitle->getPrefixedDBkey() .
"\n"
439 if ( $options[
'fix'] && count( $rowsToDeleteIfStillExists ) > 0 ) {
442 $dbw->makeList( $rowsToDeleteIfStillExists, IDatabase::LIST_OR ),
446 $this->deletedLinks += $dbw->affectedRows();
447 $this->resolvableLinks -= $dbw->affectedRows();
450 $encLastTitle = $dbw->addQuotes( $row->$titleField );
451 $encLastFrom = $dbw->addQuotes( $row->$fromField );
454 "$titleField > $encLastTitle " .
455 "OR ($titleField = $encLastTitle AND $fromField > $encLastFrom)"
458 $lbFactory->waitForReplication();
470 $prefix = $options[
'source-pseudo-namespace'];
471 $ns = $options[
'dest-namespace'];
472 $this->
output(
"Checking prefix \"$prefix\" vs namespace $ns\n" );
491 $options[
'move-talk'] &&
492 MediaWikiServices::getInstance()->getNamespaceInfo()->isSubject( $ns )
499 return $dbw->select(
'page',
506 'page_namespace' => $checkNamespaces,
507 'page_title' . $dbw->buildLike(
"$name:", $dbw->anyString() ),
522 $dbk = substr( $sourceDbk, strlen(
"$name:" ) );
525 $dbk =
"$name-" . $dbk;
528 $nsInfo = MediaWikiServices::getInstance()->getNamespaceInfo();
529 if ( $sourceNs ==
NS_TALK && $nsInfo->isSubject( $ns ) ) {
531 $destNS = $nsInfo->getTalk( $destNS );
533 $newTitle = Title::makeTitleSafe( $destNS, $dbk );
534 if ( !$newTitle || !$newTitle->canExist() ) {
549 $prefix = $options[
'add-prefix'];
550 $suffix = $options[
'add-suffix'];
551 if ( $prefix ==
'' && $suffix ==
'' ) {
555 $dbk = $prefix . $linkTarget->
getDBkey() . $suffix;
560 if ( !
$title->exists() ) {
576 $dbw->update(
'page',
579 "page_title" => $newLinkTarget->
getDBkey(),
588 $fromNamespaceTables = [
589 [
'pagelinks',
'pl' ],
590 [
'templatelinks',
'tl' ],
591 [
'imagelinks',
'il' ]
593 foreach ( $fromNamespaceTables as [ $table, $fieldPrefix ] ) {
594 $dbw->update( $table,
596 [
"{$fieldPrefix}_from_namespace" => $newLinkTarget->
getNamespace() ],
598 [
"{$fieldPrefix}_from" => $id ],
619 $revisionLookup = MediaWikiServices::getInstance()->getRevisionLookup();
620 $latestDest = $revisionLookup->getRevisionByTitle( $linkTarget, 0,
621 IDBAccessObject::READ_LATEST );
622 $latestSource = $revisionLookup->getRevisionByPageId( $id, 0,
623 IDBAccessObject::READ_LATEST );
624 if ( $latestSource->getTimestamp() > $latestDest->getTimestamp() ) {
625 $logStatus =
'cannot merge since source is later';
647 $sourceTitle = Title::makeTitle( $row->page_namespace, $row->page_title );
648 $sourceTitle->resetArticleID( $id );
649 $wikiPage =
new WikiPage( $sourceTitle );
650 $wikiPage->loadPageData(
'fromdbmaster' );
654 $dbw->update(
'revision',
656 [
'rev_page' => $destId ],
658 [
'rev_page' => $id ],
662 $dbw->delete(
'page', [
'page_id' => $id ], __METHOD__ );
676 DeferredUpdates::doUpdates();
683require_once RUN_MAINTENANCE_IF_MAIN;
Update object handling the cleanup of links tables after a page was deleted.
Abstract maintenance class for quickly writing and churning out maintenance scripts with minimal effo...
beginTransaction(IDatabase $dbw, $fname)
Begin a transaction on a DB.
commitTransaction(IDatabase $dbw, $fname)
Commit the transaction on a DB handle and wait for replica DBs to catch up.
output( $out, $channel=null)
Throw some output to the user.
hasOption( $name)
Checks to see if a particular option was set.
addDescription( $text)
Set the description text.
addOption( $name, $description, $required=false, $withArg=false, $shortName=false, $multiOccurrence=false)
Add a parameter to the script.
getOption( $name, $default=null)
Get an option, or return the default.
Maintenance script that checks for articles to fix after adding/deleting namespaces.
getAlternateTitle(LinkTarget $linkTarget, $options)
Get an alternative title to move a page to.
checkPrefix( $options)
Move the given pseudo-namespace, either replacing the colon with a hyphen (useful for pseudo-namespac...
checkLinkTable( $table, $fieldPrefix, $ns, $name, $options, $extraConds=[])
Check and repair the destination fields in a link table.
mergePage( $row, Title $newTitle)
Merge page histories.
canMerge( $id, LinkTarget $linkTarget, &$logStatus)
Determine if we can merge a page.
execute()
Do the actual work.
movePage( $id, LinkTarget $newLinkTarget)
Move a page.
__construct()
Default constructor.
int $resolvableLinks
Total number of links that need fixing that are automatically resolveable.
int $totalPages
Total number of pages that need fixing.
checkNamespace( $ns, $name, $options)
Check a given prefix and try to move it into the given destination namespace.
IMaintainableDatabase $db
checkAll( $options)
Check all namespaces.
getDestinationTitle( $ns, $name, $sourceNs, $sourceDbk)
Get the preferred destination title for a given target page.
getTargetList( $ns, $name, $options)
Find pages in main and talk namespaces that have a prefix of the new namespace so we know titles that...
int $resolvablePages
Total number of pages that need fixing that are automatically resolveable.
int $totalLinks
Total number of erroneous links.
int $deletedLinks
Total number of links deleted because they weren't automatically resolveable due to the target alread...
Represents a title within MediaWiki.
getArticleID( $flags=0)
Get the article ID for this Title from the link cache, adding it if necessary.
Class representing a MediaWiki article and history.
Advanced database interface for IDatabase handles that include maintenance methods.