MediaWiki  REL1_22
cleanupTitles.php
Go to the documentation of this file.
00001 <?php
00032 require_once __DIR__ . '/cleanupTable.inc';
00033 
00039 class TitleCleanup extends TableCleanup {
00040     public function __construct() {
00041         parent::__construct();
00042         $this->mDescription = "Script to clean up broken, unparseable titles";
00043     }
00044 
00048     protected function processRow( $row ) {
00049         global $wgContLang;
00050         $display = Title::makeName( $row->page_namespace, $row->page_title );
00051         $verified = $wgContLang->normalize( $display );
00052         $title = Title::newFromText( $verified );
00053 
00054         if ( !is_null( $title )
00055             && $title->canExist()
00056             && $title->getNamespace() == $row->page_namespace
00057             && $title->getDBkey() === $row->page_title
00058         ) {
00059             $this->progress( 0 ); // all is fine
00060 
00061             return;
00062         }
00063 
00064         if ( $row->page_namespace == NS_FILE && $this->fileExists( $row->page_title ) ) {
00065             $this->output( "file $row->page_title needs cleanup, please run cleanupImages.php.\n" );
00066             $this->progress( 0 );
00067         } elseif ( is_null( $title ) ) {
00068             $this->output( "page $row->page_id ($display) is illegal.\n" );
00069             $this->moveIllegalPage( $row );
00070             $this->progress( 1 );
00071         } else {
00072             $this->output( "page $row->page_id ($display) doesn't match self.\n" );
00073             $this->moveInconsistentPage( $row, $title );
00074             $this->progress( 1 );
00075         }
00076     }
00077 
00082     protected function fileExists( $name ) {
00083         // XXX: Doesn't actually check for file existence, just presence of image record.
00084         // This is reasonable, since cleanupImages.php only iterates over the image table.
00085         $dbr = wfGetDB( DB_SLAVE );
00086         $row = $dbr->selectRow( 'image', array( 'img_name' ), array( 'img_name' => $name ), __METHOD__ );
00087 
00088         return $row !== false;
00089     }
00090 
00094     protected function moveIllegalPage( $row ) {
00095         $legal = 'A-Za-z0-9_/\\\\-';
00096         $legalized = preg_replace_callback( "!([^$legal])!",
00097             array( &$this, 'hexChar' ),
00098             $row->page_title );
00099         if ( $legalized == '.' ) {
00100             $legalized = '(dot)';
00101         }
00102         if ( $legalized == '_' ) {
00103             $legalized = '(space)';
00104         }
00105         $legalized = 'Broken/' . $legalized;
00106 
00107         $title = Title::newFromText( $legalized );
00108         if ( is_null( $title ) ) {
00109             $clean = 'Broken/id:' . $row->page_id;
00110             $this->output( "Couldn't legalize; form '$legalized' still invalid; using '$clean'\n" );
00111             $title = Title::newFromText( $clean );
00112         } elseif ( $title->exists() ) {
00113             $clean = 'Broken/id:' . $row->page_id;
00114             $this->output( "Legalized for '$legalized' exists; using '$clean'\n" );
00115             $title = Title::newFromText( $clean );
00116         }
00117 
00118         $dest = $title->getDBkey();
00119         if ( $this->dryrun ) {
00120             $this->output( "DRY RUN: would rename $row->page_id ($row->page_namespace," .
00121                 "'$row->page_title') to ($row->page_namespace,'$dest')\n" );
00122         } else {
00123             $this->output( "renaming $row->page_id ($row->page_namespace," .
00124                 "'$row->page_title') to ($row->page_namespace,'$dest')\n" );
00125             $dbw = wfGetDB( DB_MASTER );
00126             $dbw->update( 'page',
00127                 array( 'page_title' => $dest ),
00128                 array( 'page_id' => $row->page_id ),
00129                 __METHOD__ );
00130         }
00131     }
00132 
00137     protected function moveInconsistentPage( $row, $title ) {
00138         if ( $title->exists() || $title->getInterwiki() || !$title->canExist() ) {
00139             if ( $title->getInterwiki() || !$title->canExist() ) {
00140                 $prior = $title->getPrefixedDBkey();
00141             } else {
00142                 $prior = $title->getDBkey();
00143             }
00144 
00145             # Old cleanupTitles could move articles there. See bug 23147.
00146             $ns = $row->page_namespace;
00147             if ( $ns < 0 ) {
00148                 $ns = 0;
00149             }
00150 
00151             $clean = 'Broken/' . $prior;
00152             $verified = Title::makeTitleSafe( $ns, $clean );
00153             if ( $verified->exists() ) {
00154                 $blah = "Broken/id:" . $row->page_id;
00155                 $this->output( "Couldn't legalize; form '$clean' exists; using '$blah'\n" );
00156                 $verified = Title::makeTitleSafe( $ns, $blah );
00157             }
00158             $title = $verified;
00159         }
00160         if ( is_null( $title ) ) {
00161             $this->error( "Something awry; empty title.", true );
00162         }
00163         $ns = $title->getNamespace();
00164         $dest = $title->getDBkey();
00165 
00166         if ( $this->dryrun ) {
00167             $this->output( "DRY RUN: would rename $row->page_id ($row->page_namespace," .
00168                 "'$row->page_title') to ($ns,'$dest')\n" );
00169         } else {
00170             $this->output( "renaming $row->page_id ($row->page_namespace," .
00171                 "'$row->page_title') to ($ns,'$dest')\n" );
00172             $dbw = wfGetDB( DB_MASTER );
00173             $dbw->update( 'page',
00174                 array(
00175                     'page_namespace' => $ns,
00176                     'page_title' => $dest
00177                 ),
00178                 array( 'page_id' => $row->page_id ),
00179                 __METHOD__ );
00180             LinkCache::singleton()->clear();
00181         }
00182     }
00183 }
00184 
00185 $maintClass = "TitleCleanup";
00186 require_once RUN_MAINTENANCE_IF_MAIN;