3 * Rebuild recent changes table.
6 * @addtogroup Maintenance
10 function rebuildRecentChangesTablePass1()
12 $fname = 'rebuildRecentChangesTablePass1';
13 $dbw = wfGetDB( DB_MASTER );
14 extract( $dbw->tableNames( 'recentchanges', 'cur', 'old' ) );
16 $dbw->delete( 'recentchanges', '*' );
18 print( "Loading from page and revision tables...\n" );
21 $cutoff = time() - $wgRCMaxAge;
22 $dbw->insertSelect( 'recentchanges', array( 'page', 'revision' ),
24 'rc_timestamp' => 'rev_timestamp',
25 'rc_cur_time' => 'rev_timestamp',
26 'rc_user' => 'rev_user',
27 'rc_user_text' => 'rev_user_text',
28 'rc_namespace' => 'page_namespace',
29 'rc_title' => 'page_title',
30 'rc_comment' => 'rev_comment',
31 'rc_minor' => 'rev_minor_edit',
33 'rc_new' => 'page_is_new',
34 'rc_cur_id' => 'page_id',
35 'rc_this_oldid' => 'rev_id',
36 'rc_last_oldid' => 0, // is this ok?
37 'rc_type' => $dbw->conditional( 'page_is_new != 0', RC_NEW, RC_EDIT ),
39 'rev_timestamp > ' . $dbw->addQuotes( $dbw->timestamp( $cutoff ) ),
42 array(), // INSERT options
43 array( 'ORDER BY' => 'rev_timestamp DESC', 'LIMIT' => 5000 ) // SELECT options
47 function rebuildRecentChangesTablePass2()
49 $dbw = wfGetDB( DB_MASTER );
50 list ($recentchanges, $revision) = $dbw->tableNamesN( 'recentchanges', 'revision' );
52 print( "Updating links and size differences...\n" );
54 # Fill in the rc_last_oldid field, which points to the previous edit
55 $sql = "SELECT rc_cur_id,rc_this_oldid,rc_timestamp FROM $recentchanges " .
56 "ORDER BY rc_cur_id,rc_timestamp";
57 $res = $dbw->query( $sql, DB_MASTER );
61 while ( $obj = $dbw->fetchObject( $res ) ) {
63 if( $obj->rc_cur_id != $lastCurId ) {
64 # Switch! Look up the previous last edit, if any
65 $lastCurId = intval( $obj->rc_cur_id );
66 $emit = $obj->rc_timestamp;
67 $sql2 = "SELECT rev_id, rev_len, rev_text_id FROM $revision " .
68 "WHERE rev_page={$lastCurId} ".
69 "AND rev_timestamp<'{$emit}' ORDER BY rev_timestamp DESC LIMIT 1";
70 $res2 = $dbw->query( $sql2 );
71 if( $row = $dbw->fetchObject( $res2 ) ) {
72 $lastOldId = intval( $row->rev_id );
73 $lastTextId = intval( $row->rev_text_id );
74 $lastSize = $row->rev_len; # Grab the last text size
82 $dbw->freeResult( $res2 );
84 if( $lastCurId == 0 ) {
85 print "Uhhh, something wrong? No curid\n";
87 # Check the text if not in rev_len for the last entry's text size
89 $lastText = $dbw->selectField( 'text', 'old_text', array('old_id' => $lastTextId ) );
90 $lastSize = $lastText ? strlen($lastText) : 'NULL';
92 # Grab the entry's text size
93 $res3 = $dbw->select( 'revision', array('rev_len','rev_text_id'), array('rev_id' => $obj->rc_this_oldid ) );
94 if( $row = $dbw->fetchObject( $res3 ) ) {
95 $textId = $row->rev_text_id;
96 $size = $row->rev_len;
101 # Check the text if not in rev_len for the entry's text size
103 $text = $dbw->selectField( 'text', 'old_text', array('old_id' => $textId ) );
104 $size = $text ? strlen($text) : '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 global $wgGroupPermissions, $wgUseRCPatrol;
122 $dbw = wfGetDB( DB_MASTER );
124 list ($recentchanges, $usergroups) = $dbw->tableNamesN( 'recentchanges', 'user_groups' );
126 $botgroups = $autopatrolgroups = array();
127 foreach( $wgGroupPermissions as $group => $rights ) {
128 if( isset( $rights['bot'] ) && $rights['bot'] == true ) {
129 $botgroups[] = "'" . $dbw->strencode( $group ) . "'";
131 if( $wgUseRCPatrol && isset( $rights['autopatrol'] ) && $rights['autopatrol'] == true ) {
132 $autopatrolgroups[] = "'" . $dbw->strencode( $group ) . "'";
135 # Flag our recent bot edits
136 if( !empty($botgroups) ) {
137 $botwhere = implode(',',$botgroups);
140 print( "Flagging bot account edits...\n" );
142 # Find all users in RC that are bots
143 $sql = "SELECT DISTINCT rc_user FROM $recentchanges " .
144 "LEFT JOIN $usergroups ON rc_user=ug_user " .
145 "WHERE ug_group IN($botwhere)";
146 $res = $dbw->query( $sql, DB_MASTER );
148 while( $obj = $dbw->fetchObject( $res ) ) {
149 $botusers[] = $obj->rc_user;
151 # Fill in the rc_bot field
152 if( !empty($botusers) ) {
153 $botwhere = implode(',',$botusers);
154 $sql2 = "UPDATE $recentchanges SET rc_bot=1 " .
155 "WHERE rc_user IN($botwhere)";
156 $dbw->query( $sql2 );
159 # Flag our recent autopatrolled edits
160 if( !empty($autopatrolgroups) ) {
161 $patrolwhere = implode(',',$autopatrolgroups);
162 $patrolusers = array();
164 print( "Flagging auto-patrolled edits...\n" );
166 # Find all users in RC with autopatrol rights
167 $sql = "SELECT DISTINCT rc_user FROM $recentchanges " .
168 "LEFT JOIN $usergroups ON rc_user=ug_user " .
169 "WHERE ug_group IN($patrolwhere)";
170 $res = $dbw->query( $sql, DB_MASTER );
172 while( $obj = $dbw->fetchObject( $res ) ) {
173 $patrolusers[] = $obj->rc_user;
176 # Fill in the rc_patrolled field
177 if( !empty($patrolusers) ) {
178 $patrolwhere = implode(',',$patrolusers);
179 $sql2 = "UPDATE $recentchanges SET rc_patrolled=1 " .
180 "WHERE rc_user IN($patrolwhere)";
181 $dbw->query( $sql2 );
185 $dbw->freeResult( $res );