Followup r90020: use startid for consistency, and document endid
[lhc/web/wiklou.git] / maintenance / storage / compressOld.php
1 <?php
2 /**
3 * Compress the text of a wiki.
4 *
5 * Usage:
6 *
7 * Non-wikimedia
8 * php compressOld.php [options...]
9 *
10 * Wikimedia
11 * php compressOld.php <database> [options...]
12 *
13 * Options are:
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 old_id to start from
21 * -n <endid> the old_id to stop at
22 * --extdb <cluster> store specified revisions in an external cluster (untested)
23 *
24 * This program is free software; you can redistribute it and/or modify
25 * it under the terms of the GNU General Public License as published by
26 * the Free Software Foundation; either version 2 of the License, or
27 * (at your option) any later version.
28 *
29 * This program is distributed in the hope that it will be useful,
30 * but WITHOUT ANY WARRANTY; without even the implied warranty of
31 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
32 * GNU General Public License for more details.
33 *
34 * You should have received a copy of the GNU General Public License along
35 * with this program; if not, write to the Free Software Foundation, Inc.,
36 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
37 * http://www.gnu.org/copyleft/gpl.html
38 *
39 * @file
40 * @ingroup Maintenance ExternalStorage
41 */
42
43 require_once( dirname( __FILE__ ) . '/../Maintenance.php' );
44
45 class CompressOld extends Maintenance {
46 /**
47 * @todo document
48 */
49 const LS_INDIVIDUAL = 0;
50 const LS_CHUNKED = 1;
51
52 public function __construct() {
53 parent::__construct();
54 $this->mDescription = 'Compress the text of a wiki';
55 $this->addOption( 'type', 'Set compression type to either: gzip|concat', false, true, 't' );
56 $this->addOption( 'chunksize', 'Maximum number of revisions in a concat chunk', false, true, 'c' );
57 $this->addOption( 'begin-date', 'Earliest date to check for uncompressed revisions', false, true, 'b' );
58 $this->addOption( 'end-date', 'Latest revision date to compress', false, true, 'e' );
59 $this->addOption( 'startid', 'The old_id to start from', false, true, 's' );
60 $this->addOption( 'extdb', 'Store specified revisions in an external cluster (untested)', false, true );
61 $this->addOption( 'endid', 'Stop at this old_id', false, true, 'n' );
62 }
63
64 public function execute() {
65 global $wgDBname;
66 if ( !function_exists( "gzdeflate" ) ) {
67 $this->error( "You must enable zlib support in PHP to compress old revisions!\n" .
68 "Please see http://www.php.net/manual/en/ref.zlib.php\n", true );
69 }
70
71 $type = $this->getOption( 'type', 'concat' );
72 $chunkSize = $this->getOption( 'chunksize', 20 );
73 $startId = $this->getOption( 'start-id', 0 );
74 $beginDate = $this->getOption( 'begin-date', '' );
75 $endDate = $this->getOption( 'end-date', '' );
76 $extDB = $this->getOption( 'extdb', '' );
77 $endId = $this->getOption( 'endid', false );
78
79 if ( $type != 'concat' && $type != 'gzip' ) {
80 $this->error( "Type \"{$type}\" not supported" );
81 }
82
83 if ( $extDB != '' ) {
84 $this->output( "Compressing database {$wgDBname} to external cluster {$extDB}\n"
85 . str_repeat( '-', 76 ) . "\n\n" );
86 } else {
87 $this->output( "Compressing database {$wgDBname}\n"
88 . str_repeat( '-', 76 ) . "\n\n" );
89 }
90
91 $success = true;
92 if ( $type == 'concat' ) {
93 $success = $this->compressWithConcat( $startId, $chunkSize, $beginDate,
94 $endDate, $extDB, $endId );
95 } else {
96 $this->compressOldPages( $startId, $extDB );
97 }
98
99 if ( $success ) {
100 $this->output( "Done.\n" );
101 }
102 }
103
104 /** @todo document */
105 private function compressOldPages( $start = 0, $extdb = '' ) {
106 $chunksize = 50;
107 $this->output( "Starting from old_id $start...\n" );
108 $dbw = wfGetDB( DB_MASTER );
109 do {
110 $res = $dbw->select( 'text', array( 'old_id','old_flags','old_text' ),
111 "old_id>=$start", __METHOD__, array( 'ORDER BY' => 'old_id', 'LIMIT' => $chunksize, 'FOR UPDATE' ) );
112 if( $dbw->numRows( $res ) == 0 ) {
113 break;
114 }
115 $last = $start;
116 foreach ( $res as $row ) {
117 # print " {$row->old_id} - {$row->old_namespace}:{$row->old_title}\n";
118 $this->compressPage( $row, $extdb );
119 $last = $row->old_id;
120 }
121 $start = $last + 1; # Deletion may leave long empty stretches
122 $this->output( "$start...\n" );
123 } while( true );
124 }
125
126 /** @todo document */
127 private function compressPage( $row, $extdb ) {
128 if ( false !== strpos( $row->old_flags, 'gzip' ) || false !== strpos( $row->old_flags, 'object' ) ) {
129 #print "Already compressed row {$row->old_id}\n";
130 return false;
131 }
132 $dbw = wfGetDB( DB_MASTER );
133 $flags = $row->old_flags ? "{$row->old_flags},gzip" : "gzip";
134 $compress = gzdeflate( $row->old_text );
135
136 # Store in external storage if required
137 if ( $extdb !== '' ) {
138 $storeObj = new ExternalStoreDB;
139 $compress = $storeObj->store( $extdb, $compress );
140 if ( $compress === false ) {
141 $this->error( "Unable to store object" );
142 return false;
143 }
144 }
145
146 # Update text row
147 $dbw->update( 'text',
148 array( /* SET */
149 'old_flags' => $flags,
150 'old_text' => $compress
151 ), array( /* WHERE */
152 'old_id' => $row->old_id
153 ), __METHOD__,
154 array( 'LIMIT' => 1 )
155 );
156 return true;
157 }
158
159 /** @todo document */
160 private function compressWithConcat( $startId, $maxChunkSize, $beginDate,
161 $endDate, $extdb = "", $maxPageId = false )
162 {
163 $loadStyle = self::LS_CHUNKED;
164
165 $dbr = wfGetDB( DB_SLAVE );
166 $dbw = wfGetDB( DB_MASTER );
167
168 # Set up external storage
169 if ( $extdb != '' ) {
170 $storeObj = new ExternalStoreDB;
171 }
172
173 # Get all articles by page_id
174 if ( !$maxPageId ) {
175 $maxPageId = $dbr->selectField( 'page', 'max(page_id)', '', __METHOD__ );
176 }
177 $this->output( "Starting from $startId of $maxPageId\n" );
178 $pageConds = array();
179
180 /*
181 if ( $exclude_ns0 ) {
182 print "Excluding main namespace\n";
183 $pageConds[] = 'page_namespace<>0';
184 }
185 if ( $queryExtra ) {
186 $pageConds[] = $queryExtra;
187 }
188 */
189
190 # For each article, get a list of revisions which fit the criteria
191
192 # No recompression, use a condition on old_flags
193 # Don't compress object type entities, because that might produce data loss when
194 # overwriting bulk storage concat rows. Don't compress external references, because
195 # the script doesn't yet delete rows from external storage.
196 $conds = array(
197 'old_flags NOT ' . $dbr->buildLike( $dbr->anyString(), 'object', $dbr->anyString() ) . ' AND old_flags NOT '
198 . $dbr->buildLike( $dbr->anyString(), 'external', $dbr->anyString() ) );
199
200 if ( $beginDate ) {
201 if ( !preg_match( '/^\d{14}$/', $beginDate ) ) {
202 $this->error( "Invalid begin date \"$beginDate\"\n" );
203 return false;
204 }
205 $conds[] = "rev_timestamp>'" . $beginDate . "'";
206 }
207 if ( $endDate ) {
208 if ( !preg_match( '/^\d{14}$/', $endDate ) ) {
209 $this->error( "Invalid end date \"$endDate\"\n" );
210 return false;
211 }
212 $conds[] = "rev_timestamp<'" . $endDate . "'";
213 }
214 if ( $loadStyle == self::LS_CHUNKED ) {
215 $tables = array( 'revision', 'text' );
216 $fields = array( 'rev_id', 'rev_text_id', 'old_flags', 'old_text' );
217 $conds[] = 'rev_text_id=old_id';
218 $revLoadOptions = 'FOR UPDATE';
219 } else {
220 $tables = array( 'revision' );
221 $fields = array( 'rev_id', 'rev_text_id' );
222 $revLoadOptions = array();
223 }
224
225 # Don't work with current revisions
226 # Don't lock the page table for update either -- TS 2006-04-04
227 #$tables[] = 'page';
228 #$conds[] = 'page_id=rev_page AND rev_id != page_latest';
229
230 for ( $pageId = $startId; $pageId <= $maxPageId; $pageId++ ) {
231 wfWaitForSlaves();
232
233 # Wake up
234 $dbr->ping();
235
236 # Get the page row
237 $pageRes = $dbr->select( 'page',
238 array('page_id', 'page_namespace', 'page_title','page_latest'),
239 $pageConds + array('page_id' => $pageId), __METHOD__ );
240 if ( $dbr->numRows( $pageRes ) == 0 ) {
241 continue;
242 }
243 $pageRow = $dbr->fetchObject( $pageRes );
244
245 # Display progress
246 $titleObj = Title::makeTitle( $pageRow->page_namespace, $pageRow->page_title );
247 $this->output( "$pageId\t" . $titleObj->getPrefixedDBkey() . " " );
248
249 # Load revisions
250 $revRes = $dbw->select( $tables, $fields,
251 array_merge( array(
252 'rev_page' => $pageRow->page_id,
253 # Don't operate on the current revision
254 # Use < instead of <> in case the current revision has changed
255 # since the page select, which wasn't locking
256 'rev_id < ' . $pageRow->page_latest
257 ), $conds ),
258 __METHOD__,
259 $revLoadOptions
260 );
261 $revs = array();
262 foreach ( $revRes as $revRow ) {
263 $revs[] = $revRow;
264 }
265
266 if ( count( $revs ) < 2) {
267 # No revisions matching, no further processing
268 $this->output( "\n" );
269 continue;
270 }
271
272 # For each chunk
273 $i = 0;
274 while ( $i < count( $revs ) ) {
275 if ( $i < count( $revs ) - $maxChunkSize ) {
276 $thisChunkSize = $maxChunkSize;
277 } else {
278 $thisChunkSize = count( $revs ) - $i;
279 }
280
281 $chunk = new ConcatenatedGzipHistoryBlob();
282 $stubs = array();
283 $dbw->begin();
284 $usedChunk = false;
285 $primaryOldid = $revs[$i]->rev_text_id;
286
287 # Get the text of each revision and add it to the object
288 for ( $j = 0; $j < $thisChunkSize && $chunk->isHappy(); $j++ ) {
289 $oldid = $revs[$i + $j]->rev_text_id;
290
291 # Get text
292 if ( $loadStyle == self::LS_INDIVIDUAL ) {
293 $textRow = $dbw->selectRow( 'text',
294 array( 'old_flags', 'old_text' ),
295 array( 'old_id' => $oldid ),
296 __METHOD__,
297 'FOR UPDATE'
298 );
299 $text = Revision::getRevisionText( $textRow );
300 } else {
301 $text = Revision::getRevisionText( $revs[$i + $j] );
302 }
303
304 if ( $text === false ) {
305 $this->error( "\nError, unable to get text in old_id $oldid" );
306 #$dbw->delete( 'old', array( 'old_id' => $oldid ) );
307 }
308
309 if ( $extdb == "" && $j == 0 ) {
310 $chunk->setText( $text );
311 $this->output( '.' );
312 } else {
313 # Don't make a stub if it's going to be longer than the article
314 # Stubs are typically about 100 bytes
315 if ( strlen( $text ) < 120 ) {
316 $stub = false;
317 $this->output( 'x' );
318 } else {
319 $stub = new HistoryBlobStub( $chunk->addItem( $text ) );
320 $stub->setLocation( $primaryOldid );
321 $stub->setReferrer( $oldid );
322 $this->output( '.' );
323 $usedChunk = true;
324 }
325 $stubs[$j] = $stub;
326 }
327 }
328 $thisChunkSize = $j;
329
330 # If we couldn't actually use any stubs because the pages were too small, do nothing
331 if ( $usedChunk ) {
332 if ( $extdb != "" ) {
333 # Move blob objects to External Storage
334 $stored = $storeObj->store( $extdb, serialize( $chunk ));
335 if ($stored === false) {
336 $this->error( "Unable to store object" );
337 return false;
338 }
339 # Store External Storage URLs instead of Stub placeholders
340 foreach ($stubs as $stub) {
341 if ($stub===false)
342 continue;
343 # $stored should provide base path to a BLOB
344 $url = $stored."/".$stub->getHash();
345 $dbw->update( 'text',
346 array( /* SET */
347 'old_text' => $url,
348 'old_flags' => 'external,utf-8',
349 ), array ( /* WHERE */
350 'old_id' => $stub->getReferrer(),
351 )
352 );
353 }
354 } else {
355 # Store the main object locally
356 $dbw->update( 'text',
357 array( /* SET */
358 'old_text' => serialize( $chunk ),
359 'old_flags' => 'object,utf-8',
360 ), array( /* WHERE */
361 'old_id' => $primaryOldid
362 )
363 );
364
365 # Store the stub objects
366 for ( $j = 1; $j < $thisChunkSize; $j++ ) {
367 # Skip if not compressing and don't overwrite the first revision
368 if ( $stubs[$j] !== false && $revs[$i + $j]->rev_text_id != $primaryOldid ) {
369 $dbw->update( 'text',
370 array( /* SET */
371 'old_text' => serialize($stubs[$j]),
372 'old_flags' => 'object,utf-8',
373 ), array( /* WHERE */
374 'old_id' => $revs[$i + $j]->rev_text_id
375 )
376 );
377 }
378 }
379 }
380 }
381 # Done, next
382 $this->output( "/" );
383 $dbw->commit();
384 $i += $thisChunkSize;
385 wfWaitForSlaves();
386 }
387 $this->output( "\n" );
388 }
389 return true;
390 }
391
392 }
393
394 $maintClass = 'CompressOld';
395 require_once( RUN_MAINTENANCE_IF_MAIN );