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