3 * Compress the text of a wiki.
8 * php compressOld.php [options...]
11 * php compressOld.php <database> [options...]
14 * -t <type> set compression type to either:
15 * gzip: compress revisions independently
16 * concat: concatenate revisions and compress in chunks (default)
17 * -c <chunk-size> maximum number of revisions in a concat chunk
18 * -b <begin-date> earliest date to check for uncompressed revisions
19 * -e <end-date> latest revision date to compress
20 * -s <startid> the id to start from (referring to the text table for
21 * type gzip, and to the page table for type concat)
22 * -n <endid> the page_id to stop at (only when using concat compression type)
23 * --extdb <cluster> store specified revisions in an external cluster (untested)
25 * This program is free software; you can redistribute it and/or modify
26 * it under the terms of the GNU General Public License as published by
27 * the Free Software Foundation; either version 2 of the License, or
28 * (at your option) any later version.
30 * This program is distributed in the hope that it will be useful,
31 * but WITHOUT ANY WARRANTY; without even the implied warranty of
32 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
33 * GNU General Public License for more details.
35 * You should have received a copy of the GNU General Public License along
36 * with this program; if not, write to the Free Software Foundation, Inc.,
37 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
38 * http://www.gnu.org/copyleft/gpl.html
41 * @ingroup Maintenance ExternalStorage
44 require_once __DIR__
. '/../Maintenance.php';
47 * Maintenance script that compress the text of a wiki.
49 * @ingroup Maintenance ExternalStorage
51 class CompressOld
extends Maintenance
{
53 * Option to load each revision individually.
56 const LS_INDIVIDUAL
= 0;
59 * Option to load revisions in chunks.
64 public function __construct() {
65 parent
::__construct();
66 $this->mDescription
= 'Compress the text of a wiki';
67 $this->addOption( 'type', 'Set compression type to either: gzip|concat', false, true, 't' );
70 'Maximum number of revisions in a concat chunk',
77 'Earliest date to check for uncompressed revisions',
82 $this->addOption( 'end-date', 'Latest revision date to compress', false, true, 'e' );
85 'The id to start from (gzip -> text table, concat -> page table)',
92 'Store specified revisions in an external cluster (untested)',
98 'The page_id to stop at (only when using concat compression type)',
105 public function execute() {
107 if ( !function_exists( "gzdeflate" ) ) {
108 $this->error( "You must enable zlib support in PHP to compress old revisions!\n" .
109 "Please see http://www.php.net/manual/en/ref.zlib.php\n", true );
112 $type = $this->getOption( 'type', 'concat' );
113 $chunkSize = $this->getOption( 'chunksize', 20 );
114 $startId = $this->getOption( 'startid', 0 );
115 $beginDate = $this->getOption( 'begin-date', '' );
116 $endDate = $this->getOption( 'end-date', '' );
117 $extDB = $this->getOption( 'extdb', '' );
118 $endId = $this->getOption( 'endid', false );
120 if ( $type != 'concat' && $type != 'gzip' ) {
121 $this->error( "Type \"{$type}\" not supported" );
124 if ( $extDB != '' ) {
125 $this->output( "Compressing database {$wgDBname} to external cluster {$extDB}\n"
126 . str_repeat( '-', 76 ) . "\n\n" );
128 $this->output( "Compressing database {$wgDBname}\n"
129 . str_repeat( '-', 76 ) . "\n\n" );
133 if ( $type == 'concat' ) {
134 $success = $this->compressWithConcat( $startId, $chunkSize, $beginDate,
135 $endDate, $extDB, $endId );
137 $this->compressOldPages( $startId, $extDB );
141 $this->output( "Done.\n" );
146 * Fetch the text row-by-row to 'compressPage' function for compression.
149 * @param string $extdb
151 private function compressOldPages( $start = 0, $extdb = '' ) {
153 $this->output( "Starting from old_id $start...\n" );
154 $dbw = $this->getDB( DB_MASTER
);
158 array( 'old_id', 'old_flags', 'old_text' ),
161 array( 'ORDER BY' => 'old_id', 'LIMIT' => $chunksize, 'FOR UPDATE' )
164 if ( $res->numRows() == 0 ) {
170 foreach ( $res as $row ) {
171 # print " {$row->old_id} - {$row->old_namespace}:{$row->old_title}\n";
172 $this->compressPage( $row, $extdb );
173 $last = $row->old_id
;
176 $start = $last +
1; # Deletion may leave long empty stretches
177 $this->output( "$start...\n" );
182 * Compress the text in gzip format.
184 * @param stdClass $row
185 * @param string $extdb
188 private function compressPage( $row, $extdb ) {
189 if ( false !== strpos( $row->old_flags
, 'gzip' )
190 ||
false !== strpos( $row->old_flags
, 'object' )
192 # print "Already compressed row {$row->old_id}\n";
195 $dbw = $this->getDB( DB_MASTER
);
196 $flags = $row->old_flags ?
"{$row->old_flags},gzip" : "gzip";
197 $compress = gzdeflate( $row->old_text
);
199 # Store in external storage if required
200 if ( $extdb !== '' ) {
201 $storeObj = new ExternalStoreDB
;
202 $compress = $storeObj->store( $extdb, $compress );
203 if ( $compress === false ) {
204 $this->error( "Unable to store object" );
211 $dbw->update( 'text',
213 'old_flags' => $flags,
214 'old_text' => $compress
215 ), array( /* WHERE */
216 'old_id' => $row->old_id
218 array( 'LIMIT' => 1 )
225 * Compress the text in chunks after concatenating the revisions.
227 * @param int $startId
228 * @param int $maxChunkSize
229 * @param string $beginDate
230 * @param string $endDate
231 * @param string $extdb
232 * @param bool|int $maxPageId
235 private function compressWithConcat( $startId, $maxChunkSize, $beginDate,
236 $endDate, $extdb = "", $maxPageId = false
238 $loadStyle = self
::LS_CHUNKED
;
240 $dbr = $this->getDB( DB_SLAVE
);
241 $dbw = $this->getDB( DB_MASTER
);
243 # Set up external storage
244 if ( $extdb != '' ) {
245 $storeObj = new ExternalStoreDB
;
248 # Get all articles by page_id
250 $maxPageId = $dbr->selectField( 'page', 'max(page_id)', '', __METHOD__
);
252 $this->output( "Starting from $startId of $maxPageId\n" );
253 $pageConds = array();
256 if ( $exclude_ns0 ) {
257 print "Excluding main namespace\n";
258 $pageConds[] = 'page_namespace<>0';
261 $pageConds[] = $queryExtra;
265 # For each article, get a list of revisions which fit the criteria
267 # No recompression, use a condition on old_flags
268 # Don't compress object type entities, because that might produce data loss when
269 # overwriting bulk storage concat rows. Don't compress external references, because
270 # the script doesn't yet delete rows from external storage.
272 'old_flags NOT ' . $dbr->buildLike( $dbr->anyString(), 'object', $dbr->anyString() )
273 . ' AND old_flags NOT '
274 . $dbr->buildLike( $dbr->anyString(), 'external', $dbr->anyString() )
278 if ( !preg_match( '/^\d{14}$/', $beginDate ) ) {
279 $this->error( "Invalid begin date \"$beginDate\"\n" );
283 $conds[] = "rev_timestamp>'" . $beginDate . "'";
286 if ( !preg_match( '/^\d{14}$/', $endDate ) ) {
287 $this->error( "Invalid end date \"$endDate\"\n" );
291 $conds[] = "rev_timestamp<'" . $endDate . "'";
293 if ( $loadStyle == self
::LS_CHUNKED
) {
294 $tables = array( 'revision', 'text' );
295 $fields = array( 'rev_id', 'rev_text_id', 'old_flags', 'old_text' );
296 $conds[] = 'rev_text_id=old_id';
297 $revLoadOptions = 'FOR UPDATE';
299 $tables = array( 'revision' );
300 $fields = array( 'rev_id', 'rev_text_id' );
301 $revLoadOptions = array();
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 array( 'page_id', 'page_namespace', 'page_title', 'page_latest' ),
318 $pageConds +
array( 'page_id' => $pageId ), __METHOD__
);
319 if ( $pageRes->numRows() == 0 ) {
322 $pageRow = $dbr->fetchObject( $pageRes );
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 < ' . $pageRow->page_latest
341 foreach ( $revRes as $revRow ) {
345 if ( count( $revs ) < 2 ) {
346 # No revisions matching, no further processing
347 $this->output( "\n" );
353 while ( $i < count( $revs ) ) {
354 if ( $i < count( $revs ) - $maxChunkSize ) {
355 $thisChunkSize = $maxChunkSize;
357 $thisChunkSize = count( $revs ) - $i;
360 $chunk = new ConcatenatedGzipHistoryBlob();
362 $this->beginTransaction( $dbw, __METHOD__
);
364 $primaryOldid = $revs[$i]->rev_text_id
;
366 // @codingStandardsIgnoreStart Ignore avoid function calls in a FOR loop test part warning
367 # Get the text of each revision and add it to the object
368 for ( $j = 0; $j < $thisChunkSize && $chunk->isHappy(); $j++
) {
369 // @codingStandardsIgnoreEnd
370 $oldid = $revs[$i +
$j]->rev_text_id
;
373 if ( $loadStyle == self
::LS_INDIVIDUAL
) {
374 $textRow = $dbw->selectRow( 'text',
375 array( 'old_flags', 'old_text' ),
376 array( 'old_id' => $oldid ),
380 $text = Revision
::getRevisionText( $textRow );
382 $text = Revision
::getRevisionText( $revs[$i +
$j] );
385 if ( $text === false ) {
386 $this->error( "\nError, unable to get text in old_id $oldid" );
387 # $dbw->delete( 'old', array( 'old_id' => $oldid ) );
390 if ( $extdb == "" && $j == 0 ) {
391 $chunk->setText( $text );
392 $this->output( '.' );
394 # Don't make a stub if it's going to be longer than the article
395 # Stubs are typically about 100 bytes
396 if ( strlen( $text ) < 120 ) {
398 $this->output( 'x' );
400 $stub = new HistoryBlobStub( $chunk->addItem( $text ) );
401 $stub->setLocation( $primaryOldid );
402 $stub->setReferrer( $oldid );
403 $this->output( '.' );
411 # If we couldn't actually use any stubs because the pages were too small, do nothing
413 if ( $extdb != "" ) {
414 # Move blob objects to External Storage
415 $stored = $storeObj->store( $extdb, serialize( $chunk ) );
416 if ( $stored === false ) {
417 $this->error( "Unable to store object" );
421 # Store External Storage URLs instead of Stub placeholders
422 foreach ( $stubs as $stub ) {
423 if ( $stub === false ) {
426 # $stored should provide base path to a BLOB
427 $url = $stored . "/" . $stub->getHash();
428 $dbw->update( 'text',
431 'old_flags' => 'external,utf-8',
432 ), array( /* WHERE */
433 'old_id' => $stub->getReferrer(),
438 # Store the main object locally
439 $dbw->update( 'text',
441 'old_text' => serialize( $chunk ),
442 'old_flags' => 'object,utf-8',
443 ), array( /* WHERE */
444 'old_id' => $primaryOldid
448 # Store the stub objects
449 for ( $j = 1; $j < $thisChunkSize; $j++
) {
450 # Skip if not compressing and don't overwrite the first revision
451 if ( $stubs[$j] !== false && $revs[$i +
$j]->rev_text_id
!= $primaryOldid ) {
452 $dbw->update( 'text',
454 'old_text' => serialize( $stubs[$j] ),
455 'old_flags' => 'object,utf-8',
456 ), array( /* WHERE */
457 'old_id' => $revs[$i +
$j]->rev_text_id
465 $this->output( "/" );
466 $this->commitTransaction( $dbw, __METHOD__
);
467 $i +
= $thisChunkSize;
470 $this->output( "\n" );
477 $maintClass = 'CompressOld';
478 require_once RUN_MAINTENANCE_IF_MAIN
;