47 parent::__construct();
49 $this->
addOption(
'type',
'Set compression type to either: gzip|concat',
false,
true,
't' );
52 'Maximum number of revisions in a concat chunk',
59 'Earliest date to check for uncompressed revisions',
64 $this->
addOption(
'end-date',
'Latest revision date to compress',
false,
true,
'e' );
67 'The id to start from (gzip -> text table, concat -> page table)',
74 'Store specified revisions in an external cluster (untested)',
80 'The page_id to stop at (only when using concat compression type)',
89 if ( !function_exists(
"gzdeflate" ) ) {
90 $this->
fatalError(
"You must enable zlib support in PHP to compress old revisions!\n" .
91 "Please see https://www.php.net/manual/en/ref.zlib.php\n" );
94 $type = $this->
getOption(
'type',
'concat' );
95 $chunkSize = $this->
getOption(
'chunksize', 20 );
96 $startId = $this->
getOption(
'startid', 0 );
97 $beginDate = $this->
getOption(
'begin-date',
'' );
98 $endDate = $this->
getOption(
'end-date',
'' );
100 $endId = $this->
getOption(
'endid',
false );
102 if ( $type !=
'concat' && $type !=
'gzip' ) {
103 $this->
error(
"Type \"{$type}\" not supported" );
106 if ( $extDB !=
'' ) {
107 $this->
output(
"Compressing database {$wgDBname} to external cluster {$extDB}\n"
108 . str_repeat(
'-', 76 ) .
"\n\n" );
110 $this->
output(
"Compressing database {$wgDBname}\n"
111 . str_repeat(
'-', 76 ) .
"\n\n" );
115 if ( $type ==
'concat' ) {
116 $success = $this->compressWithConcat( $startId, $chunkSize, $beginDate,
117 $endDate, $extDB, $endId );
119 $this->compressOldPages( $startId, $extDB );
123 $this->
output(
"Done.\n" );
133 private function compressOldPages( $start = 0, $extdb =
'' ) {
135 $this->
output(
"Starting from old_id $start...\n" );
138 $res = $dbw->newSelectQueryBuilder()
139 ->select( [
'old_id',
'old_flags',
'old_text' ] )
142 ->where(
"old_id>=$start" )
143 ->orderBy(
'old_id' )
144 ->limit( $chunksize )
145 ->caller( __METHOD__ )->fetchResultSet();
147 if ( $res->numRows() == 0 ) {
153 foreach ( $res as $row ) {
154 # print " {$row->old_id} - {$row->old_namespace}:{$row->old_title}\n";
155 $this->compressPage( $row, $extdb );
156 $last = $row->old_id;
159 $start = $last + 1; # Deletion may leave
long empty stretches
160 $this->
output(
"$start...\n" );
171 private function compressPage( $row, $extdb ) {
172 if ( str_contains( $row->old_flags,
'gzip' )
173 || str_contains( $row->old_flags,
'object' )
175 # print "Already compressed row {$row->old_id}\n";
179 $flags = $row->old_flags ?
"{$row->old_flags},gzip" :
"gzip";
180 $compress = gzdeflate( $row->old_text );
182 # Store in external storage if required
183 if ( $extdb !==
'' ) {
185 $storeObj = $esFactory->getDatabaseStore();
186 $compress = $storeObj->store( $extdb, $compress );
187 if ( $compress ===
false ) {
188 $this->
error(
"Unable to store object" );
195 $dbw->newUpdateQueryBuilder()
198 'old_flags' => $flags,
199 'old_text' => $compress
202 'old_id' => $row->old_id
204 ->caller( __METHOD__ )
221 private function compressWithConcat( $startId, $maxChunkSize, $beginDate,
222 $endDate, $extdb =
"", $maxPageId =
false
227 # Set up external storage
228 if ( $extdb !=
'' ) {
230 $storeObj = $esFactory->getDatabaseStore();
234 ->getBlobStoreFactory()
237 # Get all articles by page_id
239 $maxPageId = $dbr->newSelectQueryBuilder()
240 ->select(
'max(page_id)' )
242 ->caller( __METHOD__ )->fetchField();
244 $this->
output(
"Starting from $startId of $maxPageId\n" );
257 # For each article, get a list of revisions which fit the criteria
259 # No recompression, use a condition on old_flags
260 # Don't compress object type entities, because that might produce data loss when
261 # overwriting bulk storage concat rows. Don't compress external references, because
262 # the script doesn't yet delete rows from external storage.
264 $queryBuilderTemplate = $dbw->newSelectQueryBuilder()
265 ->select( [
'rev_id',
'old_id',
'old_flags',
'old_text' ] )
268 ->join(
'slots',
null,
'rev_id=slot_revision_id' )
269 ->join(
'content',
null,
'content_id=slot_content_id' )
270 ->join(
'text',
null,
'SUBSTRING(content_address, 4)=old_id' )
274 IExpression::NOT_LIKE,
275 new LikeValue( $dbr->anyString(),
'object', $dbr->anyString() )
278 IExpression::NOT_LIKE,
279 new LikeValue( $dbr->anyString(),
'external', $dbr->anyString() )
283 'slot_role_id' => $slotRoleStore->getId( SlotRecord::MAIN ),
284 'SUBSTRING(content_address, 1, 3)=' . $dbr->addQuotes(
'tt:' ),
288 if ( !preg_match(
'/^\d{14}$/', $beginDate ) ) {
289 $this->
error(
"Invalid begin date \"$beginDate\"\n" );
293 $queryBuilderTemplate->andWhere( $dbr->expr(
'rev_timestamp',
'>', $beginDate ) );
296 if ( !preg_match(
'/^\d{14}$/', $endDate ) ) {
297 $this->
error(
"Invalid end date \"$endDate\"\n" );
301 $queryBuilderTemplate->andWhere( $dbr->expr(
'rev_timestamp',
'<', $endDate ) );
304 for ( $pageId = $startId; $pageId <= $maxPageId; $pageId++ ) {
311 $pageRow = $dbr->newSelectQueryBuilder()
312 ->select( [
'page_id',
'page_namespace',
'page_title',
'rev_timestamp' ] )
314 ->straightJoin(
'revision',
null,
'page_latest = rev_id' )
315 ->where( $pageConds )
316 ->andWhere( [
'page_id' => $pageId ] )
317 ->caller( __METHOD__ )->fetchRow();
318 if ( $pageRow ===
false ) {
323 $titleObj = Title::makeTitle( $pageRow->page_namespace, $pageRow->page_title );
324 $this->
output(
"$pageId\t" . $titleObj->getPrefixedDBkey() .
" " );
327 $queryBuilder = clone $queryBuilderTemplate;
328 $revRes = $queryBuilder->where(
330 'rev_page' => $pageRow->page_id,
334 $dbr->expr(
'rev_timestamp',
'<', (
int)$pageRow->rev_timestamp ),
336 ->caller( __METHOD__ )->fetchResultSet();
339 foreach ( $revRes as $revRow ) {
343 if ( count( $revs ) < 2 ) {
344 # No revisions matching, no further processing
351 while ( $i < count( $revs ) ) {
352 if ( $i < count( $revs ) - $maxChunkSize ) {
353 $thisChunkSize = $maxChunkSize;
355 $thisChunkSize = count( $revs ) - $i;
362 $primaryOldid = $revs[$i]->old_id;
364 # Get the text of each revision and add it to the object
365 for ( $j = 0; $j < $thisChunkSize && $chunk->isHappy(); $j++ ) {
366 $oldid = $revs[$i + $j]->old_id;
368 # Get text. We do not need the full `extractBlob` since the query is built
369 # to fetch non-externalstore blobs.
370 $text = $blobStore->decompressData(
371 $revs[$i + $j]->old_text,
372 explode(
',', $revs[$i + $j]->old_flags )
375 if ( $text ===
false ) {
376 $this->
error(
"\nError, unable to get text in old_id $oldid" );
377 # $dbw->delete( 'old', [ 'old_id' => $oldid ] );
380 if ( $extdb ==
"" && $j == 0 ) {
381 $chunk->setText( $text );
384 # Don't make a stub if it's going to be longer than the article
385 # Stubs are typically about 100 bytes
386 if ( strlen( $text ) < 120 ) {
391 $stub->setLocation( $primaryOldid );
392 $stub->setReferrer( $oldid );
401 # If we couldn't actually use any stubs because the pages were too small, do nothing
403 if ( $extdb !=
"" ) {
404 # Move blob objects to External Storage
406 $stored = $storeObj->store( $extdb, serialize( $chunk ) );
407 if ( $stored ===
false ) {
408 $this->
error(
"Unable to store object" );
412 # Store External Storage URLs instead of Stub placeholders
413 foreach ( $stubs as $stub ) {
414 if ( $stub ===
false ) {
417 # $stored should provide base path to a BLOB
418 $url = $stored .
"/" . $stub->getHash();
419 $dbw->newUpdateQueryBuilder()
423 'old_flags' =>
'external,utf-8',
426 'old_id' => $stub->getReferrer(),
428 ->caller( __METHOD__ )
432 # Store the main object locally
433 $dbw->newUpdateQueryBuilder()
436 'old_text' => serialize( $chunk ),
437 'old_flags' =>
'object,utf-8',
440 'old_id' => $primaryOldid
442 ->caller( __METHOD__ )
445 # Store the stub objects
446 for ( $j = 1; $j < $thisChunkSize; $j++ ) {
447 # Skip if not compressing and don't overwrite the first revision
448 if ( $stubs[$j] !==
false && $revs[$i + $j]->old_id != $primaryOldid ) {
449 $dbw->newUpdateQueryBuilder()
452 'old_text' => serialize( $stubs[$j] ),
453 'old_flags' =>
'object,utf-8',
456 'old_id' => $revs[$i + $j]->old_id
458 ->caller( __METHOD__ )
467 $i += $thisChunkSize;
Concatenated gzip (CGZ) storage Improves compression ratio by concatenating like objects before gzipp...
Pointer object for an item within a CGZ blob stored in the text table.