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( dirname(__FILE__
) . '/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 protected function getDbType() {
44 return Maintenance
::DB_ADMIN
;
47 public function execute() {
48 $posFile = $this->getOption( 'p', 'searchUpdate.' . wfWikiId() . '.pos' );
49 $end = $this->getOption( 'e', wfTimestampNow() );
50 if ( $this->hasOption( 's' ) ) {
51 $start = $this->getOption('s');
52 } elseif( is_readable( 'searchUpdate.pos' ) ) {
53 # B/c to the old position file name which was hardcoded
54 # We can safely delete the file when we're done though.
55 $start = file_get_contents( 'searchUpdate.pos' );
56 unlink( 'searchUpdate.pos' );
58 $start = @file_get_contents
( $posFile );
60 $start = wfTimestamp( TS_MW
, time() - 86400 );
63 $lockTime = $this->getOption( 'l', 20 );
65 $this->doUpdateSearchIndex( $start, $end, $lockTime );
66 $file = fopen( $posFile, 'w' );
67 fwrite( $file, $end );
71 private function doUpdateSearchIndex( $start, $end, $maxLockTime ) {
72 global $wgDisableSearchUpdate;
74 $wgDisableSearchUpdate = false;
76 $dbw = wfGetDB( DB_MASTER
);
77 $recentchanges = $dbw->tableName( 'recentchanges' );
79 $this->output( "Updating searchindex between $start and $end\n" );
81 # Select entries from recentchanges which are on top and between the specified times
82 $start = $dbw->timestamp( $start );
83 $end = $dbw->timestamp( $end );
85 $page = $dbw->tableName( 'page' );
86 $sql = "SELECT rc_cur_id,rc_type,rc_moved_to_ns,rc_moved_to_title FROM $recentchanges
87 JOIN $page ON rc_cur_id=page_id AND rc_this_oldid=page_latest
88 WHERE rc_timestamp BETWEEN '$start' AND '$end'
90 $res = $dbw->query( $sql, __METHOD__
);
95 $this->output( " --- Waiting for lock ---" );
96 $this->lockSearchindex( $dbw );
98 $this->output( "\n" );
101 # Loop through the results and do a search update
102 foreach ( $res as $row ) {
103 # Allow reads to be processed
104 if ( $maxLockTime && time() > $lockTime +
$maxLockTime ) {
105 $this->output( " --- Relocking ---" );
106 $this->relockSearchindex( $dbw );
108 $this->output( "\n" );
110 if ( $row->rc_type
== RC_LOG
) {
112 } elseif ( $row->rc_type
== RC_MOVE ||
$row->rc_type
== RC_MOVE_OVER_REDIRECT
) {
113 # Rename searchindex entry
114 $titleObj = Title
::makeTitle( $row->rc_moved_to_ns
, $row->rc_moved_to_title
);
115 $title = $titleObj->getPrefixedDBkey();
116 $this->output( "$title..." );
117 $u = new SearchUpdate( $row->rc_cur_id
, $title, false );
118 $this->output( "\n" );
120 // Get current revision
121 $rev = Revision
::loadFromPageId( $dbw, $row->rc_cur_id
);
123 $titleObj = $rev->getTitle();
124 $title = $titleObj->getPrefixedDBkey();
125 $this->output( $title );
127 $u = new SearchUpdate( $row->rc_cur_id
, $titleObj->getText(), $rev->getText() );
129 $this->output( "\n" );
135 if ( $maxLockTime ) {
136 $this->output( " --- Unlocking --" );
137 $this->unlockSearchindex( $dbw );
138 $this->output( "\n" );
140 $this->output( "Done\n" );
144 * Lock the search index
145 * @param &$db Database object
147 private function lockSearchindex( &$db ) {
148 $write = array( 'searchindex' );
149 $read = array( 'page', 'revision', 'text', 'interwiki' );
150 $db->lockTables( $read, $write, 'updateSearchIndex.php ' . __METHOD__
);
155 * @param &$db Database object
157 private function unlockSearchindex( &$db ) {
158 $db->unlockTables( 'updateSearchIndex.php ' . __METHOD__
);
162 * Unlock and lock again
163 * Since the lock is low-priority, queued reads will be able to complete
164 * @param &$db Database object
166 private function relockSearchindex( &$db ) {
167 $this->unlockSearchindex( $db );
168 $this->lockSearchindex( $db );
172 $maintClass = "UpdateSearchIndex";
173 require_once( DO_MAINTENANCE
);