X-Git-Url: https://git.heureux-cyclage.org/?a=blobdiff_plain;f=maintenance%2FupdateSearchIndex.php;h=eed3571c2b399ab300e0445125fd09a5b6280e0e;hb=9ef2ebad9d7ed1361a84f13ba0c9b9f5e440ff13;hp=6172fa4dd831dcd0ab4ec9cee81fcf8ea4c070ec;hpb=a82c01a4b2e13dcbb0bfb7fef3b089d01420a1cb;p=lhc%2Fweb%2Fwiklou.git diff --git a/maintenance/updateSearchIndex.php b/maintenance/updateSearchIndex.php index 6172fa4dd8..eed3571c2b 100644 --- a/maintenance/updateSearchIndex.php +++ b/maintenance/updateSearchIndex.php @@ -24,10 +24,11 @@ * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. * http://www.gnu.org/copyleft/gpl.html * + * @file * @ingroup Maintenance */ - -require_once( dirname(__FILE__) . '/Maintenance.php' ); + +require_once( dirname( __FILE__ ) . '/Maintenance.php' ); class UpdateSearchIndex extends Maintenance { @@ -40,7 +41,7 @@ class UpdateSearchIndex extends Maintenance { $this->addOption( 'l', 'How long the searchindex and revision tables will be locked for', false, true ); } - protected function getDbType() { + public function getDbType() { return Maintenance::DB_ADMIN; } @@ -48,26 +49,33 @@ class UpdateSearchIndex extends Maintenance { $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' ) ) { + $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' ); + } elseif( is_readable( $posFile ) ) { + $start = file_get_contents( $posFile ); } else { - $start = @file_get_contents( $posFile ); - if ( !$start ) { - $start = wfTimestamp( TS_MW, time() - 86400 ); - } + $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 ); + if ( is_writable( dirname( realpath( $posFile ) ) ) ) { + $file = fopen( $posFile, 'w' ); + if ( $file !== false ) { + fwrite( $file, $end ); + fclose( $file ); + } else { + $this->error( "*** Couldn't write to the $posFile!\n" ); + } + } else { + $this->error( "*** Couldn't write to the $posFile!\n" ); + } } - + private function doUpdateSearchIndex( $start, $end, $maxLockTime ) { global $wgDisableSearchUpdate; @@ -89,94 +97,25 @@ class UpdateSearchIndex extends Maintenance { "; $res = $dbw->query( $sql, __METHOD__ ); + $this->updateSearchIndex( $maxLockTime, array( $this, 'searchIndexUpdateCallback' ), $dbw, $res ); - # Lock searchindex - if ( $maxLockTime ) { - $this->output( " --- Waiting for lock ---" ); - $this->lockSearchindex( $dbw ); - $lockTime = time(); - $this->output( "\n" ); - } - - # Loop through the results and do a search update - while ( $row = $dbw->fetchObject( $res ) ) { - # 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' ); - $items = array(); - - foreach( $write as $table ) { - $items[] = $db->tableName( $table ) . ' LOW_PRIORITY WRITE'; - } - foreach( $read as $table ) { - $items[] = $db->tableName( $table ) . ' READ'; + public function searchIndexUpdateCallback( $dbw, $row ) { + if ( $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 ); + $u->doUpdate(); + $this->output( "\n" ); + } elseif ( $row->rc_type !== RC_LOG ) { + $this->updateSearchIndexForPage( $dbw, $row->rc_cur_id ); } - $sql = "LOCK TABLES " . implode( ',', $items ); - $db->query( $sql, 'updateSearchIndex.php ' . __METHOD__ ); - } - - /** - * Unlock the tables - * @param &$db Database object - */ - private function unlockSearchindex( &$db ) { - $db->query( "UNLOCK TABLES", '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 ); +require_once( RUN_MAINTENANCE_IF_MAIN );