Merge "installer: Choose CACHE_ACCEL as the default if available"
[lhc/web/wiklou.git] / maintenance / rebuildrecentchanges.php
1 <?php
2 /**
3 * Rebuild recent changes from scratch. This takes several hours,
4 * 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 * @file
22 * @ingroup Maintenance
23 * @todo Document
24 */
25
26 require_once __DIR__ . '/Maintenance.php';
27
28 /**
29 * Maintenance script that rebuilds recent changes from scratch.
30 *
31 * @ingroup Maintenance
32 */
33 class RebuildRecentchanges extends Maintenance {
34 public function __construct() {
35 parent::__construct();
36 $this->mDescription = "Rebuild recent changes";
37 }
38
39 public function execute() {
40 $this->rebuildRecentChangesTablePass1();
41 $this->rebuildRecentChangesTablePass2();
42 $this->rebuildRecentChangesTablePass3();
43 $this->rebuildRecentChangesTablePass4();
44 $this->purgeFeeds();
45 $this->output( "Done.\n" );
46 }
47
48 /**
49 * Rebuild pass 1: Insert `recentchanges` entries for page revisions.
50 */
51 private function rebuildRecentChangesTablePass1() {
52 $dbw = $this->getDB( DB_MASTER );
53
54 $dbw->delete( 'recentchanges', '*' );
55
56 $this->output( "Loading from page and revision tables...\n" );
57
58 global $wgRCMaxAge;
59
60 $this->output( '$wgRCMaxAge=' . $wgRCMaxAge );
61 $days = $wgRCMaxAge / 24 / 3600;
62 if ( intval( $days ) == $days ) {
63 $this->output( " (" . $days . " days)\n" );
64 } else {
65 $this->output( " (approx. " . intval( $days ) . " days)\n" );
66 }
67
68 $cutoff = time() - $wgRCMaxAge;
69 $dbw->insertSelect( 'recentchanges', array( 'page', 'revision' ),
70 array(
71 'rc_timestamp' => 'rev_timestamp',
72 'rc_user' => 'rev_user',
73 'rc_user_text' => 'rev_user_text',
74 'rc_namespace' => 'page_namespace',
75 'rc_title' => 'page_title',
76 'rc_comment' => 'rev_comment',
77 'rc_minor' => 'rev_minor_edit',
78 'rc_bot' => 0,
79 'rc_new' => 'page_is_new',
80 'rc_cur_id' => 'page_id',
81 'rc_this_oldid' => 'rev_id',
82 'rc_last_oldid' => 0, // is this ok?
83 'rc_type' => $dbw->conditional( 'page_is_new != 0', RC_NEW, RC_EDIT ),
84 'rc_source' => $dbw->conditional(
85 'page_is_new != 0',
86 $dbw->addQuotes( RecentChange::SRC_NEW ),
87 $dbw->addQuotes( RecentChange::SRC_EDIT )
88 ),
89 'rc_deleted' => 'rev_deleted'
90 ),
91 array(
92 'rev_timestamp > ' . $dbw->addQuotes( $dbw->timestamp( $cutoff ) ),
93 'rev_page=page_id'
94 ),
95 __METHOD__,
96 array(), // INSERT options
97 array( 'ORDER BY' => 'rev_timestamp DESC', 'LIMIT' => 5000 ) // SELECT options
98 );
99 }
100
101 /**
102 * Rebuild pass 2: Enhance entries for page revisions with references to the previous revision
103 * (rc_last_oldid, rc_new etc.) and size differences (rc_old_len, rc_new_len).
104 */
105 private function rebuildRecentChangesTablePass2() {
106 $dbw = $this->getDB( DB_MASTER );
107 list( $recentchanges, $revision ) = $dbw->tableNamesN( 'recentchanges', 'revision' );
108
109 $this->output( "Updating links and size differences...\n" );
110
111 # Fill in the rc_last_oldid field, which points to the previous edit
112 $sql = "SELECT rc_cur_id,rc_this_oldid,rc_timestamp FROM $recentchanges " .
113 "ORDER BY rc_cur_id,rc_timestamp";
114 $res = $dbw->query( $sql, DB_MASTER );
115
116 $lastCurId = 0;
117 $lastOldId = 0;
118 foreach ( $res as $obj ) {
119 $new = 0;
120 if ( $obj->rc_cur_id != $lastCurId ) {
121 # Switch! Look up the previous last edit, if any
122 $lastCurId = intval( $obj->rc_cur_id );
123 $emit = $obj->rc_timestamp;
124 $sql2 = "SELECT rev_id,rev_len FROM $revision " .
125 "WHERE rev_page={$lastCurId} " .
126 "AND rev_timestamp<'{$emit}' ORDER BY rev_timestamp DESC";
127 $sql2 = $dbw->limitResult( $sql2, 1, false );
128 $res2 = $dbw->query( $sql2 );
129 $row = $dbw->fetchObject( $res2 );
130 if ( $row ) {
131 $lastOldId = intval( $row->rev_id );
132 # Grab the last text size if available
133 $lastSize = !is_null( $row->rev_len ) ? intval( $row->rev_len ) : null;
134 } else {
135 # No previous edit
136 $lastOldId = 0;
137 $lastSize = null;
138 $new = 1; // probably true
139 }
140 }
141 if ( $lastCurId == 0 ) {
142 $this->output( "Uhhh, something wrong? No curid\n" );
143 } else {
144 # Grab the entry's text size
145 $size = $dbw->selectField( 'revision', 'rev_len', array( 'rev_id' => $obj->rc_this_oldid ) );
146
147 $dbw->update( 'recentchanges',
148 array(
149 'rc_last_oldid' => $lastOldId,
150 'rc_new' => $new,
151 'rc_type' => $new,
152 'rc_source' => $new === 1 ? RecentChange::SRC_NEW : RecentChange::SRC_EDIT,
153 'rc_old_len' => $lastSize,
154 'rc_new_len' => $size,
155 ), array(
156 'rc_cur_id' => $lastCurId,
157 'rc_this_oldid' => $obj->rc_this_oldid,
158 ),
159 __METHOD__
160 );
161
162 $lastOldId = intval( $obj->rc_this_oldid );
163 $lastSize = $size;
164 }
165 }
166 }
167
168 /**
169 * Rebuild pass 3: Insert `recentchanges` entries for action logs.
170 */
171 private function rebuildRecentChangesTablePass3() {
172 $dbw = $this->getDB( DB_MASTER );
173
174 $this->output( "Loading from user, page, and logging tables...\n" );
175
176 global $wgRCMaxAge, $wgLogTypes, $wgLogRestrictions;
177 // Some logs don't go in RC. This should check for that
178 $basicRCLogs = array_diff( $wgLogTypes, array_keys( $wgLogRestrictions ) );
179
180 $cutoff = time() - $wgRCMaxAge;
181 list( $logging, $page ) = $dbw->tableNamesN( 'logging', 'page' );
182 $dbw->insertSelect(
183 'recentchanges',
184 array(
185 'user',
186 "$logging LEFT JOIN $page ON (log_namespace=page_namespace AND log_title=page_title)"
187 ),
188 array(
189 'rc_timestamp' => 'log_timestamp',
190 'rc_user' => 'log_user',
191 'rc_user_text' => 'user_name',
192 'rc_namespace' => 'log_namespace',
193 'rc_title' => 'log_title',
194 'rc_comment' => 'log_comment',
195 'rc_minor' => 0,
196 'rc_bot' => 0,
197 'rc_patrolled' => 1,
198 'rc_new' => 0,
199 'rc_this_oldid' => 0,
200 'rc_last_oldid' => 0,
201 'rc_type' => RC_LOG,
202 'rc_source' => $dbw->addQuotes( RecentChange::SRC_LOG ),
203 'rc_cur_id' => $dbw->cascadingDeletes() ? 'page_id' : 'COALESCE(page_id, 0)',
204 'rc_log_type' => 'log_type',
205 'rc_log_action' => 'log_action',
206 'rc_logid' => 'log_id',
207 'rc_params' => 'log_params',
208 'rc_deleted' => 'log_deleted'
209 ),
210 array(
211 'log_timestamp > ' . $dbw->addQuotes( $dbw->timestamp( $cutoff ) ),
212 'log_user=user_id',
213 'log_type' => $basicRCLogs,
214 ),
215 __METHOD__,
216 array(), // INSERT options
217 array( 'ORDER BY' => 'log_timestamp DESC', 'LIMIT' => 5000 ) // SELECT options
218 );
219 }
220
221 /**
222 * Rebuild pass 4: Mark bot and autopatrolled entries.
223 */
224 private function rebuildRecentChangesTablePass4() {
225 global $wgUseRCPatrol;
226
227 $dbw = $this->getDB( DB_MASTER );
228
229 list( $recentchanges, $usergroups, $user ) =
230 $dbw->tableNamesN( 'recentchanges', 'user_groups', 'user' );
231
232 $botgroups = User::getGroupsWithPermission( 'bot' );
233 $autopatrolgroups = $wgUseRCPatrol ? User::getGroupsWithPermission( 'autopatrol' ) : array();
234 # Flag our recent bot edits
235 if ( !empty( $botgroups ) ) {
236 $botwhere = $dbw->makeList( $botgroups );
237 $botusers = array();
238
239 $this->output( "Flagging bot account edits...\n" );
240
241 # Find all users that are bots
242 $sql = "SELECT DISTINCT user_name FROM $usergroups, $user " .
243 "WHERE ug_group IN($botwhere) AND user_id = ug_user";
244 $res = $dbw->query( $sql, DB_MASTER );
245
246 foreach ( $res as $obj ) {
247 $botusers[] = $dbw->addQuotes( $obj->user_name );
248 }
249 # Fill in the rc_bot field
250 if ( !empty( $botusers ) ) {
251 $botwhere = implode( ',', $botusers );
252 $sql2 = "UPDATE $recentchanges SET rc_bot=1 " .
253 "WHERE rc_user_text IN($botwhere)";
254 $dbw->query( $sql2 );
255 }
256 }
257 global $wgMiserMode;
258 # Flag our recent autopatrolled edits
259 if ( !$wgMiserMode && !empty( $autopatrolgroups ) ) {
260 $patrolwhere = $dbw->makeList( $autopatrolgroups );
261 $patrolusers = array();
262
263 $this->output( "Flagging auto-patrolled edits...\n" );
264
265 # Find all users in RC with autopatrol rights
266 $sql = "SELECT DISTINCT user_name FROM $usergroups, $user " .
267 "WHERE ug_group IN($patrolwhere) AND user_id = ug_user";
268 $res = $dbw->query( $sql, DB_MASTER );
269
270 foreach ( $res as $obj ) {
271 $patrolusers[] = $dbw->addQuotes( $obj->user_name );
272 }
273
274 # Fill in the rc_patrolled field
275 if ( !empty( $patrolusers ) ) {
276 $patrolwhere = implode( ',', $patrolusers );
277 $sql2 = "UPDATE $recentchanges SET rc_patrolled=1 " .
278 "WHERE rc_user_text IN($patrolwhere)";
279 $dbw->query( $sql2 );
280 }
281 }
282 }
283
284 /**
285 * Purge cached feeds in $messageMemc
286 */
287 private function purgeFeeds() {
288 global $wgFeedClasses, $messageMemc;
289
290 $this->output( "Deleting feed timestamps.\n" );
291
292 foreach ( $wgFeedClasses as $feed => $className ) {
293 $messageMemc->delete( wfMemcKey( 'rcfeed', $feed, 'timestamp' ) ); # Good enough for now.
294 }
295 }
296 }
297
298 $maintClass = "RebuildRecentchanges";
299 require_once RUN_MAINTENANCE_IF_MAIN;