MediaWiki  REL1_22
updateSearchIndex.php
Go to the documentation of this file.
00001 <?php
00031 require_once __DIR__ . '/Maintenance.php';
00032 
00038 class UpdateSearchIndex extends Maintenance {
00039 
00040     public function __construct() {
00041         parent::__construct();
00042         $this->mDescription = "Script for periodic off-peak updating of the search index";
00043         $this->addOption( 's', 'starting timestamp', false, true );
00044         $this->addOption( 'e', 'Ending timestamp', false, true );
00045         $this->addOption( 'p', 'File for saving/loading timestamps, searchUpdate.WIKI_ID.pos by default', false, true );
00046         $this->addOption( 'l', 'How long the searchindex and revision tables will be locked for', false, true );
00047     }
00048 
00049     public function getDbType() {
00050         return Maintenance::DB_ADMIN;
00051     }
00052 
00053     public function execute() {
00054         $posFile = $this->getOption( 'p', 'searchUpdate.' . wfWikiId() . '.pos' );
00055         $end = $this->getOption( 'e', wfTimestampNow() );
00056         if ( $this->hasOption( 's' ) ) {
00057             $start = $this->getOption( 's' );
00058         } elseif ( is_readable( 'searchUpdate.pos' ) ) {
00059             # B/c to the old position file name which was hardcoded
00060             # We can safely delete the file when we're done though.
00061             $start = file_get_contents( 'searchUpdate.pos' );
00062             unlink( 'searchUpdate.pos' );
00063         } elseif ( is_readable( $posFile ) ) {
00064             $start = file_get_contents( $posFile );
00065         } else {
00066             $start = wfTimestamp( TS_MW, time() - 86400 );
00067         }
00068         $lockTime = $this->getOption( 'l', 20 );
00069 
00070         $this->doUpdateSearchIndex( $start, $end, $lockTime );
00071         if ( is_writable( dirname( realpath( $posFile ) ) ) ) {
00072             $file = fopen( $posFile, 'w' );
00073             if ( $file !== false ) {
00074                 fwrite( $file, $end );
00075                 fclose( $file );
00076             } else {
00077                 $this->error( "*** Couldn't write to the $posFile!\n" );
00078             }
00079         } else {
00080             $this->error( "*** Couldn't write to the $posFile!\n" );
00081         }
00082     }
00083 
00084     private function doUpdateSearchIndex( $start, $end, $maxLockTime ) {
00085         global $wgDisableSearchUpdate;
00086 
00087         $wgDisableSearchUpdate = false;
00088 
00089         $dbw = wfGetDB( DB_MASTER );
00090         $recentchanges = $dbw->tableName( 'recentchanges' );
00091 
00092         $this->output( "Updating searchindex between $start and $end\n" );
00093 
00094         # Select entries from recentchanges which are on top and between the specified times
00095         $start = $dbw->timestamp( $start );
00096         $end = $dbw->timestamp( $end );
00097 
00098         $page = $dbw->tableName( 'page' );
00099         $sql = "SELECT rc_cur_id FROM $recentchanges
00100             JOIN $page ON rc_cur_id=page_id AND rc_this_oldid=page_latest
00101             WHERE rc_type != " . RC_LOG . " AND rc_timestamp BETWEEN '$start' AND '$end'";
00102         $res = $dbw->query( $sql, __METHOD__ );
00103 
00104         $this->updateSearchIndex( $maxLockTime, array( $this, 'searchIndexUpdateCallback' ), $dbw, $res );
00105 
00106         $this->output( "Done\n" );
00107     }
00108 
00109     public function searchIndexUpdateCallback( $dbw, $row ) {
00110         $this->updateSearchIndexForPage( $dbw, $row->rc_cur_id );
00111     }
00112 }
00113 
00114 $maintClass = "UpdateSearchIndex";
00115 require_once RUN_MAINTENANCE_IF_MAIN;