Periodic searchindex updates
authorTim Starling <tstarling@users.mediawiki.org>
Sat, 26 Jun 2004 03:27:08 +0000 (03:27 +0000)
committerTim Starling <tstarling@users.mediawiki.org>
Sat, 26 Jun 2004 03:27:08 +0000 (03:27 +0000)
maintenance/updateSearchIndex.inc [new file with mode: 0644]
maintenance/updateSearchIndex.php [new file with mode: 0644]

diff --git a/maintenance/updateSearchIndex.inc b/maintenance/updateSearchIndex.inc
new file mode 100644 (file)
index 0000000..1adeeb5
--- /dev/null
@@ -0,0 +1,77 @@
+<?php
+
+function updateSearchIndex( $start, $end, $maxLockTime, $quiet ) {
+       global $wgQuiet;
+       $wgQuiet = $quiet;
+       $fname = "updateSearchIndex";
+
+       output( "Updating searchindex between $start and $end\n" );
+
+       # Select entries from recentchanges which are on top and between the specified times
+       $start = wfStrencode( $start );
+       $end = wfStrencode( $end );
+
+       $sql = "SELECT rc_cur_id FROM recentchanges
+         WHERE rc_this_oldid=0 AND rc_timestamp BETWEEN '$start' AND '$end'";
+       $res = wfQuery( $sql, DB_READ, $fname );
+
+       # Lock searchindex
+       if ( $maxLockTime ) {
+               output( "Waiting for lock..." );
+               lockSearchindex();
+               $lockTime = time();
+               output( "done\n" );
+       }
+
+       # Loop through the results and do a search update
+       while ( $row = wfFetchObject( $res ) ) {
+               # Allow reads to be processed
+               if ( $maxLockTime && time() > $lockTime + $maxLockTime ) {
+                       output( "Relocking..." );
+                       relockSearchindex();
+                       $lockTime = time();
+                       output( "\n" );
+               }
+               # Get cur row
+               $curRow = wfGetArray( 'cur', array( 'cur_namespace', 'cur_title', 'cur_text' ), array( 'cur_id' => $row->rc_cur_id ) );
+               if ( $curRow ) {
+                       $titleObj = Title::makeTitle( $curRow->cur_namespace, $curRow->cur_title );
+                       $title = $titleObj->getPrefixedDBkey();
+                       output( "$title ..." );
+                       # Update searchindex
+                       $u = new SearchUpdate( $row->rc_cur_id, $curRow->cur_title, $curRow->cur_text );
+                       $u->doUpdate();
+                       output( "\n" );
+               }
+       }
+       
+       # Unlock searchindex
+       if ( $maxLockTime ) {
+               unlockSearchindex();
+       }
+       output( "Done\n" );
+}
+
+function lockSearchindex() {
+       wfQuery( "LOCK TABLES searchindex LOW_PRIORITY WRITE, cur READ", DB_WRITE );
+}
+
+function unlockSearchindex() {
+       wfQuery( "UNLOCK TABLES", DB_WRITE );
+}
+
+# Unlock and lock again
+# Since the lock is low-priority, queued reads will be able to complete
+function relockSearchindex() {
+       unlockSearchindex();
+       lockSearchindex();
+}
+
+function output( $text ) {
+       global $wgQuiet;
+       if ( !$wgQuiet ) {
+               print $text;
+       }
+}
+
+?>
diff --git a/maintenance/updateSearchIndex.php b/maintenance/updateSearchIndex.php
new file mode 100644 (file)
index 0000000..75b789e
--- /dev/null
@@ -0,0 +1,52 @@
+<?php
+
+# Script for periodic off-peak updating of the search index
+
+# Usage: php updateSearchIndex.php [-s START] [-e END] [-p POSFILE] [-l LOCKTIME] [-q]
+# Where START is the starting timestamp
+# END is the ending timestamp
+# POSFILE is a file to load timestamps from and save them to, searchUpdate.pos by default
+# LOCKTIME is how long the searchindex and cur tables will be locked for
+# -q means quiet
+
+$optionsWithArgs = array( 's', 'e', 'p' );
+
+require_once( 'commandLine.inc' );
+require_once( 'updateSearchIndex.inc' );
+
+if ( isset( $options['p'] ) ) {
+       $posFile = $options['p'];
+} else {
+       $posFile = 'searchUpdate.pos';
+}
+
+if ( isset( $options['e'] ) ) {
+       $end = $options['e'];
+} else {
+       $end = wfTimestampNow();
+}
+
+if ( isset( $options['s'] ) ) {
+       $start = $options['s'];
+} else {
+       $start = @file_get_contents( $posFile );
+       if ( !$start ) {
+               $start = wfUnix2Timestamp( time() - 86400 );
+       } 
+}
+
+if ( isset( $options['l'] ) ) {
+       $lockTime = $options['l'];
+} else {
+       $lockTime = 20;
+}
+
+$quiet = (bool)(@$options['q']);
+
+updateSearchIndex( $start, $end, $lockTime, $quiet );
+
+$file = fopen( $posFile, 'w' );
+fwrite( $file, $end );
+fclose( $file );
+
+?>