Bump $wgStyleVersion to 155.
[lhc/web/wiklou.git] / maintenance / updateSearchIndex.inc
1 <?php
2 /**
3 * @file
4 * @ingroup 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 $page = $dbw->tableName( 'page' );
27 $sql = "SELECT rc_cur_id,rc_type,rc_moved_to_ns,rc_moved_to_title FROM $recentchanges
28 JOIN $page ON rc_cur_id=page_id AND rc_this_oldid=page_latest
29 WHERE rc_timestamp BETWEEN '$start' AND '$end'
30 ";
31 $res = $dbw->query( $sql, $fname );
32
33
34 # Lock searchindex
35 if ( $maxLockTime ) {
36 output( " --- Waiting for lock ---" );
37 lockSearchindex( $dbw );
38 $lockTime = time();
39 output( "\n" );
40 }
41
42 # Loop through the results and do a search update
43 while ( $row = $dbw->fetchObject( $res ) ) {
44 # Allow reads to be processed
45 if ( $maxLockTime && time() > $lockTime + $maxLockTime ) {
46 output( " --- Relocking ---" );
47 relockSearchindex( $dbw );
48 $lockTime = time();
49 output( "\n" );
50 }
51 if ( $row->rc_type == RC_LOG ) {
52 continue;
53 } elseif ( $row->rc_type == RC_MOVE || $row->rc_type == RC_MOVE_OVER_REDIRECT ) {
54 # Rename searchindex entry
55 $titleObj = Title::makeTitle( $row->rc_moved_to_ns, $row->rc_moved_to_title );
56 $title = $titleObj->getPrefixedDBkey();
57 output( "$title..." );
58 $u = new SearchUpdate( $row->rc_cur_id, $title, false );
59 output( "\n" );
60 } else {
61 // Get current revision
62 $rev = Revision::loadFromPageId( $dbw, $row->rc_cur_id );
63 if( $rev ) {
64 $titleObj = $rev->getTitle();
65 $title = $titleObj->getPrefixedDBkey();
66 output( $title );
67 # Update searchindex
68 $u = new SearchUpdate( $row->rc_cur_id, $titleObj->getText(), $rev->getText() );
69 $u->doUpdate();
70 output( "\n" );
71 }
72 }
73 }
74
75 # Unlock searchindex
76 if ( $maxLockTime ) {
77 output( " --- Unlocking --" );
78 unlockSearchindex( $dbw );
79 output( "\n" );
80 }
81 output( "Done\n" );
82 }
83
84 function lockSearchindex( &$db ) {
85 $write = array( 'searchindex' );
86 $read = array( 'page', 'revision', 'text', 'interwiki' );
87 $items = array();
88
89 foreach( $write as $table ) {
90 $items[] = $db->tableName( $table ) . ' LOW_PRIORITY WRITE';
91 }
92 foreach( $read as $table ) {
93 $items[] = $db->tableName( $table ) . ' READ';
94 }
95 $sql = "LOCK TABLES " . implode( ',', $items );
96 $db->query( $sql, 'updateSearchIndex.inc ' . __METHOD__ );
97 }
98
99 function unlockSearchindex( &$db ) {
100 $db->query( "UNLOCK TABLES", 'updateSearchIndex.inc ' . __METHOD__ );
101 }
102
103 # Unlock and lock again
104 # Since the lock is low-priority, queued reads will be able to complete
105 function relockSearchindex( &$db ) {
106 unlockSearchindex( $db );
107 lockSearchindex( $db );
108 }
109
110 function output( $text ) {
111 global $wgQuiet;
112 if ( !$wgQuiet ) {
113 print $text;
114 }
115 }