Added description for two new tables
[mediawiki.git] / maintenance / updateSearchIndex.inc
blobed01575c32b496c00787447bc109a4ddccf78747
1 <?php
2 /**
3  * @package MediaWiki
4  * @subpackage Maintenance
5  */
7 /** */
8 function updateSearchIndex( $start, $end, $maxLockTime, $quiet ) {
9         global $wgQuiet;
10         global $wgDisableSearchUpdate;
12         $fname = "updateSearchIndex";
14         $wgQuiet = $quiet;
15         $wgDisableSearchUpdate = false;
17         $dbw =& wfGetDB( DB_MASTER );
18         $recentchanges = $dbw->tableName( 'recentchanges' );
20         output( "Updating searchindex between $start and $end\n" );
22         # Select entries from recentchanges which are on top and between the specified times
23         $start = $dbw->strencode( $start );
24         $end = $dbw->strencode( $end );
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         
34         # Lock searchindex
35         if ( $maxLockTime ) {
36                 output( "   --- Waiting for lock ---" );
37                 lockSearchindex( $dbw );
38                 $lockTime = time();
39                 output( "\n" );
40         }
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         }
75         # Unlock searchindex
76         if ( $maxLockTime ) {
77                 unlockSearchindex( $dbw );
78         }
79         output( "Done\n" );
82 function lockSearchindex( &$db ) {
83         $write = array( 'searchindex' );
84         $read = array( 'page', 'revision', 'text', 'interwiki' );
85         $items = array();
86         
87         foreach( $write as $table ) {
88                 $items[] = $db->tableName( $table ) . ' LOW_PRIORITY WRITE';
89         }
90         foreach( $read as $table ) {
91                 $items[] = $db->tableName( $table ) . ' READ';
92         }
93         $sql = "LOCK TABLES " . implode( ',', $items );
94         $db->query( $sql );
97 function unlockSearchindex( &$db ) {
98         $db->query( "UNLOCK TABLES" );
101 # Unlock and lock again
102 # Since the lock is low-priority, queued reads will be able to complete
103 function relockSearchindex( &$db ) {
104         unlockSearchindex( $db );
105         lockSearchindex( $db );
108 function output( $text ) {
109         global $wgQuiet;
110         if ( !$wgQuiet ) {
111                 print $text;
112         }