Yet another attempt to fix the populateIpChanges script
[lhc/web/wiklou.git] / maintenance / populateRecentChangesSource.php
1 <?php
2 /**
3 * Upgrade script to populate the rc_source field
4 *
5 * This program is free software; you can redistribute it and/or modify
6 * it under the terms of the GNU General Public License as published by
7 * the Free Software Foundation; either version 2 of the License, or
8 * (at your option) any later version.
9 *
10 * This program is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 * GNU General Public License for more details.
14 *
15 * You should have received a copy of the GNU General Public License along
16 * with this program; if not, write to the Free Software Foundation, Inc.,
17 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
18 * http://www.gnu.org/copyleft/gpl.html
19 *
20 * @file
21 * @ingroup Maintenance
22 */
23
24 require_once __DIR__ . '/Maintenance.php';
25
26 use Wikimedia\Rdbms\IDatabase;
27
28 /**
29 * Maintenance script to populate the rc_source field.
30 *
31 * @ingroup Maintenance
32 * @since 1.22
33 */
34 class PopulateRecentChangesSource extends LoggedUpdateMaintenance {
35 public function __construct() {
36 parent::__construct();
37 $this->addDescription(
38 'Populates rc_source field of the recentchanges table with the data in rc_type.' );
39 $this->setBatchSize( 100 );
40 }
41
42 protected function doDBUpdates() {
43 $dbw = $this->getDB( DB_MASTER );
44 if ( !$dbw->fieldExists( 'recentchanges', 'rc_source' ) ) {
45 $this->error( 'rc_source field in recentchanges table does not exist.' );
46 }
47
48 $start = $dbw->selectField( 'recentchanges', 'MIN(rc_id)', false, __METHOD__ );
49 if ( !$start ) {
50 $this->output( "Nothing to do.\n" );
51
52 return true;
53 }
54 $end = $dbw->selectField( 'recentchanges', 'MAX(rc_id)', false, __METHOD__ );
55 $end += $this->mBatchSize - 1;
56 $blockStart = $start;
57 $blockEnd = $start + $this->mBatchSize - 1;
58
59 $updatedValues = $this->buildUpdateCondition( $dbw );
60
61 while ( $blockEnd <= $end ) {
62 $cond = "rc_id BETWEEN $blockStart AND $blockEnd";
63
64 $dbw->update(
65 'recentchanges',
66 [ $updatedValues ],
67 [
68 "rc_source = ''",
69 "rc_id BETWEEN $blockStart AND $blockEnd"
70 ],
71 __METHOD__
72 );
73
74 $this->output( "." );
75 wfWaitForSlaves();
76
77 $blockStart += $this->mBatchSize;
78 $blockEnd += $this->mBatchSize;
79 }
80
81 $this->output( "\nDone.\n" );
82 }
83
84 protected function getUpdateKey() {
85 return __CLASS__;
86 }
87
88 protected function buildUpdateCondition( IDatabase $dbw ) {
89 $rcNew = $dbw->addQuotes( RC_NEW );
90 $rcSrcNew = $dbw->addQuotes( RecentChange::SRC_NEW );
91 $rcEdit = $dbw->addQuotes( RC_EDIT );
92 $rcSrcEdit = $dbw->addQuotes( RecentChange::SRC_EDIT );
93 $rcLog = $dbw->addQuotes( RC_LOG );
94 $rcSrcLog = $dbw->addQuotes( RecentChange::SRC_LOG );
95 $rcExternal = $dbw->addQuotes( RC_EXTERNAL );
96 $rcSrcExternal = $dbw->addQuotes( RecentChange::SRC_EXTERNAL );
97
98 return "rc_source = CASE
99 WHEN rc_type = $rcNew THEN $rcSrcNew
100 WHEN rc_type = $rcEdit THEN $rcSrcEdit
101 WHEN rc_type = $rcLog THEN $rcSrcLog
102 WHEN rc_type = $rcExternal THEN $rcSrcExternal
103 ELSE ''
104 END";
105 }
106 }
107
108 $maintClass = "PopulateRecentChangesSource";
109 require_once RUN_MAINTENANCE_IF_MAIN;