MediaWiki  REL1_24
attachLatest.php
Go to the documentation of this file.
00001 <?php
00027 require_once __DIR__ . '/Maintenance.php';
00028 
00035 class AttachLatest extends Maintenance {
00036     public function __construct() {
00037         parent::__construct();
00038         $this->addOption( "fix", "Actually fix the entries, will dry run otherwise" );
00039         $this->addOption( "regenerate-all",
00040             "Regenerate the page_latest field for all records in table page" );
00041         $this->mDescription = "Fix page_latest entries in the page table";
00042     }
00043 
00044     public function execute() {
00045         $this->output( "Looking for pages with page_latest set to 0...\n" );
00046         $dbw = wfGetDB( DB_MASTER );
00047         $conds = array( 'page_latest' => 0 );
00048         if ( $this->hasOption( 'regenerate-all' ) ) {
00049             $conds = '';
00050         }
00051         $result = $dbw->select( 'page',
00052             array( 'page_id', 'page_namespace', 'page_title' ),
00053             $conds,
00054             __METHOD__ );
00055 
00056         $n = 0;
00057         foreach ( $result as $row ) {
00058             $pageId = intval( $row->page_id );
00059             $title = Title::makeTitle( $row->page_namespace, $row->page_title );
00060             $name = $title->getPrefixedText();
00061             $latestTime = $dbw->selectField( 'revision',
00062                 'MAX(rev_timestamp)',
00063                 array( 'rev_page' => $pageId ),
00064                 __METHOD__ );
00065             if ( !$latestTime ) {
00066                 $this->output( wfWikiID() . " $pageId [[$name]] can't find latest rev time?!\n" );
00067                 continue;
00068             }
00069 
00070             $revision = Revision::loadFromTimestamp( $dbw, $title, $latestTime );
00071             if ( is_null( $revision ) ) {
00072                 $this->output( wfWikiID()
00073                     . " $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;