3 * Rebuild recent changes table.
6 * @addtogroup Maintenance
10 function rebuildRecentChangesTablePass1()
12 $fname = 'rebuildRecentChangesTablePass1';
13 $dbw = wfGetDB( DB_MASTER );
15 $dbw->delete( 'recentchanges', '*' );
17 print( "Loading from page and revision tables...\n" );
20 $cutoff = time() - $wgRCMaxAge;
21 $dbw->insertSelect( 'recentchanges', array( 'page', 'revision' ),
23 'rc_timestamp' => 'rev_timestamp',
24 'rc_cur_time' => 'rev_timestamp',
25 'rc_user' => 'rev_user',
26 'rc_user_text' => 'rev_user_text',
27 'rc_namespace' => 'page_namespace',
28 'rc_title' => 'page_title',
29 'rc_comment' => 'rev_comment',
30 'rc_minor' => 'rev_minor_edit',
32 'rc_new' => 'page_is_new',
33 'rc_cur_id' => 'page_id',
34 'rc_this_oldid' => 'rev_id',
35 'rc_last_oldid' => 0, // is this ok?
36 'rc_type' => $dbw->conditional( 'page_is_new != 0', RC_NEW, RC_EDIT ),
38 'rev_timestamp > ' . $dbw->addQuotes( $dbw->timestamp( $cutoff ) ),
41 array(), // INSERT options
42 array( 'ORDER BY' => 'rev_timestamp DESC', 'LIMIT' => 5000 ) // SELECT options
46 function rebuildRecentChangesTablePass2()
48 $dbw = wfGetDB( DB_MASTER );
49 list ($recentchanges, $revision) = $dbw->tableNamesN( 'recentchanges', 'revision' );
51 print( "Updating links and size differences...\n" );
53 # Fill in the rc_last_oldid field, which points to the previous edit
54 $sql = "SELECT rc_cur_id,rc_this_oldid,rc_timestamp FROM $recentchanges " .
55 "ORDER BY rc_cur_id,rc_timestamp";
56 $res = $dbw->query( $sql, DB_MASTER );
60 while ( $obj = $dbw->fetchObject( $res ) ) {
62 if( $obj->rc_cur_id != $lastCurId ) {
63 # Switch! Look up the previous last edit, if any
64 $lastCurId = intval( $obj->rc_cur_id );
65 $emit = $obj->rc_timestamp;
66 $sql2 = "SELECT rev_id, rev_len FROM $revision " .
67 "WHERE rev_page={$lastCurId} ".
68 "AND rev_timestamp<'{$emit}' ORDER BY rev_timestamp DESC LIMIT 1";
69 $res2 = $dbw->query( $sql2 );
70 if( $row = $dbw->fetchObject( $res2 ) ) {
71 $lastOldId = intval( $row->rev_id );
72 $lastSize = $row->rev_len; # Grab the last text size
79 $dbw->freeResult( $res2 );
81 if( $lastCurId == 0 ) {
82 print "Uhhh, something wrong? No curid\n";
84 # Grab the entry's text size
85 $size = $dbw->selectField( 'revision', 'rev_len', array('rev_id' => $obj->rc_this_oldid ) );
86 $size = $size ? $size : 'NULL';
88 $sql3 = "UPDATE $recentchanges SET rc_last_oldid=$lastOldId,rc_new=$new,rc_type=$new," .
89 "rc_old_len='$lastSize',rc_new_len='$size' " .
90 "WHERE rc_cur_id={$lastCurId} AND rc_this_oldid={$obj->rc_this_oldid}";
93 $lastOldId = intval( $obj->rc_this_oldid );
96 $dbw->freeResult( $res );
99 function rebuildRecentChangesTablePass3()
101 global $wgGroupPermissions, $wgUseRCPatrol;
103 $dbw = wfGetDB( DB_MASTER );
105 list($recentchanges,$usergroups,$user) = $dbw->tableNamesN( 'recentchanges', 'user_groups', 'user' );
107 $botgroups = $autopatrolgroups = array();
108 foreach( $wgGroupPermissions as $group => $rights ) {
109 if( isset( $rights['bot'] ) && $rights['bot'] == true ) {
110 $botgroups[] = "'" . $dbw->strencode( $group ) . "'";
112 if( $wgUseRCPatrol && isset( $rights['autopatrol'] ) && $rights['autopatrol'] == true ) {
113 $autopatrolgroups[] = "'" . $dbw->strencode( $group ) . "'";
116 # Flag our recent bot edits
117 if( !empty($botgroups) ) {
118 $botwhere = implode(',',$botgroups);
121 print( "Flagging bot account edits...\n" );
123 # Find all users that are bots
124 $sql = "SELECT DISTINCT user_name FROM $usergroups, $user " .
125 "WHERE ug_group IN($botwhere) AND user_id = ug_user";
126 $res = $dbw->query( $sql, DB_MASTER );
128 while( $obj = $dbw->fetchObject( $res ) ) {
129 $botusers[] = $dbw->strencode( $obj->user_name );
131 # Fill in the rc_bot field
132 if( !empty($botusers) ) {
133 $botwhere = implode(',',$botusers);
134 $sql2 = "UPDATE $recentchanges SET rc_bot=1 " .
135 "WHERE rc_user_text IN($botwhere)";
136 $dbw->query( $sql2 );
139 # Flag our recent autopatrolled edits
140 if( !empty($autopatrolgroups) ) {
141 $patrolwhere = implode(',',$autopatrolgroups);
142 $patrolusers = array();
144 print( "Flagging auto-patrolled edits...\n" );
146 # Find all users in RC with autopatrol rights
147 $sql = "SELECT DISTINCT user_name FROM $usergroups, $user " .
148 "WHERE ug_group IN($patrolwhere) AND user_id = ug_user";
149 $res = $dbw->query( $sql, DB_MASTER );
151 while( $obj = $dbw->fetchObject( $res ) ) {
152 $patrolusers[] = $dbw->strencode( $obj->user_name );
155 # Fill in the rc_patrolled field
156 if( !empty($patrolusers) ) {
157 $patrolwhere = implode(',',$patrolusers);
158 $sql2 = "UPDATE $recentchanges SET rc_patrolled=1 " .
159 "WHERE rc_user_text IN($patrolwhere)";
160 $dbw->query( $sql2 );
164 $dbw->freeResult( $res );