MediaWiki REL1_35
migrateArchiveText.php
Go to the documentation of this file.
1<?php
25
26require_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;
161require_once RUN_MAINTENANCE_IF_MAIN;
getDB()
const RUN_MAINTENANCE_IF_MAIN
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 transcation on a DB.
commitTransaction(IDatabase $dbw, $fname)
Commit the transcation 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.
MediaWikiServices is the service locator for the application scope of MediaWiki.
Maintenance script that migrates archive.ar_text and ar_flags to text storage.
setForce( $forced=true)
Sets whether a run of this maintenance script has the force parameter set.
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:25
const DB_MASTER
Definition defines.php:29