00001 <?php
00008 function updateSearchIndex( $start, $end, $maxLockTime, $quiet ) {
00009 global $wgQuiet;
00010 global $wgDisableSearchUpdate;
00011
00012 $fname = "updateSearchIndex";
00013
00014 $wgQuiet = $quiet;
00015 $wgDisableSearchUpdate = false;
00016
00017 $dbw = wfGetDB( DB_MASTER );
00018 $recentchanges = $dbw->tableName( 'recentchanges' );
00019
00020 output( "Updating searchindex between $start and $end\n" );
00021
00022 # Select entries from recentchanges which are on top and between the specified times
00023 $start = $dbw->strencode( $start );
00024 $end = $dbw->strencode( $end );
00025
00026 $page = $dbw->tableName( 'page' );
00027 $sql = "SELECT rc_cur_id,rc_type,rc_moved_to_ns,rc_moved_to_title FROM $recentchanges
00028 JOIN $page ON rc_cur_id=page_id AND rc_this_oldid=page_latest
00029 WHERE rc_timestamp BETWEEN '$start' AND '$end'
00030 ";
00031 $res = $dbw->query( $sql, $fname );
00032
00033
00034 # Lock searchindex
00035 if ( $maxLockTime ) {
00036 output( " --- Waiting for lock ---" );
00037 lockSearchindex( $dbw );
00038 $lockTime = time();
00039 output( "\n" );
00040 }
00041
00042 # Loop through the results and do a search update
00043 while ( $row = $dbw->fetchObject( $res ) ) {
00044 # Allow reads to be processed
00045 if ( $maxLockTime && time() > $lockTime + $maxLockTime ) {
00046 output( " --- Relocking ---" );
00047 relockSearchindex( $dbw );
00048 $lockTime = time();
00049 output( "\n" );
00050 }
00051 if ( $row->rc_type == RC_LOG ) {
00052 continue;
00053 } elseif ( $row->rc_type == RC_MOVE || $row->rc_type == RC_MOVE_OVER_REDIRECT ) {
00054 # Rename searchindex entry
00055 $titleObj = Title::makeTitle( $row->rc_moved_to_ns, $row->rc_moved_to_title );
00056 $title = $titleObj->getPrefixedDBkey();
00057 output( "$title..." );
00058 $u = new SearchUpdate( $row->rc_cur_id, $title, false );
00059 output( "\n" );
00060 } else {
00061
00062 $rev = Revision::loadFromPageId( $dbw, $row->rc_cur_id );
00063 if( $rev ) {
00064 $titleObj = $rev->getTitle();
00065 $title = $titleObj->getPrefixedDBkey();
00066 output( $title );
00067 # Update searchindex
00068 $u = new SearchUpdate( $row->rc_cur_id, $titleObj->getText(), $rev->getText() );
00069 $u->doUpdate();
00070 output( "\n" );
00071 }
00072 }
00073 }
00074
00075 # Unlock searchindex
00076 if ( $maxLockTime ) {
00077 output( " --- Unlocking --" );
00078 unlockSearchindex( $dbw );
00079 output( "\n" );
00080 }
00081 output( "Done\n" );
00082 }
00083
00084 function lockSearchindex( &$db ) {
00085 $write = array( 'searchindex' );
00086 $read = array( 'page', 'revision', 'text', 'interwiki' );
00087 $items = array();
00088
00089 foreach( $write as $table ) {
00090 $items[] = $db->tableName( $table ) . ' LOW_PRIORITY WRITE';
00091 }
00092 foreach( $read as $table ) {
00093 $items[] = $db->tableName( $table ) . ' READ';
00094 }
00095 $sql = "LOCK TABLES " . implode( ',', $items );
00096 $db->query( $sql, 'updateSearchIndex.inc ' . __METHOD__ );
00097 }
00098
00099 function unlockSearchindex( &$db ) {
00100 $db->query( "UNLOCK TABLES", 'updateSearchIndex.inc ' . __METHOD__ );
00101 }
00102
00103 # Unlock and lock again
00104 # Since the lock is low-priority, queued reads will be able to complete
00105 function relockSearchindex( &$db ) {
00106 unlockSearchindex( $db );
00107 lockSearchindex( $db );
00108 }
00109
00110 function output( $text ) {
00111 global $wgQuiet;
00112 if ( !$wgQuiet ) {
00113 print $text;
00114 }
00115 }