00001 <?php
00027 require_once( dirname(__FILE__) . '/Maintenance.php' );
00028
00029 class AttachLatest extends Maintenance {
00030
00031 public function __construct() {
00032 parent::__construct();
00033 $this->addOption( "fix", "Actually fix the entries, will dry run otherwise" );
00034 $this->mDescription = "Fix page_latest entries in the page table";
00035 }
00036
00037 public function execute() {
00038 $this->output( "Looking for pages with page_latest set to 0...\n" );
00039 $dbw = wfGetDB( DB_MASTER );
00040 $result = $dbw->select( 'page',
00041 array( 'page_id', 'page_namespace', 'page_title' ),
00042 array( 'page_latest' => 0 ),
00043 __METHOD__ );
00044
00045 $n = 0;
00046 foreach( $result as $row ) {
00047 $pageId = intval( $row->page_id );
00048 $title = Title::makeTitle( $row->page_namespace, $row->page_title );
00049 $name = $title->getPrefixedText();
00050 $latestTime = $dbw->selectField( 'revision',
00051 'MAX(rev_timestamp)',
00052 array( 'rev_page' => $pageId ),
00053 __METHOD__ );
00054 if( !$latestTime ) {
00055 $this->output( wfWikiID()." $pageId [[$name]] can't find latest rev time?!\n" );
00056 continue;
00057 }
00058
00059 $revision = Revision::loadFromTimestamp( $dbw, $title, $latestTime );
00060 if( is_null( $revision ) ) {
00061 $this->output( wfWikiID()." $pageId [[$name]] latest time $latestTime, can't find revision id\n" );
00062 continue;
00063 }
00064 $id = $revision->getId();
00065 $this->output( wfWikiID()." $pageId [[$name]] latest time $latestTime, rev id $id\n" );
00066 if( $this->hasOption('fix') ) {
00067 $article = new Article( $title );
00068 $article->updateRevisionOn( $dbw, $revision );
00069 }
00070 $n++;
00071 }
00072 $dbw->freeResult( $result );
00073 $this->output( "Done! Processed $n pages.\n" );
00074 if( !$this->hasOption('fix') ) {
00075 $this->output( "This was a dry run; rerun with --fix to update page_latest.\n" );
00076 }
00077 }
00078 }
00079
00080 $maintClass = "AttachLatest";
00081 require_once( DO_MAINTENANCE );