3a1b9e1a8e3571b1d799eb24a71483506b087bd4
[lhc/web/wiklou.git] / maintenance / rebuildrecentchanges.php
1 <?php
2 /**
3 * Rebuild link tracking tables from scratch. This takes several
4 * hours, depending on the database size and server configuration.
5 *
6 * This program is free software; you can redistribute it and/or modify
7 * it under the terms of the GNU General Public License as published by
8 * the Free Software Foundation; either version 2 of the License, or
9 * (at your option) any later version.
10 *
11 * This program is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 * GNU General Public License for more details.
15 *
16 * You should have received a copy of the GNU General Public License along
17 * with this program; if not, write to the Free Software Foundation, Inc.,
18 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
19 * http://www.gnu.org/copyleft/gpl.html
20 *
21 * @ingroup Maintenance
22 * @todo Document
23 */
24
25 require_once( dirname( __FILE__ ) . '/Maintenance.php' );
26
27 class RebuildRecentchanges extends Maintenance {
28 public function __construct() {
29 parent::__construct();
30 $this->mDescription = "Rebuild recent changes";
31 }
32
33 public function execute() {
34 $this->rebuildRecentChangesTablePass1();
35 $this->rebuildRecentChangesTablePass2();
36 $this->rebuildRecentChangesTablePass3();
37 $this->rebuildRecentChangesTablePass4();
38 $this->purgeFeeds();
39 $this->output( "Done.\n" );
40 }
41
42 /**
43 * Rebuild pass 1
44 * DOCUMENT ME!
45 */
46 function rebuildRecentChangesTablePass1()
47 {
48 $dbw = wfGetDB( DB_MASTER );
49
50 $dbw->delete( 'recentchanges', '*' );
51
52 $this->output( "Loading from page and revision tables...\n" );
53
54 global $wgRCMaxAge;
55
56 $this->output( '$wgRCMaxAge=' . $wgRCMaxAge );
57 $days = $wgRCMaxAge / 24 / 3600;
58 if ( intval( $days ) == $days ) {
59 $this->output( " (" . $days . " days)\n" );
60 } else {
61 $this->output( " (approx. " . intval( $days ) . " days)\n" );
62 }
63
64 $cutoff = time() - $wgRCMaxAge;
65 $dbw->insertSelect( 'recentchanges', array( 'page', 'revision' ),
66 array(
67 'rc_timestamp' => 'rev_timestamp',
68 'rc_cur_time' => 'rev_timestamp',
69 'rc_user' => 'rev_user',
70 'rc_user_text' => 'rev_user_text',
71 'rc_namespace' => 'page_namespace',
72 'rc_title' => 'page_title',
73 'rc_comment' => 'rev_comment',
74 'rc_minor' => 'rev_minor_edit',
75 'rc_bot' => 0,
76 'rc_new' => 'page_is_new',
77 'rc_cur_id' => 'page_id',
78 'rc_this_oldid' => 'rev_id',
79 'rc_last_oldid' => 0, // is this ok?
80 'rc_type' => $dbw->conditional( 'page_is_new != 0', RC_NEW, RC_EDIT ),
81 'rc_deleted' => 'rev_deleted'
82 ), array(
83 'rev_timestamp > ' . $dbw->addQuotes( $dbw->timestamp( $cutoff ) ),
84 'rev_page=page_id'
85 ), __METHOD__,
86 array(), // INSERT options
87 array( 'ORDER BY' => 'rev_timestamp DESC', 'LIMIT' => 5000 ) // SELECT options
88 );
89 }
90
91 /**
92 * Rebuild pass 2
93 * DOCUMENT ME!
94 */
95 private function rebuildRecentChangesTablePass2() {
96 $dbw = wfGetDB( DB_MASTER );
97 list ( $recentchanges, $revision ) = $dbw->tableNamesN( 'recentchanges', 'revision' );
98
99 $this->output( "Updating links and size differences...\n" );
100
101 # Fill in the rc_last_oldid field, which points to the previous edit
102 $sql = "SELECT rc_cur_id,rc_this_oldid,rc_timestamp FROM $recentchanges " .
103 "ORDER BY rc_cur_id,rc_timestamp";
104 $res = $dbw->query( $sql, DB_MASTER );
105
106 $lastCurId = 0;
107 $lastOldId = 0;
108 foreach ( $res as $obj ) {
109 $new = 0;
110 if ( $obj->rc_cur_id != $lastCurId ) {
111 # Switch! Look up the previous last edit, if any
112 $lastCurId = intval( $obj->rc_cur_id );
113 $emit = $obj->rc_timestamp;
114 $sql2 = "SELECT rev_id,rev_len FROM $revision " .
115 "WHERE rev_page={$lastCurId} " .
116 "AND rev_timestamp<'{$emit}' ORDER BY rev_timestamp DESC";
117 $sql2 = $dbw->limitResult( $sql2, 1, false );
118 $res2 = $dbw->query( $sql2 );
119 $row = $dbw->fetchObject( $res2 );
120 if ( $row ) {
121 $lastOldId = intval( $row->rev_id );
122 # Grab the last text size if available
123 $lastSize = !is_null( $row->rev_len ) ? intval( $row->rev_len ) : 'NULL';
124 } else {
125 # No previous edit
126 $lastOldId = 0;
127 $lastSize = 'NULL';
128 $new = 1; // probably true
129 }
130 }
131 if ( $lastCurId == 0 ) {
132 $this->output( "Uhhh, something wrong? No curid\n" );
133 } else {
134 # Grab the entry's text size
135 $size = $dbw->selectField( 'revision', 'rev_len', array( 'rev_id' => $obj->rc_this_oldid ) );
136 $size = !is_null( $size ) ? intval( $size ) : 'NULL';
137
138 $dbw->update( 'recentchanges',
139 array(
140 'rc_last_oldid' => $lastOldId,
141 'rc_new' => $new,
142 'rc_type' => $new,
143 'rc_old_len' => $lastSize,
144 'rc_new_len' => $size,
145 ), array(
146 'rc_cur_id' => $lastCurId,
147 'rc_this_oldid' => $obj->rc_this_oldid,
148 ),
149 __METHOD__
150 );
151
152 $lastOldId = intval( $obj->rc_this_oldid );
153 $lastSize = $size;
154 }
155 }
156 }
157
158 /**
159 * Rebuild pass 3
160 * DOCUMENT ME!
161 */
162 private function rebuildRecentChangesTablePass3() {
163 $dbw = wfGetDB( DB_MASTER );
164
165 $this->output( "Loading from user, page, and logging tables...\n" );
166
167 global $wgRCMaxAge, $wgLogTypes, $wgLogRestrictions;
168 // Some logs don't go in RC. This should check for that
169 $basicRCLogs = array_diff( $wgLogTypes, array_keys( $wgLogRestrictions ) );
170
171 // Escape...blah blah
172 $selectLogs = array();
173 foreach ( $basicRCLogs as $logtype ) {
174 $safetype = $dbw->strencode( $logtype );
175 $selectLogs[] = "'$safetype'";
176 }
177
178 $cutoff = time() - $wgRCMaxAge;
179 list( $logging, $page ) = $dbw->tableNamesN( 'logging', 'page' );
180 $dbw->insertSelect( 'recentchanges', array( 'user', "$logging LEFT JOIN $page ON (log_namespace=page_namespace AND log_title=page_title)" ),
181 array(
182 'rc_timestamp' => 'log_timestamp',
183 'rc_cur_time' => 'log_timestamp',
184 'rc_user' => 'log_user',
185 'rc_user_text' => 'user_name',
186 'rc_namespace' => 'log_namespace',
187 'rc_title' => 'log_title',
188 'rc_comment' => 'log_comment',
189 'rc_minor' => 0,
190 'rc_bot' => 0,
191 'rc_patrolled' => 1,
192 'rc_new' => 0,
193 'rc_this_oldid' => 0,
194 'rc_last_oldid' => 0,
195 'rc_type' => RC_LOG,
196 'rc_cur_id' => $dbw->cascadingDeletes() ? 'page_id' : 'COALESCE(page_id, 0)',
197 'rc_log_type' => 'log_type',
198 'rc_log_action' => 'log_action',
199 'rc_logid' => 'log_id',
200 'rc_params' => 'log_params',
201 'rc_deleted' => 'log_deleted'
202 ), array(
203 'log_timestamp > ' . $dbw->addQuotes( $dbw->timestamp( $cutoff ) ),
204 'log_user=user_id',
205 'log_type IN(' . implode( ',', $selectLogs ) . ')'
206 ), __METHOD__,
207 array(), // INSERT options
208 array( 'ORDER BY' => 'log_timestamp DESC', 'LIMIT' => 5000 ) // SELECT options
209 );
210 }
211
212 /**
213 * Rebuild pass 4
214 * DOCUMENT ME!
215 */
216 private function rebuildRecentChangesTablePass4() {
217 global $wgGroupPermissions, $wgUseRCPatrol;
218
219 $dbw = wfGetDB( DB_MASTER );
220
221 list( $recentchanges, $usergroups, $user ) = $dbw->tableNamesN( 'recentchanges', 'user_groups', 'user' );
222
223 $botgroups = $autopatrolgroups = array();
224 foreach ( $wgGroupPermissions as $group => $rights ) {
225 if ( isset( $rights['bot'] ) && $rights['bot'] ) {
226 $botgroups[] = $dbw->addQuotes( $group );
227 }
228 if ( $wgUseRCPatrol && isset( $rights['autopatrol'] ) && $rights['autopatrol'] ) {
229 $autopatrolgroups[] = $dbw->addQuotes( $group );
230 }
231 }
232 # Flag our recent bot edits
233 if ( !empty( $botgroups ) ) {
234 $botwhere = implode( ',', $botgroups );
235 $botusers = array();
236
237 $this->output( "Flagging bot account edits...\n" );
238
239 # Find all users that are bots
240 $sql = "SELECT DISTINCT user_name FROM $usergroups, $user " .
241 "WHERE ug_group IN($botwhere) AND user_id = ug_user";
242 $res = $dbw->query( $sql, DB_MASTER );
243
244 foreach ( $res as $obj ) {
245 $botusers[] = $dbw->addQuotes( $obj->user_name );
246 }
247 # Fill in the rc_bot field
248 if ( !empty( $botusers ) ) {
249 $botwhere = implode( ',', $botusers );
250 $sql2 = "UPDATE $recentchanges SET rc_bot=1 " .
251 "WHERE rc_user_text IN($botwhere)";
252 $dbw->query( $sql2 );
253 }
254 }
255 global $wgMiserMode;
256 # Flag our recent autopatrolled edits
257 if ( !$wgMiserMode && !empty( $autopatrolgroups ) ) {
258 $patrolwhere = implode( ',', $autopatrolgroups );
259 $patrolusers = array();
260
261 $this->output( "Flagging auto-patrolled edits...\n" );
262
263 # Find all users in RC with autopatrol rights
264 $sql = "SELECT DISTINCT user_name FROM $usergroups, $user " .
265 "WHERE ug_group IN($patrolwhere) AND user_id = ug_user";
266 $res = $dbw->query( $sql, DB_MASTER );
267
268 foreach ( $res as $obj ) {
269 $patrolusers[] = $dbw->addQuotes( $obj->user_name );
270 }
271
272 # Fill in the rc_patrolled field
273 if ( !empty( $patrolusers ) ) {
274 $patrolwhere = implode( ',', $patrolusers );
275 $sql2 = "UPDATE $recentchanges SET rc_patrolled=1 " .
276 "WHERE rc_user_text IN($patrolwhere)";
277 $dbw->query( $sql2 );
278 }
279 }
280 }
281
282 /**
283 * Purge cached feeds in $messageMemc
284 */
285 private function purgeFeeds() {
286 global $wgFeedClasses, $messageMemc;
287
288 $this->output( "Deleting feed timestamps.\n" );
289
290 foreach ( $wgFeedClasses as $feed => $className ) {
291 $messageMemc->delete( wfMemcKey( 'rcfeed', $feed, 'timestamp' ) ); # Good enough for now.
292 }
293 }
294
295 }
296
297 $maintClass = "RebuildRecentchanges";
298 require_once( DO_MAINTENANCE );