MediaWiki  REL1_22
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->mDescription = "Fix page_latest entries in the page table";
00041     }
00042 
00043     public function execute() {
00044         $this->output( "Looking for pages with page_latest set to 0...\n" );
00045         $dbw = wfGetDB( DB_MASTER );
00046         $result = $dbw->select( 'page',
00047             array( 'page_id', 'page_namespace', 'page_title' ),
00048             array( 'page_latest' => 0 ),
00049             __METHOD__ );
00050 
00051         $n = 0;
00052         foreach ( $result as $row ) {
00053             $pageId = intval( $row->page_id );
00054             $title = Title::makeTitle( $row->page_namespace, $row->page_title );
00055             $name = $title->getPrefixedText();
00056             $latestTime = $dbw->selectField( 'revision',
00057                 'MAX(rev_timestamp)',
00058                 array( 'rev_page' => $pageId ),
00059                 __METHOD__ );
00060             if ( !$latestTime ) {
00061                 $this->output( wfWikiID() . " $pageId [[$name]] can't find latest rev time?!\n" );
00062                 continue;
00063             }
00064 
00065             $revision = Revision::loadFromTimestamp( $dbw, $title, $latestTime );
00066             if ( is_null( $revision ) ) {
00067                 $this->output( wfWikiID() . " $pageId [[$name]] latest time $latestTime, can't find revision id\n" );
00068                 continue;
00069             }
00070             $id = $revision->getId();
00071             $this->output( wfWikiID() . " $pageId [[$name]] latest time $latestTime, rev id $id\n" );
00072             if ( $this->hasOption( 'fix' ) ) {
00073                 $page = WikiPage::factory( $title );
00074                 $page->updateRevisionOn( $dbw, $revision );
00075             }
00076             $n++;
00077         }
00078         $this->output( "Done! Processed $n pages.\n" );
00079         if ( !$this->hasOption( 'fix' ) ) {
00080             $this->output( "This was a dry run; rerun with --fix to update page_latest.\n" );
00081         }
00082     }
00083 }
00084 
00085 $maintClass = "AttachLatest";
00086 require_once RUN_MAINTENANCE_IF_MAIN;