00001 <?php
00008 function compressOldPages( $start = 0, $extdb = '' ) {
00009 $fname = 'compressOldPages';
00010
00011 $chunksize = 50;
00012 print "Starting from old_id $start...\n";
00013 $dbw = wfGetDB( DB_MASTER );
00014 do {
00015 $res = $dbw->select( 'text', array( 'old_id','old_flags','old_text' ),
00016 "old_id>=$start", $fname, array( 'ORDER BY' => 'old_id', 'LIMIT' => $chunksize, 'FOR UPDATE' ) );
00017 if( $dbw->numRows( $res ) == 0 ) {
00018 break;
00019 }
00020 $last = $start;
00021 while( $row = $dbw->fetchObject( $res ) ) {
00022 # print " {$row->old_id} - {$row->old_namespace}:{$row->old_title}\n";
00023 compressPage( $row, $extdb );
00024 $last = $row->old_id;
00025 }
00026 $dbw->freeResult( $res );
00027 $start = $last + 1; # Deletion may leave long empty stretches
00028 print "$start...\n";
00029 } while( true );
00030 }
00031
00033 function compressPage( $row, $extdb ) {
00034 $fname = 'compressPage';
00035 if ( false !== strpos( $row->old_flags, 'gzip' ) || false !== strpos( $row->old_flags, 'object' ) ) {
00036 #print "Already compressed row {$row->old_id}\n";
00037 return false;
00038 }
00039 $dbw = wfGetDB( DB_MASTER );
00040 $flags = $row->old_flags ? "{$row->old_flags},gzip" : "gzip";
00041 $compress = gzdeflate( $row->old_text );
00042
00043 # Store in external storage if required
00044 if ( $extdb !== '' ) {
00045 $storeObj = new ExternalStoreDB;
00046 $compress = $storeObj->store( $extdb, $compress );
00047 if ( $compress === false ) {
00048 print "Unable to store object\n";
00049 return false;
00050 }
00051 }
00052
00053 # Update text row
00054 $dbw->update( 'text',
00055 array(
00056 'old_flags' => $flags,
00057 'old_text' => $compress
00058 ), array(
00059 'old_id' => $row->old_id
00060 ), $fname, 'LIMIT 1'
00061 );
00062 return true;
00063 }
00064
00065 define( 'LS_INDIVIDUAL', 0 );
00066 define( 'LS_CHUNKED', 1 );
00067
00069 function compressWithConcat( $startId, $maxChunkSize, $beginDate,
00070 $endDate, $extdb="", $maxPageId = false )
00071 {
00072 $fname = 'compressWithConcat';
00073 $loadStyle = LS_CHUNKED;
00074
00075 $dbr = wfGetDB( DB_SLAVE );
00076 $dbw = wfGetDB( DB_MASTER );
00077
00078 # Set up external storage
00079 if ( $extdb != '' ) {
00080 $storeObj = new ExternalStoreDB;
00081 }
00082
00083 # Get all articles by page_id
00084 if ( !$maxPageId ) {
00085 $maxPageId = $dbr->selectField( 'page', 'max(page_id)', '', $fname );
00086 }
00087 print "Starting from $startId of $maxPageId\n";
00088 $pageConds = array();
00089
00090
00091
00092
00093
00094
00095
00096
00097
00098
00099
00100 # For each article, get a list of revisions which fit the criteria
00101
00102 # No recompression, use a condition on old_flags
00103 # Don't compress object type entities, because that might produce data loss when
00104 # overwriting bulk storage concat rows. Don't compress external references, because
00105 # the script doesn't yet delete rows from external storage.
00106 $conds = array(
00107 "old_flags NOT LIKE '%object%' AND old_flags NOT LIKE '%external%'");
00108
00109 if ( $beginDate ) {
00110 if ( !preg_match( '/^\d{14}$/', $beginDate ) ) {
00111 print "Invalid begin date \"$beginDate\"\n";
00112 return false;
00113 }
00114 $conds[] = "rev_timestamp>'" . $beginDate . "'";
00115 }
00116 if ( $endDate ) {
00117 if ( !preg_match( '/^\d{14}$/', $endDate ) ) {
00118 print "Invalid end date \"$endDate\"\n";
00119 return false;
00120 }
00121 $conds[] = "rev_timestamp<'" . $endDate . "'";
00122 }
00123 if ( $loadStyle == LS_CHUNKED ) {
00124 $tables = array( 'revision', 'text' );
00125 $fields = array( 'rev_id', 'rev_text_id', 'old_flags', 'old_text' );
00126 $conds[] = 'rev_text_id=old_id';
00127 $revLoadOptions = 'FOR UPDATE';
00128 } else {
00129 $tables = array( 'revision' );
00130 $fields = array( 'rev_id', 'rev_text_id' );
00131 $revLoadOptions = array();
00132 }
00133
00134 # Don't work with current revisions
00135 # Don't lock the page table for update either -- TS 2006-04-04
00136 #$tables[] = 'page';
00137 #$conds[] = 'page_id=rev_page AND rev_id != page_latest';
00138
00139 for ( $pageId = $startId; $pageId <= $maxPageId; $pageId++ ) {
00140 wfWaitForSlaves( 5 );
00141
00142 # Wake up
00143 $dbr->ping();
00144
00145 # Get the page row
00146 $pageRes = $dbr->select( 'page',
00147 array('page_id', 'page_namespace', 'page_title','page_latest'),
00148 $pageConds + array('page_id' => $pageId), $fname );
00149 if ( $dbr->numRows( $pageRes ) == 0 ) {
00150 continue;
00151 }
00152 $pageRow = $dbr->fetchObject( $pageRes );
00153
00154 # Display progress
00155 $titleObj = Title::makeTitle( $pageRow->page_namespace, $pageRow->page_title );
00156 print "$pageId\t" . $titleObj->getPrefixedDBkey() . " ";
00157
00158 # Load revisions
00159 $revRes = $dbw->select( $tables, $fields,
00160 array_merge( array(
00161 'rev_page' => $pageRow->page_id,
00162 # Don't operate on the current revision
00163 # Use < instead of <> in case the current revision has changed
00164 # since the page select, which wasn't locking
00165 'rev_id < ' . $pageRow->page_latest
00166 ), $conds ),
00167 $fname,
00168 $revLoadOptions
00169 );
00170 $revs = array();
00171 while ( $revRow = $dbw->fetchObject( $revRes ) ) {
00172 $revs[] = $revRow;
00173 }
00174
00175 if ( count( $revs ) < 2) {
00176 # No revisions matching, no further processing
00177 print "\n";
00178 continue;
00179 }
00180
00181 # For each chunk
00182 $i = 0;
00183 while ( $i < count( $revs ) ) {
00184 if ( $i < count( $revs ) - $maxChunkSize ) {
00185 $thisChunkSize = $maxChunkSize;
00186 } else {
00187 $thisChunkSize = count( $revs ) - $i;
00188 }
00189
00190 $chunk = new ConcatenatedGzipHistoryBlob();
00191 $stubs = array();
00192 $dbw->begin();
00193 $usedChunk = false;
00194 $primaryOldid = $revs[$i]->rev_text_id;
00195
00196 # Get the text of each revision and add it to the object
00197 for ( $j = 0; $j < $thisChunkSize && $chunk->isHappy(); $j++ ) {
00198 $oldid = $revs[$i + $j]->rev_text_id;
00199
00200 # Get text
00201 if ( $loadStyle == LS_INDIVIDUAL ) {
00202 $textRow = $dbw->selectRow( 'text',
00203 array( 'old_flags', 'old_text' ),
00204 array( 'old_id' => $oldid ),
00205 $fname,
00206 'FOR UPDATE'
00207 );
00208 $text = Revision::getRevisionText( $textRow );
00209 } else {
00210 $text = Revision::getRevisionText( $revs[$i + $j] );
00211 }
00212
00213 if ( $text === false ) {
00214 print "\nError, unable to get text in old_id $oldid\n";
00215 #$dbw->delete( 'old', array( 'old_id' => $oldid ) );
00216 }
00217
00218 if ( $extdb == "" && $j == 0 ) {
00219 $chunk->setText( $text );
00220 print '.';
00221 } else {
00222 # Don't make a stub if it's going to be longer than the article
00223 # Stubs are typically about 100 bytes
00224 if ( strlen( $text ) < 120 ) {
00225 $stub = false;
00226 print 'x';
00227 } else {
00228 $stub = new HistoryBlobStub( $chunk->addItem( $text ) );
00229 $stub->setLocation( $primaryOldid );
00230 $stub->setReferrer( $oldid );
00231 print '.';
00232 $usedChunk = true;
00233 }
00234 $stubs[$j] = $stub;
00235 }
00236 }
00237 $thisChunkSize = $j;
00238
00239 # If we couldn't actually use any stubs because the pages were too small, do nothing
00240 if ( $usedChunk ) {
00241 if ( $extdb != "" ) {
00242 # Move blob objects to External Storage
00243 $stored = $storeObj->store( $extdb, serialize( $chunk ));
00244 if ($stored === false) {
00245 print "Unable to store object\n";
00246 return false;
00247 }
00248 # Store External Storage URLs instead of Stub placeholders
00249 foreach ($stubs as $stub) {
00250 if ($stub===false)
00251 continue;
00252 # $stored should provide base path to a BLOB
00253 $url = $stored."/".$stub->getHash();
00254 $dbw->update( 'text',
00255 array(
00256 'old_text' => $url,
00257 'old_flags' => 'external,utf-8',
00258 ), array (
00259 'old_id' => $stub->getReferrer(),
00260 )
00261 );
00262 }
00263 } else {
00264 # Store the main object locally
00265 $dbw->update( 'text',
00266 array(
00267 'old_text' => serialize( $chunk ),
00268 'old_flags' => 'object,utf-8',
00269 ), array(
00270 'old_id' => $primaryOldid
00271 )
00272 );
00273
00274 # Store the stub objects
00275 for ( $j = 1; $j < $thisChunkSize; $j++ ) {
00276 # Skip if not compressing and don't overwrite the first revision
00277 if ( $stubs[$j] !== false && $revs[$i + $j]->rev_text_id != $primaryOldid ) {
00278 $dbw->update( 'text',
00279 array(
00280 'old_text' => serialize($stubs[$j]),
00281 'old_flags' => 'object,utf-8',
00282 ), array(
00283 'old_id' => $revs[$i + $j]->rev_text_id
00284 )
00285 );
00286 }
00287 }
00288 }
00289 }
00290 # Done, next
00291 print "/";
00292 $dbw->commit();
00293 $i += $thisChunkSize;
00294 wfWaitForSlaves( 5 );
00295 }
00296 print "\n";
00297 }
00298 return true;
00299 }