4 * @subpackage Maintenance
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 $end = $start + $chunksize;
20 $res = $dbw->select( 'text', array( 'old_id','old_flags','old_namespace','old_title','old_text' ),
21 "old_id>=$start", $fname, array( 'ORDER BY' => 'old_id', 'LIMIT' => $chunksize, 'FOR UPDATE' ) );
22 if( $dbw->numRows( $res ) == 0 ) {
26 while( $row = $dbw->fetchObject( $res ) ) {
27 # print " {$row->old_id} - {$row->old_namespace}:{$row->old_title}\n";
28 compressPage( $row, $extdb );
31 $dbw->freeResult( $res );
32 $start = $last + 1; # Deletion may leave long empty stretches
38 function compressPage( $row, $extdb ) {
39 $fname = 'compressPage';
40 if ( false !== strpos( $row->old_flags, 'gzip' ) || false !== strpos( $row->old_flags, 'object' ) ) {
41 #print "Already compressed row {$row->old_id}\n";
44 $dbw =& wfGetDB( DB_MASTER );
45 $flags = $row->old_flags ? "{$row->old_flags},gzip" : "gzip";
46 $compress = gzdeflate( $row->old_text );
48 # Store in external storage if required
49 if ( $extdb !== '' ) {
50 $storeObj = new ExternalStoreDB;
51 $compress = $storeObj->store( $extdb, $compress );
52 if ( $compress === false ) {
53 print "Unable to store object\n";
61 'old_flags' => $flags,
62 'old_text' => $compress
64 'old_id' => $row->old_id
70 define( 'LS_INDIVIDUAL', 0 );
71 define( 'LS_CHUNKED', 1 );
74 function compressWithConcat( $startId, $maxChunkSize, $maxChunkFactor, $factorThreshold, $beginDate,
75 $endDate, $extdb="", $maxPageId = false )
77 $fname = 'compressWithConcat';
78 $loadStyle = LS_CHUNKED;
80 $dbr =& wfGetDB( DB_SLAVE );
81 $dbw =& wfGetDB( DB_MASTER );
83 # Set up external storage
85 $storeObj = new ExternalStoreDB;
88 # Get all articles by page_id
90 $maxPageId = $dbr->selectField( 'page', 'max(page_id)', '', $fname );
92 print "Starting from $startId of $maxPageId\n";
97 print "Excluding main namespace\n";
98 $pageConds[] = 'page_namespace<>0';
101 $pageConds[] = $queryExtra;
105 # For each article, get a list of revisions which fit the criteria
107 # No recompression, use a condition on old_flags
108 # Don't compress object type entities, because that might produce data loss when
109 # overwriting bulk storage concat rows. Don't compress external references, because
110 # the script doesn't yet delete rows from external storage.
112 "old_flags NOT LIKE '%object%' AND old_flags NOT LIKE '%external%'");
115 $conds[] = "rev_timestamp>'" . $beginDate . "'";
118 $conds[] = "rev_timestamp<'" . $endDate . "'";
120 if ( $loadStyle == LS_CHUNKED ) {
121 $tables = array( 'revision', 'text' );
122 $fields = array( 'rev_id', 'rev_text_id', 'old_flags', 'old_text' );
123 $conds[] = 'rev_text_id=old_id';
124 $revLoadOptions = 'FOR UPDATE';
126 $tables = array( 'revision' );
127 $fields = array( 'rev_id', 'rev_text_id' );
128 $revLoadOptions = array();
131 # Don't work with current revisions
133 $conds[] = 'page_id=rev_page AND rev_id != page_latest';
135 $oldReadsSinceLastSlaveWait = 0; #check slave lag periodically
136 $totalMatchingRevisions = 0;
138 for ( $pageId = $startId; $pageId <= $maxPageId; $pageId++ ) {
139 wfWaitForSlaves( 5 );
145 $pageRes = $dbr->select( 'page', array('page_id', 'page_namespace', 'page_title'),
146 $pageConds + array('page_id' => $pageId), $fname );
147 if ( $dbr->numRows( $pageRes ) == 0 ) {
150 $pageRow = $dbr->fetchObject( $pageRes );
153 $titleObj = Title::makeTitle( $pageRow->page_namespace, $pageRow->page_title );
154 print "$pageId\t" . $titleObj->getPrefixedDBkey() . " ";
157 $revRes = $dbw->select( $tables, $fields,
158 array( 'rev_page' => $pageRow->page_id ) + $conds,
163 while ( $revRow = $dbw->fetchObject( $revRes ) ) {
167 if ( count( $revs ) < 2) {
168 # No revisions matching, no further processing
175 while ( $i < count( $revs ) ) {
176 if ( $i < count( $revs ) - $maxChunkSize ) {
177 $thisChunkSize = $maxChunkSize;
179 $thisChunkSize = count( $revs ) - $i;
182 $chunk = new ConcatenatedGzipHistoryBlob();
186 $primaryOldid = $revs[$i]->rev_text_id;
188 # Get the text of each revision and add it to the object
189 for ( $j = 0; $j < $thisChunkSize && $chunk->isHappy( $maxChunkFactor, $factorThreshold ); $j++ ) {
190 $oldid = $revs[$i + $j]->rev_text_id;
193 if ( $loadStyle == LS_INDIVIDUAL ) {
194 $textRow = $dbw->selectRow( 'text',
195 array( 'old_flags', 'old_text' ),
196 array( 'old_id' => $oldid ),
200 $text = Revision::getRevisionText( $textRow );
202 $text = Revision::getRevisionText( $revs[$i + $j] );
205 if ( $text === false ) {
206 print "\nError, unable to get text in old_id $oldid\n";
207 #$dbw->delete( 'old', array( 'old_id' => $oldid ) );
210 if ( $extdb == "" && $j == 0 ) {
211 $chunk->setText( $text );
214 # Don't make a stub if it's going to be longer than the article
215 # Stubs are typically about 100 bytes
216 if ( strlen( $text ) < 120 ) {
220 $stub = $chunk->addItem( $text );
221 $stub->setLocation( $primaryOldid );
222 $stub->setReferrer( $oldid );
231 # If we couldn't actually use any stubs because the pages were too small, do nothing
233 if ( $extdb != "" ) {
234 # Move blob objects to External Storage
235 $stored = $storeObj->store( $extdb, serialize( $chunk ));
236 if ($stored === false) {
237 print "Unable to store object\n";
240 # Store External Storage URLs instead of Stub placeholders
241 foreach ($stubs as $stub) {
244 # $stored should provide base path to a BLOB
245 $url = $stored."/".$stub->getHash();
246 $dbw->update( 'text',
249 'old_flags' => 'external,utf-8',
250 ), array ( /* WHERE */
251 'old_id' => $stub->getReferrer(),
256 # Store the main object locally
257 $dbw->update( 'text',
259 'old_text' => serialize( $chunk ),
260 'old_flags' => 'object,utf-8',
261 ), array( /* WHERE */
262 'old_id' => $primaryOldid
266 # Store the stub objects
267 for ( $j = 1; $j < $thisChunkSize; $j++ ) {
268 # Skip if not compressing
269 if ( $stubs[$j] !== false ) {
270 $dbw->update( 'text',
272 'old_text' => serialize($stubs[$j]),
273 'old_flags' => 'object,utf-8',
274 ), array( /* WHERE */
275 'old_id' => $revs[$i + $j]->rev_text_id
285 $i += $thisChunkSize;
286 wfWaitForSlaves( 5 );