Revision: Remove some unnecessary temporary variables for returns
[mediawiki.git] / maintenance / migrateArchiveText.php
blob68b3bcf3e2ec58a07ac476645bf0fbb67ca0896f
1 <?php
2 /**
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
20 * @file
21 * @ingroup Maintenance
24 use MediaWiki\MediaWikiServices;
26 require_once __DIR__ . '/Maintenance.php';
28 /**
29 * Maintenance script that migrates archive.ar_text and ar_flags to text storage
31 * @ingroup Maintenance
32 * @since 1.31
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'
40 $this->addOption(
41 'replace-missing',
42 "For rows with missing or unloadable data, throw away whatever is there and\n"
43 . "mark them as \"error\" in the database."
47 /**
48 * Sets whether a run of this maintenance script has the force parameter set
49 * @param bool $forced
51 public function setForce( $forced = true ) {
52 $this->mOptions['force'] = $forced;
55 protected function getUpdateKey() {
56 return __CLASS__;
59 protected function doDBUpdates() {
60 $replaceMissing = $this->hasOption( 'replace-missing' );
61 $defaultExternalStore = $this->getConfig()->get( 'DefaultExternalStore' );
62 $blobStore = MediaWikiServices::getInstance()
63 ->getBlobStoreFactory()
64 ->newSqlBlobStore();
65 $batchSize = $this->getBatchSize();
67 $dbr = $this->getDB( DB_REPLICA, [ 'vslow' ] );
68 $dbw = $this->getDB( DB_MASTER );
69 if ( !$dbr->fieldExists( 'archive', 'ar_text', __METHOD__ ) ||
70 !$dbw->fieldExists( 'archive', 'ar_text', __METHOD__ )
71 ) {
72 $this->output( "No ar_text field, so nothing to migrate.\n" );
73 return true;
76 $this->output( "Migrating ar_text to modern storage...\n" );
77 $last = 0;
78 $count = 0;
79 $errors = 0;
80 while ( true ) {
81 $res = $dbr->select(
82 'archive',
83 [ 'ar_id', 'ar_text', 'ar_flags' ],
85 'ar_text_id' => null,
86 "ar_id > $last",
88 __METHOD__,
89 [ 'LIMIT' => $batchSize, 'ORDER BY' => [ 'ar_id' ] ]
91 $numRows = $res->numRows();
93 foreach ( $res as $row ) {
94 $last = $row->ar_id;
96 // Recompress the text (and store in external storage, if
97 // applicable) if it's not already in external storage.
98 $arFlags = explode( ',', $row->ar_flags );
99 if ( !in_array( 'external', $arFlags, true ) ) {
100 $data = $blobStore->decompressData( $row->ar_text, $arFlags );
101 if ( $data !== false ) {
102 $flags = $blobStore->compressData( $data );
104 if ( $defaultExternalStore ) {
105 $data = ExternalStore::insertToDefault( $data );
106 if ( $flags ) {
107 $flags .= ',';
109 $flags .= 'external';
111 } elseif ( $replaceMissing ) {
112 $this->error( "Replacing missing data for row ar_id=$row->ar_id" );
113 $data = 'Missing data in migrateArchiveText.php on ' . date( 'c' );
114 $flags = 'error';
115 } else {
116 $this->error( "No data for row ar_id=$row->ar_id" );
117 $errors++;
118 continue;
120 } else {
121 $flags = $row->ar_flags;
122 $data = $row->ar_text;
125 $this->beginTransaction( $dbw, __METHOD__ );
126 $dbw->insert(
127 'text',
128 [ 'old_text' => $data, 'old_flags' => $flags ],
129 __METHOD__
131 $id = $dbw->insertId();
132 $dbw->update(
133 'archive',
134 [ 'ar_text_id' => $id, 'ar_text' => '', 'ar_flags' => '' ],
135 [ 'ar_id' => $row->ar_id, 'ar_text_id' => null ],
136 __METHOD__
138 $count += $dbw->affectedRows();
139 $this->commitTransaction( $dbw, __METHOD__ );
142 if ( $numRows < $batchSize ) {
143 // We must have reached the end
144 break;
147 $this->output( "... $last\n" );
148 // $this->commitTransaction() already waited for replication; no need to re-wait here
151 $this->output( "Completed ar_text migration, $count rows updated, $errors missing data.\n" );
152 if ( $errors ) {
153 $this->output( "Run with --replace-missing to overwrite missing data with an error message.\n" );
156 return $errors === 0;
160 $maintClass = MigrateArchiveText::class;
161 require_once RUN_MAINTENANCE_IF_MAIN;