One less $wgTitle, and use dbw->update() instead of raw sql
[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_old_len' => $lastSize,
141 'rc_new_len' => $size,
142 ), array(
143 'rc_cur_id' => $lastCurId,
144 'rc_this_oldid' => $obj->rc_this_oldid,
145 ),
146 __METHOD__
147 );
148
149 $lastOldId = intval( $obj->rc_this_oldid );
150 $lastSize = $size;
151 }
152 }
153 }
154
155 /**
156 * Rebuild pass 3
157 * DOCUMENT ME!
158 */
159 private function rebuildRecentChangesTablePass3() {
160 $dbw = wfGetDB( DB_MASTER );
161
162 $this->output( "Loading from user, page, and logging tables...\n" );
163
164 global $wgRCMaxAge, $wgLogTypes, $wgLogRestrictions;
165 // Some logs don't go in RC. This should check for that
166 $basicRCLogs = array_diff( $wgLogTypes, array_keys( $wgLogRestrictions ) );
167
168 // Escape...blah blah
169 $selectLogs = array();
170 foreach ( $basicRCLogs as $logtype ) {
171 $safetype = $dbw->strencode( $logtype );
172 $selectLogs[] = "'$safetype'";
173 }
174
175 $cutoff = time() - $wgRCMaxAge;
176 list( $logging, $page ) = $dbw->tableNamesN( 'logging', 'page' );
177 $dbw->insertSelect( 'recentchanges', array( 'user', "$logging LEFT JOIN $page ON (log_namespace=page_namespace AND log_title=page_title)" ),
178 array(
179 'rc_timestamp' => 'log_timestamp',
180 'rc_cur_time' => 'log_timestamp',
181 'rc_user' => 'log_user',
182 'rc_user_text' => 'user_name',
183 'rc_namespace' => 'log_namespace',
184 'rc_title' => 'log_title',
185 'rc_comment' => 'log_comment',
186 'rc_minor' => 0,
187 'rc_bot' => 0,
188 'rc_patrolled' => 1,
189 'rc_new' => 0,
190 'rc_this_oldid' => 0,
191 'rc_last_oldid' => 0,
192 'rc_type' => RC_LOG,
193 'rc_cur_id' => $dbw->cascadingDeletes() ? 'page_id' : 'COALESCE(page_id, 0)',
194 'rc_log_type' => 'log_type',
195 'rc_log_action' => 'log_action',
196 'rc_logid' => 'log_id',
197 'rc_params' => 'log_params',
198 'rc_deleted' => 'log_deleted'
199 ), array(
200 'log_timestamp > ' . $dbw->addQuotes( $dbw->timestamp( $cutoff ) ),
201 'log_user=user_id',
202 'log_type IN(' . implode( ',', $selectLogs ) . ')'
203 ), __METHOD__,
204 array(), // INSERT options
205 array( 'ORDER BY' => 'log_timestamp DESC', 'LIMIT' => 5000 ) // SELECT options
206 );
207 }
208
209 /**
210 * Rebuild pass 4
211 * DOCUMENT ME!
212 */
213 private function rebuildRecentChangesTablePass4() {
214 global $wgGroupPermissions, $wgUseRCPatrol;
215
216 $dbw = wfGetDB( DB_MASTER );
217
218 list( $recentchanges, $usergroups, $user ) = $dbw->tableNamesN( 'recentchanges', 'user_groups', 'user' );
219
220 $botgroups = $autopatrolgroups = array();
221 foreach ( $wgGroupPermissions as $group => $rights ) {
222 if ( isset( $rights['bot'] ) && $rights['bot'] ) {
223 $botgroups[] = $dbw->addQuotes( $group );
224 }
225 if ( $wgUseRCPatrol && isset( $rights['autopatrol'] ) && $rights['autopatrol'] ) {
226 $autopatrolgroups[] = $dbw->addQuotes( $group );
227 }
228 }
229 # Flag our recent bot edits
230 if ( !empty( $botgroups ) ) {
231 $botwhere = implode( ',', $botgroups );
232 $botusers = array();
233
234 $this->output( "Flagging bot account edits...\n" );
235
236 # Find all users that are bots
237 $sql = "SELECT DISTINCT user_name FROM $usergroups, $user " .
238 "WHERE ug_group IN($botwhere) AND user_id = ug_user";
239 $res = $dbw->query( $sql, DB_MASTER );
240
241 foreach ( $res as $obj ) {
242 $botusers[] = $dbw->addQuotes( $obj->user_name );
243 }
244 # Fill in the rc_bot field
245 if ( !empty( $botusers ) ) {
246 $botwhere = implode( ',', $botusers );
247 $sql2 = "UPDATE $recentchanges SET rc_bot=1 " .
248 "WHERE rc_user_text IN($botwhere)";
249 $dbw->query( $sql2 );
250 }
251 }
252 global $wgMiserMode;
253 # Flag our recent autopatrolled edits
254 if ( !$wgMiserMode && !empty( $autopatrolgroups ) ) {
255 $patrolwhere = implode( ',', $autopatrolgroups );
256 $patrolusers = array();
257
258 $this->output( "Flagging auto-patrolled edits...\n" );
259
260 # Find all users in RC with autopatrol rights
261 $sql = "SELECT DISTINCT user_name FROM $usergroups, $user " .
262 "WHERE ug_group IN($patrolwhere) AND user_id = ug_user";
263 $res = $dbw->query( $sql, DB_MASTER );
264
265 foreach ( $res as $obj ) {
266 $patrolusers[] = $dbw->addQuotes( $obj->user_name );
267 }
268
269 # Fill in the rc_patrolled field
270 if ( !empty( $patrolusers ) ) {
271 $patrolwhere = implode( ',', $patrolusers );
272 $sql2 = "UPDATE $recentchanges SET rc_patrolled=1 " .
273 "WHERE rc_user_text IN($patrolwhere)";
274 $dbw->query( $sql2 );
275 }
276 }
277 }
278
279 /**
280 * Purge cached feeds in $messageMemc
281 */
282 private function purgeFeeds() {
283 global $wgFeedClasses, $messageMemc;
284
285 $this->output( "Deleting feed timestamps.\n" );
286
287 foreach ( $wgFeedClasses as $feed => $className ) {
288 $messageMemc->delete( wfMemcKey( 'rcfeed', $feed, 'timestamp' ) ); # Good enough for now.
289 }
290 }
291
292 }
293
294 $maintClass = "RebuildRecentchanges";
295 require_once( DO_MAINTENANCE );