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 & SCHEMA_COMPAT_WRITE_NEW
) === 0 ) {
77 '...cannot update while \$wgMultiContentRevisionSchemaMigrationStage '
78 . 'does not have the SCHEMA_COMPAT_WRITE_NEW bit set.'
83 $this->initServices();
85 if ( $this->getOption( 'reuse-content', false ) ) {
86 $this->loadContentMap();
89 foreach ( $this->getTables() as $table ) {
90 $this->populateTable( $table );
93 $elapsed = microtime( true ) - $t0;
94 $this->writeln( "Done. Processed $this->totalCount rows in $elapsed seconds" );
101 private function getTables() {
102 $table = $this->getOption( 'table', 'all' );
103 $validTableOptions = [ 'all', 'revision', 'archive' ];
105 if ( !in_array( $table, $validTableOptions ) ) {
106 $this->fatalError( 'Invalid table. Must be either `revision` or `archive` or `all`' );
109 if ( $table === 'all' ) {
110 $tables = [ 'revision', 'archive' ];
112 $tables = [ $table ];
118 private function loadContentMap() {
119 $t0 = microtime( true );
120 $this->writeln( "Loading existing content table rows..." );
121 $this->contentRowMap
= [];
122 $dbr = $this->getDB( DB_REPLICA
);
127 [ 'content_id', 'content_address', 'content_model' ],
128 $from ?
"content_id > $from" : '',
130 [ 'ORDER BY' => 'content_id', 'LIMIT' => $this->getBatchSize() ]
132 if ( !$res ||
!$res->numRows() ) {
135 foreach ( $res as $row ) {
136 $from = $row->content_id
;
137 $this->contentRowMap
["{$row->content_model}:{$row->content_address}"] = $row->content_id
;
140 $elapsed = microtime( true ) - $t0;
141 $this->writeln( "Loaded " . count( $this->contentRowMap
) . " rows in $elapsed seconds" );
145 * @param string $table
147 private function populateTable( $table ) {
148 $t0 = microtime( true );
150 $this->writeln( "Populating $table..." );
152 if ( $table === 'revision' ) {
154 $tables = [ 'revision', 'slots', 'page' ];
158 'sha1' => 'rev_sha1',
159 'text_id' => 'rev_text_id',
160 'content_model' => 'rev_content_model',
161 'namespace' => 'page_namespace',
162 'title' => 'page_title',
165 'slots' => [ 'LEFT JOIN', 'rev_id=slot_revision_id' ],
166 'page' => [ 'LEFT JOIN', 'rev_page=page_id' ],
169 $idField = 'ar_rev_id';
170 $tables = [ 'archive', 'slots' ];
172 'rev_id' => 'ar_rev_id',
175 'text_id' => 'ar_text_id',
176 'content_model' => 'ar_content_model',
177 'namespace' => 'ar_namespace',
178 'title' => 'ar_title',
181 'slots' => [ 'LEFT JOIN', 'ar_rev_id=slot_revision_id' ],
185 $minmax = $this->dbw
->selectRow(
187 [ 'min' => "MIN( $idField )", 'max' => "MAX( $idField )" ],
191 $batchSize = $this->getBatchSize();
193 for ( $startId = $minmax->min
; $startId <= $minmax->max
; $startId +
= $batchSize ) {
194 $endId = min( $startId +
$batchSize - 1, $minmax->max
);
195 $rows = $this->dbw
->select(
199 "$idField >= $startId",
200 "$idField <= $endId",
201 'slot_revision_id IS NULL',
204 [ 'ORDER BY' => 'rev_id' ],
207 if ( $rows->numRows() !== 0 ) {
208 $this->populateContentTablesForRowBatch( $rows, $startId, $table );
211 $elapsed = microtime( true ) - $t0;
213 "... $table processed up to revision id $endId of {$minmax->max}"
214 . " ($this->count rows in $elapsed seconds)"
218 $elapsed = microtime( true ) - $t0;
219 $this->writeln( "Done populating $table table. Processed $this->count rows in $elapsed seconds" );
223 * @param ResultWrapper $rows
224 * @param int $startId
225 * @param string $table
228 private function populateContentTablesForRowBatch( ResultWrapper
$rows, $startId, $table ) {
229 $this->beginTransaction( $this->dbw
, __METHOD__
);
231 if ( $this->contentRowMap
=== null ) {
234 $map = &$this->contentRowMap
;
239 // Step 1: Figure out content rows needing insertion.
241 foreach ( $rows as $row ) {
242 $revisionId = $row->rev_id
;
244 Assert
::invariant( $revisionId !== null, 'rev_id must not be null' );
246 $modelId = $this->contentModelStore
->acquireId( $this->getContentModel( $row ) );
247 $address = SqlBlobStore
::makeAddressFromTextId( $row->text_id
);
249 $key = "{$modelId}:{$address}";
250 $contentKeys[$revisionId] = $key;
252 if ( !isset( $map[$key] ) ) {
255 'content_size' => (int)$row->len
,
256 'content_sha1' => $row->sha1
,
257 'content_model' => $modelId,
258 'content_address' => $address,
263 // Step 2: Insert them, then read them back in for use in the next step.
264 if ( $contentRows ) {
265 $id = $this->dbw
->selectField( 'content', 'MAX(content_id)', '', __METHOD__
);
266 $this->dbw
->insert( 'content', $contentRows, __METHOD__
);
267 $res = $this->dbw
->select(
269 [ 'content_id', 'content_model', 'content_address' ],
270 'content_id > ' . (int)$id,
273 foreach ( $res as $row ) {
274 $key = $row->content_model
. ':' . $row->content_address
;
275 $map[$key] = $row->content_id
;
279 // Step 3: Insert the slot rows.
281 foreach ( $rows as $row ) {
282 $revisionId = $row->rev_id
;
283 $contentId = $map[$contentKeys[$revisionId]] ??
false;
284 if ( $contentId === false ) {
285 throw new \
RuntimeException( "Content row for $revisionId not found after content insert" );
288 'slot_revision_id' => $revisionId,
289 'slot_role_id' => $this->mainRoleId
,
290 'slot_content_id' => $contentId,
291 // There's no way to really know the previous revision, so assume no inheriting.
292 // rev_parent_id can get changed on undeletions, and deletions can screw up
293 // rev_timestamp ordering.
294 'slot_origin' => $revisionId,
297 $this->dbw
->insert( 'slots', $slotRows, __METHOD__
);
298 $this->count +
= count( $slotRows );
299 $this->totalCount +
= count( $slotRows );
300 } catch ( \Exception
$e ) {
301 $this->rollbackTransaction( $this->dbw
, __METHOD__
);
302 $this->fatalError( "Failed to populate content table $table row batch starting at $startId "
303 . "due to exception: " . $e->__toString() );
306 $this->commitTransaction( $this->dbw
, __METHOD__
);
310 * @param \stdClass $row
313 private function getContentModel( $row ) {
314 if ( isset( $row->content_model
) ) {
315 return $row->content_model
;
318 $title = Title
::makeTitle( $row->namespace, $row->title
);
320 return ContentHandler
::getDefaultModelFor( $title );
326 private function writeln( $msg ) {
327 $this->output( "$msg\n" );
331 $maintClass = 'PopulateContentTables';
332 require_once RUN_MAINTENANCE_IF_MAIN
;