X-Git-Url: https://git.heureux-cyclage.org/?a=blobdiff_plain;f=maintenance%2FupdateSearchIndex.php;h=6bb290ff1d25a8d8d71c99e53784d19de65c03f5;hb=5b7f1f3b592e34853484ba4748e2067a73218799;hp=75b789e785fa2cfd00bef09ac9299e485b6d8e95;hpb=72652c4bc72e7043f3bf12b71abc959510132b5d;p=lhc%2Fweb%2Fwiklou.git diff --git a/maintenance/updateSearchIndex.php b/maintenance/updateSearchIndex.php index 75b789e785..6bb290ff1d 100644 --- a/maintenance/updateSearchIndex.php +++ b/maintenance/updateSearchIndex.php @@ -1,52 +1,173 @@ mDescription = "Script for periodic off-peak updating of the search index"; + $this->addOption( 's', 'starting timestamp', false, true ); + $this->addOption( 'e', 'Ending timestamp', false, true ); + $this->addOption( 'p', 'File for saving/loading timestamps, searchUpdate.WIKI_ID.pos by default', false, true ); + $this->addOption( 'l', 'How long the searchindex and revision tables will be locked for', false, true ); + } -$optionsWithArgs = array( 's', 'e', 'p' ); + protected function getDbType() { + return Maintenance::DB_ADMIN; + } -require_once( 'commandLine.inc' ); -require_once( 'updateSearchIndex.inc' ); + public function execute() { + $posFile = $this->getOption( 'p', 'searchUpdate.' . wfWikiId() . '.pos' ); + $end = $this->getOption( 'e', wfTimestampNow() ); + if ( $this->hasOption( 's' ) ) { + $start = $this->getOption('s'); + } elseif( is_readable( 'searchUpdate.pos' ) ) { + # B/c to the old position file name which was hardcoded + # We can safely delete the file when we're done though. + $start = file_get_contents( 'searchUpdate.pos' ); + unlink( 'searchUpdate.pos' ); + } else { + $start = @file_get_contents( $posFile ); + if ( !$start ) { + $start = wfTimestamp( TS_MW, time() - 86400 ); + } + } + $lockTime = $this->getOption( 'l', 20 ); + + $this->doUpdateSearchIndex( $start, $end, $lockTime ); + $file = fopen( $posFile, 'w' ); + fwrite( $file, $end ); + fclose( $file ); + } + + private function doUpdateSearchIndex( $start, $end, $maxLockTime ) { + global $wgDisableSearchUpdate; -if ( isset( $options['p'] ) ) { - $posFile = $options['p']; -} else { - $posFile = 'searchUpdate.pos'; -} + $wgDisableSearchUpdate = false; -if ( isset( $options['e'] ) ) { - $end = $options['e']; -} else { - $end = wfTimestampNow(); -} + $dbw = wfGetDB( DB_MASTER ); + $recentchanges = $dbw->tableName( 'recentchanges' ); -if ( isset( $options['s'] ) ) { - $start = $options['s']; -} else { - $start = @file_get_contents( $posFile ); - if ( !$start ) { - $start = wfUnix2Timestamp( time() - 86400 ); - } -} + $this->output( "Updating searchindex between $start and $end\n" ); -if ( isset( $options['l'] ) ) { - $lockTime = $options['l']; -} else { - $lockTime = 20; -} + # Select entries from recentchanges which are on top and between the specified times + $start = $dbw->timestamp( $start ); + $end = $dbw->timestamp( $end ); + + $page = $dbw->tableName( 'page' ); + $sql = "SELECT rc_cur_id,rc_type,rc_moved_to_ns,rc_moved_to_title FROM $recentchanges + JOIN $page ON rc_cur_id=page_id AND rc_this_oldid=page_latest + WHERE rc_timestamp BETWEEN '$start' AND '$end' + "; + $res = $dbw->query( $sql, __METHOD__ ); -$quiet = (bool)(@$options['q']); -updateSearchIndex( $start, $end, $lockTime, $quiet ); + # Lock searchindex + if ( $maxLockTime ) { + $this->output( " --- Waiting for lock ---" ); + $this->lockSearchindex( $dbw ); + $lockTime = time(); + $this->output( "\n" ); + } -$file = fopen( $posFile, 'w' ); -fwrite( $file, $end ); -fclose( $file ); + # Loop through the results and do a search update + foreach ( $res as $row ) { + # Allow reads to be processed + if ( $maxLockTime && time() > $lockTime + $maxLockTime ) { + $this->output( " --- Relocking ---" ); + $this->relockSearchindex( $dbw ); + $lockTime = time(); + $this->output( "\n" ); + } + if ( $row->rc_type == RC_LOG ) { + continue; + } elseif ( $row->rc_type == RC_MOVE || $row->rc_type == RC_MOVE_OVER_REDIRECT ) { + # Rename searchindex entry + $titleObj = Title::makeTitle( $row->rc_moved_to_ns, $row->rc_moved_to_title ); + $title = $titleObj->getPrefixedDBkey(); + $this->output( "$title..." ); + $u = new SearchUpdate( $row->rc_cur_id, $title, false ); + $this->output( "\n" ); + } else { + // Get current revision + $rev = Revision::loadFromPageId( $dbw, $row->rc_cur_id ); + if( $rev ) { + $titleObj = $rev->getTitle(); + $title = $titleObj->getPrefixedDBkey(); + $this->output( $title ); + # Update searchindex + $u = new SearchUpdate( $row->rc_cur_id, $titleObj->getText(), $rev->getText() ); + $u->doUpdate(); + $this->output( "\n" ); + } + } + } + + # Unlock searchindex + if ( $maxLockTime ) { + $this->output( " --- Unlocking --" ); + $this->unlockSearchindex( $dbw ); + $this->output( "\n" ); + } + $this->output( "Done\n" ); + } + + /** + * Lock the search index + * @param &$db Database object + */ + private function lockSearchindex( &$db ) { + $write = array( 'searchindex' ); + $read = array( 'page', 'revision', 'text', 'interwiki' ); + $db->lockTables( $read, $write, 'updateSearchIndex.php ' . __METHOD__ ); + } + + /** + * Unlock the tables + * @param &$db Database object + */ + private function unlockSearchindex( &$db ) { + $db->unlockTables( 'updateSearchIndex.php ' . __METHOD__ ); + } + + /** + * Unlock and lock again + * Since the lock is low-priority, queued reads will be able to complete + * @param &$db Database object + */ + private function relockSearchindex( &$db ) { + $this->unlockSearchindex( $db ); + $this->lockSearchindex( $db ); + } +} -?> +$maintClass = "UpdateSearchIndex"; +require_once( DO_MAINTENANCE );