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