summaryrefslogtreecommitdiff
path: root/maintenance/updateSearchIndex.inc
blob: bf2b8c37e6180c74acdb43b062ca2ca8141291e1 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
<?php
/**
 * @addtogroup Maintenance
 */

/** */
function updateSearchIndex( $start, $end, $maxLockTime, $quiet ) {
	global $wgQuiet;
	global $wgDisableSearchUpdate;

	$fname = "updateSearchIndex";

	$wgQuiet = $quiet;
	$wgDisableSearchUpdate = false;

	$dbw = wfGetDB( DB_MASTER );
	$recentchanges = $dbw->tableName( 'recentchanges' );

	output( "Updating searchindex between $start and $end\n" );

	# Select entries from recentchanges which are on top and between the specified times
	$start = $dbw->strencode( $start );
	$end = $dbw->strencode( $end );

	$page = $dbw->tableName( 'page' );
	$sql = "SELECT rc_cur_id,rc_type,rc_moved_to_ns,rc_moved_to_title FROM $recentchanges
	  JOIN $page ON rc_cur_id=page_id AND rc_this_oldid=page_latest
	  WHERE rc_timestamp BETWEEN '$start' AND '$end'
	  ";
	$res = $dbw->query( $sql, $fname );
	

	# Lock searchindex
	if ( $maxLockTime ) {
		output( "   --- Waiting for lock ---" );
		lockSearchindex( $dbw );
		$lockTime = time();
		output( "\n" );
	}

	# Loop through the results and do a search update
	while ( $row = $dbw->fetchObject( $res ) ) {
		# Allow reads to be processed
		if ( $maxLockTime && time() > $lockTime + $maxLockTime ) {
			output( "    --- Relocking ---" );
			relockSearchindex( $dbw );
			$lockTime = time();
			output( "\n" );
		}
		if ( $row->rc_type == RC_LOG ) {
			continue;
		} elseif ( $row->rc_type == RC_MOVE || $row->rc_type == RC_MOVE_OVER_REDIRECT ) {
			# Rename searchindex entry
			$titleObj = Title::makeTitle( $row->rc_moved_to_ns, $row->rc_moved_to_title );
			$title = $titleObj->getPrefixedDBkey();
			output( "$title..." );
			$u = new SearchUpdate( $row->rc_cur_id, $title, false );
			output( "\n" );
		} else {
			// Get current revision
			$rev = Revision::loadFromPageId( $dbw, $row->rc_cur_id );
			if( $rev ) {
				$titleObj = $rev->getTitle();
				$title = $titleObj->getPrefixedDBkey();
				output( $title );
				# Update searchindex
				$u = new SearchUpdate( $row->rc_cur_id, $titleObj->getText(), $rev->getText() );
				$u->doUpdate();
				output( "\n" );
			}
		}
	}

	# Unlock searchindex
	if ( $maxLockTime ) {
		output( "    --- Unlocking --" );
		unlockSearchindex( $dbw );
		output( "\n" );
	}
	output( "Done\n" );
}

function lockSearchindex( &$db ) {
	$write = array( 'searchindex' );
	$read = array( 'page', 'revision', 'text', 'interwiki' );
	$items = array();
	
	foreach( $write as $table ) {
		$items[] = $db->tableName( $table ) . ' LOW_PRIORITY WRITE';
	}
	foreach( $read as $table ) {
		$items[] = $db->tableName( $table ) . ' READ';
	}
	$sql = "LOCK TABLES " . implode( ',', $items );
	$db->query( $sql, 'updateSearchIndex.inc ' . __METHOD__ );
}

function unlockSearchindex( &$db ) {
	$db->query( "UNLOCK TABLES", 'updateSearchIndex.inc ' . __METHOD__ );
}

# Unlock and lock again
# Since the lock is low-priority, queued reads will be able to complete
function relockSearchindex( &$db ) {
	unlockSearchindex( $db );
	lockSearchindex( $db );
}

function output( $text ) {
	global $wgQuiet;
	if ( !$wgQuiet ) {
		print $text;
	}
}

?>