* (bug 5161) Don't try to load template list for nonexistent pages
[lhc/web/wiklou.git] / maintenance / updateSearchIndex.inc
1 <?php
2 /**
3 * @package MediaWiki
4 * @subpackage Maintenance
5 */
6
7 /** */
8 function updateSearchIndex( $start, $end, $maxLockTime, $quiet ) {
9 global $wgQuiet;
10 global $wgDisableSearchUpdate;
11
12 $fname = "updateSearchIndex";
13
14 $wgQuiet = $quiet;
15 $wgDisableSearchUpdate = false;
16
17 $dbw =& wfGetDB( DB_MASTER );
18 $recentchanges = $dbw->tableName( 'recentchanges' );
19
20 output( "Updating searchindex between $start and $end\n" );
21
22 # Select entries from recentchanges which are on top and between the specified times
23 $start = $dbw->strencode( $start );
24 $end = $dbw->strencode( $end );
25
26 $sql = "SELECT rc_cur_id,rc_type,rc_moved_to_ns,rc_moved_to_title FROM $recentchanges
27 WHERE rc_this_oldid=0 AND rc_timestamp BETWEEN '$start' AND '$end'";
28 $res = $dbw->query( $sql, $fname );
29
30 # Lock searchindex
31 if ( $maxLockTime ) {
32 output( " --- Waiting for lock ---" );
33 lockSearchindex();
34 $lockTime = time();
35 output( "\n" );
36 }
37
38 # Loop through the results and do a search update
39 while ( $row = $dbw->fetchObject( $res ) ) {
40 # Allow reads to be processed
41 if ( $maxLockTime && time() > $lockTime + $maxLockTime ) {
42 output( " --- Relocking ---" );
43 relockSearchindex();
44 $lockTime = time();
45 output( "\n" );
46 }
47 if ( $row->rc_type == RC_LOG ) {
48 continue;
49 } elseif ( $row->rc_type == RC_MOVE || $row->rc_type == RC_MOVE_OVER_REDIRECT ) {
50 # Rename searchindex entry
51 $titleObj = Title::makeTitle( $row->rc_moved_to_ns, $row->rc_moved_to_title );
52 $title = $titleObj->getPrefixedDBkey();
53 output( "$title..." );
54 $u = new SearchUpdate( $row->rc_cur_id, $title, false );
55 output( "\n" );
56 } else {
57 # Get cur row
58 $curRow = $dbw->selectRow( 'cur', array( 'cur_namespace', 'cur_title', 'cur_text' ),
59 array( 'cur_id' => $row->rc_cur_id ), $fname, 'FOR UPDATE' );
60 if ( $curRow ) {
61 $titleObj = Title::makeTitle( $curRow->cur_namespace, $curRow->cur_title );
62 $title = $titleObj->getPrefixedDBkey();
63 output( $title );
64 # Update searchindex
65 $u = new SearchUpdate( $row->rc_cur_id, $curRow->cur_title, $curRow->cur_text );
66 $u->doUpdate();
67 output( "\n" );
68 }
69 }
70 }
71
72 # Unlock searchindex
73 if ( $maxLockTime ) {
74 unlockSearchindex();
75 }
76 output( "Done\n" );
77 }
78
79 function lockSearchindex( &$db ) {
80 $dbw =& wfGetDB( DB_MASTER );
81 extract( $dbw->tableNames( 'searchindex', 'cur', 'interwiki' ) );
82 $dbw->query( "LOCK TABLES $searchindex LOW_PRIORITY WRITE, $cur READ, $interwiki READ" );
83 }
84
85 function unlockSearchindex() {
86 $dbw =& wfGetDB( DB_MASTER );
87 $dbw->query( "UNLOCK TABLES" );
88 }
89
90 # Unlock and lock again
91 # Since the lock is low-priority, queued reads will be able to complete
92 function relockSearchindex() {
93 unlockSearchindex();
94 lockSearchindex();
95 }
96
97 function output( $text ) {
98 global $wgQuiet;
99 if ( !$wgQuiet ) {
100 print $text;
101 }
102 }
103
104 ?>