00001 <?php
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023
00024
00025
00026
00027
00028
00029
00030
00031 require_once( dirname(__FILE__) . '/cleanupTable.inc' );
00032
00033 class TitleCleanup extends TableCleanup {
00034 public function __construct() {
00035 parent::__construct();
00036 $this->mDescription = "Script to clean up broken, unparseable titles";
00037 }
00038
00039 protected function processRow( $row ) {
00040 global $wgContLang;
00041 $display = Title::makeName( $row->page_namespace, $row->page_title );
00042 $verified = $wgContLang->normalize( $display );
00043 $title = Title::newFromText( $verified );
00044
00045 if( !is_null( $title )
00046 && $title->canExist()
00047 && $title->getNamespace() == $row->page_namespace
00048 && $title->getDBkey() === $row->page_title )
00049 {
00050 return $this->progress( 0 );
00051 }
00052
00053 if( $row->page_namespace == NS_FILE && $this->fileExists( $row->page_title ) ) {
00054 $this->output( "file $row->page_title needs cleanup, please run cleanupImages.php.\n" );
00055 return $this->progress( 0 );
00056 } elseif( is_null( $title ) ) {
00057 $this->output( "page $row->page_id ($display) is illegal.\n" );
00058 $this->moveIllegalPage( $row );
00059 return $this->progress( 1 );
00060 } else {
00061 $this->output( "page $row->page_id ($display) doesn't match self.\n" );
00062 $this->moveInconsistentPage( $row, $title );
00063 return $this->progress( 1 );
00064 }
00065 }
00066
00067 protected function fileExists( $name ) {
00068
00069
00070 $dbr = wfGetDB( DB_SLAVE );
00071 $row = $dbr->selectRow( 'image', array( 'img_name' ), array( 'img_name' => $name ), __METHOD__ );
00072 return $row !== false;
00073 }
00074
00075 protected function moveIllegalPage( $row ) {
00076 $legal = 'A-Za-z0-9_/\\\\-';
00077 $legalized = preg_replace_callback( "!([^$legal])!",
00078 array( &$this, 'hexChar' ),
00079 $row->page_title );
00080 if( $legalized == '.' ) $legalized = '(dot)';
00081 if( $legalized == '_' ) $legalized = '(space)';
00082 $legalized = 'Broken/' . $legalized;
00083
00084 $title = Title::newFromText( $legalized );
00085 if( is_null( $title ) ) {
00086 $clean = 'Broken/id:' . $row->page_id;
00087 $this->output( "Couldn't legalize; form '$legalized' still invalid; using '$clean'\n" );
00088 $title = Title::newFromText( $clean );
00089 } elseif( $title->exists() ) {
00090 $clean = 'Broken/id:' . $row->page_id;
00091 $this->output( "Legalized for '$legalized' exists; using '$clean'\n" );
00092 $title = Title::newFromText( $clean );
00093 }
00094
00095 $dest = $title->getDBkey();
00096 if( $this->dryrun ) {
00097 $this->output( "DRY RUN: would rename $row->page_id ($row->page_namespace,'$row->page_title') to ($row->page_namespace,'$dest')\n" );
00098 } else {
00099 $this->output( "renaming $row->page_id ($row->page_namespace,'$row->page_title') to ($row->page_namespace,'$dest')\n" );
00100 $dbw = wfGetDB( DB_MASTER );
00101 $dbw->update( 'page',
00102 array( 'page_title' => $dest ),
00103 array( 'page_id' => $row->page_id ),
00104 __METHOD__ );
00105 }
00106 }
00107
00108 protected function moveInconsistentPage( $row, $title ) {
00109 if( $title->exists() || $title->getInterwiki() ) {
00110 if( $title->getInterwiki() ) {
00111 $prior = $title->getPrefixedDbKey();
00112 } else {
00113 $prior = $title->getDBkey();
00114 }
00115 $clean = 'Broken/' . $prior;
00116 $verified = Title::makeTitleSafe( $row->page_namespace, $clean );
00117 if( $verified->exists() ) {
00118 $blah = "Broken/id:" . $row->page_id;
00119 $this->output( "Couldn't legalize; form '$clean' exists; using '$blah'\n" );
00120 $verified = Title::makeTitleSafe( $row->page_namespace, $blah );
00121 }
00122 $title = $verified;
00123 }
00124 if( is_null( $title ) ) {
00125 $this->error( "Something awry; empty title.", true );
00126 }
00127 $ns = $title->getNamespace();
00128 $dest = $title->getDBkey();
00129 if( $this->dryrun ) {
00130 $this->output( "DRY RUN: would rename $row->page_id ($row->page_namespace,'$row->page_title') to ($row->page_namespace,'$dest')\n" );
00131 } else {
00132 $this->output( "renaming $row->page_id ($row->page_namespace,'$row->page_title') to ($ns,'$dest')\n" );
00133 $dbw = wfGetDB( DB_MASTER );
00134 $dbw->update( 'page',
00135 array(
00136 'page_namespace' => $ns,
00137 'page_title' => $dest
00138 ),
00139 array( 'page_id' => $row->page_id ),
00140 __METHOD__ );
00141 $linkCache = LinkCache::singleton();
00142 $linkCache->clear();
00143 }
00144 }
00145 }
00146
00147 $maintClass = "TitleCleanup";
00148 require_once( DO_MAINTENANCE );