4 * @ingroup Maintenance ExternalStorage
8 function compressOldPages( $start = 0, $extdb = '' ) {
9 $fname = 'compressOldPages';
12 print "Starting from old_id $start...\n";
13 $dbw = wfGetDB( DB_MASTER );
15 $res = $dbw->select( 'text', array( 'old_id','old_flags','old_text' ),
16 "old_id>=$start", $fname, array( 'ORDER BY' => 'old_id', 'LIMIT' => $chunksize, 'FOR UPDATE' ) );
17 if( $dbw->numRows( $res ) == 0 ) {
21 while( $row = $dbw->fetchObject( $res ) ) {
22 # print " {$row->old_id} - {$row->old_namespace}:{$row->old_title}\n";
23 compressPage( $row, $extdb );
26 $dbw->freeResult( $res );
27 $start = $last + 1; # Deletion may leave long empty stretches
33 function compressPage( $row, $extdb ) {
34 $fname = 'compressPage';
35 if ( false !== strpos( $row->old_flags, 'gzip' ) || false !== strpos( $row->old_flags, 'object' ) ) {
36 #print "Already compressed row {$row->old_id}\n";
39 $dbw = wfGetDB( DB_MASTER );
40 $flags = $row->old_flags ? "{$row->old_flags},gzip" : "gzip";
41 $compress = gzdeflate( $row->old_text );
43 # Store in external storage if required
44 if ( $extdb !== '' ) {
45 $storeObj = new ExternalStoreDB;
46 $compress = $storeObj->store( $extdb, $compress );
47 if ( $compress === false ) {
48 print "Unable to store object\n";
56 'old_flags' => $flags,
57 'old_text' => $compress
59 'old_id' => $row->old_id
66 define( 'LS_INDIVIDUAL', 0 );
67 define( 'LS_CHUNKED', 1 );
70 function compressWithConcat( $startId, $maxChunkSize, $beginDate,
71 $endDate, $extdb="", $maxPageId = false )
73 $fname = 'compressWithConcat';
74 $loadStyle = LS_CHUNKED;
76 $dbr = wfGetDB( DB_SLAVE );
77 $dbw = wfGetDB( DB_MASTER );
79 # Set up external storage
81 $storeObj = new ExternalStoreDB;
84 # Get all articles by page_id
86 $maxPageId = $dbr->selectField( 'page', 'max(page_id)', '', $fname );
88 print "Starting from $startId of $maxPageId\n";
93 print "Excluding main namespace\n";
94 $pageConds[] = 'page_namespace<>0';
97 $pageConds[] = $queryExtra;
101 # For each article, get a list of revisions which fit the criteria
103 # No recompression, use a condition on old_flags
104 # Don't compress object type entities, because that might produce data loss when
105 # overwriting bulk storage concat rows. Don't compress external references, because
106 # the script doesn't yet delete rows from external storage.
108 "old_flags NOT LIKE '%object%' AND old_flags NOT LIKE '%external%'");
111 if ( !preg_match( '/^\d{14}$/', $beginDate ) ) {
112 print "Invalid begin date \"$beginDate\"\n";
115 $conds[] = "rev_timestamp>'" . $beginDate . "'";
118 if ( !preg_match( '/^\d{14}$/', $endDate ) ) {
119 print "Invalid end date \"$endDate\"\n";
122 $conds[] = "rev_timestamp<'" . $endDate . "'";
124 if ( $loadStyle == LS_CHUNKED ) {
125 $tables = array( 'revision', 'text' );
126 $fields = array( 'rev_id', 'rev_text_id', 'old_flags', 'old_text' );
127 $conds[] = 'rev_text_id=old_id';
128 $revLoadOptions = 'FOR UPDATE';
130 $tables = array( 'revision' );
131 $fields = array( 'rev_id', 'rev_text_id' );
132 $revLoadOptions = array();
135 # Don't work with current revisions
136 # Don't lock the page table for update either -- TS 2006-04-04
138 #$conds[] = 'page_id=rev_page AND rev_id != page_latest';
140 for ( $pageId = $startId; $pageId <= $maxPageId; $pageId++ ) {
141 wfWaitForSlaves( 5 );
147 $pageRes = $dbr->select( 'page',
148 array('page_id', 'page_namespace', 'page_title','page_latest'),
149 $pageConds + array('page_id' => $pageId), $fname );
150 if ( $dbr->numRows( $pageRes ) == 0 ) {
153 $pageRow = $dbr->fetchObject( $pageRes );
156 $titleObj = Title::makeTitle( $pageRow->page_namespace, $pageRow->page_title );
157 print "$pageId\t" . $titleObj->getPrefixedDBkey() . " ";
160 $revRes = $dbw->select( $tables, $fields,
162 'rev_page' => $pageRow->page_id,
163 # Don't operate on the current revision
164 # Use < instead of <> in case the current revision has changed
165 # since the page select, which wasn't locking
166 'rev_id < ' . $pageRow->page_latest
172 while ( $revRow = $dbw->fetchObject( $revRes ) ) {
176 if ( count( $revs ) < 2) {
177 # No revisions matching, no further processing
184 while ( $i < count( $revs ) ) {
185 if ( $i < count( $revs ) - $maxChunkSize ) {
186 $thisChunkSize = $maxChunkSize;
188 $thisChunkSize = count( $revs ) - $i;
191 $chunk = new ConcatenatedGzipHistoryBlob();
195 $primaryOldid = $revs[$i]->rev_text_id;
197 # Get the text of each revision and add it to the object
198 for ( $j = 0; $j < $thisChunkSize && $chunk->isHappy(); $j++ ) {
199 $oldid = $revs[$i + $j]->rev_text_id;
202 if ( $loadStyle == LS_INDIVIDUAL ) {
203 $textRow = $dbw->selectRow( 'text',
204 array( 'old_flags', 'old_text' ),
205 array( 'old_id' => $oldid ),
209 $text = Revision::getRevisionText( $textRow );
211 $text = Revision::getRevisionText( $revs[$i + $j] );
214 if ( $text === false ) {
215 print "\nError, unable to get text in old_id $oldid\n";
216 #$dbw->delete( 'old', array( 'old_id' => $oldid ) );
219 if ( $extdb == "" && $j == 0 ) {
220 $chunk->setText( $text );
223 # Don't make a stub if it's going to be longer than the article
224 # Stubs are typically about 100 bytes
225 if ( strlen( $text ) < 120 ) {
229 $stub = new HistoryBlobStub( $chunk->addItem( $text ) );
230 $stub->setLocation( $primaryOldid );
231 $stub->setReferrer( $oldid );
240 # If we couldn't actually use any stubs because the pages were too small, do nothing
242 if ( $extdb != "" ) {
243 # Move blob objects to External Storage
244 $stored = $storeObj->store( $extdb, serialize( $chunk ));
245 if ($stored === false) {
246 print "Unable to store object\n";
249 # Store External Storage URLs instead of Stub placeholders
250 foreach ($stubs as $stub) {
253 # $stored should provide base path to a BLOB
254 $url = $stored."/".$stub->getHash();
255 $dbw->update( 'text',
258 'old_flags' => 'external,utf-8',
259 ), array ( /* WHERE */
260 'old_id' => $stub->getReferrer(),
265 # Store the main object locally
266 $dbw->update( 'text',
268 'old_text' => serialize( $chunk ),
269 'old_flags' => 'object,utf-8',
270 ), array( /* WHERE */
271 'old_id' => $primaryOldid
275 # Store the stub objects
276 for ( $j = 1; $j < $thisChunkSize; $j++ ) {
277 # Skip if not compressing and don't overwrite the first revision
278 if ( $stubs[$j] !== false && $revs[$i + $j]->rev_text_id != $primaryOldid ) {
279 $dbw->update( 'text',
281 'old_text' => serialize($stubs[$j]),
282 'old_flags' => 'object,utf-8',
283 ), array( /* WHERE */
284 'old_id' => $revs[$i + $j]->rev_text_id
294 $i += $thisChunkSize;
295 wfWaitForSlaves( 5 );