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