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 
47  protected function getUpdateKey() {
48  return __CLASS__;
49  }
50 
51  protected function doDBUpdates() {
52  $replaceMissing = $this->hasOption( 'replace-missing' );
53  $defaultExternalStore = $this->getConfig()->get( 'DefaultExternalStore' );
54  $blobStore = MediaWikiServices::getInstance()
55  ->getBlobStoreFactory()
56  ->newSqlBlobStore();
57  $batchSize = $this->getBatchSize();
58 
59  $dbr = $this->getDB( DB_REPLICA, [ 'vslow' ] );
60  $dbw = $this->getDB( DB_PRIMARY );
61  if ( !$dbr->fieldExists( 'archive', 'ar_text', __METHOD__ ) ||
62  !$dbw->fieldExists( 'archive', 'ar_text', __METHOD__ )
63  ) {
64  $this->output( "No ar_text field, so nothing to migrate.\n" );
65  return true;
66  }
67 
68  $this->output( "Migrating ar_text to modern storage...\n" );
69  $last = 0;
70  $count = 0;
71  $errors = 0;
72  while ( true ) {
73  $res = $dbr->select(
74  'archive',
75  [ 'ar_id', 'ar_text', 'ar_flags' ],
76  [
77  'ar_text_id' => null,
78  "ar_id > $last",
79  ],
80  __METHOD__,
81  [ 'LIMIT' => $batchSize, 'ORDER BY' => [ 'ar_id' ] ]
82  );
83  $numRows = $res->numRows();
84 
85  foreach ( $res as $row ) {
86  $last = $row->ar_id;
87 
88  // Recompress the text (and store in external storage, if
89  // applicable) if it's not already in external storage.
90  $arFlags = explode( ',', $row->ar_flags );
91  if ( !in_array( 'external', $arFlags, true ) ) {
92  $data = $blobStore->decompressData( $row->ar_text, $arFlags );
93  if ( $data !== false ) {
94  $flags = $blobStore->compressData( $data );
95 
96  if ( $defaultExternalStore ) {
97  $data = ExternalStore::insertToDefault( $data );
98  if ( $flags ) {
99  $flags .= ',';
100  }
101  $flags .= 'external';
102  }
103  } elseif ( $replaceMissing ) {
104  $this->error( "Replacing missing data for row ar_id=$row->ar_id" );
105  $data = 'Missing data in migrateArchiveText.php on ' . date( 'c' );
106  $flags = 'error';
107  } else {
108  $this->error( "No data for row ar_id=$row->ar_id" );
109  $errors++;
110  continue;
111  }
112  } else {
113  $flags = $row->ar_flags;
114  $data = $row->ar_text;
115  }
116 
117  $this->beginTransaction( $dbw, __METHOD__ );
118  $dbw->insert(
119  'text',
120  [ 'old_text' => $data, 'old_flags' => $flags ],
121  __METHOD__
122  );
123  $id = $dbw->insertId();
124  $dbw->update(
125  'archive',
126  [ 'ar_text_id' => $id, 'ar_text' => '', 'ar_flags' => '' ],
127  [ 'ar_id' => $row->ar_id, 'ar_text_id' => null ],
128  __METHOD__
129  );
130  $count += $dbw->affectedRows();
131  $this->commitTransaction( $dbw, __METHOD__ );
132  }
133 
134  if ( $numRows < $batchSize ) {
135  // We must have reached the end
136  break;
137  }
138 
139  $this->output( "... $last\n" );
140  // $this->commitTransaction() already waited for replication; no need to re-wait here
141  }
142 
143  $this->output( "Completed ar_text migration, $count rows updated, $errors missing data.\n" );
144  if ( $errors ) {
145  $this->output( "Run with --replace-missing to overwrite missing data with an error message.\n" );
146  }
147 
148  return $errors === 0;
149  }
150 }
151 
152 $maintClass = MigrateArchiveText::class;
153 require_once RUN_MAINTENANCE_IF_MAIN;
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:184
MigrateArchiveText\__construct
__construct()
Default constructor.
Definition: migrateArchiveText.php:35
Maintenance\addDescription
addDescription( $text)
Set the description text.
Definition: Maintenance.php:329
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 transaction on a DB.
Definition: Maintenance.php:1392
MigrateArchiveText\getUpdateKey
getUpdateKey()
Get the update key name to go in the update log table.
Definition: migrateArchiveText.php:47
Maintenance\getConfig
getConfig()
Definition: Maintenance.php:597
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:249
DB_REPLICA
const DB_REPLICA
Definition: defines.php:25
DB_PRIMARY
const DB_PRIMARY
Definition: defines.php:27
Maintenance\commitTransaction
commitTransaction(IDatabase $dbw, $fname)
Commit the transaction on a DB handle and wait for replica DBs to catch up.
Definition: Maintenance.php:1407
Maintenance\getDB
getDB( $db, $groups=[], $dbDomain=false)
Returns a database to be used by current maintenance script.
Definition: Maintenance.php:1361
Maintenance\getBatchSize
getBatchSize()
Returns batch size.
Definition: Maintenance.php:368
Maintenance\error
error( $err, $die=0)
Throw an error to the user.
Definition: Maintenance.php:464
Maintenance\output
output( $out, $channel=null)
Throw some output to the user.
Definition: Maintenance.php:435
Maintenance\hasOption
hasOption( $name)
Checks to see if a particular option was set.
Definition: Maintenance.php:271
MigrateArchiveText\doDBUpdates
doDBUpdates()
Do the actual work.
Definition: migrateArchiveText.php:51
$maintClass
$maintClass
Definition: migrateArchiveText.php:152