summaryrefslogtreecommitdiff
path: root/maintenance/populateRecentChangesSource.php
blob: 25a51d72133d368cea727614ab4d1b80b5acef62 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
<?php
/**
 * Upgrade script to populate the rc_source field
 *
 * This program is free software; you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation; either version 2 of the License, or
 * (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License along
 * with this program; if not, write to the Free Software Foundation, Inc.,
 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
 * http://www.gnu.org/copyleft/gpl.html
 *
 * @file
 * @ingroup Maintenance
 */

require_once __DIR__ . '/Maintenance.php';

/**
 * Maintenance script to populate the rc_source field.
 *
 * @ingroup Maintenance
 * @since 1.22
 */
class PopulateRecentChangesSource extends LoggedUpdateMaintenance {
	public function __construct() {
		parent::__construct();
		$this->mDescription =
			"Populates rc_source field of the recentchanges table with the data in rc_type.";
		$this->setBatchSize( 100 );
	}

	protected function doDBUpdates() {
		$dbw = $this->getDB( DB_MASTER );
		if ( !$dbw->fieldExists( 'recentchanges', 'rc_source' ) ) {
			$this->error( 'rc_source field in recentchanges table does not exist.' );
		}

		$start = $dbw->selectField( 'recentchanges', 'MIN(rc_id)', false, __METHOD__ );
		if ( !$start ) {
			$this->output( "Nothing to do.\n" );

			return true;
		}
		$end = $dbw->selectField( 'recentchanges', 'MAX(rc_id)', false, __METHOD__ );
		$end += $this->mBatchSize - 1;
		$blockStart = $start;
		$blockEnd = $start + $this->mBatchSize - 1;

		$updatedValues = $this->buildUpdateCondition( $dbw );

		while ( $blockEnd <= $end ) {
			$cond = "rc_id BETWEEN $blockStart AND $blockEnd";

			$dbw->update(
				'recentchanges',
				array( $updatedValues ),
				array(
					"rc_source = ''",
					"rc_id BETWEEN $blockStart AND $blockEnd"
				),
				__METHOD__
			);

			$this->output( "." );
			wfWaitForSlaves();

			$blockStart += $this->mBatchSize;
			$blockEnd += $this->mBatchSize;
		}

		$this->output( "\nDone.\n" );
	}

	protected function getUpdateKey() {
		return __CLASS__;
	}

	protected function buildUpdateCondition( DatabaseBase $dbw ) {
		$rcNew = $dbw->addQuotes( RC_NEW );
		$rcSrcNew = $dbw->addQuotes( RecentChange::SRC_NEW );
		$rcEdit = $dbw->addQuotes( RC_EDIT );
		$rcSrcEdit = $dbw->addQuotes( RecentChange::SRC_EDIT );
		$rcLog = $dbw->addQuotes( RC_LOG );
		$rcSrcLog = $dbw->addQuotes( RecentChange::SRC_LOG );
		$rcExternal = $dbw->addQuotes( RC_EXTERNAL );
		$rcSrcExternal = $dbw->addQuotes( RecentChange::SRC_EXTERNAL );

		return "rc_source = CASE
					WHEN rc_type = $rcNew THEN $rcSrcNew
					WHEN rc_type = $rcEdit THEN $rcSrcEdit
					WHEN rc_type = $rcLog THEN $rcSrcLog
					WHEN rc_type = $rcExternal THEN $rcSrcExternal
					ELSE ''
				END";
	}
}

$maintClass = "PopulateRecentChangesSource";
require_once RUN_MAINTENANCE_IF_MAIN;