MediaWiki REL1_39
migrateArchiveText.php
Go to the documentation of this file.
1<?php
26
27require_once __DIR__ . '/Maintenance.php';
28
36 public function __construct() {
37 parent::__construct();
38 $this->addDescription(
39 'Migrates content from pre-1.5 ar_text and ar_flags columns to text storage'
40 );
41 $this->addOption(
42 'replace-missing',
43 "For rows with missing or unloadable data, throw away whatever is there and\n"
44 . "mark them as \"error\" in the database."
45 );
46 }
47
48 protected function getUpdateKey() {
49 return __CLASS__;
50 }
51
52 protected function doDBUpdates() {
53 $replaceMissing = $this->hasOption( 'replace-missing' );
54 $defaultExternalStore = $this->getConfig()->get( MainConfigNames::DefaultExternalStore );
55 $blobStore = MediaWikiServices::getInstance()
56 ->getBlobStoreFactory()
57 ->newSqlBlobStore();
58 $batchSize = $this->getBatchSize();
59
60 $dbr = $this->getDB( DB_REPLICA, [ 'vslow' ] );
61 $dbw = $this->getDB( DB_PRIMARY );
62 if ( !$dbr->fieldExists( 'archive', 'ar_text', __METHOD__ ) ||
63 !$dbw->fieldExists( 'archive', 'ar_text', __METHOD__ )
64 ) {
65 $this->output( "No ar_text field, so nothing to migrate.\n" );
66 return true;
67 }
68
69 $this->output( "Migrating ar_text to modern storage...\n" );
70 $last = 0;
71 $count = 0;
72 $errors = 0;
73 while ( true ) {
74 $res = $dbr->select(
75 'archive',
76 [ 'ar_id', 'ar_text', 'ar_flags' ],
77 [
78 'ar_text_id' => null,
79 "ar_id > $last",
80 ],
81 __METHOD__,
82 [ 'LIMIT' => $batchSize, 'ORDER BY' => [ 'ar_id' ] ]
83 );
84 $numRows = $res->numRows();
85
86 foreach ( $res as $row ) {
87 $last = $row->ar_id;
88
89 // Recompress the text (and store in external storage, if
90 // applicable) if it's not already in external storage.
91 $arFlags = explode( ',', $row->ar_flags );
92 if ( !in_array( 'external', $arFlags, true ) ) {
93 $data = $blobStore->decompressData( $row->ar_text, $arFlags );
94 if ( $data !== false ) {
95 $flags = $blobStore->compressData( $data );
96
97 if ( $defaultExternalStore ) {
98 $data = ExternalStore::insertToDefault( $data );
99 if ( $flags ) {
100 $flags .= ',';
101 }
102 $flags .= 'external';
103 }
104 } elseif ( $replaceMissing ) {
105 $this->error( "Replacing missing data for row ar_id=$row->ar_id" );
106 $data = 'Missing data in migrateArchiveText.php on ' . date( 'c' );
107 $flags = 'error';
108 } else {
109 $this->error( "No data for row ar_id=$row->ar_id" );
110 $errors++;
111 continue;
112 }
113 } else {
114 $flags = $row->ar_flags;
115 $data = $row->ar_text;
116 }
117
118 $this->beginTransaction( $dbw, __METHOD__ );
119 $dbw->insert(
120 'text',
121 [ 'old_text' => $data, 'old_flags' => $flags ],
122 __METHOD__
123 );
124 $id = $dbw->insertId();
125 $dbw->update(
126 'archive',
127 [ 'ar_text_id' => $id, 'ar_text' => '', 'ar_flags' => '' ],
128 [ 'ar_id' => $row->ar_id, 'ar_text_id' => null ],
129 __METHOD__
130 );
131 $count += $dbw->affectedRows();
132 $this->commitTransaction( $dbw, __METHOD__ );
133 }
134
135 if ( $numRows < $batchSize ) {
136 // We must have reached the end
137 break;
138 }
139
140 $this->output( "... $last\n" );
141 // $this->commitTransaction() already waited for replication; no need to re-wait here
142 }
143
144 $this->output( "Completed ar_text migration, $count rows updated, $errors missing data.\n" );
145 if ( $errors ) {
146 $this->output( "Run with --replace-missing to overwrite missing data with an error message.\n" );
147 }
148
149 return $errors === 0;
150 }
151}
152
153$maintClass = MigrateArchiveText::class;
154require_once RUN_MAINTENANCE_IF_MAIN;
getDB()
static insertToDefault( $data, array $params=[])
Like insert() above, but does more of the work for us.
Class for scripts that perform database maintenance and want to log the update in updatelog so we can...
error( $err, $die=0)
Throw an error to the user.
beginTransaction(IDatabase $dbw, $fname)
Begin a transaction on a DB.
commitTransaction(IDatabase $dbw, $fname)
Commit the transaction on a DB handle and wait for replica DBs to catch up.
output( $out, $channel=null)
Throw some output to the user.
hasOption( $name)
Checks to see if a particular option was set.
getBatchSize()
Returns batch size.
addDescription( $text)
Set the description text.
addOption( $name, $description, $required=false, $withArg=false, $shortName=false, $multiOccurrence=false)
Add a parameter to the script.
A class containing constants representing the names of configuration variables.
Service locator for MediaWiki core services.
Maintenance script that migrates archive.ar_text and ar_flags to text storage.
doDBUpdates()
Do the actual work.
__construct()
Default constructor.
getUpdateKey()
Get the update key name to go in the update log table.
const DB_REPLICA
Definition defines.php:26
const DB_PRIMARY
Definition defines.php:28