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
65 define( 'LS_INDIVIDUAL', 0 );
66 define( 'LS_CHUNKED', 1 );
69 function compressWithConcat( $startId, $maxChunkSize, $beginDate,
70 $endDate, $extdb="", $maxPageId = false )
72 $fname = 'compressWithConcat';
73 $loadStyle = LS_CHUNKED;
75 $dbr = wfGetDB( DB_SLAVE );
76 $dbw = wfGetDB( DB_MASTER );
78 # Set up external storage
80 $storeObj = new ExternalStoreDB;
83 # Get all articles by page_id
85 $maxPageId = $dbr->selectField( 'page', 'max(page_id)', '', $fname );
87 print "Starting from $startId of $maxPageId\n";
92 print "Excluding main namespace\n";
93 $pageConds[] = 'page_namespace<>0';
96 $pageConds[] = $queryExtra;
100 # For each article, get a list of revisions which fit the criteria
102 # No recompression, use a condition on old_flags
103 # Don't compress object type entities, because that might produce data loss when
104 # overwriting bulk storage concat rows. Don't compress external references, because
105 # the script doesn't yet delete rows from external storage.
107 "old_flags NOT LIKE '%object%' AND old_flags NOT LIKE '%external%'");
110 if ( !preg_match( '/^\d{14}$/', $beginDate ) ) {
111 print "Invalid begin date \"$beginDate\"\n";
114 $conds[] = "rev_timestamp>'" . $beginDate . "'";
117 if ( !preg_match( '/^\d{14}$/', $endDate ) ) {
118 print "Invalid end date \"$endDate\"\n";
121 $conds[] = "rev_timestamp<'" . $endDate . "'";
123 if ( $loadStyle == LS_CHUNKED ) {
124 $tables = array( 'revision', 'text' );
125 $fields = array( 'rev_id', 'rev_text_id', 'old_flags', 'old_text' );
126 $conds[] = 'rev_text_id=old_id';
127 $revLoadOptions = 'FOR UPDATE';
129 $tables = array( 'revision' );
130 $fields = array( 'rev_id', 'rev_text_id' );
131 $revLoadOptions = array();
134 # Don't work with current revisions
135 # Don't lock the page table for update either -- TS 2006-04-04
137 #$conds[] = 'page_id=rev_page AND rev_id != page_latest';
139 for ( $pageId = $startId; $pageId <= $maxPageId; $pageId++ ) {
140 wfWaitForSlaves( 5 );
146 $pageRes = $dbr->select( 'page',
147 array('page_id', 'page_namespace', 'page_title','page_latest'),
148 $pageConds + array('page_id' => $pageId), $fname );
149 if ( $dbr->numRows( $pageRes ) == 0 ) {
152 $pageRow = $dbr->fetchObject( $pageRes );
155 $titleObj = Title::makeTitle( $pageRow->page_namespace, $pageRow->page_title );
156 print "$pageId\t" . $titleObj->getPrefixedDBkey() . " ";
159 $revRes = $dbw->select( $tables, $fields,
161 'rev_page' => $pageRow->page_id,
162 # Don't operate on the current revision
163 # Use < instead of <> in case the current revision has changed
164 # since the page select, which wasn't locking
165 'rev_id < ' . $pageRow->page_latest
171 while ( $revRow = $dbw->fetchObject( $revRes ) ) {
175 if ( count( $revs ) < 2) {
176 # No revisions matching, no further processing
183 while ( $i < count( $revs ) ) {
184 if ( $i < count( $revs ) - $maxChunkSize ) {
185 $thisChunkSize = $maxChunkSize;
187 $thisChunkSize = count( $revs ) - $i;
190 $chunk = new ConcatenatedGzipHistoryBlob();
194 $primaryOldid = $revs[$i]->rev_text_id;
196 # Get the text of each revision and add it to the object
197 for ( $j = 0; $j < $thisChunkSize && $chunk->isHappy(); $j++ ) {
198 $oldid = $revs[$i + $j]->rev_text_id;
201 if ( $loadStyle == LS_INDIVIDUAL ) {
202 $textRow = $dbw->selectRow( 'text',
203 array( 'old_flags', 'old_text' ),
204 array( 'old_id' => $oldid ),
208 $text = Revision::getRevisionText( $textRow );
210 $text = Revision::getRevisionText( $revs[$i + $j] );
213 if ( $text === false ) {
214 print "\nError, unable to get text in old_id $oldid\n";
215 #$dbw->delete( 'old', array( 'old_id' => $oldid ) );
218 if ( $extdb == "" && $j == 0 ) {
219 $chunk->setText( $text );
222 # Don't make a stub if it's going to be longer than the article
223 # Stubs are typically about 100 bytes
224 if ( strlen( $text ) < 120 ) {
228 $stub = new HistoryBlobStub( $chunk->addItem( $text ) );
229 $stub->setLocation( $primaryOldid );
230 $stub->setReferrer( $oldid );
239 # If we couldn't actually use any stubs because the pages were too small, do nothing
241 if ( $extdb != "" ) {
242 # Move blob objects to External Storage
243 $stored = $storeObj->store( $extdb, serialize( $chunk ));
244 if ($stored === false) {
245 print "Unable to store object\n";
248 # Store External Storage URLs instead of Stub placeholders
249 foreach ($stubs as $stub) {
252 # $stored should provide base path to a BLOB
253 $url = $stored."/".$stub->getHash();
254 $dbw->update( 'text',
257 'old_flags' => 'external,utf-8',
258 ), array ( /* WHERE */
259 'old_id' => $stub->getReferrer(),
264 # Store the main object locally
265 $dbw->update( 'text',
267 'old_text' => serialize( $chunk ),
268 'old_flags' => 'object,utf-8',
269 ), array( /* WHERE */
270 'old_id' => $primaryOldid
274 # Store the stub objects
275 for ( $j = 1; $j < $thisChunkSize; $j++ ) {
276 # Skip if not compressing and don't overwrite the first revision
277 if ( $stubs[$j] !== false && $revs[$i + $j]->rev_text_id != $primaryOldid ) {
278 $dbw->update( 'text',
280 'old_text' => serialize($stubs[$j]),
281 'old_flags' => 'object,utf-8',
282 ), array( /* WHERE */
283 'old_id' => $revs[$i + $j]->rev_text_id
293 $i += $thisChunkSize;
294 wfWaitForSlaves( 5 );