MediaWiki  REL1_20
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 
00045         protected function processRow( $row ) {
00046                 global $wgContLang;
00047                 $display = Title::makeName( $row->page_namespace, $row->page_title );
00048                 $verified = $wgContLang->normalize( $display );
00049                 $title = Title::newFromText( $verified );
00050 
00051                 if ( !is_null( $title )
00052                         && $title->canExist()
00053                         && $title->getNamespace() == $row->page_namespace
00054                         && $title->getDBkey() === $row->page_title )
00055                 {
00056                         return $this->progress( 0 );  // all is fine
00057                 }
00058 
00059                 if ( $row->page_namespace == NS_FILE && $this->fileExists( $row->page_title ) ) {
00060                         $this->output( "file $row->page_title needs cleanup, please run cleanupImages.php.\n" );
00061                         return $this->progress( 0 );
00062                 } elseif ( is_null( $title ) ) {
00063                         $this->output( "page $row->page_id ($display) is illegal.\n" );
00064                         $this->moveIllegalPage( $row );
00065                         return $this->progress( 1 );
00066                 } else {
00067                         $this->output( "page $row->page_id ($display) doesn't match self.\n" );
00068                         $this->moveInconsistentPage( $row, $title );
00069                         return $this->progress( 1 );
00070                 }
00071         }
00072 
00073         protected function fileExists( $name ) {
00074                 // XXX: Doesn't actually check for file existence, just presence of image record.
00075                 // This is reasonable, since cleanupImages.php only iterates over the image table.
00076                 $dbr = wfGetDB( DB_SLAVE );
00077                 $row = $dbr->selectRow( 'image', array( 'img_name' ), array( 'img_name' => $name ), __METHOD__ );
00078                 return $row !== false;
00079         }
00080 
00081         protected function moveIllegalPage( $row ) {
00082                 $legal = 'A-Za-z0-9_/\\\\-';
00083                 $legalized = preg_replace_callback( "!([^$legal])!",
00084                         array( &$this, 'hexChar' ),
00085                         $row->page_title );
00086                 if ( $legalized == '.' ) $legalized = '(dot)';
00087                 if ( $legalized == '_' ) $legalized = '(space)';
00088                 $legalized = 'Broken/' . $legalized;
00089 
00090                 $title = Title::newFromText( $legalized );
00091                 if ( is_null( $title ) ) {
00092                         $clean = 'Broken/id:' . $row->page_id;
00093                         $this->output( "Couldn't legalize; form '$legalized' still invalid; using '$clean'\n" );
00094                         $title = Title::newFromText( $clean );
00095                 } elseif ( $title->exists() ) {
00096                         $clean = 'Broken/id:' . $row->page_id;
00097                         $this->output( "Legalized for '$legalized' exists; using '$clean'\n" );
00098                         $title = Title::newFromText( $clean );
00099                 }
00100 
00101                 $dest = $title->getDBkey();
00102                 if ( $this->dryrun ) {
00103                         $this->output( "DRY RUN: would rename $row->page_id ($row->page_namespace,'$row->page_title') to ($row->page_namespace,'$dest')\n" );
00104                 } else {
00105                         $this->output( "renaming $row->page_id ($row->page_namespace,'$row->page_title') to ($row->page_namespace,'$dest')\n" );
00106                         $dbw = wfGetDB( DB_MASTER );
00107                         $dbw->update( 'page',
00108                                 array( 'page_title' => $dest ),
00109                                 array( 'page_id' => $row->page_id ),
00110                                 __METHOD__ );
00111                 }
00112         }
00113 
00114         protected function moveInconsistentPage( $row, $title ) {
00115                 if ( $title->exists() || $title->getInterwiki() || !$title->canExist() ) {
00116                         if ( $title->getInterwiki() || !$title->canExist() ) {
00117                                 $prior = $title->getPrefixedDbKey();
00118                         } else {
00119                                 $prior = $title->getDBkey();
00120                         }
00121 
00122                         # Old cleanupTitles could move articles there. See bug 23147.
00123                         $ns = $row->page_namespace;
00124                         if ( $ns < 0 ) $ns = 0;
00125 
00126                         $clean = 'Broken/' . $prior;
00127                         $verified = Title::makeTitleSafe( $ns, $clean );
00128                         if ( $verified->exists() ) {
00129                                 $blah = "Broken/id:" . $row->page_id;
00130                                 $this->output( "Couldn't legalize; form '$clean' exists; using '$blah'\n" );
00131                                 $verified = Title::makeTitleSafe( $ns, $blah );
00132                         }
00133                         $title = $verified;
00134                 }
00135                 if ( is_null( $title ) ) {
00136                         $this->error( "Something awry; empty title.", true );
00137                 }
00138                 $ns = $title->getNamespace();
00139                 $dest = $title->getDBkey();
00140 
00141                 if ( $this->dryrun ) {
00142                         $this->output( "DRY RUN: would rename $row->page_id ($row->page_namespace,'$row->page_title') to ($ns,'$dest')\n" );
00143                 } else {
00144                         $this->output( "renaming $row->page_id ($row->page_namespace,'$row->page_title') to ($ns,'$dest')\n" );
00145                         $dbw = wfGetDB( DB_MASTER );
00146                         $dbw->update( 'page',
00147                                 array(
00148                                         'page_namespace' => $ns,
00149                                         'page_title' => $dest
00150                                 ),
00151                                 array( 'page_id' => $row->page_id ),
00152                                 __METHOD__ );
00153                         LinkCache::singleton()->clear();
00154                 }
00155         }
00156 }
00157 
00158 $maintClass = "TitleCleanup";
00159 require_once( RUN_MAINTENANCE_IF_MAIN );