summaryrefslogtreecommitdiff
path: root/www/wiki/maintenance/migrateArchiveText.php
blob: b2b14cbdc4c389e153c2eed34f379e3611a712a2 (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
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
<?php
/**
 * Migrate archive.ar_text and ar_flags to modern storage
 *
 * 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 that migrates archive.ar_text and ar_flags to text storage
 *
 * @ingroup Maintenance
 * @since 1.31
 */
class MigrateArchiveText extends LoggedUpdateMaintenance {
	public function __construct() {
		parent::__construct();
		$this->addDescription(
			'Migrates content from pre-1.5 ar_text and ar_flags columns to text storage'
		);
		$this->addOption(
			'replace-missing',
			"For rows with missing or unloadable data, throw away whatever is there and\n"
			. "mark them as \"error\" in the database."
		);
	}

	/**
	 * Sets whether a run of this maintenance script has the force parameter set
	 * @param bool $forced
	 */
	public function setForce( $forced = true ) {
		$this->mOptions['force'] = $forced;
	}

	protected function getUpdateKey() {
		return __CLASS__;
	}

	protected function doDBUpdates() {
		global $wgDefaultExternalStore;

		$replaceMissing = $this->hasOption( 'replace-missing' );
		$batchSize = $this->getBatchSize();

		$dbr = $this->getDB( DB_REPLICA, [ 'vslow' ] );
		$dbw = $this->getDB( DB_MASTER );
		if ( !$dbr->fieldExists( 'archive', 'ar_text', __METHOD__ ) ||
			!$dbw->fieldExists( 'archive', 'ar_text', __METHOD__ )
		) {
			$this->output( "No ar_text field, so nothing to migrate.\n" );
			return true;
		}

		$this->output( "Migrating ar_text to modern storage...\n" );
		$last = 0;
		$count = 0;
		$errors = 0;
		while ( true ) {
			$res = $dbr->select(
				'archive',
				[ 'ar_id', 'ar_text', 'ar_flags' ],
				[
					'ar_text_id' => null,
					"ar_id > $last",
				],
				__METHOD__,
				[ 'LIMIT' => $batchSize, 'ORDER BY' => [ 'ar_id' ] ]
			);
			$numRows = $res->numRows();

			foreach ( $res as $row ) {
				$last = $row->ar_id;

				// Recompress the text (and store in external storage, if
				// applicable) if it's not already in external storage.
				if ( !in_array( 'external', explode( ',', $row->ar_flags ), true ) ) {
					$data = Revision::getRevisionText( $row, 'ar_' );
					if ( $data !== false ) {
						$flags = Revision::compressRevisionText( $data );

						if ( $wgDefaultExternalStore ) {
							$data = ExternalStore::insertToDefault( $data );
							if ( !$data ) {
								throw new MWException( "Unable to store text to external storage" );
							}
							if ( $flags ) {
								$flags .= ',';
							}
							$flags .= 'external';
						}
					} elseif ( $replaceMissing ) {
						$this->error( "Replacing missing data for row ar_id=$row->ar_id" );
						$data = 'Missing data in migrateArchiveText.php on ' . date( 'c' );
						$flags = 'error';
					} else {
						$this->error( "No data for row ar_id=$row->ar_id" );
						$errors++;
						continue;
					}
				} else {
					$flags = $row->ar_flags;
					$data = $row->ar_text;
				}

				$this->beginTransaction( $dbw, __METHOD__ );
				$dbw->insert(
					'text',
					[ 'old_text' => $data, 'old_flags' => $flags ],
					__METHOD__
				);
				$id = $dbw->insertId();
				$dbw->update(
					'archive',
					[ 'ar_text_id' => $id, 'ar_text' => '', 'ar_flags' => '' ],
					[ 'ar_id' => $row->ar_id, 'ar_text_id' => null ],
					__METHOD__
				);
				$count += $dbw->affectedRows();
				$this->commitTransaction( $dbw, __METHOD__ );
			}

			if ( $numRows < $batchSize ) {
				// We must have reached the end
				break;
			}

			$this->output( "... $last\n" );
			// $this->commitTransaction() already waited for replication; no need to re-wait here
		}

		$this->output( "Completed ar_text migration, $count rows updated, $errors missing data.\n" );
		if ( $errors ) {
			$this->output( "Run with --replace-missing to overwrite missing data with an error message.\n" );
		}

		return $errors === 0;
	}
}

$maintClass = MigrateArchiveText::class;
require_once RUN_MAINTENANCE_IF_MAIN;