]> scripts.mit.edu Git - autoinstallsdev/mediawiki.git/blob - maintenance/rebuildrecentchanges.inc
MediaWiki 1.15.1
[autoinstallsdev/mediawiki.git] / maintenance / rebuildrecentchanges.inc
1 <?php
2 /**
3  * Rebuild recent changes table.
4  *
5  * @file
6  * @todo document
7  * @ingroup Maintenance
8  */
9
10 /** Public entry; more passes might come in! :) */
11 function rebuildRecentChangesTable() {
12         rebuildRecentChangesTablePass1();
13         rebuildRecentChangesTablePass2();
14         rebuildRecentChangesTablePass3();
15         rebuildRecentChangesTablePass4();
16 }
17
18 /** */
19 function rebuildRecentChangesTablePass1()
20 {
21         $dbw = wfGetDB( DB_MASTER );
22
23         $dbw->delete( 'recentchanges', '*' );
24
25         print( "Loading from page and revision tables...\n" );
26
27         global $wgRCMaxAge;
28
29         print( '$wgRCMaxAge=' . $wgRCMaxAge );
30         $days = $wgRCMaxAge / 24 / 3600;
31         if ( intval($days) == $days ) {
32                         print( " (" . $days . " days)\n" );
33         } else {
34                         print( " (approx. " .  intval($days) . " days)\n" );
35         }
36
37         $cutoff = time() - $wgRCMaxAge;
38         $dbw->insertSelect( 'recentchanges', array( 'page', 'revision' ),
39                 array(
40                         'rc_timestamp'  => 'rev_timestamp',
41                         'rc_cur_time'   => 'rev_timestamp',
42                         'rc_user'       => 'rev_user',
43                         'rc_user_text'  => 'rev_user_text',
44                         'rc_namespace'  => 'page_namespace',
45                         'rc_title'      => 'page_title',
46                         'rc_comment'    => 'rev_comment',
47                         'rc_minor'      => 'rev_minor_edit',
48                         'rc_bot'        => 0,
49                         'rc_new'        => 'page_is_new',
50                         'rc_cur_id'     => 'page_id',
51                         'rc_this_oldid' => 'rev_id',
52                         'rc_last_oldid' => 0, // is this ok?
53                         'rc_type'       => $dbw->conditional( 'page_is_new != 0', RC_NEW, RC_EDIT ),
54                         'rc_deleted'    => 'rev_deleted'
55                 ), array(
56                         'rev_timestamp > ' . $dbw->addQuotes( $dbw->timestamp( $cutoff ) ),
57                         'rev_page=page_id'
58                 ), __METHOD__,
59                 array(), // INSERT options
60                 array( 'ORDER BY' => 'rev_timestamp DESC', 'LIMIT' => 5000 ) // SELECT options
61         );
62 }
63
64 function rebuildRecentChangesTablePass2()
65 {
66         $dbw = wfGetDB( DB_MASTER );
67         list ($recentchanges, $revision) = $dbw->tableNamesN( 'recentchanges', 'revision' );
68
69         print( "Updating links and size differences...\n" );
70
71         # Fill in the rc_last_oldid field, which points to the previous edit
72         $sql = "SELECT rc_cur_id,rc_this_oldid,rc_timestamp FROM $recentchanges " .
73           "ORDER BY rc_cur_id,rc_timestamp";
74         $res = $dbw->query( $sql, DB_MASTER );
75
76         $lastCurId = 0;
77         $lastOldId = 0;
78         while ( $obj = $dbw->fetchObject( $res ) ) {
79                 $new = 0;
80                 if( $obj->rc_cur_id != $lastCurId ) {
81                         # Switch! Look up the previous last edit, if any
82                         $lastCurId = intval( $obj->rc_cur_id );
83                         $emit = $obj->rc_timestamp;
84                         $sql2 = "SELECT rev_id,rev_len FROM $revision " .
85                                 "WHERE rev_page={$lastCurId} ".
86                                 "AND rev_timestamp<'{$emit}' ORDER BY rev_timestamp DESC LIMIT 1";
87                         $res2 = $dbw->query( $sql2 );
88                         if( $row = $dbw->fetchObject( $res2 ) ) {
89                                 $lastOldId = intval($row->rev_id);
90                                 # Grab the last text size if available
91                                 $lastSize = !is_null($row->rev_len) ? intval($row->rev_len) : 'NULL';
92                         } else {
93                                 # No previous edit
94                                 $lastOldId = 0;
95                                 $lastSize = 'NULL';
96                                 $new = 1; // probably true
97                         }
98                         $dbw->freeResult( $res2 );
99                 }
100                 if( $lastCurId == 0 ) {
101                         print "Uhhh, something wrong? No curid\n";
102                 } else {
103                         # Grab the entry's text size
104                         $size = $dbw->selectField( 'revision', 'rev_len', array('rev_id' => $obj->rc_this_oldid ) );
105                         $size = !is_null($size) ? intval($size) : 'NULL';
106                         
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 );
111                         
112                         $lastOldId = intval( $obj->rc_this_oldid );
113                 }
114         }
115         $dbw->freeResult( $res );
116 }
117
118 function rebuildRecentChangesTablePass3()
119 {
120         $dbw = wfGetDB( DB_MASTER );
121
122         print( "Loading from user, page, and logging tables...\n" );
123         
124         global $wgRCMaxAge;
125         // Some logs don't go in RC. This can't really detect all of those.
126         // At least do the basics logs for a standard install...
127         // FIXME: this needs to be maintained
128         $basicRCLogs = array( 
129                 'block',
130                 'protect',
131                 'rights',
132                 'delete',
133                 'upload',
134                 'move',
135                 'import',
136                 'merge' );
137         // Escape...blah blah
138         $selectLogs = array();
139         foreach( $basicRCLogs as $logtype ) {
140                 $safetype = $dbw->strencode( $logtype );
141                 $selectLogs[] = "'$safetype'";
142         }
143         
144         $cutoff = time() - $wgRCMaxAge;
145         $dbw->insertSelect( 'recentchanges', array( 'logging', 'page', 'user' ),
146                 array(
147                         'rc_timestamp'  => 'log_timestamp',
148                         'rc_cur_time'   => 'log_timestamp',
149                         'rc_user'       => 'log_user',
150                         'rc_user_text'  => 'user_name',
151                         'rc_namespace'  => 'log_namespace',
152                         'rc_title'      => 'log_title',
153                         'rc_comment'    => 'log_comment',
154                         'rc_minor'      => 0,
155                         'rc_bot'        => 0,
156                         'rc_patrolled'  => 1,
157                         'rc_new'        => 0,
158                         'rc_this_oldid' => 0,
159                         'rc_last_oldid' => 0,
160                         'rc_type'       => RC_LOG,
161                         'rc_cur_id'     => 'page_id',
162                         'rc_log_type'   => 'log_type',
163                         'rc_log_action' => 'log_action',
164                         'rc_logid'      => 'log_id',
165                         'rc_params'     => 'log_params',
166                         'rc_deleted'    => 'log_deleted'
167                 ), array(
168                         'log_timestamp > ' . $dbw->addQuotes( $dbw->timestamp( $cutoff ) ),
169                         'log_user=user_id',
170                         'log_namespace=page_namespace',
171                         'log_title=page_title',
172                         'log_type IN(' . implode(',',$selectLogs) . ')'
173                 ), __METHOD__,
174                 array(), // INSERT options
175                 array( 'ORDER BY' => 'log_timestamp DESC', 'LIMIT' => 5000 ) // SELECT options
176         );
177 }
178
179 function rebuildRecentChangesTablePass4()
180 {
181         global $wgGroupPermissions, $wgUseRCPatrol;
182                         
183         $dbw = wfGetDB( DB_MASTER );
184         
185         list($recentchanges,$usergroups,$user) = $dbw->tableNamesN( 'recentchanges', 'user_groups', 'user' );
186
187         $botgroups = $autopatrolgroups = array();
188         foreach( $wgGroupPermissions as $group => $rights ) {
189                 if( isset( $rights['bot'] ) && $rights['bot'] == true ) {
190                         $botgroups[] = $dbw->addQuotes( $group );
191                 }
192                 if( $wgUseRCPatrol && isset( $rights['autopatrol'] ) && $rights['autopatrol'] == true ) {
193                         $autopatrolgroups[] = $dbw->addQuotes( $group );
194                 }
195         }
196         # Flag our recent bot edits
197         if( !empty($botgroups) ) {
198                 $botwhere = implode(',',$botgroups);
199                 $botusers = array();
200
201                 print( "Flagging bot account edits...\n" );
202
203                 # Find all users that are bots
204                 $sql = "SELECT DISTINCT user_name FROM $usergroups, $user " .
205                         "WHERE ug_group IN($botwhere) AND user_id = ug_user";
206                 $res = $dbw->query( $sql, DB_MASTER );
207
208                 while( $obj = $dbw->fetchObject( $res ) ) {
209                         $botusers[] = $dbw->addQuotes( $obj->user_name );
210                 }
211                 # Fill in the rc_bot field
212                 if( !empty($botusers) ) {
213                         $botwhere = implode(',',$botusers);
214                         $sql2 = "UPDATE $recentchanges SET rc_bot=1 " .
215                                 "WHERE rc_user_text IN($botwhere)";
216                         $dbw->query( $sql2 );
217                 }
218         }
219         global $wgMiserMode;
220         # Flag our recent autopatrolled edits
221         if( !$wgMiserMode && !empty($autopatrolgroups) ) {
222                 $patrolwhere = implode(',',$autopatrolgroups);
223                 $patrolusers = array();
224
225                 print( "Flagging auto-patrolled edits...\n" );
226
227                 # Find all users in RC with autopatrol rights
228                 $sql = "SELECT DISTINCT user_name FROM $usergroups, $user " .
229                         "WHERE ug_group IN($patrolwhere) AND user_id = ug_user";
230                 $res = $dbw->query( $sql, DB_MASTER );
231
232                 while( $obj = $dbw->fetchObject( $res ) ) {
233                         $patrolusers[] = $dbw->addQuotes( $obj->user_name );
234                 }
235                 
236                 # Fill in the rc_patrolled field
237                 if( !empty($patrolusers) ) {
238                         $patrolwhere = implode(',',$patrolusers);
239                         $sql2 = "UPDATE $recentchanges SET rc_patrolled=1 " .
240                                 "WHERE rc_user_text IN($patrolwhere)";
241                         $dbw->query( $sql2 );
242                 }
243         }
244         
245         $dbw->freeResult( $res );
246 }