3 * Script for periodic off-peak updating of the search index
5 * Usage: php updateSearchIndex.php [-s START] [-e END] [-p POSFILE] [-l LOCKTIME] [-q]
6 * Where START is the starting timestamp
7 * END is the ending timestamp
8 * POSFILE is a file to load timestamps from and save them to, searchUpdate.WIKI_ID.pos by default
9 * LOCKTIME is how long the searchindex and revision tables will be locked for
12 * This program is free software; you can redistribute it and/or modify
13 * it under the terms of the GNU General Public License as published by
14 * the Free Software Foundation; either version 2 of the License, or
15 * (at your option) any later version.
17 * This program is distributed in the hope that it will be useful,
18 * but WITHOUT ANY WARRANTY; without even the implied warranty of
19 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
20 * GNU General Public License for more details.
22 * You should have received a copy of the GNU General Public License along
23 * with this program; if not, write to the Free Software Foundation, Inc.,
24 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
25 * http://www.gnu.org/copyleft/gpl.html
27 * @ingroup Maintenance
30 require_once( "Maintenance.php" );
32 class UpdateSearchIndex
extends Maintenance
{
34 public function __construct() {
35 parent
::__construct();
36 $this->mDescription
= "Script for periodic off-peak updating of the search index";
37 $this->addOption( 's', 'starting timestamp', false, true );
38 $this->addOption( 'e', 'Ending timestamp', false, true );
39 $this->addOption( 'p', 'File for saving/loading timestamps, searchUpdate.WIKI_ID.pos by default', false, true );
40 $this->addOption( 'l', 'How long the searchindex and revision tables will be locked for', false, true );
43 public function execute() {
44 $posFile = $this->getOption( 'p', 'searchUpdate.' . wfWikiId() . '.pos' );
45 $end = $this->getOption( 'e', wfTimestampNow() );
46 if ( $this->hasOption( 's' ) ) {
47 $start = $this->getOption('s');
48 } elseif( is_readable( 'searchUpdate.pos' ) ) {
49 # B/c to the old position file name which was hardcoded
50 # We can safely delete the file when we're done though.
51 $start = file_get_contents( 'searchUpdate.pos' );
52 unlink( 'searchUpdate.pos' );
54 $start = @file_get_contents
( $posFile );
56 $start = wfTimestamp( TS_MW
, time() - 86400 );
59 $lockTime = $this->getOption( 'l', 20 );
61 $this->doUpdateSearchIndex( $start, $end, $lockTime );
62 $file = fopen( $posFile, 'w' );
63 fwrite( $file, $end );
67 private function doUpdateSearchIndex( $start, $end, $maxLockTime ) {
68 global $wgDisableSearchUpdate;
70 $wgDisableSearchUpdate = false;
72 $dbw = wfGetDB( DB_MASTER
);
73 $recentchanges = $dbw->tableName( 'recentchanges' );
75 $this->output( "Updating searchindex between $start and $end\n" );
77 # Select entries from recentchanges which are on top and between the specified times
78 $start = $dbw->timestamp( $start );
79 $end = $dbw->timestamp( $end );
81 $page = $dbw->tableName( 'page' );
82 $sql = "SELECT rc_cur_id,rc_type,rc_moved_to_ns,rc_moved_to_title FROM $recentchanges
83 JOIN $page ON rc_cur_id=page_id AND rc_this_oldid=page_latest
84 WHERE rc_timestamp BETWEEN '$start' AND '$end'
86 $res = $dbw->query( $sql, __METHOD__
);
91 $this->output( " --- Waiting for lock ---" );
92 $this->lockSearchindex( $dbw );
94 $this->output( "\n" );
97 # Loop through the results and do a search update
98 while ( $row = $dbw->fetchObject( $res ) ) {
99 # Allow reads to be processed
100 if ( $maxLockTime && time() > $lockTime +
$maxLockTime ) {
101 $this->output( " --- Relocking ---" );
102 $this->relockSearchindex( $dbw );
104 $this->output( "\n" );
106 if ( $row->rc_type
== RC_LOG
) {
108 } elseif ( $row->rc_type
== RC_MOVE ||
$row->rc_type
== RC_MOVE_OVER_REDIRECT
) {
109 # Rename searchindex entry
110 $titleObj = Title
::makeTitle( $row->rc_moved_to_ns
, $row->rc_moved_to_title
);
111 $title = $titleObj->getPrefixedDBkey();
112 $this->output( "$title..." );
113 $u = new SearchUpdate( $row->rc_cur_id
, $title, false );
114 $this->output( "\n" );
116 // Get current revision
117 $rev = Revision
::loadFromPageId( $dbw, $row->rc_cur_id
);
119 $titleObj = $rev->getTitle();
120 $title = $titleObj->getPrefixedDBkey();
121 $this->output( $title );
123 $u = new SearchUpdate( $row->rc_cur_id
, $titleObj->getText(), $rev->getText() );
125 $this->output( "\n" );
131 if ( $maxLockTime ) {
132 $this->output( " --- Unlocking --" );
133 $this->unlockSearchindex( $dbw );
134 $this->output( "\n" );
136 $this->output( "Done\n" );
140 * Lock the search index
141 * @param &$db Database object
143 private function lockSearchindex( &$db ) {
144 $write = array( 'searchindex' );
145 $read = array( 'page', 'revision', 'text', 'interwiki' );
148 foreach( $write as $table ) {
149 $items[] = $db->tableName( $table ) . ' LOW_PRIORITY WRITE';
151 foreach( $read as $table ) {
152 $items[] = $db->tableName( $table ) . ' READ';
154 $sql = "LOCK TABLES " . implode( ',', $items );
155 $db->query( $sql, 'updateSearchIndex.php ' . __METHOD__
);
160 * @param &$db Database object
162 private function unlockSearchindex( &$db ) {
163 $db->query( "UNLOCK TABLES", 'updateSearchIndex.php ' . __METHOD__
);
167 * Unlock and lock again
168 * Since the lock is low-priority, queued reads will be able to complete
169 * @param &$db Database object
171 private function relockSearchindex( &$db ) {
172 $this->unlockSearchindex( $db );
173 $this->lockSearchindex( $db );
177 $maintClass = "UpdateSearchIndex";
178 require_once( DO_MAINTENANCE
);