MediaWiki  REL1_19
orphans.php
Go to the documentation of this file.
00001 <?php
00030 require_once( dirname( __FILE__ ) . '/Maintenance.php' );
00031 
00032 class Orphans extends Maintenance {
00033         public function __construct() {
00034                 parent::__construct();
00035                 $this->mDescription = "Look for 'orphan' revisions hooked to pages which don't exist\n" .
00036                                                                 "And 'childless' pages with no revisions\n" .
00037                                                                 "Then, kill the poor widows and orphans\n" .
00038                                                                 "Man this is depressing";
00039                 $this->addOption( 'fix', 'Actually fix broken entries' );
00040         }
00041 
00042         public function execute() {
00043                 global $wgTitle;
00044                 $wgTitle = Title::newFromText( 'Orphan revision cleanup script' );
00045                 $this->checkOrphans( $this->hasOption( 'fix' ) );
00046                 $this->checkSeparation( $this->hasOption( 'fix' ) );
00047                 # Does not work yet, do not use
00048                 # $this->checkWidows( $this->hasOption( 'fix' ) );
00049         }
00050 
00056         private function lockTables( $db, $extraTable = array() ) {
00057                 $tbls = array( 'page', 'revision', 'redirect' );
00058                 if ( $extraTable ) {
00059                         $tbls = array_merge( $tbls, $extraTable );
00060                 }
00061                 $db->lockTables( array(), $tbls, __METHOD__, false );
00062         }
00063 
00068         private function checkOrphans( $fix ) {
00069                 $dbw = wfGetDB( DB_MASTER );
00070                 $page = $dbw->tableName( 'page' );
00071                 $revision = $dbw->tableName( 'revision' );
00072 
00073                 if ( $fix ) {
00074                         $this->lockTables( $dbw );
00075                 }
00076 
00077                 $this->output( "Checking for orphan revision table entries... (this may take a while on a large wiki)\n" );
00078                 $result = $dbw->query( "
00079                         SELECT *
00080                         FROM $revision LEFT OUTER JOIN $page ON rev_page=page_id
00081                         WHERE page_id IS NULL
00082                 " );
00083                 $orphans = $dbw->numRows( $result );
00084                 if ( $orphans > 0 ) {
00085                         global $wgContLang;
00086                         $this->output( "$orphans orphan revisions...\n" );
00087                         $this->output( sprintf( "%10s %10s %14s %20s %s\n", 'rev_id', 'rev_page', 'rev_timestamp', 'rev_user_text', 'rev_comment' ) );
00088                         foreach ( $result as $row ) {
00089                                 $comment = ( $row->rev_comment == '' )
00090                                         ? ''
00091                                         : '(' . $wgContLang->truncate( $row->rev_comment, 40 ) . ')';
00092                                 $this->output( sprintf( "%10d %10d %14s %20s %s\n",
00093                                         $row->rev_id,
00094                                         $row->rev_page,
00095                                         $row->rev_timestamp,
00096                                         $wgContLang->truncate( $row->rev_user_text, 17 ),
00097                                         $comment ) );
00098                                 if ( $fix ) {
00099                                         $dbw->delete( 'revision', array( 'rev_id' => $row->rev_id ) );
00100                                 }
00101                         }
00102                         if ( !$fix ) {
00103                                 $this->output( "Run again with --fix to remove these entries automatically.\n" );
00104                         }
00105                 } else {
00106                         $this->output( "No orphans! Yay!\n" );
00107                 }
00108 
00109                 if ( $fix ) {
00110                         $dbw->unlockTables( __METHOD__ );
00111                 }
00112         }
00113 
00120         private function checkWidows( $fix ) {
00121                 $dbw = wfGetDB( DB_MASTER );
00122                 $page = $dbw->tableName( 'page' );
00123                 $revision = $dbw->tableName( 'revision' );
00124 
00125                 if ( $fix ) {
00126                         $this->lockTables( $dbw );
00127                 }
00128 
00129                 $this->output( "\nChecking for childless page table entries... (this may take a while on a large wiki)\n" );
00130                 $result = $dbw->query( "
00131                         SELECT *
00132                         FROM $page LEFT OUTER JOIN $revision ON page_latest=rev_id
00133                         WHERE rev_id IS NULL
00134                 " );
00135                 $widows = $dbw->numRows( $result );
00136                 if ( $widows > 0 ) {
00137                         $this->output( "$widows childless pages...\n" );
00138                         $this->output( sprintf( "%10s %11s %2s %s\n", 'page_id', 'page_latest', 'ns', 'page_title' ) );
00139                         foreach ( $result as $row ) {
00140                                 printf( "%10d %11d %2d %s\n",
00141                                         $row->page_id,
00142                                         $row->page_latest,
00143                                         $row->page_namespace,
00144                                         $row->page_title );
00145                                 if ( $fix ) {
00146                                         $dbw->delete( 'page', array( 'page_id' => $row->page_id ) );
00147                                 }
00148                         }
00149                         if ( !$fix ) {
00150                                 $this->output( "Run again with --fix to remove these entries automatically.\n" );
00151                         }
00152                 } else {
00153                         $this->output( "No childless pages! Yay!\n" );
00154                 }
00155 
00156                 if ( $fix ) {
00157                         $dbw->unlockTables( __METHOD__ );
00158                 }
00159         }
00160 
00165         private function checkSeparation( $fix ) {
00166                 $dbw = wfGetDB( DB_MASTER );
00167                 $page     = $dbw->tableName( 'page' );
00168                 $revision = $dbw->tableName( 'revision' );
00169 
00170                 if ( $fix ) {
00171                         $this->lockTables( $dbw, array( 'user', 'text' ) );
00172                 }
00173 
00174                 $this->output( "\nChecking for pages whose page_latest links are incorrect... (this may take a while on a large wiki)\n" );
00175                 $result = $dbw->query( "
00176                         SELECT *
00177                         FROM $page LEFT OUTER JOIN $revision ON page_latest=rev_id
00178                 " );
00179                 $found = 0;
00180                 foreach ( $result as $row ) {
00181                         $result2 = $dbw->query( "
00182                                 SELECT MAX(rev_timestamp) as max_timestamp
00183                                 FROM $revision
00184                                 WHERE rev_page=$row->page_id
00185                         " );
00186                         $row2 = $dbw->fetchObject( $result2 );
00187                         if ( $row2 ) {
00188                                 if ( $row->rev_timestamp != $row2->max_timestamp ) {
00189                                         if ( $found == 0 ) {
00190                                                 $this->output( sprintf( "%10s %10s %14s %14s\n",
00191                                                         'page_id', 'rev_id', 'timestamp', 'max timestamp' ) );
00192                                         }
00193                                         ++$found;
00194                                         $this->output( sprintf( "%10d %10d %14s %14s\n",
00195                                                 $row->page_id,
00196                                                 $row->page_latest,
00197                                                 $row->rev_timestamp,
00198                                                 $row2->max_timestamp ) );
00199                                         if ( $fix ) {
00200                                                 # ...
00201                                                 $maxId = $dbw->selectField(
00202                                                         'revision',
00203                                                         'rev_id',
00204                                                         array(
00205                                                                 'rev_page'      => $row->page_id,
00206                                                                 'rev_timestamp' => $row2->max_timestamp ) );
00207                                                 $this->output( "... updating to revision $maxId\n" );
00208                                                 $maxRev = Revision::newFromId( $maxId );
00209                                                 $title = Title::makeTitle( $row->page_namespace, $row->page_title );
00210                                                 $article = WikiPage::factory( $title );
00211                                                 $article->updateRevisionOn( $dbw, $maxRev );
00212                                         }
00213                                 }
00214                         } else {
00215                                 $this->output( "wtf\n" );
00216                         }
00217                 }
00218 
00219                 if ( $found ) {
00220                         $this->output( "Found $found pages with incorrect latest revision.\n" );
00221                 } else {
00222                         $this->output( "No pages with incorrect latest revision. Yay!\n" );
00223                 }
00224                 if ( !$fix && $found > 0 ) {
00225                         $this->output( "Run again with --fix to remove these entries automatically.\n" );
00226                 }
00227 
00228                 if ( $fix ) {
00229                         $dbw->unlockTables( __METHOD__ );
00230                 }
00231         }
00232 }
00233 
00234 $maintClass = "Orphans";
00235 require_once( RUN_MAINTENANCE_IF_MAIN );