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