eb9797f0b016069e5066a4e54f26c0a6eceab5f7
[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 $batchSize = $this->getBatchSize();
45 if ( !$dbw->fieldExists( 'recentchanges', 'rc_source' ) ) {
46 $this->error( 'rc_source field in recentchanges table does not exist.' );
47 }
48
49 $start = $dbw->selectField( 'recentchanges', 'MIN(rc_id)', false, __METHOD__ );
50 if ( !$start ) {
51 $this->output( "Nothing to do.\n" );
52
53 return true;
54 }
55 $end = $dbw->selectField( 'recentchanges', 'MAX(rc_id)', false, __METHOD__ );
56 $end += $batchSize - 1;
57 $blockStart = $start;
58 $blockEnd = $start + $batchSize - 1;
59
60 $updatedValues = $this->buildUpdateCondition( $dbw );
61
62 while ( $blockEnd <= $end ) {
63 $cond = "rc_id BETWEEN $blockStart AND $blockEnd";
64
65 $dbw->update(
66 'recentchanges',
67 [ $updatedValues ],
68 [
69 "rc_source = ''",
70 "rc_id BETWEEN $blockStart AND $blockEnd"
71 ],
72 __METHOD__
73 );
74
75 $this->output( "." );
76 wfWaitForSlaves();
77
78 $blockStart += $batchSize;
79 $blockEnd += $batchSize;
80 }
81
82 $this->output( "\nDone.\n" );
83 }
84
85 protected function getUpdateKey() {
86 return __CLASS__;
87 }
88
89 protected function buildUpdateCondition( IDatabase $dbw ) {
90 $rcNew = $dbw->addQuotes( RC_NEW );
91 $rcSrcNew = $dbw->addQuotes( RecentChange::SRC_NEW );
92 $rcEdit = $dbw->addQuotes( RC_EDIT );
93 $rcSrcEdit = $dbw->addQuotes( RecentChange::SRC_EDIT );
94 $rcLog = $dbw->addQuotes( RC_LOG );
95 $rcSrcLog = $dbw->addQuotes( RecentChange::SRC_LOG );
96 $rcExternal = $dbw->addQuotes( RC_EXTERNAL );
97 $rcSrcExternal = $dbw->addQuotes( RecentChange::SRC_EXTERNAL );
98
99 return "rc_source = CASE
100 WHEN rc_type = $rcNew THEN $rcSrcNew
101 WHEN rc_type = $rcEdit THEN $rcSrcEdit
102 WHEN rc_type = $rcLog THEN $rcSrcLog
103 WHEN rc_type = $rcExternal THEN $rcSrcExternal
104 ELSE ''
105 END";
106 }
107 }
108
109 $maintClass = "PopulateRecentChangesSource";
110 require_once RUN_MAINTENANCE_IF_MAIN;