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