3 * Rebuild recent changes table.
6 * @addtogroup Maintenance
9 /** Public entry; more passes might come in! :) */
10 function rebuildRecentChangesTable() {
11 rebuildRecentChangesTablePass1();
12 rebuildRecentChangesTablePass2();
13 rebuildRecentChangesTablePass3();
14 rebuildRecentChangesTablePass4();
18 function rebuildRecentChangesTablePass1()
20 $dbw = wfGetDB( DB_MASTER );
22 $dbw->delete( 'recentchanges', '*' );
24 print( "Loading from page and revision tables...\n" );
28 print( '$wgRCMaxAge=' . $wgRCMaxAge );
29 $days = $wgRCMaxAge / 24 / 3600;
30 if ( intval($days) == $days ) {
31 print( " (" . $days . " days)\n" );
33 print( " (approx. " . intval($days) . " days)\n" );
36 $cutoff = time() - $wgRCMaxAge;
37 $dbw->insertSelect( 'recentchanges', array( 'page', 'revision' ),
39 'rc_timestamp' => 'rev_timestamp',
40 'rc_cur_time' => 'rev_timestamp',
41 'rc_user' => 'rev_user',
42 'rc_user_text' => 'rev_user_text',
43 'rc_namespace' => 'page_namespace',
44 'rc_title' => 'page_title',
45 'rc_comment' => 'rev_comment',
46 'rc_minor' => 'rev_minor_edit',
48 'rc_new' => 'page_is_new',
49 'rc_cur_id' => 'page_id',
50 'rc_this_oldid' => 'rev_id',
51 'rc_last_oldid' => 0, // is this ok?
52 'rc_type' => $dbw->conditional( 'page_is_new != 0', RC_NEW, RC_EDIT ),
53 'rc_deleted' => 'rev_deleted'
55 'rev_timestamp > ' . $dbw->addQuotes( $dbw->timestamp( $cutoff ) ),
58 array(), // INSERT options
59 array( 'ORDER BY' => 'rev_timestamp DESC', 'LIMIT' => 5000 ) // SELECT options
63 function rebuildRecentChangesTablePass2()
65 $dbw = wfGetDB( DB_MASTER );
66 list ($recentchanges, $revision) = $dbw->tableNamesN( 'recentchanges', 'revision' );
68 print( "Updating links and size differences...\n" );
70 # Fill in the rc_last_oldid field, which points to the previous edit
71 $sql = "SELECT rc_cur_id,rc_this_oldid,rc_timestamp FROM $recentchanges " .
72 "ORDER BY rc_cur_id,rc_timestamp";
73 $res = $dbw->query( $sql, DB_MASTER );
77 while ( $obj = $dbw->fetchObject( $res ) ) {
79 if( $obj->rc_cur_id != $lastCurId ) {
80 # Switch! Look up the previous last edit, if any
81 $lastCurId = intval( $obj->rc_cur_id );
82 $emit = $obj->rc_timestamp;
83 $sql2 = "SELECT rev_id,rev_len FROM $revision " .
84 "WHERE rev_page={$lastCurId} ".
85 "AND rev_timestamp<'{$emit}' ORDER BY rev_timestamp DESC LIMIT 1";
86 $res2 = $dbw->query( $sql2 );
87 if( $row = $dbw->fetchObject( $res2 ) ) {
88 $lastOldId = intval($row->rev_id);
89 # Grab the last text size if available
90 $lastSize = !is_null($row->rev_len) ? intval($row->rev_len) : 'NULL';
95 $new = 1; // probably true
97 $dbw->freeResult( $res2 );
99 if( $lastCurId == 0 ) {
100 print "Uhhh, something wrong? No curid\n";
102 # Grab the entry's text size
103 $size = $dbw->selectField( 'revision', 'rev_len', array('rev_id' => $obj->rc_this_oldid ) );
104 $size = !is_null($size) ? intval($size) : 'NULL';
106 $sql3 = "UPDATE $recentchanges SET rc_last_oldid=$lastOldId,rc_new=$new,rc_type=$new," .
107 "rc_old_len=$lastSize,rc_new_len=$size " .
108 "WHERE rc_cur_id={$lastCurId} AND rc_this_oldid={$obj->rc_this_oldid}";
109 $dbw->query( $sql3 );
111 $lastOldId = intval( $obj->rc_this_oldid );
114 $dbw->freeResult( $res );
117 function rebuildRecentChangesTablePass3()
119 $dbw = wfGetDB( DB_MASTER );
121 print( "Loading from user, page, and logging tables...\n" );
124 // Some logs don't go in RC. This can't really detect all of those.
125 // At least do the basics logs for a standard install...
126 // FIXME: this needs to be maintained
127 $basicRCLogs = array(
136 // Escape...blah blah
137 $selectLogs = array();
138 foreach( $basicRCLogs as $logtype ) {
139 $safetype = $dbw->strencode( $logtype );
140 $selectLogs[] = "'$safetype'";
143 $cutoff = time() - $wgRCMaxAge;
144 $dbw->insertSelect( 'recentchanges', array( 'logging', 'page', 'user' ),
146 'rc_timestamp' => 'log_timestamp',
147 'rc_cur_time' => 'log_timestamp',
148 'rc_user' => 'log_user',
149 'rc_user_text' => 'user_name',
150 'rc_namespace' => 'log_namespace',
151 'rc_title' => 'log_title',
152 'rc_comment' => 'log_comment',
157 'rc_this_oldid' => 0,
158 'rc_last_oldid' => 0,
160 'rc_cur_id' => 'page_id',
161 'rc_log_type' => 'log_type',
162 'rc_log_action' => 'log_action',
163 'rc_logid' => 'log_id',
164 'rc_params' => 'log_params',
165 'rc_deleted' => 'log_deleted'
167 'log_timestamp > ' . $dbw->addQuotes( $dbw->timestamp( $cutoff ) ),
169 'log_namespace=page_namespace',
170 'log_title=page_title',
171 'log_type IN(' . implode(',',$selectLogs) . ')'
173 array(), // INSERT options
174 array( 'ORDER BY' => 'log_timestamp DESC', 'LIMIT' => 5000 ) // SELECT options
178 function rebuildRecentChangesTablePass4()
180 global $wgGroupPermissions, $wgUseRCPatrol;
182 $dbw = wfGetDB( DB_MASTER );
184 list($recentchanges,$usergroups,$user) = $dbw->tableNamesN( 'recentchanges', 'user_groups', 'user' );
186 $botgroups = $autopatrolgroups = array();
187 foreach( $wgGroupPermissions as $group => $rights ) {
188 if( isset( $rights['bot'] ) && $rights['bot'] == true ) {
189 $botgroups[] = $dbw->addQuotes( $group );
191 if( $wgUseRCPatrol && isset( $rights['autopatrol'] ) && $rights['autopatrol'] == true ) {
192 $autopatrolgroups[] = $dbw->addQuotes( $group );
195 # Flag our recent bot edits
196 if( !empty($botgroups) ) {
197 $botwhere = implode(',',$botgroups);
200 print( "Flagging bot account edits...\n" );
202 # Find all users that are bots
203 $sql = "SELECT DISTINCT user_name FROM $usergroups, $user " .
204 "WHERE ug_group IN($botwhere) AND user_id = ug_user";
205 $res = $dbw->query( $sql, DB_MASTER );
207 while( $obj = $dbw->fetchObject( $res ) ) {
208 $botusers[] = $dbw->addQuotes( $obj->user_name );
210 # Fill in the rc_bot field
211 if( !empty($botusers) ) {
212 $botwhere = implode(',',$botusers);
213 $sql2 = "UPDATE $recentchanges SET rc_bot=1 " .
214 "WHERE rc_user_text IN($botwhere)";
215 $dbw->query( $sql2 );
219 # Flag our recent autopatrolled edits
220 if( !$wgMiserMode && !empty($autopatrolgroups) ) {
221 $patrolwhere = implode(',',$autopatrolgroups);
222 $patrolusers = array();
224 print( "Flagging auto-patrolled edits...\n" );
226 # Find all users in RC with autopatrol rights
227 $sql = "SELECT DISTINCT user_name FROM $usergroups, $user " .
228 "WHERE ug_group IN($patrolwhere) AND user_id = ug_user";
229 $res = $dbw->query( $sql, DB_MASTER );
231 while( $obj = $dbw->fetchObject( $res ) ) {
232 $patrolusers[] = $dbw->addQuotes( $obj->user_name );
235 # Fill in the rc_patrolled field
236 if( !empty($patrolusers) ) {
237 $patrolwhere = implode(',',$patrolusers);
238 $sql2 = "UPDATE $recentchanges SET rc_patrolled=1 " .
239 "WHERE rc_user_text IN($patrolwhere)";
240 $dbw->query( $sql2 );
244 $dbw->freeResult( $res );