Merge "Removed odd "partitionsNoPush" setting to simplify the code"
[lhc/web/wiklou.git] / includes / jobqueue / jobs / RecentChangesUpdateJob.php
1 <?php
2 /**
3 * This program is free software; you can redistribute it and/or modify
4 * it under the terms of the GNU General Public License as published by
5 * the Free Software Foundation; either version 2 of the License, or
6 * (at your option) any later version.
7 *
8 * This program is distributed in the hope that it will be useful,
9 * but WITHOUT ANY WARRANTY; without even the implied warranty of
10 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
11 * GNU General Public License for more details.
12 *
13 * You should have received a copy of the GNU General Public License along
14 * with this program; if not, write to the Free Software Foundation, Inc.,
15 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
16 * http://www.gnu.org/copyleft/gpl.html
17 *
18 * @file
19 * @author Aaron Schulz
20 * @ingroup JobQueue
21 */
22
23 /**
24 * Job for pruning recent changes
25 *
26 * @ingroup JobQueue
27 * @since 1.25
28 */
29 class RecentChangesUpdateJob extends Job {
30 function __construct( $title, $params ) {
31 parent::__construct( 'recentChangesUpdate', $title, $params );
32
33 if ( !isset( $params['type'] ) ) {
34 throw new Exception( "Missing 'type' parameter." );
35 }
36
37 $this->removeDuplicates = true;
38 }
39
40 /**
41 * @return RecentChangesUpdateJob
42 */
43 final public static function newPurgeJob() {
44 return new self(
45 SpecialPage::getTitleFor( 'Recentchanges' ), array( 'type' => 'purge' )
46 );
47 }
48
49 public function run() {
50 if ( $this->params['type'] === 'purge' ) {
51 $this->purgeExpiredRows();
52 } else {
53 throw new Exception( "Invalid 'type' parameter '{$this->params['type']}'." );
54 }
55
56 return true;
57 }
58
59 protected function purgeExpiredRows() {
60 global $wgRCMaxAge;
61
62 $lockKey = wfWikiID() . ':recentchanges-prune';
63
64 $dbw = wfGetDB( DB_MASTER );
65 if ( !$dbw->lock( $lockKey, __METHOD__, 1 ) ) {
66 return; // already in progress
67 }
68 $batchSize = 100; // Avoid slave lag
69
70 $cutoff = $dbw->timestamp( time() - $wgRCMaxAge );
71 do {
72 $rcIds = $dbw->selectFieldValues( 'recentchanges',
73 'rc_id',
74 array( 'rc_timestamp < ' . $dbw->addQuotes( $cutoff ) ),
75 __METHOD__,
76 array( 'LIMIT' => $batchSize )
77 );
78 if ( $rcIds ) {
79 $dbw->delete( 'recentchanges', array( 'rc_id' => $rcIds ), __METHOD__ );
80 }
81 // No need for this to be in a transaction.
82 $dbw->commit( __METHOD__, 'flush' );
83
84 if ( count( $rcIds ) === $batchSize ) {
85 // There might be more, so try waiting for slaves
86 if ( !wfWaitForSlaves( null, false, false, /* $timeout = */ 3 ) ) {
87 // Another job will continue anyway
88 break;
89 }
90 }
91 } while ( $rcIds );
92
93 $dbw->unlock( $lockKey, __METHOD__ );
94 }
95 }