56 parent::__construct();
58 $this->
addOption(
'type',
'Set compression type to either: gzip|concat',
false,
true,
't' );
61 'Maximum number of revisions in a concat chunk',
68 'Earliest date to check for uncompressed revisions',
73 $this->
addOption(
'end-date',
'Latest revision date to compress',
false,
true,
'e' );
76 'The id to start from (gzip -> text table, concat -> page table)',
83 'Store specified revisions in an external cluster (untested)',
89 'The page_id to stop at (only when using concat compression type)',
98 if ( !function_exists(
"gzdeflate" ) ) {
99 $this->
fatalError(
"You must enable zlib support in PHP to compress old revisions!\n" .
100 "Please see https://www.php.net/manual/en/ref.zlib.php\n" );
104 $chunkSize = $this->
getOption(
'chunksize', 20 );
105 $startId = $this->
getOption(
'startid', 0 );
106 $beginDate = $this->
getOption(
'begin-date',
'' );
107 $endDate = $this->
getOption(
'end-date',
'' );
108 $extDB = $this->
getOption(
'extdb',
'' );
109 $endId = $this->
getOption(
'endid',
false );
112 $this->
error(
"Type \"{$type}\" not supported" );
115 if ( $extDB !=
'' ) {
116 $this->
output(
"Compressing database {$wgDBname} to external cluster {$extDB}\n"
117 . str_repeat(
'-', 76 ) .
"\n\n" );
119 $this->
output(
"Compressing database {$wgDBname}\n"
120 . str_repeat(
'-', 76 ) .
"\n\n" );
124 if (
$type ==
'concat' ) {
125 $success = $this->compressWithConcat( $startId, $chunkSize, $beginDate,
126 $endDate, $extDB, $endId );
128 $this->compressOldPages( $startId, $extDB );
132 $this->
output(
"Done.\n" );
142 private function compressOldPages( $start = 0, $extdb =
'' ) {
144 $this->
output(
"Starting from old_id $start...\n" );
149 [
'old_id',
'old_flags',
'old_text' ],
152 [
'ORDER BY' =>
'old_id',
'LIMIT' => $chunksize,
'FOR UPDATE' ]
155 if (
$res->numRows() == 0 ) {
161 foreach (
$res as $row ) {
162 # print " {$row->old_id} - {$row->old_namespace}:{$row->old_title}\n";
163 $this->compressPage( $row, $extdb );
164 $last = $row->old_id;
167 $start = $last + 1; # Deletion may leave
long empty stretches
168 $this->
output(
"$start...\n" );
179 private function compressPage( $row, $extdb ) {
180 if ( strpos( $row->old_flags,
'gzip' ) !==
false
181 || strpos( $row->old_flags,
'object' ) !==
false
183 # print "Already compressed row {$row->old_id}\n";
187 $flags = $row->old_flags ?
"{$row->old_flags},gzip" :
"gzip";
188 $compress = gzdeflate( $row->old_text );
190 # Store in external storage if required
191 if ( $extdb !==
'' ) {
192 $esFactory = MediaWikiServices::getInstance()->getExternalStoreFactory();
194 $storeObj = $esFactory->getStore(
'DB' );
195 $compress = $storeObj->store( $extdb, $compress );
196 if ( $compress ===
false ) {
197 $this->
error(
"Unable to store object" );
204 $dbw->update(
'text',
206 'old_flags' => $flags,
207 'old_text' => $compress
209 'old_id' => $row->old_id
228 private function compressWithConcat( $startId, $maxChunkSize, $beginDate,
229 $endDate, $extdb =
"", $maxPageId =
false
234 # Set up external storage
235 if ( $extdb !=
'' ) {
236 $esFactory = MediaWikiServices::getInstance()->getExternalStoreFactory();
238 $storeObj = $esFactory->getStore(
'DB' );
241 $blobStore = MediaWikiServices::getInstance()
242 ->getBlobStoreFactory()
245 # Get all articles by page_id
247 $maxPageId =
$dbr->selectField(
'page',
'max(page_id)',
'', __METHOD__ );
249 $this->
output(
"Starting from $startId of $maxPageId\n" );
262 # For each article, get a list of revisions which fit the criteria
264 # No recompression, use a condition on old_flags
265 # Don't compress object type entities, because that might produce data loss when
266 # overwriting bulk storage concat rows. Don't compress external references, because
267 # the script doesn't yet delete rows from external storage.
269 'old_flags NOT ' .
$dbr->buildLike(
$dbr->anyString(),
'object',
$dbr->anyString() )
270 .
' AND old_flags NOT '
271 .
$dbr->buildLike(
$dbr->anyString(),
'external',
$dbr->anyString() )
275 if ( !preg_match(
'/^\d{14}$/', $beginDate ) ) {
276 $this->
error(
"Invalid begin date \"$beginDate\"\n" );
280 $conds[] =
"rev_timestamp>'" . $beginDate .
"'";
283 if ( !preg_match(
'/^\d{14}$/', $endDate ) ) {
284 $this->
error(
"Invalid end date \"$endDate\"\n" );
288 $conds[] =
"rev_timestamp<'" . $endDate .
"'";
291 $slotRoleStore = MediaWikiServices::getInstance()->getSlotRoleStore();
292 $tables = [
'revision',
'slots',
'content',
'text' ];
293 $conds = array_merge( [
294 'rev_id=slot_revision_id',
295 'slot_role_id=' . $slotRoleStore->getId( SlotRecord::MAIN ),
296 'content_id=slot_content_id',
297 'SUBSTRING(content_address, 1, 3)=' .
$dbr->addQuotes(
'tt:' ),
298 'SUBSTRING(content_address, 4)=old_id',
301 $fields = [
'rev_id',
'old_id',
'old_flags',
'old_text' ];
302 $revLoadOptions =
'FOR UPDATE';
304 # Don't work with current revisions
305 # Don't lock the page table for update either -- TS 2006-04-04
306 # $tables[] = 'page';
307 # $conds[] = 'page_id=rev_page AND rev_id != page_latest';
309 for ( $pageId = $startId; $pageId <= $maxPageId; $pageId++ ) {
316 $pageRes =
$dbr->select(
'page',
317 [
'page_id',
'page_namespace',
'page_title',
'page_latest' ],
318 $pageConds + [
'page_id' => $pageId ], __METHOD__ );
319 if ( $pageRes->numRows() == 0 ) {
322 $pageRow = $pageRes->fetchObject();
325 $titleObj = Title::makeTitle( $pageRow->page_namespace, $pageRow->page_title );
326 $this->
output(
"$pageId\t" . $titleObj->getPrefixedDBkey() .
" " );
329 $revRes = $dbw->select( $tables, $fields,
331 'rev_page' => $pageRow->page_id,
332 # Don
't operate on the current revision
333 # Use < instead of <> in case the current revision has changed
334 # since the page select, which wasn't locking
335 'rev_id < ' . (
int)$pageRow->page_latest
341 foreach ( $revRes as $revRow ) {
345 if ( count( $revs ) < 2 ) {
346 # No revisions matching, no further processing
353 while ( $i < count( $revs ) ) {
354 if ( $i < count( $revs ) - $maxChunkSize ) {
355 $thisChunkSize = $maxChunkSize;
357 $thisChunkSize = count( $revs ) - $i;
364 $primaryOldid = $revs[$i]->old_id;
366 # Get the text of each revision and add it to the object
367 for ( $j = 0; $j < $thisChunkSize && $chunk->isHappy(); $j++ ) {
368 $oldid = $revs[$i + $j]->old_id;
370 # Get text. We do not need the full `extractBlob` since the query is built
371 # to fetch non-externalstore blobs.
372 $text = $blobStore->decompressData(
373 $revs[$i + $j]->old_text,
374 explode(
',', $revs[$i + $j]->old_flags )
377 if ( $text ===
false ) {
378 $this->
error(
"\nError, unable to get text in old_id $oldid" );
379 # $dbw->delete( 'old', [ 'old_id' => $oldid ] );
382 if ( $extdb ==
"" && $j == 0 ) {
383 $chunk->setText( $text );
386 # Don't make a stub if it's going to be longer than the article
387 # Stubs are typically about 100 bytes
388 if ( strlen( $text ) < 120 ) {
393 $stub->setLocation( $primaryOldid );
394 $stub->setReferrer( $oldid );
403 # If we couldn't actually use any stubs because the pages were too small, do nothing
405 if ( $extdb !=
"" ) {
406 # Move blob objects to External Storage
408 $stored = $storeObj->store( $extdb, serialize( $chunk ) );
409 if ( $stored ===
false ) {
410 $this->
error(
"Unable to store object" );
414 # Store External Storage URLs instead of Stub placeholders
415 foreach ( $stubs as $stub ) {
416 if ( $stub ===
false ) {
419 # $stored should provide base path to a BLOB
420 $url = $stored .
"/" . $stub->getHash();
421 $dbw->update(
'text',
424 'old_flags' =>
'external,utf-8',
426 'old_id' => $stub->getReferrer(),
432 # Store the main object locally
433 $dbw->update(
'text',
435 'old_text' => serialize( $chunk ),
436 'old_flags' =>
'object,utf-8',
438 'old_id' => $primaryOldid
443 # Store the stub objects
444 for ( $j = 1; $j < $thisChunkSize; $j++ ) {
445 # Skip if not compressing and don't overwrite the first revision
446 if ( $stubs[$j] !==
false && $revs[$i + $j]->old_id != $primaryOldid ) {
447 $dbw->update(
'text',
449 'old_text' => serialize( $stubs[$j] ),
450 'old_flags' =>
'object,utf-8',
452 'old_id' => $revs[$i + $j]->old_id
463 $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.
Abstract maintenance class for quickly writing and churning out maintenance scripts with minimal effo...
error( $err, $die=0)
Throw an error to the user.
beginTransaction(IDatabase $dbw, $fname)
Begin a transaction on a DB.
commitTransaction(IDatabase $dbw, $fname)
Commit the transaction on a DB handle and wait for replica DBs to catch up.
output( $out, $channel=null)
Throw some output to the user.
waitForReplication()
Wait for replica DBs to catch up.
addDescription( $text)
Set the description text.
addOption( $name, $description, $required=false, $withArg=false, $shortName=false, $multiOccurrence=false)
Add a parameter to the script.
getOption( $name, $default=null)
Get an option, or return the default.
fatalError( $msg, $exitCode=1)
Output a message and terminate the current script.