]> scripts.mit.edu Git - autoinstalls/mediawiki.git/blob - maintenance/updateSearchIndex.php
MediaWiki 1.16.0
[autoinstalls/mediawiki.git] / maintenance / updateSearchIndex.php
1 <?php
2 /**
3  * Script for periodic off-peak updating of the search index
4  *
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
10  * -q means quiet
11  *
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.
16  *
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.
21  *
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
26  *
27  * @ingroup Maintenance
28  */
29  
30 require_once( dirname(__FILE__) . '/Maintenance.php' );
31
32 class UpdateSearchIndex extends Maintenance {
33
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 );
41         }
42
43         public function getDbType() {
44                 return Maintenance::DB_ADMIN;
45         }
46
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' );
57                 } else {
58                         $start = @file_get_contents( $posFile );
59                         if ( !$start ) {
60                                 $start = wfTimestamp( TS_MW, time() - 86400 );
61                         }
62                 }
63                 $lockTime = $this->getOption( 'l', 20 );
64                 
65                 $this->doUpdateSearchIndex( $start, $end, $lockTime );
66                 $file = fopen( $posFile, 'w' );
67                 fwrite( $file, $end );
68                 fclose( $file );
69         }
70         
71         private function doUpdateSearchIndex( $start, $end, $maxLockTime ) {
72                 global $wgDisableSearchUpdate;
73
74                 $wgDisableSearchUpdate = false;
75
76                 $dbw = wfGetDB( DB_MASTER );
77                 $recentchanges = $dbw->tableName( 'recentchanges' );
78
79                 $this->output( "Updating searchindex between $start and $end\n" );
80
81                 # Select entries from recentchanges which are on top and between the specified times
82                 $start = $dbw->timestamp( $start );
83                 $end = $dbw->timestamp( $end );
84
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'
89                   ";
90                 $res = $dbw->query( $sql, __METHOD__ );
91
92
93                 # Lock searchindex
94                 if ( $maxLockTime ) {
95                         $this->output( "   --- Waiting for lock ---" );
96                         $this->lockSearchindex( $dbw );
97                         $lockTime = time();
98                         $this->output( "\n" );
99                 }
100
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 );
107                                 $lockTime = time();
108                                 $this->output( "\n" );
109                         }
110                         if ( $row->rc_type == RC_LOG ) {
111                                 continue;
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" );
119                         } else {
120                                 // Get current revision
121                                 $rev = Revision::loadFromPageId( $dbw, $row->rc_cur_id );
122                                 if( $rev ) {
123                                         $titleObj = $rev->getTitle();
124                                         $title = $titleObj->getPrefixedDBkey();
125                                         $this->output( $title );
126                                         # Update searchindex
127                                         $u = new SearchUpdate( $row->rc_cur_id, $titleObj->getText(), $rev->getText() );
128                                         $u->doUpdate();
129                                         $this->output( "\n" );
130                                 }
131                         }
132                 }
133
134                 # Unlock searchindex
135                 if ( $maxLockTime ) {
136                         $this->output( "    --- Unlocking --" );
137                         $this->unlockSearchindex( $dbw );
138                         $this->output( "\n" );
139                 }
140                 $this->output( "Done\n" );
141         }
142
143         /**
144          * Lock the search index
145          * @param &$db Database object
146          */
147         private function lockSearchindex( &$db ) {
148                 $write = array( 'searchindex' );
149                 $read = array( 'page', 'revision', 'text', 'interwiki' );
150                 $db->lockTables( $read, $write, 'updateSearchIndex.php ' . __METHOD__ );
151         }
152
153         /**
154          * Unlock the tables
155          * @param &$db Database object
156          */
157         private function unlockSearchindex( &$db ) {
158                 $db->unlockTables( 'updateSearchIndex.php ' . __METHOD__ );
159         }
160         
161         /**
162          * Unlock and lock again
163          * Since the lock is low-priority, queued reads will be able to complete
164          * @param &$db Database object
165          */
166         private function relockSearchindex( &$db ) {
167                 $this->unlockSearchindex( $db );
168                 $this->lockSearchindex( $db );
169         }
170 }
171
172 $maintClass = "UpdateSearchIndex";
173 require_once( DO_MAINTENANCE );