179 if ( strpos( $row->old_flags,
'gzip' ) !==
false
180 || strpos( $row->old_flags,
'object' ) !==
false
182 # print "Already compressed row {$row->old_id}\n";
186 $flags = $row->old_flags ?
"{$row->old_flags},gzip" :
"gzip";
187 $compress = gzdeflate( $row->old_text );
189 # Store in external storage if required
190 if ( $extdb !==
'' ) {
191 $esFactory = MediaWikiServices::getInstance()->getExternalStoreFactory();
193 $storeObj = $esFactory->getStore(
'DB' );
194 $compress = $storeObj->store( $extdb, $compress );
195 if ( $compress ===
false ) {
196 $this->
error(
"Unable to store object" );
203 $dbw->update(
'text',
205 'old_flags' => $flags,
206 'old_text' => $compress
208 'old_id' => $row->old_id
228 $endDate, $extdb =
"", $maxPageId =
false
233 # Set up external storage
234 if ( $extdb !=
'' ) {
235 $esFactory = MediaWikiServices::getInstance()->getExternalStoreFactory();
237 $storeObj = $esFactory->getStore(
'DB' );
240 $blobStore = MediaWikiServices::getInstance()
241 ->getBlobStoreFactory()
244 # Get all articles by page_id
246 $maxPageId =
$dbr->selectField(
'page',
'max(page_id)',
'', __METHOD__ );
248 $this->
output(
"Starting from $startId of $maxPageId\n" );
261 # For each article, get a list of revisions which fit the criteria
263 # No recompression, use a condition on old_flags
264 # Don't compress object type entities, because that might produce data loss when
265 # overwriting bulk storage concat rows. Don't compress external references, because
266 # the script doesn't yet delete rows from external storage.
268 'old_flags NOT ' .
$dbr->buildLike(
$dbr->anyString(),
'object',
$dbr->anyString() )
269 .
' AND old_flags NOT '
270 .
$dbr->buildLike(
$dbr->anyString(),
'external',
$dbr->anyString() )
274 if ( !preg_match(
'/^\d{14}$/', $beginDate ) ) {
275 $this->
error(
"Invalid begin date \"$beginDate\"\n" );
279 $conds[] =
"rev_timestamp>'" . $beginDate .
"'";
282 if ( !preg_match(
'/^\d{14}$/', $endDate ) ) {
283 $this->
error(
"Invalid end date \"$endDate\"\n" );
287 $conds[] =
"rev_timestamp<'" . $endDate .
"'";
290 $slotRoleStore = MediaWikiServices::getInstance()->getSlotRoleStore();
291 $tables = [
'revision',
'slots',
'content',
'text' ];
292 $conds = array_merge( [
293 'rev_id=slot_revision_id',
294 'slot_role_id=' . $slotRoleStore->getId( SlotRecord::MAIN ),
295 'content_id=slot_content_id',
296 'SUBSTRING(content_address, 1, 3)=' .
$dbr->addQuotes(
'tt:' ),
297 'SUBSTRING(content_address, 4)=old_id',
300 $fields = [
'rev_id',
'old_id',
'old_flags',
'old_text' ];
301 $revLoadOptions =
'FOR UPDATE';
303 # Don't work with current revisions
304 # Don't lock the page table for update either -- TS 2006-04-04
305 # $tables[] = 'page';
306 # $conds[] = 'page_id=rev_page AND rev_id != page_latest';
308 $lbFactory = MediaWikiServices::getInstance()->getDBLoadBalancerFactory();
310 for ( $pageId = $startId; $pageId <= $maxPageId; $pageId++ ) {
311 $lbFactory->waitForReplication();
317 $pageRes =
$dbr->select(
'page',
318 [
'page_id',
'page_namespace',
'page_title',
'page_latest' ],
319 $pageConds + [
'page_id' => $pageId ], __METHOD__ );
320 if ( $pageRes->numRows() == 0 ) {
323 $pageRow =
$dbr->fetchObject( $pageRes );
326 $titleObj = Title::makeTitle( $pageRow->page_namespace, $pageRow->page_title );
327 $this->
output(
"$pageId\t" . $titleObj->getPrefixedDBkey() .
" " );
330 $revRes = $dbw->select( $tables, $fields,
332 'rev_page' => $pageRow->page_id,
333 # Don
't operate on the current revision
334 # Use < instead of <> in case the current revision has changed
335 # since the page select, which wasn't locking
336 'rev_id < ' . $pageRow->page_latest
342 foreach ( $revRes as $revRow ) {
346 if ( count( $revs ) < 2 ) {
347 # No revisions matching, no further processing
354 while ( $i < count( $revs ) ) {
355 if ( $i < count( $revs ) - $maxChunkSize ) {
356 $thisChunkSize = $maxChunkSize;
358 $thisChunkSize = count( $revs ) - $i;
365 $primaryOldid = $revs[$i]->old_id;
367 # Get the text of each revision and add it to the object
368 for ( $j = 0; $j < $thisChunkSize && $chunk->isHappy(); $j++ ) {
369 $oldid = $revs[$i + $j]->old_id;
371 # Get text. We do not need the full `extractBlob` since the query is built
372 # to fetch non-externalstore blobs.
373 $text = $blobStore->decompressData(
374 $revs[$i + $j]->old_text,
375 explode(
',', $revs[$i + $j]->old_flags )
378 if ( $text ===
false ) {
379 $this->
error(
"\nError, unable to get text in old_id $oldid" );
380 # $dbw->delete( 'old', [ 'old_id' => $oldid ] );
383 if ( $extdb ==
"" && $j == 0 ) {
384 $chunk->setText( $text );
387 # Don't make a stub if it's going to be longer than the article
388 # Stubs are typically about 100 bytes
389 if ( strlen( $text ) < 120 ) {
394 $stub->setLocation( $primaryOldid );
395 $stub->setReferrer( $oldid );
404 # If we couldn't actually use any stubs because the pages were too small, do nothing
406 if ( $extdb !=
"" ) {
407 # 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',
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',
450 'old_flags' =>
'object,utf-8',
452 'old_id' => $revs[$i + $j]->old_id
463 $i += $thisChunkSize;