3 * Migrate archive.ar_text and ar_flags to modern storage
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.
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.
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
21 * @ingroup Maintenance
24 require_once __DIR__
. '/Maintenance.php';
27 * Maintenance script that migrates archive.ar_text and ar_flags to text storage
29 * @ingroup Maintenance
32 class MigrateArchiveText
extends LoggedUpdateMaintenance
{
33 public function __construct() {
34 parent
::__construct();
35 $this->addDescription(
36 'Migrates content from pre-1.5 ar_text and ar_flags columns to text storage'
40 "For rows with missing or unloadable data, throw away whatever is there and\n"
41 . "mark them as \"error\" in the database."
46 * Sets whether a run of this maintenance script has the force parameter set
49 public function setForce( $forced = true ) {
50 $this->mOptions
['force'] = $forced;
53 protected function getUpdateKey() {
57 protected function doDBUpdates() {
58 $replaceMissing = $this->hasOption( 'replace-missing' );
59 $defaultExternalStore = $this->getConfig()->get( 'DefaultExternalStore' );
60 $batchSize = $this->getBatchSize();
62 $dbr = $this->getDB( DB_REPLICA
, [ 'vslow' ] );
63 $dbw = $this->getDB( DB_MASTER
);
64 if ( !$dbr->fieldExists( 'archive', 'ar_text', __METHOD__
) ||
65 !$dbw->fieldExists( 'archive', 'ar_text', __METHOD__
)
67 $this->output( "No ar_text field, so nothing to migrate.\n" );
71 $this->output( "Migrating ar_text to modern storage...\n" );
78 [ 'ar_id', 'ar_text', 'ar_flags' ],
84 [ 'LIMIT' => $batchSize, 'ORDER BY' => [ 'ar_id' ] ]
86 $numRows = $res->numRows();
88 foreach ( $res as $row ) {
91 // Recompress the text (and store in external storage, if
92 // applicable) if it's not already in external storage.
93 if ( !in_array( 'external', explode( ',', $row->ar_flags
), true ) ) {
94 $data = Revision
::getRevisionText( $row, 'ar_' );
95 if ( $data !== false ) {
96 $flags = Revision
::compressRevisionText( $data );
98 if ( $defaultExternalStore ) {
99 $data = ExternalStore
::insertToDefault( $data );
103 $flags .= 'external';
105 } elseif ( $replaceMissing ) {
106 $this->error( "Replacing missing data for row ar_id=$row->ar_id" );
107 $data = 'Missing data in migrateArchiveText.php on ' . date( 'c' );
110 $this->error( "No data for row ar_id=$row->ar_id" );
115 $flags = $row->ar_flags
;
116 $data = $row->ar_text
;
119 $this->beginTransaction( $dbw, __METHOD__
);
122 [ 'old_text' => $data, 'old_flags' => $flags ],
125 $id = $dbw->insertId();
128 [ 'ar_text_id' => $id, 'ar_text' => '', 'ar_flags' => '' ],
129 [ 'ar_id' => $row->ar_id
, 'ar_text_id' => null ],
132 $count +
= $dbw->affectedRows();
133 $this->commitTransaction( $dbw, __METHOD__
);
136 if ( $numRows < $batchSize ) {
137 // We must have reached the end
141 $this->output( "... $last\n" );
142 // $this->commitTransaction() already waited for replication; no need to re-wait here
145 $this->output( "Completed ar_text migration, $count rows updated, $errors missing data.\n" );
147 $this->output( "Run with --replace-missing to overwrite missing data with an error message.\n" );
150 return $errors === 0;
154 $maintClass = MigrateArchiveText
::class;
155 require_once RUN_MAINTENANCE_IF_MAIN
;