MediaWiki  REL1_23
attachLatest.php
Go to the documentation of this file.
00001 <?php
00027 require_once __DIR__ . '/Maintenance.php';
00028 
00035 class AttachLatest extends Maintenance {
00036 
00037     public function __construct() {
00038         parent::__construct();
00039         $this->addOption( "fix", "Actually fix the entries, will dry run otherwise" );
00040         $this->addOption( "regenerate-all",
00041             "Regenerate the page_latest field for all records in table page" );
00042         $this->mDescription = "Fix page_latest entries in the page table";
00043     }
00044 
00045     public function execute() {
00046         $this->output( "Looking for pages with page_latest set to 0...\n" );
00047         $dbw = wfGetDB( DB_MASTER );
00048         $conds = array( 'page_latest' => 0 );
00049         if ( $this->hasOption( 'regenerate-all' ) ) {
00050             $conds = '';
00051         }
00052         $result = $dbw->select( 'page',
00053             array( 'page_id', 'page_namespace', 'page_title' ),
00054             $conds,
00055             __METHOD__ );
00056 
00057         $n = 0;
00058         foreach ( $result as $row ) {
00059             $pageId = intval( $row->page_id );
00060             $title = Title::makeTitle( $row->page_namespace, $row->page_title );
00061             $name = $title->getPrefixedText();
00062             $latestTime = $dbw->selectField( 'revision',
00063                 'MAX(rev_timestamp)',
00064                 array( 'rev_page' => $pageId ),
00065                 __METHOD__ );
00066             if ( !$latestTime ) {
00067                 $this->output( wfWikiID() . " $pageId [[$name]] can't find latest rev time?!\n" );
00068                 continue;
00069             }
00070 
00071             $revision = Revision::loadFromTimestamp( $dbw, $title, $latestTime );
00072             if ( is_null( $revision ) ) {
00073                 $this->output( wfWikiID() . " $pageId [[$name]] latest time $latestTime, can't find revision id\n" );
00074                 continue;
00075             }
00076             $id = $revision->getId();
00077             $this->output( wfWikiID() . " $pageId [[$name]] latest time $latestTime, rev id $id\n" );
00078             if ( $this->hasOption( 'fix' ) ) {
00079                 $page = WikiPage::factory( $title );
00080                 $page->updateRevisionOn( $dbw, $revision );
00081             }
00082             $n++;
00083         }
00084         $this->output( "Done! Processed $n pages.\n" );
00085         if ( !$this->hasOption( 'fix' ) ) {
00086             $this->output( "This was a dry run; rerun with --fix to update page_latest.\n" );
00087         }
00088     }
00089 }
00090 
00091 $maintClass = "AttachLatest";
00092 require_once RUN_MAINTENANCE_IF_MAIN;