3 * This program is free software; you can redistribute it and/or modify
4 * it under the terms of the GNU General Public License as published by
5 * the Free Software Foundation; either version 2 of the License, or
6 * (at your option) any later version.
8 * This program is distributed in the hope that it will be useful,
9 * but WITHOUT ANY WARRANTY; without even the implied warranty of
10 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
11 * GNU General Public License for more details.
13 * You should have received a copy of the GNU General Public License along
14 * with this program; if not, write to the Free Software Foundation, Inc.,
15 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
16 * http://www.gnu.org/copyleft/gpl.html
19 * @ingroup Maintenance
22 use MediaWiki\MediaWikiServices
;
23 use MediaWiki\Storage\NameTableStore
;
24 use MediaWiki\Storage\SqlBlobStore
;
25 use Wikimedia\Assert\Assert
;
26 use Wikimedia\Rdbms\IDatabase
;
27 use Wikimedia\Rdbms\ResultWrapper
;
29 require_once __DIR__
. '/Maintenance.php';
32 * Populate the content and slot tables.
35 class PopulateContentTables
extends Maintenance
{
40 /** @var NameTableStore */
41 private $contentModelStore;
46 /** @var array|null Map "{$modelId}:{$address}" to content_id */
47 private $contentRowMap = null;
49 private $count = 0, $totalCount = 0;
51 public function __construct() {
52 parent
::__construct();
54 $this->addDescription( 'Populate content and slot tables' );
55 $this->addOption( 'table', 'revision or archive table, or `all` to populate both', false,
57 $this->addOption( 'reuse-content',
58 'Reuse content table rows when the address and model are the same. '
59 . 'This will increase the script\'s time and memory usage, perhaps significantly.',
61 $this->addOption( 'start-revision', 'The rev_id to start at', false, true );
62 $this->addOption( 'start-archive', 'The ar_rev_id to start at', false, true );
63 $this->setBatchSize( 500 );
66 private function initServices() {
67 $this->dbw
= $this->getDB( DB_MASTER
);
68 $this->contentModelStore
= MediaWikiServices
::getInstance()->getContentModelStore();
69 $this->mainRoleId
= MediaWikiServices
::getInstance()->getSlotRoleStore()->acquireId( 'main' );
72 public function execute() {
73 global $wgMultiContentRevisionSchemaMigrationStage;
75 $t0 = microtime( true );
77 if ( ( $wgMultiContentRevisionSchemaMigrationStage & SCHEMA_COMPAT_WRITE_NEW
) === 0 ) {
79 '...cannot update while \$wgMultiContentRevisionSchemaMigrationStage '
80 . 'does not have the SCHEMA_COMPAT_WRITE_NEW bit set.'
85 $this->initServices();
87 if ( $this->getOption( 'reuse-content', false ) ) {
88 $this->loadContentMap();
91 foreach ( $this->getTables() as $table ) {
92 $this->populateTable( $table );
95 $elapsed = microtime( true ) - $t0;
96 $this->writeln( "Done. Processed $this->totalCount rows in $elapsed seconds" );
103 private function getTables() {
104 $table = $this->getOption( 'table', 'all' );
105 $validTableOptions = [ 'all', 'revision', 'archive' ];
107 if ( !in_array( $table, $validTableOptions ) ) {
108 $this->fatalError( 'Invalid table. Must be either `revision` or `archive` or `all`' );
111 if ( $table === 'all' ) {
112 $tables = [ 'revision', 'archive' ];
114 $tables = [ $table ];
120 private function loadContentMap() {
121 $t0 = microtime( true );
122 $this->writeln( "Loading existing content table rows..." );
123 $this->contentRowMap
= [];
124 $dbr = $this->getDB( DB_REPLICA
);
129 [ 'content_id', 'content_address', 'content_model' ],
130 $from ?
"content_id > $from" : '',
132 [ 'ORDER BY' => 'content_id', 'LIMIT' => $this->getBatchSize() ]
134 if ( !$res ||
!$res->numRows() ) {
137 foreach ( $res as $row ) {
138 $from = $row->content_id
;
139 $this->contentRowMap
["{$row->content_model}:{$row->content_address}"] = $row->content_id
;
142 $elapsed = microtime( true ) - $t0;
143 $this->writeln( "Loaded " . count( $this->contentRowMap
) . " rows in $elapsed seconds" );
147 * @param string $table
149 private function populateTable( $table ) {
150 $t0 = microtime( true );
152 $this->writeln( "Populating $table..." );
154 if ( $table === 'revision' ) {
156 $tables = [ 'revision', 'slots', 'page' ];
160 'sha1' => 'rev_sha1',
161 'text_id' => 'rev_text_id',
162 'content_model' => 'rev_content_model',
163 'namespace' => 'page_namespace',
164 'title' => 'page_title',
167 'slots' => [ 'LEFT JOIN', 'rev_id=slot_revision_id' ],
168 'page' => [ 'LEFT JOIN', 'rev_page=page_id' ],
170 $startOption = 'start-revision';
172 $idField = 'ar_rev_id';
173 $tables = [ 'archive', 'slots' ];
175 'rev_id' => 'ar_rev_id',
178 'text_id' => 'ar_text_id',
179 'content_model' => 'ar_content_model',
180 'namespace' => 'ar_namespace',
181 'title' => 'ar_title',
184 'slots' => [ 'LEFT JOIN', 'ar_rev_id=slot_revision_id' ],
186 $startOption = 'start-archive';
189 $minmax = $this->dbw
->selectRow(
191 [ 'min' => "MIN( $idField )", 'max' => "MAX( $idField )" ],
195 if ( $this->hasOption( $startOption ) ) {
196 $minmax->min
= (int)$this->getOption( $startOption );
198 if ( !$minmax ||
!is_numeric( $minmax->min
) ||
!is_numeric( $minmax->max
) ) {
200 $minmax = (object)[ 'min' => 1, 'max' => 0 ];
203 $batchSize = $this->getBatchSize();
205 for ( $startId = $minmax->min
; $startId <= $minmax->max
; $startId +
= $batchSize ) {
206 $endId = min( $startId +
$batchSize - 1, $minmax->max
);
207 $rows = $this->dbw
->select(
211 "$idField >= $startId",
212 "$idField <= $endId",
213 'slot_revision_id IS NULL',
216 [ 'ORDER BY' => 'rev_id' ],
219 if ( $rows->numRows() !== 0 ) {
220 $this->populateContentTablesForRowBatch( $rows, $startId, $table );
223 $elapsed = microtime( true ) - $t0;
225 "... $table processed up to revision id $endId of {$minmax->max}"
226 . " ($this->count rows in $elapsed seconds)"
230 $elapsed = microtime( true ) - $t0;
231 $this->writeln( "Done populating $table table. Processed $this->count rows in $elapsed seconds" );
235 * @param ResultWrapper $rows
236 * @param int $startId
237 * @param string $table
240 private function populateContentTablesForRowBatch( ResultWrapper
$rows, $startId, $table ) {
241 $this->beginTransaction( $this->dbw
, __METHOD__
);
243 if ( $this->contentRowMap
=== null ) {
246 $map = &$this->contentRowMap
;
251 // Step 1: Figure out content rows needing insertion.
253 foreach ( $rows as $row ) {
254 $revisionId = $row->rev_id
;
256 Assert
::invariant( $revisionId !== null, 'rev_id must not be null' );
258 $modelId = $this->contentModelStore
->acquireId( $this->getContentModel( $row ) );
259 $address = SqlBlobStore
::makeAddressFromTextId( $row->text_id
);
261 $key = "{$modelId}:{$address}";
262 $contentKeys[$revisionId] = $key;
264 if ( !isset( $map[$key] ) ) {
267 'content_size' => (int)$row->len
,
268 'content_sha1' => $row->sha1
,
269 'content_model' => $modelId,
270 'content_address' => $address,
275 // Step 2: Insert them, then read them back in for use in the next step.
276 if ( $contentRows ) {
277 $id = $this->dbw
->selectField( 'content', 'MAX(content_id)', '', __METHOD__
);
278 $this->dbw
->insert( 'content', $contentRows, __METHOD__
);
279 $res = $this->dbw
->select(
281 [ 'content_id', 'content_model', 'content_address' ],
282 'content_id > ' . (int)$id,
285 foreach ( $res as $row ) {
286 $key = $row->content_model
. ':' . $row->content_address
;
287 $map[$key] = $row->content_id
;
291 // Step 3: Insert the slot rows.
293 foreach ( $rows as $row ) {
294 $revisionId = $row->rev_id
;
295 $contentId = $map[$contentKeys[$revisionId]] ??
false;
296 if ( $contentId === false ) {
297 throw new \
RuntimeException( "Content row for $revisionId not found after content insert" );
300 'slot_revision_id' => $revisionId,
301 'slot_role_id' => $this->mainRoleId
,
302 'slot_content_id' => $contentId,
303 // There's no way to really know the previous revision, so assume no inheriting.
304 // rev_parent_id can get changed on undeletions, and deletions can screw up
305 // rev_timestamp ordering.
306 'slot_origin' => $revisionId,
309 $this->dbw
->insert( 'slots', $slotRows, __METHOD__
);
310 $this->count +
= count( $slotRows );
311 $this->totalCount +
= count( $slotRows );
312 } catch ( \Exception
$e ) {
313 $this->rollbackTransaction( $this->dbw
, __METHOD__
);
314 $this->fatalError( "Failed to populate content table $table row batch starting at $startId "
315 . "due to exception: " . $e->__toString() );
318 $this->commitTransaction( $this->dbw
, __METHOD__
);
322 * @param \stdClass $row
325 private function getContentModel( $row ) {
326 if ( isset( $row->content_model
) ) {
327 return $row->content_model
;
330 $title = Title
::makeTitle( $row->namespace, $row->title
);
332 return ContentHandler
::getDefaultModelFor( $title );
338 private function writeln( $msg ) {
339 $this->output( "$msg\n" );
343 $maintClass = 'PopulateContentTables';
344 require_once RUN_MAINTENANCE_IF_MAIN
;