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->setBatchSize( 500 );
64 private function initServices() {
65 $this->dbw
= $this->getDB( DB_MASTER
);
66 $this->contentModelStore
= MediaWikiServices
::getInstance()->getContentModelStore();
67 $this->mainRoleId
= MediaWikiServices
::getInstance()->getSlotRoleStore()->acquireId( 'main' );
70 public function execute() {
71 global $wgMultiContentRevisionSchemaMigrationStage;
73 $t0 = microtime( true );
75 if ( $wgMultiContentRevisionSchemaMigrationStage < MIGRATION_WRITE_BOTH
) {
77 "...cannot update while \$wgMultiContentRevisionSchemaMigrationStage < MIGRATION_WRITE_BOTH"
82 $this->initServices();
84 if ( $this->getOption( 'reuse-content', false ) ) {
85 $this->loadContentMap();
88 foreach ( $this->getTables() as $table ) {
89 $this->populateTable( $table );
92 $elapsed = microtime( true ) - $t0;
93 $this->writeln( "Done. Processed $this->totalCount rows in $elapsed seconds" );
99 private function getTables() {
100 $table = $this->getOption( 'table', 'all' );
101 $validTableOptions = [ 'all', 'revision', 'archive' ];
103 if ( !in_array( $table, $validTableOptions ) ) {
104 $this->fatalError( 'Invalid table. Must be either `revision` or `archive` or `all`' );
107 if ( $table === 'all' ) {
108 $tables = [ 'revision', 'archive' ];
110 $tables = [ $table ];
116 private function loadContentMap() {
117 $t0 = microtime( true );
118 $this->writeln( "Loading existing content table rows..." );
119 $this->contentRowMap
= [];
120 $dbr = $this->getDB( DB_REPLICA
);
125 [ 'content_id', 'content_address', 'content_model' ],
126 $from ?
"content_id > $from" : '',
128 [ 'ORDER BY' => 'content_id', 'LIMIT' => $this->getBatchSize() ]
130 if ( !$res ||
!$res->numRows() ) {
133 foreach ( $res as $row ) {
134 $from = $row->content_id
;
135 $this->contentRowMap
["{$row->content_model}:{$row->content_address}"] = $row->content_id
;
138 $elapsed = microtime( true ) - $t0;
139 $this->writeln( "Loaded " . count( $this->contentRowMap
) . " rows in $elapsed seconds" );
143 * @param string $table
145 private function populateTable( $table ) {
146 $t0 = microtime( true );
148 $this->writeln( "Populating $table..." );
150 if ( $table === 'revision' ) {
152 $tables = [ 'revision', 'slots', 'page' ];
156 'sha1' => 'rev_sha1',
157 'text_id' => 'rev_text_id',
158 'content_model' => 'rev_content_model',
159 'namespace' => 'page_namespace',
160 'title' => 'page_title',
163 'slots' => [ 'LEFT JOIN', 'rev_id=slot_revision_id' ],
164 'page' => [ 'LEFT JOIN', 'rev_page=page_id' ],
167 $idField = 'ar_rev_id';
168 $tables = [ 'archive', 'slots' ];
170 'rev_id' => 'ar_rev_id',
173 'text_id' => 'ar_text_id',
174 'content_model' => 'ar_content_model',
175 'namespace' => 'ar_namespace',
176 'title' => 'ar_title',
179 'slots' => [ 'LEFT JOIN', 'ar_rev_id=slot_revision_id' ],
183 $minmax = $this->dbw
->selectRow(
185 [ 'min' => "MIN( $idField )", 'max' => "MAX( $idField )" ],
189 $batchSize = $this->getBatchSize();
191 for ( $startId = $minmax->min
; $startId <= $minmax->max
; $startId +
= $batchSize ) {
192 $endId = min( $startId +
$batchSize - 1, $minmax->max
);
193 $rows = $this->dbw
->select(
197 "$idField >= $startId",
198 "$idField <= $endId",
199 'slot_revision_id IS NULL',
202 [ 'ORDER BY' => 'rev_id' ],
205 if ( $rows->numRows() !== 0 ) {
206 $this->populateContentTablesForRowBatch( $rows, $startId, $table );
209 $elapsed = microtime( true ) - $t0;
211 "... $table processed up to revision id $endId of {$minmax->max}"
212 . " ($this->count rows in $elapsed seconds)"
216 $elapsed = microtime( true ) - $t0;
217 $this->writeln( "Done populating $table table. Processed $this->count rows in $elapsed seconds" );
221 * @param ResultWrapper $rows
222 * @param int $startId
223 * @param string $table
226 private function populateContentTablesForRowBatch( ResultWrapper
$rows, $startId, $table ) {
227 $this->beginTransaction( $this->dbw
, __METHOD__
);
229 if ( $this->contentRowMap
=== null ) {
232 $map = &$this->contentRowMap
;
237 // Step 1: Figure out content rows needing insertion.
239 foreach ( $rows as $row ) {
240 $revisionId = $row->rev_id
;
242 Assert
::invariant( $revisionId !== null, 'rev_id must not be null' );
244 $modelId = $this->contentModelStore
->acquireId( $this->getContentModel( $row ) );
245 $address = SqlBlobStore
::makeAddressFromTextId( $row->text_id
);
247 $key = "{$modelId}:{$address}";
248 $contentKeys[$revisionId] = $key;
250 if ( !isset( $map[$key] ) ) {
253 'content_size' => (int)$row->len
,
254 'content_sha1' => $row->sha1
,
255 'content_model' => $modelId,
256 'content_address' => $address,
261 // Step 2: Insert them, then read them back in for use in the next step.
262 if ( $contentRows ) {
263 $id = $this->dbw
->selectField( 'content', 'MAX(content_id)', '', __METHOD__
);
264 $this->dbw
->insert( 'content', $contentRows, __METHOD__
);
265 $res = $this->dbw
->select(
267 [ 'content_id', 'content_model', 'content_address' ],
268 'content_id > ' . (int)$id,
271 foreach ( $res as $row ) {
272 $key = $row->content_model
. ':' . $row->content_address
;
273 $map[$key] = $row->content_id
;
277 // Step 3: Insert the slot rows.
279 foreach ( $rows as $row ) {
280 $revisionId = $row->rev_id
;
281 $contentId = $map[$contentKeys[$revisionId]] ??
false;
282 if ( $contentId === false ) {
283 throw new \
RuntimeException( "Content row for $revisionId not found after content insert" );
286 'slot_revision_id' => $revisionId,
287 'slot_role_id' => $this->mainRoleId
,
288 'slot_content_id' => $contentId,
289 // There's no way to really know the previous revision, so assume no inheriting.
290 // rev_parent_id can get changed on undeletions, and deletions can screw up
291 // rev_timestamp ordering.
292 'slot_origin' => $revisionId,
295 $this->dbw
->insert( 'slots', $slotRows, __METHOD__
);
296 $this->count +
= count( $slotRows );
297 $this->totalCount +
= count( $slotRows );
298 } catch ( \Exception
$e ) {
299 $this->rollbackTransaction( $this->dbw
, __METHOD__
);
300 $this->fatalError( "Failed to populate content table $table row batch starting at $startId "
301 . "due to exception: " . $e->__toString() );
304 $this->commitTransaction( $this->dbw
, __METHOD__
);
308 * @param \stdClass $row
311 private function getContentModel( $row ) {
312 if ( isset( $row->content_model
) ) {
313 return $row->content_model
;
316 $title = Title
::makeTitle( $row->namespace, $row->title
);
318 return ContentHandler
::getDefaultModelFor( $title );
324 private function writeln( $msg ) {
325 $this->output( "$msg\n" );
329 $maintClass = 'PopulateContentTables';
330 require_once RUN_MAINTENANCE_IF_MAIN
;