Applying live hack from Wikimedia code base: option and reporting tweaks for populate...
[mediawiki.git] / maintenance / rebuildrecentchanges.inc
blob8b2c78053d2cafa344f738c8e9cfa1448bbc6eb8
1 <?php
2 /**
3  * Rebuild recent changes table.
4  *
5  * @todo document
6  * @addtogroup Maintenance
7  */
9 /** Public entry; more passes might come in! :) */
10 function rebuildRecentChangesTable() {
11         rebuildRecentChangesTablePass1();
12         rebuildRecentChangesTablePass2();
13         rebuildRecentChangesTablePass3();
16 /** */
17 function rebuildRecentChangesTablePass1()
19         $fname = 'rebuildRecentChangesTablePass1';
20         $dbw = wfGetDB( DB_MASTER );
22         $dbw->delete( 'recentchanges', '*' );
24         print( "Loading from page and revision tables...\n" );
26         global $wgRCMaxAge;
27         $cutoff = time() - $wgRCMaxAge;
28         $dbw->insertSelect( 'recentchanges', array( 'page', 'revision' ),
29                 array(
30                         'rc_timestamp'  => 'rev_timestamp',
31                         'rc_cur_time'   => 'rev_timestamp',
32                         'rc_user'       => 'rev_user',
33                         'rc_user_text'  => 'rev_user_text',
34                         'rc_namespace'  => 'page_namespace',
35                         'rc_title'      => 'page_title',
36                         'rc_comment'    => 'rev_comment',
37                         'rc_minor'      => 'rev_minor_edit',
38                         'rc_bot'        => 0,
39                         'rc_new'        => 'page_is_new',
40                         'rc_cur_id'     => 'page_id',
41                         'rc_this_oldid' => 'rev_id',
42                         'rc_last_oldid' => 0, // is this ok?
43                         'rc_type'       => $dbw->conditional( 'page_is_new != 0', RC_NEW, RC_EDIT ),
44                 ), array(
45                         'rev_timestamp > ' . $dbw->addQuotes( $dbw->timestamp( $cutoff ) ),
46                         'rev_page=page_id'
47                 ), $fname,
48                 array(), // INSERT options
49                 array( 'ORDER BY' => 'rev_timestamp DESC', 'LIMIT' => 5000 ) // SELECT options
50         );
53 function rebuildRecentChangesTablePass2()
55         $dbw = wfGetDB( DB_MASTER );
56         list ($recentchanges, $revision) = $dbw->tableNamesN( 'recentchanges', 'revision' );
58         print( "Updating links and size differences...\n" );
60         # Fill in the rc_last_oldid field, which points to the previous edit
61         $sql = "SELECT rc_cur_id,rc_this_oldid,rc_timestamp FROM $recentchanges " .
62           "ORDER BY rc_cur_id,rc_timestamp";
63         $res = $dbw->query( $sql, DB_MASTER );
65         $lastCurId = 0;
66         $lastOldId = 0;
67         while ( $obj = $dbw->fetchObject( $res ) ) {
68                 $new = 0;
69                 if( $obj->rc_cur_id != $lastCurId ) {
70                         # Switch! Look up the previous last edit, if any
71                         $lastCurId = intval( $obj->rc_cur_id );
72                         $emit = $obj->rc_timestamp;
73                         $sql2 = "SELECT rev_id, rev_len FROM $revision " .
74                                 "WHERE rev_page={$lastCurId} ".
75                                 "AND rev_timestamp<'{$emit}' ORDER BY rev_timestamp DESC LIMIT 1";
76                         $res2 = $dbw->query( $sql2 );
77                         if( $row = $dbw->fetchObject( $res2 ) ) {
78                                 $lastOldId = intval( $row->rev_id );
79                                 $lastSize = $row->rev_len; # Grab the last text size
80                         } else {
81                                 # No previous edit
82                                 $lastOldId = 0;
83                                 $lastSize = 'NULL';
84                                 $new = 1;
85                         }
86                         $dbw->freeResult( $res2 );
87                 }
88                 if( $lastCurId == 0 ) {
89                         print "Uhhh, something wrong? No curid\n";
90                 } else {
91                         # Grab the entry's text size
92                         $size = $dbw->selectField( 'revision', 'rev_len', array('rev_id' => $obj->rc_this_oldid ) );
93                         $size = $size ? $size : 'NULL';
94                         
95                         $sql3 = "UPDATE $recentchanges SET rc_last_oldid=$lastOldId,rc_new=$new,rc_type=$new," .
96                                 "rc_old_len='$lastSize',rc_new_len='$size' " .
97                                 "WHERE rc_cur_id={$lastCurId} AND rc_this_oldid={$obj->rc_this_oldid}";
98                         $dbw->query( $sql3 );
99                         
100                         $lastOldId = intval( $obj->rc_this_oldid );
101                 }
102         }
103         $dbw->freeResult( $res );
106 function rebuildRecentChangesTablePass3()
108         global $wgGroupPermissions, $wgUseRCPatrol;
109                         
110         $dbw = wfGetDB( DB_MASTER );
111         
112         list($recentchanges,$usergroups,$user) = $dbw->tableNamesN( 'recentchanges', 'user_groups', 'user' );
114         $botgroups = $autopatrolgroups = array();
115         foreach( $wgGroupPermissions as $group => $rights ) {
116                 if( isset( $rights['bot'] ) && $rights['bot'] == true ) {
117                         $botgroups[] = $dbw->addQuotes( $group );
118                 }
119                 if( $wgUseRCPatrol && isset( $rights['autopatrol'] ) && $rights['autopatrol'] == true ) {
120                         $autopatrolgroups[] = $dbw->addQuotes( $group );
121                 }
122         }
123         # Flag our recent bot edits
124         if( !empty($botgroups) ) {
125                 $botwhere = implode(',',$botgroups);
126                 $botusers = array();
128                 print( "Flagging bot account edits...\n" );
130                 # Find all users that are bots
131                 $sql = "SELECT DISTINCT user_name FROM $usergroups, $user " .
132                         "WHERE ug_group IN($botwhere) AND user_id = ug_user";
133                 $res = $dbw->query( $sql, DB_MASTER );
135                 while( $obj = $dbw->fetchObject( $res ) ) {
136                         $botusers[] = $dbw->addQuotes( $obj->user_name );
137                 }
138                 # Fill in the rc_bot field
139                 if( !empty($botusers) ) {
140                         $botwhere = implode(',',$botusers);
141                         $sql2 = "UPDATE $recentchanges SET rc_bot=1 " .
142                                 "WHERE rc_user_text IN($botwhere)";
143                         $dbw->query( $sql2 );
144                 }
145         }
146         # Flag our recent autopatrolled edits
147         if( !empty($autopatrolgroups) ) {
148                 $patrolwhere = implode(',',$autopatrolgroups);
149                 $patrolusers = array();
151                 print( "Flagging auto-patrolled edits...\n" );
153                 # Find all users in RC with autopatrol rights
154                 $sql = "SELECT DISTINCT user_name FROM $usergroups, $user " .
155                         "WHERE ug_group IN($patrolwhere) AND user_id = ug_user";
156                 $res = $dbw->query( $sql, DB_MASTER );
158                 while( $obj = $dbw->fetchObject( $res ) ) {
159                         $patrolusers[] = $dbw->addQuotes( $obj->user_name );
160                 }
161                 
162                 # Fill in the rc_patrolled field
163                 if( !empty($patrolusers) ) {
164                         $patrolwhere = implode(',',$patrolusers);
165                         $sql2 = "UPDATE $recentchanges SET rc_patrolled=1 " .
166                                 "WHERE rc_user_text IN($patrolwhere)";
167                         $dbw->query( $sql2 );
168                 }
169         }
170         
171         $dbw->freeResult( $res );