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