MediaWiki REL1_31
migrateArchiveText.php
Go to the documentation of this file.
1<?php
24require_once __DIR__ . '/Maintenance.php';
25
33 public function __construct() {
34 parent::__construct();
35 $this->addDescription(
36 'Migrates content from pre-1.5 ar_text and ar_flags columns to text storage'
37 );
38 $this->addOption(
39 'replace-missing',
40 "For rows with missing or unloadable data, throw away whatever is there and\n"
41 . "mark them as \"error\" in the database."
42 );
43 }
44
49 public function setForce( $forced = true ) {
50 $this->mOptions['force'] = $forced;
51 }
52
53 protected function getUpdateKey() {
54 return __CLASS__;
55 }
56
57 protected function doDBUpdates() {
59
60 $replaceMissing = $this->hasOption( 'replace-missing' );
61 $batchSize = $this->getBatchSize();
62
63 $dbr = $this->getDB( DB_REPLICA, [ 'vslow' ] );
64 $dbw = $this->getDB( DB_MASTER );
65 if ( !$dbr->fieldExists( 'archive', 'ar_text', __METHOD__ ) ||
66 !$dbw->fieldExists( 'archive', 'ar_text', __METHOD__ )
67 ) {
68 $this->output( "No ar_text field, so nothing to migrate.\n" );
69 return true;
70 }
71
72 $this->output( "Migrating ar_text to modern storage...\n" );
73 $last = 0;
74 $count = 0;
75 $errors = 0;
76 while ( true ) {
77 $res = $dbr->select(
78 'archive',
79 [ 'ar_id', 'ar_text', 'ar_flags' ],
80 [
81 'ar_text_id' => null,
82 "ar_id > $last",
83 ],
84 __METHOD__,
85 [ 'LIMIT' => $batchSize, 'ORDER BY' => [ 'ar_id' ] ]
86 );
87 $numRows = $res->numRows();
88
89 foreach ( $res as $row ) {
90 $last = $row->ar_id;
91
92 // Recompress the text (and store in external storage, if
93 // applicable) if it's not already in external storage.
94 if ( !in_array( 'external', explode( ',', $row->ar_flags ), true ) ) {
95 $data = Revision::getRevisionText( $row, 'ar_' );
96 if ( $data !== false ) {
97 $flags = Revision::compressRevisionText( $data );
98
100 $data = ExternalStore::insertToDefault( $data );
101 if ( !$data ) {
102 throw new MWException( "Unable to store text to external storage" );
103 }
104 if ( $flags ) {
105 $flags .= ',';
106 }
107 $flags .= 'external';
108 }
109 } elseif ( $replaceMissing ) {
110 $this->error( "Replacing missing data for row ar_id=$row->ar_id" );
111 $data = 'Missing data in migrateArchiveText.php on ' . date( 'c' );
112 $flags = 'error';
113 } else {
114 $this->error( "No data for row ar_id=$row->ar_id" );
115 $errors++;
116 continue;
117 }
118 } else {
119 $flags = $row->ar_flags;
120 $data = $row->ar_text;
121 }
122
123 $this->beginTransaction( $dbw, __METHOD__ );
124 $dbw->insert(
125 'text',
126 [ 'old_text' => $data, 'old_flags' => $flags ],
127 __METHOD__
128 );
129 $id = $dbw->insertId();
130 $dbw->update(
131 'archive',
132 [ 'ar_text_id' => $id, 'ar_text' => '', 'ar_flags' => '' ],
133 [ 'ar_id' => $row->ar_id, 'ar_text_id' => null ],
134 __METHOD__
135 );
136 $count += $dbw->affectedRows();
137 $this->commitTransaction( $dbw, __METHOD__ );
138 }
139
140 if ( $numRows < $batchSize ) {
141 // We must have reached the end
142 break;
143 }
144
145 $this->output( "... $last\n" );
146 // $this->commitTransaction() already waited for replication; no need to re-wait here
147 }
148
149 $this->output( "Completed ar_text migration, $count rows updated, $errors missing data.\n" );
150 if ( $errors ) {
151 $this->output( "Run with --replace-missing to overwrite missing data with an error message.\n" );
152 }
153
154 return $errors === 0;
155 }
156}
157
158$maintClass = MigrateArchiveText::class;
159require_once RUN_MAINTENANCE_IF_MAIN;
array $wgDefaultExternalStore
The place to put new revisions, false to put them in the local text table.
Class for scripts that perform database maintenance and want to log the update in updatelog so we can...
MediaWiki exception.
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.
getDB( $db, $groups=[], $wiki=false)
Returns a database to be used by current maintenance script.
hasOption( $name)
Checks to see if a particular param exists.
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.
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.
$res
Definition database.txt:21
design txt This is a brief overview of the new design More thorough and up to date information is available on the documentation wiki at etc Handles the details of getting and saving to the user table of the and dealing with sessions and cookies OutputPage Encapsulates the entire HTML page that will be sent in response to any server request It is used by calling its functions to add in any and then calling output() to send it all. It could be easily changed to send incrementally if that becomes useful
do that in ParserLimitReportFormat instead use this to modify the parameters of the image all existing parser cache entries will be invalid To avoid you ll need to handle that somehow(e.g. with the RejectParserCacheValue hook) because MediaWiki won 't do it for you. & $defaults error
Definition hooks.txt:2612
require_once RUN_MAINTENANCE_IF_MAIN
$last
const DB_REPLICA
Definition defines.php:25
const DB_MASTER
Definition defines.php:29