Merge maintenance-work branch (now with less errors!):
[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( "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 global $wgTitle;
35 $wgTitle = Title::newFromText( "Rebuild recent changes script" );
36 $this->rebuildRecentChangesTablePass1();
37 $this->rebuildRecentChangesTablePass2();
38 $this->rebuildRecentChangesTablePass3();
39 $this->rebuildRecentChangesTablePass4();
40 $this->output( "Done.\n" );
41 }
42
43 /**
44 * Rebuild pass 1
45 * DOCUMENT ME!
46 */
47 function rebuildRecentChangesTablePass1()
48 {
49 $dbw = wfGetDB( DB_MASTER );
50
51 $dbw->delete( 'recentchanges', '*' );
52
53 $this->output( "Loading from page and revision tables...\n" );
54
55 global $wgRCMaxAge;
56
57 $this->output( '$wgRCMaxAge=' . $wgRCMaxAge );
58 $days = $wgRCMaxAge / 24 / 3600;
59 if ( intval($days) == $days ) {
60 $this->output( " (" . $days . " days)\n" );
61 } else {
62 $this->output( " (approx. " . intval($days) . " days)\n" );
63 }
64
65 $cutoff = time() - $wgRCMaxAge;
66 $dbw->insertSelect( 'recentchanges', array( 'page', 'revision' ),
67 array(
68 'rc_timestamp' => 'rev_timestamp',
69 'rc_cur_time' => 'rev_timestamp',
70 'rc_user' => 'rev_user',
71 'rc_user_text' => 'rev_user_text',
72 'rc_namespace' => 'page_namespace',
73 'rc_title' => 'page_title',
74 'rc_comment' => 'rev_comment',
75 'rc_minor' => 'rev_minor_edit',
76 'rc_bot' => 0,
77 'rc_new' => 'page_is_new',
78 'rc_cur_id' => 'page_id',
79 'rc_this_oldid' => 'rev_id',
80 'rc_last_oldid' => 0, // is this ok?
81 'rc_type' => $dbw->conditional( 'page_is_new != 0', RC_NEW, RC_EDIT ),
82 'rc_deleted' => 'rev_deleted'
83 ), array(
84 'rev_timestamp > ' . $dbw->addQuotes( $dbw->timestamp( $cutoff ) ),
85 'rev_page=page_id'
86 ), __METHOD__,
87 array(), // INSERT options
88 array( 'ORDER BY' => 'rev_timestamp DESC', 'LIMIT' => 5000 ) // SELECT options
89 );
90 }
91
92 /**
93 * Rebuild pass 2
94 * DOCUMENT ME!
95 */
96 private function rebuildRecentChangesTablePass2() {
97 $dbw = wfGetDB( DB_MASTER );
98 list ($recentchanges, $revision) = $dbw->tableNamesN( 'recentchanges', 'revision' );
99
100 $this->output( "Updating links and size differences...\n" );
101
102 # Fill in the rc_last_oldid field, which points to the previous edit
103 $sql = "SELECT rc_cur_id,rc_this_oldid,rc_timestamp FROM $recentchanges " .
104 "ORDER BY rc_cur_id,rc_timestamp";
105 $res = $dbw->query( $sql, DB_MASTER );
106
107 $lastCurId = 0;
108 $lastOldId = 0;
109 while ( $obj = $dbw->fetchObject( $res ) ) {
110 $new = 0;
111 if( $obj->rc_cur_id != $lastCurId ) {
112 # Switch! Look up the previous last edit, if any
113 $lastCurId = intval( $obj->rc_cur_id );
114 $emit = $obj->rc_timestamp;
115 $sql2 = "SELECT rev_id,rev_len FROM $revision " .
116 "WHERE rev_page={$lastCurId} ".
117 "AND rev_timestamp<'{$emit}' ORDER BY rev_timestamp DESC LIMIT 1";
118 $res2 = $dbw->query( $sql2 );
119 if( $row = $dbw->fetchObject( $res2 ) ) {
120 $lastOldId = intval($row->rev_id);
121 # Grab the last text size if available
122 $lastSize = !is_null($row->rev_len) ? intval($row->rev_len) : 'NULL';
123 } else {
124 # No previous edit
125 $lastOldId = 0;
126 $lastSize = 'NULL';
127 $new = 1; // probably true
128 }
129 $dbw->freeResult( $res2 );
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 $sql3 = "UPDATE $recentchanges SET rc_last_oldid=$lastOldId,rc_new=$new,rc_type=$new," .
139 "rc_old_len=$lastSize,rc_new_len=$size " .
140 "WHERE rc_cur_id={$lastCurId} AND rc_this_oldid={$obj->rc_this_oldid}";
141 $dbw->query( $sql3 );
142
143 $lastOldId = intval( $obj->rc_this_oldid );
144 $lastSize = $size;
145 }
146 }
147 $dbw->freeResult( $res );
148 }
149
150 /**
151 * Rebuild pass 3
152 * DOCUMENT ME!
153 */
154 private function rebuildRecentChangesTablePass3() {
155 $dbw = wfGetDB( DB_MASTER );
156
157 $this->output( "Loading from user, page, and logging tables...\n" );
158
159 global $wgRCMaxAge, $wgLogTypes, $wgLogRestrictions;
160 // Some logs don't go in RC. This should check for that
161 $basicRCLogs = array_diff( $wgLogTypes, array_keys( $wgLogRestrictions ) );
162
163 // Escape...blah blah
164 $selectLogs = array();
165 foreach( $basicRCLogs as $logtype ) {
166 $safetype = $dbw->strencode( $logtype );
167 $selectLogs[] = "'$safetype'";
168 }
169
170 $cutoff = time() - $wgRCMaxAge;
171 list($logging, $page) = $dbw->tableNamesN( 'logging', 'page' );
172 $dbw->insertSelect( 'recentchanges', array( 'user', "$logging LEFT JOIN $page ON (log_namespace=page_namespace AND log_title=page_title)" ),
173 array(
174 'rc_timestamp' => 'log_timestamp',
175 'rc_cur_time' => 'log_timestamp',
176 'rc_user' => 'log_user',
177 'rc_user_text' => 'user_name',
178 'rc_namespace' => 'log_namespace',
179 'rc_title' => 'log_title',
180 'rc_comment' => 'log_comment',
181 'rc_minor' => 0,
182 'rc_bot' => 0,
183 'rc_patrolled' => 1,
184 'rc_new' => 0,
185 'rc_this_oldid' => 0,
186 'rc_last_oldid' => 0,
187 'rc_type' => RC_LOG,
188 'rc_cur_id' => 'COALESCE(page_id, 0)',
189 'rc_log_type' => 'log_type',
190 'rc_log_action' => 'log_action',
191 'rc_logid' => 'log_id',
192 'rc_params' => 'log_params',
193 'rc_deleted' => 'log_deleted'
194 ), array(
195 'log_timestamp > ' . $dbw->addQuotes( $dbw->timestamp( $cutoff ) ),
196 'log_user=user_id',
197 'log_type IN(' . implode(',',$selectLogs) . ')'
198 ), __METHOD__,
199 array(), // INSERT options
200 array( 'ORDER BY' => 'log_timestamp DESC', 'LIMIT' => 5000 ) // SELECT options
201 );
202 }
203
204 /**
205 * Rebuild pass 4
206 * DOCUMENT ME!
207 */
208 private function rebuildRecentChangesTablePass4() {
209 global $wgGroupPermissions, $wgUseRCPatrol;
210
211 $dbw = wfGetDB( DB_MASTER );
212
213 list($recentchanges,$usergroups,$user) = $dbw->tableNamesN( 'recentchanges', 'user_groups', 'user' );
214
215 $botgroups = $autopatrolgroups = array();
216 foreach( $wgGroupPermissions as $group => $rights ) {
217 if( isset( $rights['bot'] ) && $rights['bot'] == true ) {
218 $botgroups[] = $dbw->addQuotes( $group );
219 }
220 if( $wgUseRCPatrol && isset( $rights['autopatrol'] ) && $rights['autopatrol'] == true ) {
221 $autopatrolgroups[] = $dbw->addQuotes( $group );
222 }
223 }
224 # Flag our recent bot edits
225 if( !empty($botgroups) ) {
226 $botwhere = implode(',',$botgroups);
227 $botusers = array();
228
229 $this->output( "Flagging bot account edits...\n" );
230
231 # Find all users that are bots
232 $sql = "SELECT DISTINCT user_name FROM $usergroups, $user " .
233 "WHERE ug_group IN($botwhere) AND user_id = ug_user";
234 $res = $dbw->query( $sql, DB_MASTER );
235
236 while( $obj = $dbw->fetchObject( $res ) ) {
237 $botusers[] = $dbw->addQuotes( $obj->user_name );
238 }
239 # Fill in the rc_bot field
240 if( !empty($botusers) ) {
241 $botwhere = implode(',',$botusers);
242 $sql2 = "UPDATE $recentchanges SET rc_bot=1 " .
243 "WHERE rc_user_text IN($botwhere)";
244 $dbw->query( $sql2 );
245 }
246 }
247 global $wgMiserMode;
248 # Flag our recent autopatrolled edits
249 if( !$wgMiserMode && !empty($autopatrolgroups) ) {
250 $patrolwhere = implode(',',$autopatrolgroups);
251 $patrolusers = array();
252
253 $this->output( "Flagging auto-patrolled edits...\n" );
254
255 # Find all users in RC with autopatrol rights
256 $sql = "SELECT DISTINCT user_name FROM $usergroups, $user " .
257 "WHERE ug_group IN($patrolwhere) AND user_id = ug_user";
258 $res = $dbw->query( $sql, DB_MASTER );
259
260 while( $obj = $dbw->fetchObject( $res ) ) {
261 $patrolusers[] = $dbw->addQuotes( $obj->user_name );
262 }
263
264 # Fill in the rc_patrolled field
265 if( !empty($patrolusers) ) {
266 $patrolwhere = implode(',',$patrolusers);
267 $sql2 = "UPDATE $recentchanges SET rc_patrolled=1 " .
268 "WHERE rc_user_text IN($patrolwhere)";
269 $dbw->query( $sql2 );
270 }
271 }
272
273 $dbw->freeResult( $res );
274 }
275 }
276
277 $maintClass = "RebuildRecentchanges";
278 require_once( DO_MAINTENANCE );