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
106 # No recompression, use a condition on old_flags
108 "old_flags NOT LIKE '%object%' " .
109 " AND (old_flags NOT LIKE '%external%' OR old_text NOT LIKE 'DB://%/%/%')");
112 $conds[] = "rev_timestamp>'" . $beginDate . "'";
115 $conds[] = "rev_timestamp<'" . $endDate . "'";
117 if ( $loadStyle == LS_CHUNKED ) {
118 $tables = array( 'revision', 'text' );
119 $fields = array( 'rev_id', 'rev_text_id', 'old_flags', 'old_text' );
120 $conds[] = 'rev_text_id=old_id';
121 $revLoadOptions = 'FOR UPDATE';
123 $tables = array( 'revision' );
124 $fields = array( 'rev_id', 'rev_text_id' );
125 $revLoadOptions = array();
128 # Don't work with current revisions
130 $conds[] = 'page_id=rev_page AND rev_id != page_latest';
132 $oldReadsSinceLastSlaveWait = 0; #check slave lag periodically
133 $totalMatchingRevisions = 0;
135 for ( $pageId = $startId; $pageId <= $maxPageId; $pageId++ ) {
136 wfWaitForSlaves( 10 );
138 $pageRes = $dbr->select( 'page', array('page_id', 'page_namespace', 'page_title'),
139 $pageConds + array('page_id' => $pageId), $fname );
140 if ( $dbr->numRows( $pageRes ) == 0 ) {
143 $pageRow = $dbr->fetchObject( $pageRes );
146 $titleObj = Title::makeTitle( $pageRow->page_namespace, $pageRow->page_title );
147 print "$pageId\t" . $titleObj->getPrefixedDBkey() . " ";
150 $revRes = $dbw->select( $tables, $fields,
151 array( 'rev_page' => $pageRow->page_id ) + $conds,
156 while ( $revRow = $dbw->fetchObject( $revRes ) ) {
160 if ( count( $revs ) < 2) {
161 # No revisions matching, no further processing
168 while ( $i < count( $revs ) ) {
169 if ( $i < count( $revs ) - $maxChunkSize ) {
170 $thisChunkSize = $maxChunkSize;
172 $thisChunkSize = count( $revs ) - $i;
175 $chunk = new ConcatenatedGzipHistoryBlob();
179 $primaryOldid = $revs[$i]->rev_text_id;
181 # Get the text of each revision and add it to the object
182 for ( $j = 0; $j < $thisChunkSize && $chunk->isHappy( $maxChunkFactor, $factorThreshold ); $j++ ) {
183 $oldid = $revs[$i + $j]->rev_text_id;
186 if ( $loadStyle == LS_INDIVIDUAL ) {
187 $textRow = $dbw->selectRow( 'text',
188 array( 'old_flags', 'old_text' ),
189 array( 'old_id' => $oldid ),
193 $text = Revision::getRevisionText( $textRow );
195 $text = Revision::getRevisionText( $revs[$i + $j] );
198 if ( $text === false ) {
199 print "\nError, unable to get text in old_id $oldid\n";
200 #$dbw->delete( 'old', array( 'old_id' => $oldid ) );
203 if ( $extdb == "" && $j == 0 ) {
204 $chunk->setText( $text );
207 # Don't make a stub if it's going to be longer than the article
208 # Stubs are typically about 100 bytes
209 if ( strlen( $text ) < 120 ) {
213 $stub = $chunk->addItem( $text );
214 $stub->setLocation( $primaryOldid );
215 $stub->setReferrer( $oldid );
216 $hash = $stub->getHash();
225 # If we couldn't actually use any stubs because the pages were too small, do nothing
227 if ( $extdb != "" ) {
228 # Move blob objects to External Storage
229 $stored = $storeObj->store( $extdb, serialize( $chunk ));
230 if ($stored === false) {
231 print "Unable to store object\n";
234 # Store External Storage URLs instead of Stub placeholders
235 foreach ($stubs as $stub) {
238 # $stored should provide base path to a BLOB
239 $url = $stored."/".$stub->getHash();
240 $dbw->update( 'text',
243 'old_flags' => 'external,utf-8',
244 ), array ( /* WHERE */
245 'old_id' => $stub->getReferrer(),
250 # Store the main object locally
251 $dbw->update( 'text',
253 'old_text' => serialize( $chunk ),
254 'old_flags' => 'object,utf-8',
255 ), array( /* WHERE */
256 'old_id' => $primaryOldid
260 # Store the stub objects
261 for ( $j = 1; $j < $thisChunkSize; $j++ ) {
262 # Skip if not compressing
263 if ( $stubs[$j] !== false ) {
264 $dbw->update( 'text',
266 'old_text' => serialize($stubs[$j]),
267 'old_flags' => 'object,utf-8',
268 ), array( /* WHERE */
269 'old_id' => $revs[$i + $j]->rev_text_id
279 $i += $thisChunkSize;