MediaWiki  1.23.2
rebuildrecentchanges.php
Go to the documentation of this file.
1 <?php
26 require_once __DIR__ . '/Maintenance.php';
27 
34  public function __construct() {
35  parent::__construct();
36  $this->mDescription = "Rebuild recent changes";
37  }
38 
39  public function execute() {
44  $this->purgeFeeds();
45  $this->output( "Done.\n" );
46  }
47 
52  private function rebuildRecentChangesTablePass1() {
53  $dbw = wfGetDB( DB_MASTER );
54 
55  $dbw->delete( 'recentchanges', '*' );
56 
57  $this->output( "Loading from page and revision tables...\n" );
58 
59  global $wgRCMaxAge;
60 
61  $this->output( '$wgRCMaxAge=' . $wgRCMaxAge );
62  $days = $wgRCMaxAge / 24 / 3600;
63  if ( intval( $days ) == $days ) {
64  $this->output( " (" . $days . " days)\n" );
65  } else {
66  $this->output( " (approx. " . intval( $days ) . " days)\n" );
67  }
68 
69  $cutoff = time() - $wgRCMaxAge;
70  $dbw->insertSelect( 'recentchanges', array( 'page', 'revision' ),
71  array(
72  'rc_timestamp' => 'rev_timestamp',
73  'rc_user' => 'rev_user',
74  'rc_user_text' => 'rev_user_text',
75  'rc_namespace' => 'page_namespace',
76  'rc_title' => 'page_title',
77  'rc_comment' => 'rev_comment',
78  'rc_minor' => 'rev_minor_edit',
79  'rc_bot' => 0,
80  'rc_new' => 'page_is_new',
81  'rc_cur_id' => 'page_id',
82  'rc_this_oldid' => 'rev_id',
83  'rc_last_oldid' => 0, // is this ok?
84  'rc_type' => $dbw->conditional( 'page_is_new != 0', RC_NEW, RC_EDIT ),
85  'rc_source' => $dbw->conditional( 'page_is_new != 0', $dbw->addQuotes( RecentChange::SRC_NEW ), $dbw->addQuotes( RecentChange::SRC_EDIT ) ),
86  'rc_deleted' => 'rev_deleted'
87  ), array(
88  'rev_timestamp > ' . $dbw->addQuotes( $dbw->timestamp( $cutoff ) ),
89  'rev_page=page_id'
90  ), __METHOD__,
91  array(), // INSERT options
92  array( 'ORDER BY' => 'rev_timestamp DESC', 'LIMIT' => 5000 ) // SELECT options
93  );
94  }
95 
100  private function rebuildRecentChangesTablePass2() {
101  $dbw = wfGetDB( DB_MASTER );
102  list( $recentchanges, $revision ) = $dbw->tableNamesN( 'recentchanges', 'revision' );
103 
104  $this->output( "Updating links and size differences...\n" );
105 
106  # Fill in the rc_last_oldid field, which points to the previous edit
107  $sql = "SELECT rc_cur_id,rc_this_oldid,rc_timestamp FROM $recentchanges " .
108  "ORDER BY rc_cur_id,rc_timestamp";
109  $res = $dbw->query( $sql, DB_MASTER );
110 
111  $lastCurId = 0;
112  $lastOldId = 0;
113  foreach ( $res as $obj ) {
114  $new = 0;
115  if ( $obj->rc_cur_id != $lastCurId ) {
116  # Switch! Look up the previous last edit, if any
117  $lastCurId = intval( $obj->rc_cur_id );
118  $emit = $obj->rc_timestamp;
119  $sql2 = "SELECT rev_id,rev_len FROM $revision " .
120  "WHERE rev_page={$lastCurId} " .
121  "AND rev_timestamp<'{$emit}' ORDER BY rev_timestamp DESC";
122  $sql2 = $dbw->limitResult( $sql2, 1, false );
123  $res2 = $dbw->query( $sql2 );
124  $row = $dbw->fetchObject( $res2 );
125  if ( $row ) {
126  $lastOldId = intval( $row->rev_id );
127  # Grab the last text size if available
128  $lastSize = !is_null( $row->rev_len ) ? intval( $row->rev_len ) : null;
129  } else {
130  # No previous edit
131  $lastOldId = 0;
132  $lastSize = null;
133  $new = 1; // probably true
134  }
135  }
136  if ( $lastCurId == 0 ) {
137  $this->output( "Uhhh, something wrong? No curid\n" );
138  } else {
139  # Grab the entry's text size
140  $size = $dbw->selectField( 'revision', 'rev_len', array( 'rev_id' => $obj->rc_this_oldid ) );
141 
142  $dbw->update( 'recentchanges',
143  array(
144  'rc_last_oldid' => $lastOldId,
145  'rc_new' => $new,
146  'rc_type' => $new,
147  'rc_source' => $new === 1 ? RecentChange::SRC_NEW : RecentChange::SRC_EDIT,
148  'rc_old_len' => $lastSize,
149  'rc_new_len' => $size,
150  ), array(
151  'rc_cur_id' => $lastCurId,
152  'rc_this_oldid' => $obj->rc_this_oldid,
153  ),
154  __METHOD__
155  );
156 
157  $lastOldId = intval( $obj->rc_this_oldid );
158  $lastSize = $size;
159  }
160  }
161  }
162 
167  private function rebuildRecentChangesTablePass3() {
168  $dbw = wfGetDB( DB_MASTER );
169 
170  $this->output( "Loading from user, page, and logging tables...\n" );
171 
172  global $wgRCMaxAge, $wgLogTypes, $wgLogRestrictions;
173  // Some logs don't go in RC. This should check for that
174  $basicRCLogs = array_diff( $wgLogTypes, array_keys( $wgLogRestrictions ) );
175 
176  // Escape...blah blah
177  $selectLogs = array();
178  foreach ( $basicRCLogs as $logtype ) {
179  $safetype = $dbw->strencode( $logtype );
180  $selectLogs[] = "'$safetype'";
181  }
182 
183  $cutoff = time() - $wgRCMaxAge;
184  list( $logging, $page ) = $dbw->tableNamesN( 'logging', 'page' );
185  $dbw->insertSelect( 'recentchanges', array( 'user', "$logging LEFT JOIN $page ON (log_namespace=page_namespace AND log_title=page_title)" ),
186  array(
187  'rc_timestamp' => 'log_timestamp',
188  'rc_user' => 'log_user',
189  'rc_user_text' => 'user_name',
190  'rc_namespace' => 'log_namespace',
191  'rc_title' => 'log_title',
192  'rc_comment' => 'log_comment',
193  'rc_minor' => 0,
194  'rc_bot' => 0,
195  'rc_patrolled' => 1,
196  'rc_new' => 0,
197  'rc_this_oldid' => 0,
198  'rc_last_oldid' => 0,
199  'rc_type' => RC_LOG,
200  'rc_source' => $dbw->addQuotes( RecentChange::SRC_LOG ),
201  'rc_cur_id' => $dbw->cascadingDeletes() ? 'page_id' : 'COALESCE(page_id, 0)',
202  'rc_log_type' => 'log_type',
203  'rc_log_action' => 'log_action',
204  'rc_logid' => 'log_id',
205  'rc_params' => 'log_params',
206  'rc_deleted' => 'log_deleted'
207  ), array(
208  'log_timestamp > ' . $dbw->addQuotes( $dbw->timestamp( $cutoff ) ),
209  'log_user=user_id',
210  'log_type IN(' . implode( ',', $selectLogs ) . ')'
211  ), __METHOD__,
212  array(), // INSERT options
213  array( 'ORDER BY' => 'log_timestamp DESC', 'LIMIT' => 5000 ) // SELECT options
214  );
215  }
216 
221  private function rebuildRecentChangesTablePass4() {
222  global $wgUseRCPatrol;
223 
224  $dbw = wfGetDB( DB_MASTER );
225 
226  list( $recentchanges, $usergroups, $user ) = $dbw->tableNamesN( 'recentchanges', 'user_groups', 'user' );
227 
228  $botgroups = User::getGroupsWithPermission( 'bot' );
229  $autopatrolgroups = $wgUseRCPatrol ? User::getGroupsWithPermission( 'autopatrol' ) : array();
230  # Flag our recent bot edits
231  if ( !empty( $botgroups ) ) {
232  $botwhere = $dbw->makeList( $botgroups );
233  $botusers = array();
234 
235  $this->output( "Flagging bot account edits...\n" );
236 
237  # Find all users that are bots
238  $sql = "SELECT DISTINCT user_name FROM $usergroups, $user " .
239  "WHERE ug_group IN($botwhere) AND user_id = ug_user";
240  $res = $dbw->query( $sql, DB_MASTER );
241 
242  foreach ( $res as $obj ) {
243  $botusers[] = $dbw->addQuotes( $obj->user_name );
244  }
245  # Fill in the rc_bot field
246  if ( !empty( $botusers ) ) {
247  $botwhere = implode( ',', $botusers );
248  $sql2 = "UPDATE $recentchanges SET rc_bot=1 " .
249  "WHERE rc_user_text IN($botwhere)";
250  $dbw->query( $sql2 );
251  }
252  }
253  global $wgMiserMode;
254  # Flag our recent autopatrolled edits
255  if ( !$wgMiserMode && !empty( $autopatrolgroups ) ) {
256  $patrolwhere = $dbw->makeList( $autopatrolgroups );
257  $patrolusers = array();
258 
259  $this->output( "Flagging auto-patrolled edits...\n" );
260 
261  # Find all users in RC with autopatrol rights
262  $sql = "SELECT DISTINCT user_name FROM $usergroups, $user " .
263  "WHERE ug_group IN($patrolwhere) AND user_id = ug_user";
264  $res = $dbw->query( $sql, DB_MASTER );
265 
266  foreach ( $res as $obj ) {
267  $patrolusers[] = $dbw->addQuotes( $obj->user_name );
268  }
269 
270  # Fill in the rc_patrolled field
271  if ( !empty( $patrolusers ) ) {
272  $patrolwhere = implode( ',', $patrolusers );
273  $sql2 = "UPDATE $recentchanges SET rc_patrolled=1 " .
274  "WHERE rc_user_text IN($patrolwhere)";
275  $dbw->query( $sql2 );
276  }
277  }
278  }
279 
283  private function purgeFeeds() {
284  global $wgFeedClasses, $messageMemc;
285 
286  $this->output( "Deleting feed timestamps.\n" );
287 
288  foreach ( $wgFeedClasses as $feed => $className ) {
289  $messageMemc->delete( wfMemcKey( 'rcfeed', $feed, 'timestamp' ) ); # Good enough for now.
290  }
291  }
292 
293 }
294 
295 $maintClass = "RebuildRecentchanges";
296 require_once RUN_MAINTENANCE_IF_MAIN;
DB_MASTER
const DB_MASTER
Definition: Defines.php:56
php
skin txt MediaWiki includes four core it has been set as the default in MediaWiki since the replacing Monobook it had been been the default skin since before being replaced by Vector largely rewritten in while keeping its appearance Several legacy skins were removed in the as the burden of supporting them became too heavy to bear Those in etc for skin dependent CSS etc for skin dependent JavaScript These can also be customised on a per user by etc This feature has led to a wide variety of user styles becoming that gallery is a good place to ending in php
Definition: skin.txt:62
wfGetDB
& wfGetDB( $db, $groups=array(), $wiki=false)
Get a Database object.
Definition: GlobalFunctions.php:3650
RC_LOG
const RC_LOG
Definition: Defines.php:181
$messageMemc
globals will be eliminated from MediaWiki replaced by an application object which would be passed to constructors Whether that would be an convenient solution remains to be but certainly PHP makes such object oriented programming models easier than they were in previous versions For the time being MediaWiki programmers will have to work in an environment with some global context At the time of globals were initialised on startup by MediaWiki of these were configuration which are documented in DefaultSettings php There is no comprehensive documentation for the remaining however some of the most important ones are listed below They are typically initialised either in index php or in Setup php For a description of the see design txt $wgTitle Title object created from the request URL $wgOut OutputPage object for HTTP response $wgUser User object for the user associated with the current request $wgLang Language object selected by user preferences $wgContLang Language object associated with the wiki being viewed $wgParser Parser object Parser extensions register their hooks here $wgRequest WebRequest to get request data $messageMemc
Definition: globals.txt:25
RUN_MAINTENANCE_IF_MAIN
require_once RUN_MAINTENANCE_IF_MAIN
Definition: maintenance.txt:50
RC_EDIT
const RC_EDIT
Definition: Defines.php:178
RebuildRecentchanges\execute
execute()
Do the actual work.
Definition: rebuildrecentchanges.php:39
Maintenance
Abstract maintenance class for quickly writing and churning out maintenance scripts with minimal effo...
Definition: maintenance.txt:39
RecentChange\SRC_LOG
const SRC_LOG
Definition: RecentChange.php:68
wfMemcKey
wfMemcKey()
Get a cache key.
Definition: GlobalFunctions.php:3571
$maintClass
$maintClass
Definition: rebuildrecentchanges.php:295
array
the array() calling protocol came about after MediaWiki 1.4rc1.
List of Api Query prop modules.
RecentChange\SRC_EDIT
const SRC_EDIT
Definition: RecentChange.php:66
global
when a variable name is used in a it is silently declared as a new masking the global
Definition: design.txt:93
RecentChange\SRC_NEW
const SRC_NEW
Definition: RecentChange.php:67
RebuildRecentchanges
Maintenance script that rebuilds recent changes from scratch.
Definition: rebuildrecentchanges.php:33
list
deferred txt A few of the database updates required by various functions here can be deferred until after the result page is displayed to the user For updating the view updating the linked to tables after a etc PHP does not yet have any way to tell the server to actually return and disconnect while still running these but it might have such a feature in the future We handle these by creating a deferred update object and putting those objects on a global list
Definition: deferred.txt:11
$size
$size
Definition: RandomTest.php:75
RebuildRecentchanges\rebuildRecentChangesTablePass1
rebuildRecentChangesTablePass1()
Rebuild pass 1 DOCUMENT ME!
Definition: rebuildrecentchanges.php:52
RebuildRecentchanges\rebuildRecentChangesTablePass4
rebuildRecentChangesTablePass4()
Rebuild pass 4 DOCUMENT ME!
Definition: rebuildrecentchanges.php:221
RebuildRecentchanges\rebuildRecentChangesTablePass3
rebuildRecentChangesTablePass3()
Rebuild pass 3 DOCUMENT ME!
Definition: rebuildrecentchanges.php:167
RC_NEW
const RC_NEW
Definition: Defines.php:179
$user
please add to it if you re going to add events to the MediaWiki code where normally authentication against an external auth plugin would be creating a account $user
Definition: hooks.txt:237
as
This document is intended to provide useful advice for parties seeking to redistribute MediaWiki to end users It s targeted particularly at maintainers for Linux since it s been observed that distribution packages of MediaWiki often break We ve consistently had to recommend that users seeking support use official tarballs instead of their distribution s and this often solves whatever problem the user is having It would be nice if this could such as
Definition: distributors.txt:9
Maintenance\output
output( $out, $channel=null)
Throw some output to the user.
Definition: Maintenance.php:314
$res
$res
Definition: database.txt:21
RebuildRecentchanges\__construct
__construct()
Default constructor.
Definition: rebuildrecentchanges.php:34
RebuildRecentchanges\purgeFeeds
purgeFeeds()
Purge cached feeds in $messageMemc.
Definition: rebuildrecentchanges.php:283
RebuildRecentchanges\rebuildRecentChangesTablePass2
rebuildRecentChangesTablePass2()
Rebuild pass 2 DOCUMENT ME!
Definition: rebuildrecentchanges.php:100
User\getGroupsWithPermission
static getGroupsWithPermission( $role)
Get all the groups who have a given permission.
Definition: User.php:4123