MediaWiki  master
migrateArchiveText.php
Go to the documentation of this file.
1 <?php
25 
26 require_once __DIR__ . '/Maintenance.php';
27 
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'
39  );
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."
44  );
45  }
46 
51  public function setForce( $forced = true ) {
52  $this->mOptions['force'] = $forced;
53  }
54 
55  protected function getUpdateKey() {
56  return __CLASS__;
57  }
58 
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();
66 
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;
74  }
75 
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' ],
84  [
85  'ar_text_id' => null,
86  "ar_id > $last",
87  ],
88  __METHOD__,
89  [ 'LIMIT' => $batchSize, 'ORDER BY' => [ 'ar_id' ] ]
90  );
91  $numRows = $res->numRows();
92 
93  foreach ( $res as $row ) {
94  $last = $row->ar_id;
95 
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 );
103 
104  if ( $defaultExternalStore ) {
105  $data = ExternalStore::insertToDefault( $data );
106  if ( $flags ) {
107  $flags .= ',';
108  }
109  $flags .= 'external';
110  }
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;
119  }
120  } else {
121  $flags = $row->ar_flags;
122  $data = $row->ar_text;
123  }
124 
125  $this->beginTransaction( $dbw, __METHOD__ );
126  $dbw->insert(
127  'text',
128  [ 'old_text' => $data, 'old_flags' => $flags ],
129  __METHOD__
130  );
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__
137  );
138  $count += $dbw->affectedRows();
139  $this->commitTransaction( $dbw, __METHOD__ );
140  }
141 
142  if ( $numRows < $batchSize ) {
143  // We must have reached the end
144  break;
145  }
146 
147  $this->output( "... $last\n" );
148  // $this->commitTransaction() already waited for replication; no need to re-wait here
149  }
150 
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" );
154  }
155 
156  return $errors === 0;
157  }
158 }
159 
160 $maintClass = MigrateArchiveText::class;
161 require_once RUN_MAINTENANCE_IF_MAIN;
RUN_MAINTENANCE_IF_MAIN
const RUN_MAINTENANCE_IF_MAIN
Definition: Maintenance.php:38
ExternalStore\insertToDefault
static insertToDefault( $data, array $params=[])
Like insert() above, but does more of the work for us.
Definition: ExternalStore.php:135
MediaWiki\MediaWikiServices
MediaWikiServices is the service locator for the application scope of MediaWiki.
Definition: MediaWikiServices.php:152
MigrateArchiveText\__construct
__construct()
Default constructor.
Definition: migrateArchiveText.php:35
Maintenance\addDescription
addDescription( $text)
Set the description text.
Definition: Maintenance.php:327
MigrateArchiveText
Maintenance script that migrates archive.ar_text and ar_flags to text storage.
Definition: migrateArchiveText.php:34
$res
$res
Definition: testCompression.php:57
$dbr
$dbr
Definition: testCompression.php:54
Maintenance\beginTransaction
beginTransaction(IDatabase $dbw, $fname)
Begin a transcation on a DB.
Definition: Maintenance.php:1397
MigrateArchiveText\getUpdateKey
getUpdateKey()
Get the update key name to go in the update log table.
Definition: migrateArchiveText.php:55
Maintenance\getConfig
getConfig()
Definition: Maintenance.php:596
LoggedUpdateMaintenance
Class for scripts that perform database maintenance and want to log the update in updatelog so we can...
Definition: LoggedUpdateMaintenance.php:26
Maintenance\addOption
addOption( $name, $description, $required=false, $withArg=false, $shortName=false, $multiOccurrence=false)
Add a parameter to the script.
Definition: Maintenance.php:245
DB_REPLICA
const DB_REPLICA
Definition: defines.php:25
DB_MASTER
const DB_MASTER
Definition: defines.php:26
MigrateArchiveText\setForce
setForce( $forced=true)
Sets whether a run of this maintenance script has the force parameter set.
Definition: migrateArchiveText.php:51
Maintenance\commitTransaction
commitTransaction(IDatabase $dbw, $fname)
Commit the transcation on a DB handle and wait for replica DBs to catch up.
Definition: Maintenance.php:1412
Maintenance\getDB
getDB( $db, $groups=[], $dbDomain=false)
Returns a database to be used by current maintenance script.
Definition: Maintenance.php:1366
Maintenance\getBatchSize
getBatchSize()
Returns batch size.
Definition: Maintenance.php:366
Maintenance\error
error( $err, $die=0)
Throw an error to the user.
Definition: Maintenance.php:463
Maintenance\output
output( $out, $channel=null)
Throw some output to the user.
Definition: Maintenance.php:434
Maintenance\hasOption
hasOption( $name)
Checks to see if a particular option exists.
Definition: Maintenance.php:266
MigrateArchiveText\doDBUpdates
doDBUpdates()
Do the actual work.
Definition: migrateArchiveText.php:59
$maintClass
$maintClass
Definition: migrateArchiveText.php:160