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,
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"
251 $result = MediaWikiServices::getInstance()->getInterwikiLookup()->getAllPrefixes();
253 foreach ( $result as $row ) {
254 $prefixes[] = $row[
'iw_prefix'];
270 $count = $targets->numRows();
271 $this->totalPages += $count;
276 $dryRunNote = $options[
'fix'] ?
'' :
' DRY RUN ONLY';
279 foreach ( $targets as $row ) {
283 $ns, $name, $row->page_namespace, $row->page_title );
286 $logStatus =
'invalid title';
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';
302 $logStatus =
'alternate title is invalid';
303 } elseif ( $newTitle->exists() ) {
305 $logStatus =
'title conflict';
308 $logStatus =
'alternate';
313 $logStatus =
'no conflict';
318 $logTitle =
"id={$row->page_id} ns={$row->page_namespace} dbk={$row->page_title}";
323 $this->
output(
"$logTitle *** $logStatus\n" );
327 $this->
output(
"$logTitle -> " .
328 $newTitle->getPrefixedDBkey() .
" ($logStatus)$dryRunNote\n" );
330 if ( $options[
'fix'] ) {
331 $pageOK = $this->
movePage( $row->page_id, $newTitle );
335 $this->
output(
"$logTitle => " .
336 $newTitle->getPrefixedDBkey() .
" (merge)$dryRunNote\n" );
338 if ( $options[
'fix'] ) {
339 $pageOK = $this->
mergePage( $row, $newTitle );
345 $this->resolvablePages++;
369 $fromField =
"{$fieldPrefix}_from";
370 $namespaceField =
"{$fieldPrefix}_namespace";
371 $titleField =
"{$fieldPrefix}_title";
373 $lbFactory = MediaWikiServices::getInstance()->getDBLoadBalancerFactory();
377 [ $fromField, $namespaceField, $titleField ],
382 $namespaceField => 0,
383 $titleField . $dbw->buildLike(
"$name:", $dbw->anyString() )
388 'ORDER BY' => [ $titleField, $fromField ],
389 'LIMIT' => $batchSize
393 if (
$res->numRows() == 0 ) {
397 $rowsToDeleteIfStillExists = [];
399 foreach (
$res as $row ) {
400 $logTitle =
"from={$row->$fromField} ns={$row->$namespaceField} " .
401 "dbk={$row->$titleField}";
403 $ns, $name, $row->$namespaceField, $row->$titleField );
406 $this->
output(
"$table $logTitle *** INVALID\n" );
409 $this->resolvableLinks++;
410 if ( !$options[
'fix'] ) {
411 $this->
output(
"$table $logTitle -> " .
412 $destTitle->getPrefixedDBkey() .
" DRY RUN\n" );
416 $dbw->update( $table,
419 $namespaceField => $destTitle->getNamespace(),
420 $titleField => $destTitle->getDBkey()
424 $namespaceField => 0,
425 $titleField => $row->$titleField,
426 $fromField => $row->$fromField
432 $rowsToDeleteIfStillExists[] = $dbw->makeList(
434 $fromField => $row->$fromField,
435 $namespaceField => $row->$namespaceField,
436 $titleField => $row->$titleField,
441 $this->
output(
"$table $logTitle -> " .
442 $destTitle->getPrefixedDBkey() .
"\n"
446 if ( $options[
'fix'] && count( $rowsToDeleteIfStillExists ) > 0 ) {
449 $dbw->makeList( $rowsToDeleteIfStillExists, IDatabase::LIST_OR ),
453 $this->deletedLinks += $dbw->affectedRows();
454 $this->resolvableLinks -= $dbw->affectedRows();
457 $encLastTitle = $dbw->addQuotes( $row->$titleField );
458 $encLastFrom = $dbw->addQuotes( $row->$fromField );
461 "$titleField > $encLastTitle " .
462 "OR ($titleField = $encLastTitle AND $fromField > $encLastFrom)"
465 $lbFactory->waitForReplication();
477 $prefix = $options[
'source-pseudo-namespace'];
478 $ns = $options[
'dest-namespace'];
479 $this->
output(
"Checking prefix \"$prefix\" vs namespace $ns\n" );
498 $options[
'move-talk'] &&
499 MediaWikiServices::getInstance()->getNamespaceInfo()->isSubject( $ns )
506 return $dbw->select(
'page',
513 'page_namespace' => $checkNamespaces,
514 'page_title' . $dbw->buildLike(
"$name:", $dbw->anyString() ),
529 $dbk = substr( $sourceDbk, strlen(
"$name:" ) );
532 $dbk =
"$name-" . $dbk;
535 $nsInfo = MediaWikiServices::getInstance()->getNamespaceInfo();
536 if ( $sourceNs ==
NS_TALK && $nsInfo->isSubject( $ns ) ) {
538 $destNS = $nsInfo->getTalk( $destNS );
540 $newTitle = Title::makeTitleSafe( $destNS, $dbk );
541 if ( !$newTitle || !$newTitle->canExist() ) {
556 $prefix = $options[
'add-prefix'];
557 $suffix = $options[
'add-suffix'];
558 if ( $prefix ==
'' && $suffix ==
'' ) {
562 $dbk = $prefix . $linkTarget->
getDBkey() . $suffix;
567 if ( !
$title->exists() ) {
583 $dbw->update(
'page',
586 "page_title" => $newLinkTarget->
getDBkey(),
595 $fromNamespaceTables = [
596 [
'pagelinks',
'pl' ],
597 [
'templatelinks',
'tl' ],
598 [
'imagelinks',
'il' ]
600 foreach ( $fromNamespaceTables as $tableInfo ) {
601 list( $table, $fieldPrefix ) = $tableInfo;
602 $dbw->update( $table,
604 [
"{$fieldPrefix}_from_namespace" => $newLinkTarget->
getNamespace() ],
606 [
"{$fieldPrefix}_from" => $id ],
627 $revisionLookup = MediaWikiServices::getInstance()->getRevisionLookup();
628 $latestDest = $revisionLookup->getRevisionByTitle( $linkTarget, 0,
629 IDBAccessObject::READ_LATEST );
630 $latestSource = $revisionLookup->getRevisionByPageId( $id, 0,
631 IDBAccessObject::READ_LATEST );
632 if ( $latestSource->getTimestamp() > $latestDest->getTimestamp() ) {
633 $logStatus =
'cannot merge since source is later';
655 $sourceTitle = Title::makeTitle( $row->page_namespace, $row->page_title );
656 $sourceTitle->resetArticleID( $id );
657 $wikiPage =
new WikiPage( $sourceTitle );
658 $wikiPage->loadPageData(
'fromdbmaster' );
662 $dbw->update(
'revision',
664 [
'rev_page' => $destId ],
666 [
'rev_page' => $id ],
670 $dbw->delete(
'page', [
'page_id' => $id ], __METHOD__ );
684 DeferredUpdates::doUpdates();
const 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 transcation on a DB.
commitTransaction(IDatabase $dbw, $fname)
Commit the transcation 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.
getInterwikiList()
Get the interwiki list.
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.