Revert r54244 which was stupid and fix this properly. Require commandLine.inc/Mainten...
[lhc/web/wiklou.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 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' );
53 } else {
54 $start = @file_get_contents( $posFile );
55 if ( !$start ) {
56 $start = wfTimestamp( TS_MW, time() - 86400 );
57 }
58 }
59 $lockTime = $this->getOption( 'l', 20 );
60
61 $this->doUpdateSearchIndex( $start, $end, $lockTime );
62 $file = fopen( $posFile, 'w' );
63 fwrite( $file, $end );
64 fclose( $file );
65 }
66
67 private function doUpdateSearchIndex( $start, $end, $maxLockTime ) {
68 global $wgDisableSearchUpdate;
69
70 $wgDisableSearchUpdate = false;
71
72 $dbw = wfGetDB( DB_MASTER );
73 $recentchanges = $dbw->tableName( 'recentchanges' );
74
75 $this->output( "Updating searchindex between $start and $end\n" );
76
77 # Select entries from recentchanges which are on top and between the specified times
78 $start = $dbw->timestamp( $start );
79 $end = $dbw->timestamp( $end );
80
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'
85 ";
86 $res = $dbw->query( $sql, __METHOD__ );
87
88
89 # Lock searchindex
90 if ( $maxLockTime ) {
91 $this->output( " --- Waiting for lock ---" );
92 $this->lockSearchindex( $dbw );
93 $lockTime = time();
94 $this->output( "\n" );
95 }
96
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 );
103 $lockTime = time();
104 $this->output( "\n" );
105 }
106 if ( $row->rc_type == RC_LOG ) {
107 continue;
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" );
115 } else {
116 // Get current revision
117 $rev = Revision::loadFromPageId( $dbw, $row->rc_cur_id );
118 if( $rev ) {
119 $titleObj = $rev->getTitle();
120 $title = $titleObj->getPrefixedDBkey();
121 $this->output( $title );
122 # Update searchindex
123 $u = new SearchUpdate( $row->rc_cur_id, $titleObj->getText(), $rev->getText() );
124 $u->doUpdate();
125 $this->output( "\n" );
126 }
127 }
128 }
129
130 # Unlock searchindex
131 if ( $maxLockTime ) {
132 $this->output( " --- Unlocking --" );
133 $this->unlockSearchindex( $dbw );
134 $this->output( "\n" );
135 }
136 $this->output( "Done\n" );
137 }
138
139 /**
140 * Lock the search index
141 * @param &$db Database object
142 */
143 private function lockSearchindex( &$db ) {
144 $write = array( 'searchindex' );
145 $read = array( 'page', 'revision', 'text', 'interwiki' );
146 $items = array();
147
148 foreach( $write as $table ) {
149 $items[] = $db->tableName( $table ) . ' LOW_PRIORITY WRITE';
150 }
151 foreach( $read as $table ) {
152 $items[] = $db->tableName( $table ) . ' READ';
153 }
154 $sql = "LOCK TABLES " . implode( ',', $items );
155 $db->query( $sql, 'updateSearchIndex.php ' . __METHOD__ );
156 }
157
158 /**
159 * Unlock the tables
160 * @param &$db Database object
161 */
162 private function unlockSearchindex( &$db ) {
163 $db->query( "UNLOCK TABLES", 'updateSearchIndex.php ' . __METHOD__ );
164 }
165
166 /**
167 * Unlock and lock again
168 * Since the lock is low-priority, queued reads will be able to complete
169 * @param &$db Database object
170 */
171 private function relockSearchindex( &$db ) {
172 $this->unlockSearchindex( $db );
173 $this->lockSearchindex( $db );
174 }
175 }
176
177 $maintClass = "UpdateSearchIndex";
178 require_once( DO_MAINTENANCE );