3 * @addtogroup Maintenance
7 require_once( 'Revision.php' );
8 require_once( 'ExternalStoreDB.php' );
11 function compressOldPages( $start = 0, $extdb = '' ) {
12 $fname = 'compressOldPages';
15 print "Starting from old_id $start...\n";
16 $dbw = wfGetDB( DB_MASTER );
18 $res = $dbw->select( 'text', array( 'old_id','old_flags','old_namespace','old_title','old_text' ),
19 "old_id>=$start", $fname, array( 'ORDER BY' => 'old_id', 'LIMIT' => $chunksize, 'FOR UPDATE' ) );
20 if( $dbw->numRows( $res ) == 0 ) {
24 while( $row = $dbw->fetchObject( $res ) ) {
25 # print " {$row->old_id} - {$row->old_namespace}:{$row->old_title}\n";
26 compressPage( $row, $extdb );
29 $dbw->freeResult( $res );
30 $start = $last + 1; # Deletion may leave long empty stretches
36 function compressPage( $row, $extdb ) {
37 $fname = 'compressPage';
38 if ( false !== strpos( $row->old_flags, 'gzip' ) || false !== strpos( $row->old_flags, 'object' ) ) {
39 #print "Already compressed row {$row->old_id}\n";
42 $dbw = wfGetDB( DB_MASTER );
43 $flags = $row->old_flags ? "{$row->old_flags},gzip" : "gzip";
44 $compress = gzdeflate( $row->old_text );
46 # Store in external storage if required
47 if ( $extdb !== '' ) {
48 $storeObj = new ExternalStoreDB;
49 $compress = $storeObj->store( $extdb, $compress );
50 if ( $compress === false ) {
51 print "Unable to store object\n";
59 'old_flags' => $flags,
60 'old_text' => $compress
62 'old_id' => $row->old_id
68 define( 'LS_INDIVIDUAL', 0 );
69 define( 'LS_CHUNKED', 1 );
72 function compressWithConcat( $startId, $maxChunkSize, $maxChunkFactor, $factorThreshold, $beginDate,
73 $endDate, $extdb="", $maxPageId = false )
75 $fname = 'compressWithConcat';
76 $loadStyle = LS_CHUNKED;
78 $dbr = wfGetDB( DB_SLAVE );
79 $dbw = wfGetDB( DB_MASTER );
81 # Set up external storage
83 $storeObj = new ExternalStoreDB;
86 # Get all articles by page_id
88 $maxPageId = $dbr->selectField( 'page', 'max(page_id)', '', $fname );
90 print "Starting from $startId of $maxPageId\n";
95 print "Excluding main namespace\n";
96 $pageConds[] = 'page_namespace<>0';
99 $pageConds[] = $queryExtra;
103 # For each article, get a list of revisions which fit the criteria
105 # No recompression, use a condition on old_flags
106 # Don't compress object type entities, because that might produce data loss when
107 # overwriting bulk storage concat rows. Don't compress external references, because
108 # the script doesn't yet delete rows from external storage.
110 "old_flags NOT LIKE '%object%' AND old_flags NOT LIKE '%external%'");
113 if ( !preg_match( '/^\d{14}$/', $beginDate ) ) {
114 print "Invalid begin date \"$beginDate\"\n";
117 $conds[] = "rev_timestamp>'" . $beginDate . "'";
120 if ( !preg_match( '/^\d{14}$/', $endDate ) ) {
121 print "Invalid end date \"$endDate\"\n";
124 $conds[] = "rev_timestamp<'" . $endDate . "'";
126 if ( $loadStyle == LS_CHUNKED ) {
127 $tables = array( 'revision', 'text' );
128 $fields = array( 'rev_id', 'rev_text_id', 'old_flags', 'old_text' );
129 $conds[] = 'rev_text_id=old_id';
130 $revLoadOptions = 'FOR UPDATE';
132 $tables = array( 'revision' );
133 $fields = array( 'rev_id', 'rev_text_id' );
134 $revLoadOptions = array();
137 # Don't work with current revisions
138 # Don't lock the page table for update either -- TS 2006-04-04
140 #$conds[] = 'page_id=rev_page AND rev_id != page_latest';
142 for ( $pageId = $startId; $pageId <= $maxPageId; $pageId++ ) {
143 wfWaitForSlaves( 5 );
149 $pageRes = $dbr->select( 'page',
150 array('page_id', 'page_namespace', 'page_title','page_latest'),
151 $pageConds + array('page_id' => $pageId), $fname );
152 if ( $dbr->numRows( $pageRes ) == 0 ) {
155 $pageRow = $dbr->fetchObject( $pageRes );
158 $titleObj = Title::makeTitle( $pageRow->page_namespace, $pageRow->page_title );
159 print "$pageId\t" . $titleObj->getPrefixedDBkey() . " ";
162 $revRes = $dbw->select( $tables, $fields,
164 'rev_page' => $pageRow->page_id,
165 # Don't operate on the current revision
166 # Use < instead of <> in case the current revision has changed
167 # since the page select, which wasn't locking
168 'rev_id < ' . $pageRow->page_latest
174 while ( $revRow = $dbw->fetchObject( $revRes ) ) {
178 if ( count( $revs ) < 2) {
179 # No revisions matching, no further processing
186 while ( $i < count( $revs ) ) {
187 if ( $i < count( $revs ) - $maxChunkSize ) {
188 $thisChunkSize = $maxChunkSize;
190 $thisChunkSize = count( $revs ) - $i;
193 $chunk = new ConcatenatedGzipHistoryBlob();
197 $primaryOldid = $revs[$i]->rev_text_id;
199 # Get the text of each revision and add it to the object
200 for ( $j = 0; $j < $thisChunkSize && $chunk->isHappy( $maxChunkFactor, $factorThreshold ); $j++ ) {
201 $oldid = $revs[$i + $j]->rev_text_id;
204 if ( $loadStyle == LS_INDIVIDUAL ) {
205 $textRow = $dbw->selectRow( 'text',
206 array( 'old_flags', 'old_text' ),
207 array( 'old_id' => $oldid ),
211 $text = Revision::getRevisionText( $textRow );
213 $text = Revision::getRevisionText( $revs[$i + $j] );
216 if ( $text === false ) {
217 print "\nError, unable to get text in old_id $oldid\n";
218 #$dbw->delete( 'old', array( 'old_id' => $oldid ) );
221 if ( $extdb == "" && $j == 0 ) {
222 $chunk->setText( $text );
225 # Don't make a stub if it's going to be longer than the article
226 # Stubs are typically about 100 bytes
227 if ( strlen( $text ) < 120 ) {
231 $stub = $chunk->addItem( $text );
232 $stub->setLocation( $primaryOldid );
233 $stub->setReferrer( $oldid );
242 # If we couldn't actually use any stubs because the pages were too small, do nothing
244 if ( $extdb != "" ) {
245 # Move blob objects to External Storage
246 $stored = $storeObj->store( $extdb, serialize( $chunk ));
247 if ($stored === false) {
248 print "Unable to store object\n";
251 # Store External Storage URLs instead of Stub placeholders
252 foreach ($stubs as $stub) {
255 # $stored should provide base path to a BLOB
256 $url = $stored."/".$stub->getHash();
257 $dbw->update( 'text',
260 'old_flags' => 'external,utf-8',
261 ), array ( /* WHERE */
262 'old_id' => $stub->getReferrer(),
267 # Store the main object locally
268 $dbw->update( 'text',
270 'old_text' => serialize( $chunk ),
271 'old_flags' => 'object,utf-8',
272 ), array( /* WHERE */
273 'old_id' => $primaryOldid
277 # Store the stub objects
278 for ( $j = 1; $j < $thisChunkSize; $j++ ) {
279 # Skip if not compressing and don't overwrite the first revision
280 if ( $stubs[$j] !== false && $revs[$i + $j]->rev_text_id != $primaryOldid ) {
281 $dbw->update( 'text',
283 'old_text' => serialize($stubs[$j]),
284 'old_flags' => 'object,utf-8',
285 ), array( /* WHERE */
286 'old_id' => $revs[$i + $j]->rev_text_id
296 $i += $thisChunkSize;
297 wfWaitForSlaves( 5 );