00001 <?php
00030 require_once( dirname(__FILE__) . '/Maintenance.php' );
00031
00032 class UpdateSearchIndex extends Maintenance {
00033
00034 public function __construct() {
00035 parent::__construct();
00036 $this->mDescription = "Script for periodic off-peak updating of the search index";
00037 $this->addOption( 's', 'starting timestamp', false, true );
00038 $this->addOption( 'e', 'Ending timestamp', false, true );
00039 $this->addOption( 'p', 'File for saving/loading timestamps, searchUpdate.WIKI_ID.pos by default', false, true );
00040 $this->addOption( 'l', 'How long the searchindex and revision tables will be locked for', false, true );
00041 }
00042
00043 public function getDbType() {
00044 return Maintenance::DB_ADMIN;
00045 }
00046
00047 public function execute() {
00048 $posFile = $this->getOption( 'p', 'searchUpdate.' . wfWikiId() . '.pos' );
00049 $end = $this->getOption( 'e', wfTimestampNow() );
00050 if ( $this->hasOption( 's' ) ) {
00051 $start = $this->getOption('s');
00052 } elseif( is_readable( 'searchUpdate.pos' ) ) {
00053 # B/c to the old position file name which was hardcoded
00054 # We can safely delete the file when we're done though.
00055 $start = file_get_contents( 'searchUpdate.pos' );
00056 unlink( 'searchUpdate.pos' );
00057 } else {
00058 $start = @file_get_contents( $posFile );
00059 if ( !$start ) {
00060 $start = wfTimestamp( TS_MW, time() - 86400 );
00061 }
00062 }
00063 $lockTime = $this->getOption( 'l', 20 );
00064
00065 $this->doUpdateSearchIndex( $start, $end, $lockTime );
00066 $file = fopen( $posFile, 'w' );
00067 fwrite( $file, $end );
00068 fclose( $file );
00069 }
00070
00071 private function doUpdateSearchIndex( $start, $end, $maxLockTime ) {
00072 global $wgDisableSearchUpdate;
00073
00074 $wgDisableSearchUpdate = false;
00075
00076 $dbw = wfGetDB( DB_MASTER );
00077 $recentchanges = $dbw->tableName( 'recentchanges' );
00078
00079 $this->output( "Updating searchindex between $start and $end\n" );
00080
00081 # Select entries from recentchanges which are on top and between the specified times
00082 $start = $dbw->timestamp( $start );
00083 $end = $dbw->timestamp( $end );
00084
00085 $page = $dbw->tableName( 'page' );
00086 $sql = "SELECT rc_cur_id,rc_type,rc_moved_to_ns,rc_moved_to_title FROM $recentchanges
00087 JOIN $page ON rc_cur_id=page_id AND rc_this_oldid=page_latest
00088 WHERE rc_timestamp BETWEEN '$start' AND '$end'
00089 ";
00090 $res = $dbw->query( $sql, __METHOD__ );
00091
00092
00093 # Lock searchindex
00094 if ( $maxLockTime ) {
00095 $this->output( " --- Waiting for lock ---" );
00096 $this->lockSearchindex( $dbw );
00097 $lockTime = time();
00098 $this->output( "\n" );
00099 }
00100
00101 # Loop through the results and do a search update
00102 foreach ( $res as $row ) {
00103 # Allow reads to be processed
00104 if ( $maxLockTime && time() > $lockTime + $maxLockTime ) {
00105 $this->output( " --- Relocking ---" );
00106 $this->relockSearchindex( $dbw );
00107 $lockTime = time();
00108 $this->output( "\n" );
00109 }
00110 if ( $row->rc_type == RC_LOG ) {
00111 continue;
00112 } elseif ( $row->rc_type == RC_MOVE || $row->rc_type == RC_MOVE_OVER_REDIRECT ) {
00113 # Rename searchindex entry
00114 $titleObj = Title::makeTitle( $row->rc_moved_to_ns, $row->rc_moved_to_title );
00115 $title = $titleObj->getPrefixedDBkey();
00116 $this->output( "$title..." );
00117 $u = new SearchUpdate( $row->rc_cur_id, $title, false );
00118 $this->output( "\n" );
00119 } else {
00120
00121 $rev = Revision::loadFromPageId( $dbw, $row->rc_cur_id );
00122 if( $rev ) {
00123 $titleObj = $rev->getTitle();
00124 $title = $titleObj->getPrefixedDBkey();
00125 $this->output( $title );
00126 # Update searchindex
00127 $u = new SearchUpdate( $row->rc_cur_id, $titleObj->getText(), $rev->getText() );
00128 $u->doUpdate();
00129 $this->output( "\n" );
00130 }
00131 }
00132 }
00133
00134 # Unlock searchindex
00135 if ( $maxLockTime ) {
00136 $this->output( " --- Unlocking --" );
00137 $this->unlockSearchindex( $dbw );
00138 $this->output( "\n" );
00139 }
00140 $this->output( "Done\n" );
00141 }
00142
00147 private function lockSearchindex( &$db ) {
00148 $write = array( 'searchindex' );
00149 $read = array( 'page', 'revision', 'text', 'interwiki' );
00150 $db->lockTables( $read, $write, 'updateSearchIndex.php ' . __METHOD__ );
00151 }
00152
00157 private function unlockSearchindex( &$db ) {
00158 $db->unlockTables( 'updateSearchIndex.php ' . __METHOD__ );
00159 }
00160
00166 private function relockSearchindex( &$db ) {
00167 $this->unlockSearchindex( $db );
00168 $this->lockSearchindex( $db );
00169 }
00170 }
00171
00172 $maintClass = "UpdateSearchIndex";
00173 require_once( DO_MAINTENANCE );