27 require_once __DIR__ .
'/Maintenance.php';
54 parent::__construct();
55 $this->
addDescription(
'Find and fix pages affected by namespace addition/removal' );
56 $this->
addOption(
'fix',
'Attempt to automatically fix errors' );
57 $this->
addOption(
'merge',
"Instead of renaming conflicts, do a history merge with " .
58 "the correct title" );
59 $this->
addOption(
'add-suffix',
"Dupes will be renamed with correct namespace with " .
60 "<text> appended after the article name",
false,
true );
61 $this->
addOption(
'add-prefix',
"Dupes will be renamed with correct namespace with " .
62 "<text> prepended before the article name",
false,
true );
63 $this->
addOption(
'source-pseudo-namespace',
"Move all pages with the given source " .
64 "prefix (with an implied colon following it). If --dest-namespace is not specified, " .
65 "the colon will be replaced with a hyphen.",
67 $this->
addOption(
'dest-namespace',
"In combination with --source-pseudo-namespace, " .
68 "specify the namespace ID of the destination.",
false,
true );
69 $this->
addOption(
'move-talk',
"If this is specified, pages in the Talk namespace that " .
70 "begin with a conflicting prefix will be renamed, for example " .
71 "Talk:File:Foo -> File_Talk:Foo" );
78 'add-suffix' => $this->
getOption(
'add-suffix',
'' ),
79 'add-prefix' => $this->
getOption(
'add-prefix',
'' ),
80 'move-talk' => $this->
hasOption(
'move-talk' ),
81 'source-pseudo-namespace' => $this->
getOption(
'source-pseudo-namespace',
'' ),
82 'dest-namespace' => intval( $this->
getOption(
'dest-namespace', 0 ) ) ];
84 if ( $options[
'source-pseudo-namespace'] !==
'' ) {
87 $retval = $this->
checkAll( $options );
91 $this->
output(
"\nLooks good!\n" );
93 $this->
output(
"\nOh noeees\n" );
105 $contLang = MediaWikiServices::getInstance()->getContentLanguage();
111 $name = $contLang->ucfirst( $prefix );
117 MediaWikiServices::getInstance()->getNamespaceInfo()->getCanonicalNamespaces()
121 if ( $name !==
'' ) {
122 $spaces[$name] = $ns;
125 foreach ( $contLang->getNamespaces() as $ns => $name ) {
126 if ( $name !==
'' ) {
127 $spaces[$name] = $ns;
130 foreach ( $this->
getConfig()->get(
'NamespaceAliases' ) as $name => $ns ) {
131 $spaces[$name] = $ns;
133 foreach ( $contLang->getNamespaceAliases() as $name => $ns ) {
134 $spaces[$name] = $ns;
139 $capitalLinks = $this->
getConfig()->get(
'CapitalLinks' );
140 foreach ( $spaces as $name => $ns ) {
142 $moreNames[] = $contLang->uc( $name );
143 $moreNames[] = $contLang->ucfirst( $contLang->lc( $name ) );
144 $moreNames[] = $contLang->ucwords( $name );
145 $moreNames[] = $contLang->ucwords( $contLang->lc( $name ) );
146 $moreNames[] = $contLang->ucwordbreaks( $name );
147 $moreNames[] = $contLang->ucwordbreaks( $contLang->lc( $name ) );
148 if ( !$capitalLinks ) {
149 foreach ( $moreNames as $altName ) {
150 $moreNames[] = $contLang->lcfirst( $altName );
152 $moreNames[] = $contLang->lcfirst( $name );
154 foreach ( array_unique( $moreNames ) as $altName ) {
155 if ( $altName !== $name ) {
156 $spaces[$altName] = $ns;
163 $origSpaces = $spaces;
164 uksort( $spaces,
function ( $a, $b ) use ( $origSpaces ) {
165 return $origSpaces[$a] <=> $origSpaces[$b]
170 foreach ( $spaces as $name => $ns ) {
174 $this->
output(
"{$this->totalPages} pages to fix, " .
175 "{$this->resolvablePages} were resolvable.\n\n" );
177 foreach ( $spaces as $name => $ns ) {
197 $this->
checkLinkTable(
'templatelinks',
'tl', $ns, $name, $options );
204 [
'rd_interwiki' =>
null ] );
206 [
'rd_interwiki' =>
'' ] );
210 $this->
output(
"{$this->totalLinks} links to fix, " .
211 "{$this->resolvableLinks} were resolvable.\n" );
222 $result = MediaWikiServices::getInstance()->getInterwikiLookup()->getAllPrefixes();
224 foreach ( $result as $row ) {
225 $prefixes[] = $row[
'iw_prefix'];
241 $count = $targets->numRows();
242 $this->totalPages += $count;
247 $dryRunNote = $options[
'fix'] ?
'' :
' DRY RUN ONLY';
250 foreach ( $targets as $row ) {
254 $ns, $name, $row->page_namespace, $row->page_title );
257 $logStatus =
'invalid title';
259 } elseif ( $newTitle->exists() ) {
260 if ( $options[
'merge'] ) {
261 if ( $this->
canMerge( $row->page_id, $newTitle, $logStatus ) ) {
266 } elseif ( $options[
'add-prefix'] ==
'' && $options[
'add-suffix'] ==
'' ) {
268 $logStatus =
'dest title exists and --add-prefix not specified';
273 $logStatus =
'alternate title is invalid';
274 } elseif ( $newTitle->exists() ) {
276 $logStatus =
'title conflict';
279 $logStatus =
'alternate';
284 $logStatus =
'no conflict';
289 $logTitle =
"id={$row->page_id} ns={$row->page_namespace} dbk={$row->page_title}";
294 $this->
output(
"$logTitle *** $logStatus\n" );
298 $this->
output(
"$logTitle -> " .
299 $newTitle->getPrefixedDBkey() .
" ($logStatus)$dryRunNote\n" );
301 if ( $options[
'fix'] ) {
302 $pageOK = $this->
movePage( $row->page_id, $newTitle );
306 $this->
output(
"$logTitle => " .
307 $newTitle->getPrefixedDBkey() .
" (merge)$dryRunNote\n" );
309 if ( $options[
'fix'] ) {
310 $pageOK = $this->
mergePage( $row, $newTitle );
316 $this->resolvablePages++;
340 $fromField =
"{$fieldPrefix}_from";
341 $namespaceField =
"{$fieldPrefix}_namespace";
342 $titleField =
"{$fieldPrefix}_title";
347 [ $fromField, $namespaceField, $titleField ],
348 array_merge( $batchConds, $extraConds, [
349 $namespaceField => 0,
350 $titleField . $dbw->buildLike(
"$name:", $dbw->anyString() )
354 'ORDER BY' => [ $titleField, $fromField ],
355 'LIMIT' => $batchSize
359 if (
$res->numRows() == 0 ) {
362 foreach (
$res as $row ) {
363 $logTitle =
"from={$row->$fromField} ns={$row->$namespaceField} " .
364 "dbk={$row->$titleField}";
366 $ns, $name, $row->$namespaceField, $row->$titleField );
369 $this->
output(
"$table $logTitle *** INVALID\n" );
372 $this->resolvableLinks++;
373 if ( !$options[
'fix'] ) {
374 $this->
output(
"$table $logTitle -> " .
375 $destTitle->getPrefixedDBkey() .
" DRY RUN\n" );
379 $dbw->update( $table,
382 $namespaceField => $destTitle->getNamespace(),
383 $titleField => $destTitle->getDBkey()
387 $namespaceField => 0,
388 $titleField => $row->$titleField,
389 $fromField => $row->$fromField
394 $this->
output(
"$table $logTitle -> " .
395 $destTitle->getPrefixedDBkey() .
"\n" );
397 $encLastTitle = $dbw->addQuotes( $row->$titleField );
398 $encLastFrom = $dbw->addQuotes( $row->$fromField );
401 "$titleField > $encLastTitle " .
402 "OR ($titleField = $encLastTitle AND $fromField > $encLastFrom)" ];
416 $prefix = $options[
'source-pseudo-namespace'];
417 $ns = $options[
'dest-namespace'];
418 $this->
output(
"Checking prefix \"$prefix\" vs namespace $ns\n" );
437 $options[
'move-talk'] &&
438 MediaWikiServices::getInstance()->getNamespaceInfo()->isSubject( $ns )
445 return $dbw->select(
'page',
452 'page_namespace' => $checkNamespaces,
453 'page_title' . $dbw->buildLike(
"$name:", $dbw->anyString() ),
468 $dbk = substr( $sourceDbk, strlen(
"$name:" ) );
471 $dbk =
"$name-" . $dbk;
474 $nsInfo = MediaWikiServices::getInstance()->getNamespaceInfo();
475 if ( $sourceNs ==
NS_TALK && $nsInfo->isSubject( $ns ) ) {
477 $destNS = $nsInfo->getTalk( $destNS );
480 if ( !$newTitle || !$newTitle->canExist() ) {
495 $prefix = $options[
'add-prefix'];
496 $suffix = $options[
'add-suffix'];
497 if ( $prefix ==
'' && $suffix ==
'' ) {
501 $dbk = $prefix . $linkTarget->
getDBkey() . $suffix;
506 if ( !
$title->exists() ) {
522 $dbw->update(
'page',
525 "page_title" => $newLinkTarget->
getDBkey(),
533 $fromNamespaceTables = [
534 [
'pagelinks',
'pl' ],
535 [
'templatelinks',
'tl' ],
536 [
'imagelinks',
'il' ] ];
537 foreach ( $fromNamespaceTables as $tableInfo ) {
538 list( $table, $fieldPrefix ) = $tableInfo;
539 $dbw->update( $table,
541 [
"{$fieldPrefix}_from_namespace" => $newLinkTarget->
getNamespace() ],
543 [
"{$fieldPrefix}_from" => $id ],
565 if ( $latestSource->getTimestamp() > $latestDest->getTimestamp() ) {
566 $logStatus =
'cannot merge since source is later';
589 $sourceTitle->resetArticleID( $id );
590 $wikiPage =
new WikiPage( $sourceTitle );
591 $wikiPage->loadPageData(
'fromdbmaster' );
595 $dbw->update(
'revision',
597 [
'rev_page' => $destId ],
599 [
'rev_page' => $id ],
602 $dbw->delete(
'page', [
'page_id' => $id ], __METHOD__ );