3 * Rebuild recent changes table.
10 /** Public entry; more passes might come in! :) */
11 function rebuildRecentChangesTable() {
12 rebuildRecentChangesTablePass1();
13 rebuildRecentChangesTablePass2();
14 rebuildRecentChangesTablePass3();
15 rebuildRecentChangesTablePass4();
19 function rebuildRecentChangesTablePass1()
21 $dbw = wfGetDB( DB_MASTER );
23 $dbw->delete( 'recentchanges', '*' );
25 print( "Loading from page and revision tables...\n" );
29 print( '$wgRCMaxAge=' . $wgRCMaxAge );
30 $days = $wgRCMaxAge / 24 / 3600;
31 if ( intval($days) == $days ) {
32 print( " (" . $days . " days)\n" );
34 print( " (approx. " . intval($days) . " days)\n" );
37 $cutoff = time() - $wgRCMaxAge;
38 $dbw->insertSelect( 'recentchanges', array( 'page', 'revision' ),
40 'rc_timestamp' => 'rev_timestamp',
41 'rc_cur_time' => 'rev_timestamp',
42 'rc_user' => 'rev_user',
43 'rc_user_text' => 'rev_user_text',
44 'rc_namespace' => 'page_namespace',
45 'rc_title' => 'page_title',
46 'rc_comment' => 'rev_comment',
47 'rc_minor' => 'rev_minor_edit',
49 'rc_new' => 'page_is_new',
50 'rc_cur_id' => 'page_id',
51 'rc_this_oldid' => 'rev_id',
52 'rc_last_oldid' => 0, // is this ok?
53 'rc_type' => $dbw->conditional( 'page_is_new != 0', RC_NEW, RC_EDIT ),
54 'rc_deleted' => 'rev_deleted'
56 'rev_timestamp > ' . $dbw->addQuotes( $dbw->timestamp( $cutoff ) ),
59 array(), // INSERT options
60 array( 'ORDER BY' => 'rev_timestamp DESC', 'LIMIT' => 5000 ) // SELECT options
64 function rebuildRecentChangesTablePass2()
66 $dbw = wfGetDB( DB_MASTER );
67 list ($recentchanges, $revision) = $dbw->tableNamesN( 'recentchanges', 'revision' );
69 print( "Updating links and size differences...\n" );
71 # Fill in the rc_last_oldid field, which points to the previous edit
72 $sql = "SELECT rc_cur_id,rc_this_oldid,rc_timestamp FROM $recentchanges " .
73 "ORDER BY rc_cur_id,rc_timestamp";
74 $res = $dbw->query( $sql, DB_MASTER );
78 while ( $obj = $dbw->fetchObject( $res ) ) {
80 if( $obj->rc_cur_id != $lastCurId ) {
81 # Switch! Look up the previous last edit, if any
82 $lastCurId = intval( $obj->rc_cur_id );
83 $emit = $obj->rc_timestamp;
84 $sql2 = "SELECT rev_id,rev_len FROM $revision " .
85 "WHERE rev_page={$lastCurId} ".
86 "AND rev_timestamp<'{$emit}' ORDER BY rev_timestamp DESC LIMIT 1";
87 $res2 = $dbw->query( $sql2 );
88 if( $row = $dbw->fetchObject( $res2 ) ) {
89 $lastOldId = intval($row->rev_id);
90 # Grab the last text size if available
91 $lastSize = !is_null($row->rev_len) ? intval($row->rev_len) : 'NULL';
96 $new = 1; // probably true
98 $dbw->freeResult( $res2 );
100 if( $lastCurId == 0 ) {
101 print "Uhhh, something wrong? No curid\n";
103 # Grab the entry's text size
104 $size = $dbw->selectField( 'revision', 'rev_len', array('rev_id' => $obj->rc_this_oldid ) );
105 $size = !is_null($size) ? intval($size) : 'NULL';
107 $sql3 = "UPDATE $recentchanges SET rc_last_oldid=$lastOldId,rc_new=$new,rc_type=$new," .
108 "rc_old_len=$lastSize,rc_new_len=$size " .
109 "WHERE rc_cur_id={$lastCurId} AND rc_this_oldid={$obj->rc_this_oldid}";
110 $dbw->query( $sql3 );
112 $lastOldId = intval( $obj->rc_this_oldid );
115 $dbw->freeResult( $res );
118 function rebuildRecentChangesTablePass3()
120 $dbw = wfGetDB( DB_MASTER );
122 print( "Loading from user, page, and logging tables...\n" );
125 // Some logs don't go in RC. This can't really detect all of those.
126 // At least do the basics logs for a standard install...
127 // FIXME: this needs to be maintained
128 $basicRCLogs = array(
137 // Escape...blah blah
138 $selectLogs = array();
139 foreach( $basicRCLogs as $logtype ) {
140 $safetype = $dbw->strencode( $logtype );
141 $selectLogs[] = "'$safetype'";
144 $cutoff = time() - $wgRCMaxAge;
145 $dbw->insertSelect( 'recentchanges', array( 'logging', 'page', 'user' ),
147 'rc_timestamp' => 'log_timestamp',
148 'rc_cur_time' => 'log_timestamp',
149 'rc_user' => 'log_user',
150 'rc_user_text' => 'user_name',
151 'rc_namespace' => 'log_namespace',
152 'rc_title' => 'log_title',
153 'rc_comment' => 'log_comment',
158 'rc_this_oldid' => 0,
159 'rc_last_oldid' => 0,
161 'rc_cur_id' => 'page_id',
162 'rc_log_type' => 'log_type',
163 'rc_log_action' => 'log_action',
164 'rc_logid' => 'log_id',
165 'rc_params' => 'log_params',
166 'rc_deleted' => 'log_deleted'
168 'log_timestamp > ' . $dbw->addQuotes( $dbw->timestamp( $cutoff ) ),
170 'log_namespace=page_namespace',
171 'log_title=page_title',
172 'log_type IN(' . implode(',',$selectLogs) . ')'
174 array(), // INSERT options
175 array( 'ORDER BY' => 'log_timestamp DESC', 'LIMIT' => 5000 ) // SELECT options
179 function rebuildRecentChangesTablePass4()
181 global $wgGroupPermissions, $wgUseRCPatrol;
183 $dbw = wfGetDB( DB_MASTER );
185 list($recentchanges,$usergroups,$user) = $dbw->tableNamesN( 'recentchanges', 'user_groups', 'user' );
187 $botgroups = $autopatrolgroups = array();
188 foreach( $wgGroupPermissions as $group => $rights ) {
189 if( isset( $rights['bot'] ) && $rights['bot'] == true ) {
190 $botgroups[] = $dbw->addQuotes( $group );
192 if( $wgUseRCPatrol && isset( $rights['autopatrol'] ) && $rights['autopatrol'] == true ) {
193 $autopatrolgroups[] = $dbw->addQuotes( $group );
196 # Flag our recent bot edits
197 if( !empty($botgroups) ) {
198 $botwhere = implode(',',$botgroups);
201 print( "Flagging bot account edits...\n" );
203 # Find all users that are bots
204 $sql = "SELECT DISTINCT user_name FROM $usergroups, $user " .
205 "WHERE ug_group IN($botwhere) AND user_id = ug_user";
206 $res = $dbw->query( $sql, DB_MASTER );
208 while( $obj = $dbw->fetchObject( $res ) ) {
209 $botusers[] = $dbw->addQuotes( $obj->user_name );
211 # Fill in the rc_bot field
212 if( !empty($botusers) ) {
213 $botwhere = implode(',',$botusers);
214 $sql2 = "UPDATE $recentchanges SET rc_bot=1 " .
215 "WHERE rc_user_text IN($botwhere)";
216 $dbw->query( $sql2 );
220 # Flag our recent autopatrolled edits
221 if( !$wgMiserMode && !empty($autopatrolgroups) ) {
222 $patrolwhere = implode(',',$autopatrolgroups);
223 $patrolusers = array();
225 print( "Flagging auto-patrolled edits...\n" );
227 # Find all users in RC with autopatrol rights
228 $sql = "SELECT DISTINCT user_name FROM $usergroups, $user " .
229 "WHERE ug_group IN($patrolwhere) AND user_id = ug_user";
230 $res = $dbw->query( $sql, DB_MASTER );
232 while( $obj = $dbw->fetchObject( $res ) ) {
233 $patrolusers[] = $dbw->addQuotes( $obj->user_name );
236 # Fill in the rc_patrolled field
237 if( !empty($patrolusers) ) {
238 $patrolwhere = implode(',',$patrolusers);
239 $sql2 = "UPDATE $recentchanges SET rc_patrolled=1 " .
240 "WHERE rc_user_text IN($patrolwhere)";
241 $dbw->query( $sql2 );
245 $dbw->freeResult( $res );