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