4 * @ingroup Maintenance ExternalStorage
8 require_once( 'Revision.php' );
9 require_once( 'ExternalStoreDB.php' );
12 function compressOldPages( $start = 0, $extdb = '' ) {
13 $fname = 'compressOldPages';
16 print "Starting from old_id $start...\n";
17 $dbw = wfGetDB( DB_MASTER );
19 $res = $dbw->select( 'text', array( 'old_id','old_flags','old_text' ),
20 "old_id>=$start", $fname, array( 'ORDER BY' => 'old_id', 'LIMIT' => $chunksize, 'FOR UPDATE' ) );
21 if( $dbw->numRows( $res ) == 0 ) {
25 while( $row = $dbw->fetchObject( $res ) ) {
26 # print " {$row->old_id} - {$row->old_namespace}:{$row->old_title}\n";
27 compressPage( $row, $extdb );
30 $dbw->freeResult( $res );
31 $start = $last + 1; # Deletion may leave long empty stretches
37 function compressPage( $row, $extdb ) {
38 $fname = 'compressPage';
39 if ( false !== strpos( $row->old_flags, 'gzip' ) || false !== strpos( $row->old_flags, 'object' ) ) {
40 #print "Already compressed row {$row->old_id}\n";
43 $dbw = wfGetDB( DB_MASTER );
44 $flags = $row->old_flags ? "{$row->old_flags},gzip" : "gzip";
45 $compress = gzdeflate( $row->old_text );
47 # Store in external storage if required
48 if ( $extdb !== '' ) {
49 $storeObj = new ExternalStoreDB;
50 $compress = $storeObj->store( $extdb, $compress );
51 if ( $compress === false ) {
52 print "Unable to store object\n";
60 'old_flags' => $flags,
61 'old_text' => $compress
63 'old_id' => $row->old_id
69 define( 'LS_INDIVIDUAL', 0 );
70 define( 'LS_CHUNKED', 1 );
73 function compressWithConcat( $startId, $maxChunkSize, $maxChunkFactor, $factorThreshold, $beginDate,
74 $endDate, $extdb="", $maxPageId = false )
76 $fname = 'compressWithConcat';
77 $loadStyle = LS_CHUNKED;
79 $dbr = wfGetDB( DB_SLAVE );
80 $dbw = wfGetDB( DB_MASTER );
82 # Set up external storage
84 $storeObj = new ExternalStoreDB;
87 # Get all articles by page_id
89 $maxPageId = $dbr->selectField( 'page', 'max(page_id)', '', $fname );
91 print "Starting from $startId of $maxPageId\n";
96 print "Excluding main namespace\n";
97 $pageConds[] = 'page_namespace<>0';
100 $pageConds[] = $queryExtra;
104 # For each article, get a list of revisions which fit the criteria
106 # No recompression, use a condition on old_flags
107 # Don't compress object type entities, because that might produce data loss when
108 # overwriting bulk storage concat rows. Don't compress external references, because
109 # the script doesn't yet delete rows from external storage.
111 "old_flags NOT LIKE '%object%' AND old_flags NOT LIKE '%external%'");
114 if ( !preg_match( '/^\d{14}$/', $beginDate ) ) {
115 print "Invalid begin date \"$beginDate\"\n";
118 $conds[] = "rev_timestamp>'" . $beginDate . "'";
121 if ( !preg_match( '/^\d{14}$/', $endDate ) ) {
122 print "Invalid end date \"$endDate\"\n";
125 $conds[] = "rev_timestamp<'" . $endDate . "'";
127 if ( $loadStyle == LS_CHUNKED ) {
128 $tables = array( 'revision', 'text' );
129 $fields = array( 'rev_id', 'rev_text_id', 'old_flags', 'old_text' );
130 $conds[] = 'rev_text_id=old_id';
131 $revLoadOptions = 'FOR UPDATE';
133 $tables = array( 'revision' );
134 $fields = array( 'rev_id', 'rev_text_id' );
135 $revLoadOptions = array();
138 # Don't work with current revisions
139 # Don't lock the page table for update either -- TS 2006-04-04
141 #$conds[] = 'page_id=rev_page AND rev_id != page_latest';
143 for ( $pageId = $startId; $pageId <= $maxPageId; $pageId++ ) {
144 wfWaitForSlaves( 5 );
150 $pageRes = $dbr->select( 'page',
151 array('page_id', 'page_namespace', 'page_title','page_latest'),
152 $pageConds + array('page_id' => $pageId), $fname );
153 if ( $dbr->numRows( $pageRes ) == 0 ) {
156 $pageRow = $dbr->fetchObject( $pageRes );
159 $titleObj = Title::makeTitle( $pageRow->page_namespace, $pageRow->page_title );
160 print "$pageId\t" . $titleObj->getPrefixedDBkey() . " ";
163 $revRes = $dbw->select( $tables, $fields,
165 'rev_page' => $pageRow->page_id,
166 # Don't operate on the current revision
167 # Use < instead of <> in case the current revision has changed
168 # since the page select, which wasn't locking
169 'rev_id < ' . $pageRow->page_latest
175 while ( $revRow = $dbw->fetchObject( $revRes ) ) {
179 if ( count( $revs ) < 2) {
180 # No revisions matching, no further processing
187 while ( $i < count( $revs ) ) {
188 if ( $i < count( $revs ) - $maxChunkSize ) {
189 $thisChunkSize = $maxChunkSize;
191 $thisChunkSize = count( $revs ) - $i;
194 $chunk = new ConcatenatedGzipHistoryBlob();
198 $primaryOldid = $revs[$i]->rev_text_id;
200 # Get the text of each revision and add it to the object
201 for ( $j = 0; $j < $thisChunkSize && $chunk->isHappy( $maxChunkFactor, $factorThreshold ); $j++ ) {
202 $oldid = $revs[$i + $j]->rev_text_id;
205 if ( $loadStyle == LS_INDIVIDUAL ) {
206 $textRow = $dbw->selectRow( 'text',
207 array( 'old_flags', 'old_text' ),
208 array( 'old_id' => $oldid ),
212 $text = Revision::getRevisionText( $textRow );
214 $text = Revision::getRevisionText( $revs[$i + $j] );
217 if ( $text === false ) {
218 print "\nError, unable to get text in old_id $oldid\n";
219 #$dbw->delete( 'old', array( 'old_id' => $oldid ) );
222 if ( $extdb == "" && $j == 0 ) {
223 $chunk->setText( $text );
226 # Don't make a stub if it's going to be longer than the article
227 # Stubs are typically about 100 bytes
228 if ( strlen( $text ) < 120 ) {
232 $stub = $chunk->addItem( $text );
233 $stub->setLocation( $primaryOldid );
234 $stub->setReferrer( $oldid );
243 # If we couldn't actually use any stubs because the pages were too small, do nothing
245 if ( $extdb != "" ) {
246 # Move blob objects to External Storage
247 $stored = $storeObj->store( $extdb, serialize( $chunk ));
248 if ($stored === false) {
249 print "Unable to store object\n";
252 # Store External Storage URLs instead of Stub placeholders
253 foreach ($stubs as $stub) {
256 # $stored should provide base path to a BLOB
257 $url = $stored."/".$stub->getHash();
258 $dbw->update( 'text',
261 'old_flags' => 'external,utf-8',
262 ), array ( /* WHERE */
263 'old_id' => $stub->getReferrer(),
268 # Store the main object locally
269 $dbw->update( 'text',
271 'old_text' => serialize( $chunk ),
272 'old_flags' => 'object,utf-8',
273 ), array( /* WHERE */
274 'old_id' => $primaryOldid
278 # Store the stub objects
279 for ( $j = 1; $j < $thisChunkSize; $j++ ) {
280 # Skip if not compressing and don't overwrite the first revision
281 if ( $stubs[$j] !== false && $revs[$i + $j]->rev_text_id != $primaryOldid ) {
282 $dbw->update( 'text',
284 'old_text' => serialize($stubs[$j]),
285 'old_flags' => 'object,utf-8',
286 ), array( /* WHERE */
287 'old_id' => $revs[$i + $j]->rev_text_id
297 $i += $thisChunkSize;
298 wfWaitForSlaves( 5 );