Remove cruft from earlier testing
[mediawiki.git] / maintenance / rebuildrecentchanges.inc
blobc706e1b2d272f4d53228b01277596b2b5f6b2ae3
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();
14         rebuildRecentChangesTablePass4();
17 /** */
18 function rebuildRecentChangesTablePass1()
20         $dbw = wfGetDB( DB_MASTER );
22         $dbw->delete( 'recentchanges', '*' );
24         print( "Loading from page and revision tables...\n" );
26         global $wgRCMaxAge;
28         print( '$wgRCMaxAge=' . $wgRCMaxAge );
29         $days = $wgRCMaxAge / 24 / 3600;
30         if ( intval($days) == $days ) {
31                         print( " (" . $days . " days)\n" );
32         } else {
33                         print( " (approx. " .  intval($days) . " days)\n" );
34         }
36         $cutoff = time() - $wgRCMaxAge;
37         $dbw->insertSelect( 'recentchanges', array( 'page', 'revision' ),
38                 array(
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',
47                         'rc_bot'        => 0,
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'
54                 ), array(
55                         'rev_timestamp > ' . $dbw->addQuotes( $dbw->timestamp( $cutoff ) ),
56                         'rev_page=page_id'
57                 ), __METHOD__,
58                 array(), // INSERT options
59                 array( 'ORDER BY' => 'rev_timestamp DESC', 'LIMIT' => 5000 ) // SELECT options
60         );
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 FROM $recentchanges " .
72           "ORDER BY rc_cur_id,rc_timestamp";
73         $res = $dbw->query( $sql, DB_MASTER );
75         $lastCurId = 0;
76         $lastOldId = 0;
77         while ( $obj = $dbw->fetchObject( $res ) ) {
78                 $new = 0;
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                         $sql2 = "SELECT rev_id,rev_len FROM $revision " .
83                                 "WHERE rev_page={$lastCurId} ".
84                                 "AND rev_id < '{$obj->rc_this_oldid}' ORDER BY rev_id DESC LIMIT 1";
85                         $res2 = $dbw->query( $sql2 );
86                         if( $row = $dbw->fetchObject( $res2 ) ) {
87                                 $lastOldId = intval($row->rev_id);
88                                 # Grab the last text size if available
89                                 $lastSize = !is_null($row->rev_len) ? intval($row->rev_len) : 'NULL';
90                         } else {
91                                 # No previous edit
92                                 $lastOldId = 0;
93                                 $lastSize = 'NULL';
94                                 $new = 1; // probably true
95                         }
96                         $dbw->freeResult( $res2 );
97                 }
98                 if( $lastCurId == 0 ) {
99                         print "Uhhh, something wrong? No curid\n";
100                 } else {
101                         # Grab the entry's text size
102                         $size = $dbw->selectField( 'revision', 'rev_len', array('rev_id' => $obj->rc_this_oldid ) );
103                         $size = !is_null($size) ? intval($size) : 'NULL';
104                         
105                         $sql3 = "UPDATE $recentchanges SET rc_last_oldid=$lastOldId,rc_new=$new,rc_type=$new," .
106                                 "rc_old_len=$lastSize,rc_new_len=$size " .
107                                 "WHERE rc_cur_id={$lastCurId} AND rc_this_oldid={$obj->rc_this_oldid}";
108                         $dbw->query( $sql3 );
109                         
110                         $lastOldId = intval( $obj->rc_this_oldid );
111                 }
112         }
113         $dbw->freeResult( $res );
116 function rebuildRecentChangesTablePass3()
118         $dbw = wfGetDB( DB_MASTER );
120         print( "Loading from user, page, and logging tables...\n" );
121         
122         global $wgRCMaxAge;
123         // Some logs don't go in RC. This can't really detect all of those.
124         // At least do the basics logs for a standard install...
125         // FIXME: this needs to be maintained
126         $basicRCLogs = array( 
127                 'block',
128                 'protect',
129                 'rights',
130                 'delete',
131                 'upload',
132                 'move',
133                 'import',
134                 'merge' );
135         // Escape...blah blah
136         $selectLogs = array();
137         foreach( $basicRCLogs as $logtype ) {
138                 $safetype = $dbw->strencode( $logtype );
139                 $selectLogs[] = "'$safetype'";
140         }
141         
142         $cutoff = time() - $wgRCMaxAge;
143         $dbw->insertSelect( 'recentchanges', array( 'logging', 'page', 'user' ),
144                 array(
145                         'rc_timestamp'  => 'log_timestamp',
146                         'rc_cur_time'   => 'log_timestamp',
147                         'rc_user'       => 'log_user',
148                         'rc_user_text'  => 'user_name',
149                         'rc_namespace'  => 'log_namespace',
150                         'rc_title'      => 'log_title',
151                         'rc_comment'    => 'log_comment',
152                         'rc_minor'      => 0,
153                         'rc_bot'        => 0,
154                         'rc_patrolled'  => 1,
155                         'rc_new'        => 0,
156                         'rc_this_oldid' => 0,
157                         'rc_last_oldid' => 0,
158                         'rc_type'       => RC_LOG,
159                         'rc_cur_id'     => 'page_id',
160                         'rc_log_type'   => 'log_type',
161                         'rc_log_action' => 'log_action',
162                         'rc_logid'      => 'log_id',
163                         'rc_params'     => 'log_params',
164                         'rc_deleted'    => 'log_deleted'
165                 ), array(
166                         'log_timestamp > ' . $dbw->addQuotes( $dbw->timestamp( $cutoff ) ),
167                         'log_user=user_id',
168                         'log_namespace=page_namespace',
169                         'log_title=page_title',
170                         'log_type IN(' . implode(',',$selectLogs) . ')'
171                 ), __METHOD__,
172                 array(), // INSERT options
173                 array( 'ORDER BY' => 'log_timestamp DESC', 'LIMIT' => 5000 ) // SELECT options
174         );
177 function rebuildRecentChangesTablePass4()
179         global $wgGroupPermissions, $wgUseRCPatrol;
180                         
181         $dbw = wfGetDB( DB_MASTER );
182         
183         list($recentchanges,$usergroups,$user) = $dbw->tableNamesN( 'recentchanges', 'user_groups', 'user' );
185         $botgroups = $autopatrolgroups = array();
186         foreach( $wgGroupPermissions as $group => $rights ) {
187                 if( isset( $rights['bot'] ) && $rights['bot'] == true ) {
188                         $botgroups[] = $dbw->addQuotes( $group );
189                 }
190                 if( $wgUseRCPatrol && isset( $rights['autopatrol'] ) && $rights['autopatrol'] == true ) {
191                         $autopatrolgroups[] = $dbw->addQuotes( $group );
192                 }
193         }
194         # Flag our recent bot edits
195         if( !empty($botgroups) ) {
196                 $botwhere = implode(',',$botgroups);
197                 $botusers = array();
199                 print( "Flagging bot account edits...\n" );
201                 # Find all users that are bots
202                 $sql = "SELECT DISTINCT user_name FROM $usergroups, $user " .
203                         "WHERE ug_group IN($botwhere) AND user_id = ug_user";
204                 $res = $dbw->query( $sql, DB_MASTER );
206                 while( $obj = $dbw->fetchObject( $res ) ) {
207                         $botusers[] = $dbw->addQuotes( $obj->user_name );
208                 }
209                 # Fill in the rc_bot field
210                 if( !empty($botusers) ) {
211                         $botwhere = implode(',',$botusers);
212                         $sql2 = "UPDATE $recentchanges SET rc_bot=1 " .
213                                 "WHERE rc_user_text IN($botwhere)";
214                         $dbw->query( $sql2 );
215                 }
216         }
217         global $wgMiserMode;
218         # Flag our recent autopatrolled edits
219         if( !$wgMiserMode && !empty($autopatrolgroups) ) {
220                 $patrolwhere = implode(',',$autopatrolgroups);
221                 $patrolusers = array();
223                 print( "Flagging auto-patrolled edits...\n" );
225                 # Find all users in RC with autopatrol rights
226                 $sql = "SELECT DISTINCT user_name FROM $usergroups, $user " .
227                         "WHERE ug_group IN($patrolwhere) AND user_id = ug_user";
228                 $res = $dbw->query( $sql, DB_MASTER );
230                 while( $obj = $dbw->fetchObject( $res ) ) {
231                         $patrolusers[] = $dbw->addQuotes( $obj->user_name );
232                 }
233                 
234                 # Fill in the rc_patrolled field
235                 if( !empty($patrolusers) ) {
236                         $patrolwhere = implode(',',$patrolusers);
237                         $sql2 = "UPDATE $recentchanges SET rc_patrolled=1 " .
238                                 "WHERE rc_user_text IN($patrolwhere)";
239                         $dbw->query( $sql2 );
240                 }
241         }
242         
243         $dbw->freeResult( $res );