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 use MediaWiki\MediaWikiServices
;
26 require_once __DIR__
. '/Maintenance.php';
29 * Maintenance script that migrates archive.ar_text and ar_flags to text storage
31 * @ingroup Maintenance
34 class MigrateArchiveText
extends LoggedUpdateMaintenance
{
35 public function __construct() {
36 parent
::__construct();
37 $this->addDescription(
38 'Migrates content from pre-1.5 ar_text and ar_flags columns to text storage'
42 "For rows with missing or unloadable data, throw away whatever is there and\n"
43 . "mark them as \"error\" in the database."
48 * Sets whether a run of this maintenance script has the force parameter set
51 public function setForce( $forced = true ) {
52 $this->mOptions
['force'] = $forced;
55 protected function getUpdateKey() {
59 protected function doDBUpdates() {
60 $replaceMissing = $this->hasOption( 'replace-missing' );
61 $defaultExternalStore = $this->getConfig()->get( 'DefaultExternalStore' );
62 // @phan-suppress-next-line PhanAccessMethodInternal
63 $blobStore = MediaWikiServices
::getInstance()
64 ->getBlobStoreFactory()
66 $batchSize = $this->getBatchSize();
68 $dbr = $this->getDB( DB_REPLICA
, [ 'vslow' ] );
69 $dbw = $this->getDB( DB_MASTER
);
70 if ( !$dbr->fieldExists( 'archive', 'ar_text', __METHOD__
) ||
71 !$dbw->fieldExists( 'archive', 'ar_text', __METHOD__
)
73 $this->output( "No ar_text field, so nothing to migrate.\n" );
77 $this->output( "Migrating ar_text to modern storage...\n" );
84 [ 'ar_id', 'ar_text', 'ar_flags' ],
90 [ 'LIMIT' => $batchSize, 'ORDER BY' => [ 'ar_id' ] ]
92 $numRows = $res->numRows();
94 foreach ( $res as $row ) {
97 // Recompress the text (and store in external storage, if
98 // applicable) if it's not already in external storage.
99 $arFlags = explode( ',', $row->ar_flags
);
100 if ( !in_array( 'external', $arFlags, true ) ) {
101 $data = $blobStore->decompressData( $row->ar_text
, $arFlags );
102 if ( $data !== false ) {
103 $flags = Revision
::compressRevisionText( $data );
105 if ( $defaultExternalStore ) {
106 $data = ExternalStore
::insertToDefault( $data );
110 $flags .= 'external';
112 } elseif ( $replaceMissing ) {
113 $this->error( "Replacing missing data for row ar_id=$row->ar_id" );
114 $data = 'Missing data in migrateArchiveText.php on ' . date( 'c' );
117 $this->error( "No data for row ar_id=$row->ar_id" );
122 $flags = $row->ar_flags
;
123 $data = $row->ar_text
;
126 $this->beginTransaction( $dbw, __METHOD__
);
129 [ 'old_text' => $data, 'old_flags' => $flags ],
132 $id = $dbw->insertId();
135 [ 'ar_text_id' => $id, 'ar_text' => '', 'ar_flags' => '' ],
136 [ 'ar_id' => $row->ar_id
, 'ar_text_id' => null ],
139 $count +
= $dbw->affectedRows();
140 $this->commitTransaction( $dbw, __METHOD__
);
143 if ( $numRows < $batchSize ) {
144 // We must have reached the end
148 $this->output( "... $last\n" );
149 // $this->commitTransaction() already waited for replication; no need to re-wait here
152 $this->output( "Completed ar_text migration, $count rows updated, $errors missing data.\n" );
154 $this->output( "Run with --replace-missing to overwrite missing data with an error message.\n" );
157 return $errors === 0;
161 $maintClass = MigrateArchiveText
::class;
162 require_once RUN_MAINTENANCE_IF_MAIN
;