MediaWiki REL1_37
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
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;
153require_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.
MediaWikiServices is the service locator for the application scope of MediaWiki.
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:25
const DB_PRIMARY
Definition defines.php:27