27use Wikimedia\Assert\Assert;
31require_once __DIR__ .
'/Maintenance.php';
60 parent::__construct();
63 $this->
addOption(
'table',
'revision or archive table, or `all` to populate both',
false,
66 'Reuse content table rows when the address and model are the same. '
67 .
'This will increase the script\'s time and memory usage, perhaps significantly.',
69 $this->
addOption(
'start-revision',
'The rev_id to start at',
false,
true );
70 $this->
addOption(
'start-archive',
'The ar_rev_id to start at',
false,
true );
76 $this->
contentModelStore = MediaWikiServices::getInstance()->getContentModelStore();
77 $this->
slotRoleStore = MediaWikiServices::getInstance()->getSlotRoleStore();
78 $this->
blobStore = MediaWikiServices::getInstance()->getBlobStore();
89 $this->
getConfig()->get(
'MultiContentRevisionSchemaMigrationStage' );
91 $t0 = microtime(
true );
95 '...cannot update while \$wgMultiContentRevisionSchemaMigrationStage '
96 .
'does not have the SCHEMA_COMPAT_WRITE_NEW bit set.'
103 if ( $this->
getOption(
'reuse-content',
false ) ) {
112 $this->
writeln(
"Done. Processed $this->totalCount rows in $elapsed seconds" );
120 $table = $this->
getOption(
'table',
'all' );
124 $this->
fatalError(
'Invalid table. Must be either `revision` or `archive` or `all`' );
127 if ( $table ===
'all' ) {
128 $tables = [
'revision',
'archive' ];
130 $tables = [ $table ];
137 $t0 = microtime(
true );
138 $this->
writeln(
"Loading existing content table rows..." );
145 [
'content_id',
'content_address',
'content_model' ],
146 $from ?
"content_id > $from" :
'',
148 [
'ORDER BY' =>
'content_id',
'LIMIT' => $this->
getBatchSize() ]
153 foreach (
$res as $row ) {
154 $from = $row->content_id;
155 $this->
contentRowMap[
"{$row->content_model}:{$row->content_address}"] = $row->content_id;
166 $t0 = microtime(
true );
168 $this->
writeln(
"Populating $table..." );
170 if ( $table ===
'revision' ) {
172 $tables = [
'revision',
'slots',
'page' ];
176 'sha1' =>
'rev_sha1',
177 'text_id' =>
'rev_text_id',
178 'content_model' =>
'rev_content_model',
179 'namespace' =>
'page_namespace',
180 'title' =>
'page_title',
183 'slots' => [
'LEFT JOIN',
'rev_id=slot_revision_id' ],
184 'page' => [
'LEFT JOIN',
'rev_page=page_id' ],
188 $idField =
'ar_rev_id';
189 $tables = [
'archive',
'slots' ];
191 'rev_id' =>
'ar_rev_id',
194 'text_id' =>
'ar_text_id',
195 'content_model' =>
'ar_content_model',
196 'namespace' =>
'ar_namespace',
197 'title' =>
'ar_title',
200 'slots' => [
'LEFT JOIN',
'ar_rev_id=slot_revision_id' ],
205 if ( !$this->
dbw->fieldExists( $table, $fields[
'text_id'],
__METHOD__ ) ) {
206 $this->
writeln(
"No need to populate, $table.{$fields['text_id']} field does not exist" );
212 [
'min' =>
"MIN( $idField )",
'max' =>
"MAX( $idField )" ],
226 for ( $startId =
$minmax->min; $startId <=
$minmax->max; $startId += $batchSize ) {
227 $endId = min( $startId + $batchSize - 1,
$minmax->max );
228 $rows = $this->
dbw->select(
232 "$idField >= $startId",
233 "$idField <= $endId",
234 'slot_revision_id IS NULL',
237 [
'ORDER BY' =>
'rev_id' ],
240 if ( $rows->numRows() !== 0 ) {
246 "... $table processed up to revision id $endId of {$minmax->max}"
247 .
" ($this->count rows in $elapsed seconds)"
252 $this->
writeln(
"Done populating $table table. Processed $this->count rows in $elapsed seconds" );
274 foreach ( $rows
as $row ) {
277 Assert::invariant(
$revisionId !==
null,
'rev_id must not be null' );
281 $address = SqlBlobStore::makeAddressFromTextId( $row->text_id );
283 $key =
"{$modelId}:{$address}";
291 'content_size' => (
int)$row->len,
292 'content_sha1' => $row->sha1,
293 'content_model' => $modelId,
294 'content_address' => $address,
301 $id = $this->
dbw->selectField(
'content',
'MAX(content_id)',
'',
__METHOD__ );
305 [
'content_id',
'content_model',
'content_address' ],
306 'content_id > ' . (
int)$id,
309 foreach (
$res as $row ) {
310 $key = $row->content_model .
':' . $row->content_address;
311 $map[$key] = $row->content_id;
317 foreach ( $rows
as $row ) {
336 }
catch ( \Exception
$e ) {
338 $this->
fatalError(
"Failed to populate content table $table row batch starting at $startId "
339 .
"due to exception: " . $e->__toString() );
350 if (
isset( $row->content_model ) ) {
351 return $row->content_model;
354 $title = Title::makeTitle( $row->namespace, $row->title );
356 return ContentHandler::getDefaultModelFor(
$title );
363 $this->
output(
"$msg\n" );
375 if ( !
isset( $row->content_model ) ) {
377 $row->content_model = $model;
380 if (
isset( $row->len ) &&
isset( $row->sha1 ) && $row->sha1 !==
'' ) {
387 if ( !
isset( $row->len ) ) {
389 $handler = ContentHandler::getForModelID( $model );
395 if ( !
isset( $row->sha1 ) || $row->sha1 ===
'' ) {
396 $row->sha1 = SlotRecord::base36Sha1(
$blob );
const RUN_MAINTENANCE_IF_MAIN
Abstract maintenance class for quickly writing and churning out maintenance scripts with minimal effo...
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 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.
getOption( $name, $default=null)
Get an option, or return the default.
rollbackTransaction(IDatabase $dbw, $fname)
Rollback the transcation on a DB handle.
setBatchSize( $s=0)
Set the batch size.
fatalError( $msg, $exitCode=1)
Output a message and terminate the current script.
Populate the content and slot tables.
array null $contentRowMap
Map "{$modelId}:{$address}" to content_id.
NameTableStore $slotRoleStore
populateContentTablesForRowBatch(IResultWrapper $rows, $startId, $table)
__construct()
Default constructor.
execute()
Do the actual work.
fillMissingFields( $row, $model, $address)
Compute any missing fields in $row.
NameTableStore $contentModelStore
const SCHEMA_COMPAT_WRITE_NEW