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\SlotRecord
;
25 use MediaWiki\Storage\SqlBlobStore
;
26 use Wikimedia\Assert\Assert
;
27 use Wikimedia\Rdbms\IDatabase
;
28 use Wikimedia\Rdbms\ResultWrapper
;
30 require_once __DIR__
. '/Maintenance.php';
33 * Populate the content and slot tables.
36 class PopulateContentTables
extends Maintenance
{
41 /** @var NameTableStore */
42 private $contentModelStore;
47 /** @var array|null Map "{$modelId}:{$address}" to content_id */
48 private $contentRowMap = null;
50 private $count = 0, $totalCount = 0;
52 public function __construct() {
53 parent
::__construct();
55 $this->addDescription( 'Populate content and slot tables' );
56 $this->addOption( 'table', 'revision or archive table, or `all` to populate both', false,
58 $this->addOption( 'reuse-content',
59 'Reuse content table rows when the address and model are the same. '
60 . 'This will increase the script\'s time and memory usage, perhaps significantly.',
62 $this->addOption( 'start-revision', 'The rev_id to start at', false, true );
63 $this->addOption( 'start-archive', 'The ar_rev_id to start at', false, true );
64 $this->setBatchSize( 500 );
67 private function initServices() {
68 $this->dbw
= $this->getDB( DB_MASTER
);
69 $this->contentModelStore
= MediaWikiServices
::getInstance()->getContentModelStore();
70 $this->mainRoleId
= MediaWikiServices
::getInstance()->getSlotRoleStore()
71 ->acquireId( SlotRecord
::MAIN
);
74 public function execute() {
75 global $wgMultiContentRevisionSchemaMigrationStage;
77 $t0 = microtime( true );
79 if ( ( $wgMultiContentRevisionSchemaMigrationStage & SCHEMA_COMPAT_WRITE_NEW
) === 0 ) {
81 '...cannot update while \$wgMultiContentRevisionSchemaMigrationStage '
82 . 'does not have the SCHEMA_COMPAT_WRITE_NEW bit set.'
87 $this->initServices();
89 if ( $this->getOption( 'reuse-content', false ) ) {
90 $this->loadContentMap();
93 foreach ( $this->getTables() as $table ) {
94 $this->populateTable( $table );
97 $elapsed = microtime( true ) - $t0;
98 $this->writeln( "Done. Processed $this->totalCount rows in $elapsed seconds" );
105 private function getTables() {
106 $table = $this->getOption( 'table', 'all' );
107 $validTableOptions = [ 'all', 'revision', 'archive' ];
109 if ( !in_array( $table, $validTableOptions ) ) {
110 $this->fatalError( 'Invalid table. Must be either `revision` or `archive` or `all`' );
113 if ( $table === 'all' ) {
114 $tables = [ 'revision', 'archive' ];
116 $tables = [ $table ];
122 private function loadContentMap() {
123 $t0 = microtime( true );
124 $this->writeln( "Loading existing content table rows..." );
125 $this->contentRowMap
= [];
126 $dbr = $this->getDB( DB_REPLICA
);
131 [ 'content_id', 'content_address', 'content_model' ],
132 $from ?
"content_id > $from" : '',
134 [ 'ORDER BY' => 'content_id', 'LIMIT' => $this->getBatchSize() ]
136 if ( !$res ||
!$res->numRows() ) {
139 foreach ( $res as $row ) {
140 $from = $row->content_id
;
141 $this->contentRowMap
["{$row->content_model}:{$row->content_address}"] = $row->content_id
;
144 $elapsed = microtime( true ) - $t0;
145 $this->writeln( "Loaded " . count( $this->contentRowMap
) . " rows in $elapsed seconds" );
149 * @param string $table
151 private function populateTable( $table ) {
152 $t0 = microtime( true );
154 $this->writeln( "Populating $table..." );
156 if ( $table === 'revision' ) {
158 $tables = [ 'revision', 'slots', 'page' ];
162 'sha1' => 'rev_sha1',
163 'text_id' => 'rev_text_id',
164 'content_model' => 'rev_content_model',
165 'namespace' => 'page_namespace',
166 'title' => 'page_title',
169 'slots' => [ 'LEFT JOIN', 'rev_id=slot_revision_id' ],
170 'page' => [ 'LEFT JOIN', 'rev_page=page_id' ],
172 $startOption = 'start-revision';
174 $idField = 'ar_rev_id';
175 $tables = [ 'archive', 'slots' ];
177 'rev_id' => 'ar_rev_id',
180 'text_id' => 'ar_text_id',
181 'content_model' => 'ar_content_model',
182 'namespace' => 'ar_namespace',
183 'title' => 'ar_title',
186 'slots' => [ 'LEFT JOIN', 'ar_rev_id=slot_revision_id' ],
188 $startOption = 'start-archive';
191 $minmax = $this->dbw
->selectRow(
193 [ 'min' => "MIN( $idField )", 'max' => "MAX( $idField )" ],
197 if ( $this->hasOption( $startOption ) ) {
198 $minmax->min
= (int)$this->getOption( $startOption );
200 if ( !$minmax ||
!is_numeric( $minmax->min
) ||
!is_numeric( $minmax->max
) ) {
202 $minmax = (object)[ 'min' => 1, 'max' => 0 ];
205 $batchSize = $this->getBatchSize();
207 for ( $startId = $minmax->min
; $startId <= $minmax->max
; $startId +
= $batchSize ) {
208 $endId = min( $startId +
$batchSize - 1, $minmax->max
);
209 $rows = $this->dbw
->select(
213 "$idField >= $startId",
214 "$idField <= $endId",
215 'slot_revision_id IS NULL',
218 [ 'ORDER BY' => 'rev_id' ],
221 if ( $rows->numRows() !== 0 ) {
222 $this->populateContentTablesForRowBatch( $rows, $startId, $table );
225 $elapsed = microtime( true ) - $t0;
227 "... $table processed up to revision id $endId of {$minmax->max}"
228 . " ($this->count rows in $elapsed seconds)"
232 $elapsed = microtime( true ) - $t0;
233 $this->writeln( "Done populating $table table. Processed $this->count rows in $elapsed seconds" );
237 * @param ResultWrapper $rows
238 * @param int $startId
239 * @param string $table
242 private function populateContentTablesForRowBatch( ResultWrapper
$rows, $startId, $table ) {
243 $this->beginTransaction( $this->dbw
, __METHOD__
);
245 if ( $this->contentRowMap
=== null ) {
248 $map = &$this->contentRowMap
;
253 // Step 1: Figure out content rows needing insertion.
255 foreach ( $rows as $row ) {
256 $revisionId = $row->rev_id
;
258 Assert
::invariant( $revisionId !== null, 'rev_id must not be null' );
260 $modelId = $this->contentModelStore
->acquireId( $this->getContentModel( $row ) );
261 $address = SqlBlobStore
::makeAddressFromTextId( $row->text_id
);
263 $key = "{$modelId}:{$address}";
264 $contentKeys[$revisionId] = $key;
266 if ( !isset( $map[$key] ) ) {
269 'content_size' => (int)$row->len
,
270 'content_sha1' => $row->sha1
,
271 'content_model' => $modelId,
272 'content_address' => $address,
277 // Step 2: Insert them, then read them back in for use in the next step.
278 if ( $contentRows ) {
279 $id = $this->dbw
->selectField( 'content', 'MAX(content_id)', '', __METHOD__
);
280 $this->dbw
->insert( 'content', $contentRows, __METHOD__
);
281 $res = $this->dbw
->select(
283 [ 'content_id', 'content_model', 'content_address' ],
284 'content_id > ' . (int)$id,
287 foreach ( $res as $row ) {
288 $key = $row->content_model
. ':' . $row->content_address
;
289 $map[$key] = $row->content_id
;
293 // Step 3: Insert the slot rows.
295 foreach ( $rows as $row ) {
296 $revisionId = $row->rev_id
;
297 $contentId = $map[$contentKeys[$revisionId]] ??
false;
298 if ( $contentId === false ) {
299 throw new \
RuntimeException( "Content row for $revisionId not found after content insert" );
302 'slot_revision_id' => $revisionId,
303 'slot_role_id' => $this->mainRoleId
,
304 'slot_content_id' => $contentId,
305 // There's no way to really know the previous revision, so assume no inheriting.
306 // rev_parent_id can get changed on undeletions, and deletions can screw up
307 // rev_timestamp ordering.
308 'slot_origin' => $revisionId,
311 $this->dbw
->insert( 'slots', $slotRows, __METHOD__
);
312 $this->count +
= count( $slotRows );
313 $this->totalCount +
= count( $slotRows );
314 } catch ( \Exception
$e ) {
315 $this->rollbackTransaction( $this->dbw
, __METHOD__
);
316 $this->fatalError( "Failed to populate content table $table row batch starting at $startId "
317 . "due to exception: " . $e->__toString() );
320 $this->commitTransaction( $this->dbw
, __METHOD__
);
324 * @param \stdClass $row
327 private function getContentModel( $row ) {
328 if ( isset( $row->content_model
) ) {
329 return $row->content_model
;
332 $title = Title
::makeTitle( $row->namespace, $row->title
);
334 return ContentHandler
::getDefaultModelFor( $title );
340 private function writeln( $msg ) {
341 $this->output( "$msg\n" );
345 $maintClass = 'PopulateContentTables';
346 require_once RUN_MAINTENANCE_IF_MAIN
;