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, $endDate )
76 $fname = 'compressWithConcat';
77 $loadStyle = LS_CHUNKED;
79 $dbr =& wfGetDB( DB_SLAVE );
80 $dbw =& wfGetDB( DB_MASTER );
82 # Get all articles by page_id
83 $maxPageId = $dbr->selectField( 'page', 'max(page_id)', '', $fname );
88 print "Excluding main namespace\n";
89 $pageConds[] = 'page_namespace<>0';
92 $pageConds[] = $queryExtra;
96 # For each article, get a list of revisions which fit the criteria
97 # No recompression, use a condition on old_flags
99 "old_flags NOT LIKE '%object%' " .
100 " AND (old_flags NOT LIKE '%external%' OR old_text NOT LIKE 'DB://%/%/%')");
103 $conds[] = "rev_timestamp>'" . $beginDate . "'";
106 $conds[] = "rev_timestamp<'" . $endDate . "'";
108 if ( $loadStyle == LS_CHUNKED ) {
109 $tables = array( 'revision', 'text' );
110 $fields = array( 'rev_id', 'rev_text_id', 'old_flags', 'old_text' );
111 $conds[] = 'rev_text_id=old_id';
112 $revLoadOptions = 'FOR UPDATE';
114 $tables = array( 'revision' );
115 $fields = array( 'rev_id', 'rev_text_id' );
116 $revLoadOptions = array();
119 # Don't work with current revisions
121 $conds[] = 'page_id=rev_page AND rev_id != page_latest';
123 $oldReadsSinceLastSlaveWait = 0; #check slave lag periodically
124 $totalMatchingRevisions = 0;
126 for ( $pageId = $startId; $pageId <= $maxPageId; $pageId++ ) {
127 $pageRes = $dbr->select( 'page', array('page_id', 'page_namespace', 'page_title'),
128 $pageConds + array('page_id' => $pageId), $fname );
129 if ( $dbr->numRows( $pageRes ) == 0 ) {
132 $pageRow = $dbr->fetchObject( $pageRes );
135 $titleObj = Title::makeTitle( $pageRow->page_namespace, $pageRow->page_title );
136 print "$pageId\t" . $titleObj->getPrefixedDBkey() . " ";
139 $revRes = $dbw->select( $tables, $fields,
140 array( 'rev_page' => $pageRow->page_id ) + $conds,
145 while ( $revRow = $dbw->fetchObject( $revRes ) ) {
149 if ( count( $revs ) < 2) {
150 # No revisions matching, no further processing
157 while ( $i < count( $revs ) ) {
158 if ( $i < count( $revs ) - $maxChunkSize ) {
159 $thisChunkSize = $maxChunkSize;
161 $thisChunkSize = count( $revs ) - $i;
164 $chunk = new ConcatenatedGzipHistoryBlob();
168 $primaryOldid = $revs[$i]->rev_text_id;
170 # Get the text of each revision and add it to the object
171 for ( $j = 0; $j < $thisChunkSize && $chunk->isHappy( $maxChunkFactor, $factorThreshold ); $j++ ) {
172 $oldid = $revs[$i + $j]->rev_text_id;
175 if ( $loadStyle == LS_INDIVIDUAL ) {
176 $textRow = $dbw->selectRow( 'text',
177 array( 'old_flags', 'old_text' ),
178 array( 'old_id' => $oldid ),
182 $text = Revision::getRevisionText( $textRow );
184 $text = Revision::getRevisionText( $revs[$i + $j] );
187 if ( $text === false ) {
188 print "\nError, unable to get text in old_id $oldid\n";
189 #$dbw->delete( 'old', array( 'old_id' => $oldid ) );
193 $chunk->setText( $text );
196 # Don't make a stub if it's going to be longer than the article
197 # Stubs are typically about 100 bytes
198 if ( strlen( $text ) < 120 ) {
202 $stub = $chunk->addItem( $text );
203 $stub->setLocation( $primaryOldid );
204 $hash = $stub->getHash();
205 $stub = serialize( $stub );
214 # If we couldn't actually use any stubs because the pages were too small, do nothing
216 # Store the main object
217 $dbw->update( 'text',
219 'old_text' => serialize( $chunk ),
220 'old_flags' => 'object,utf-8',
221 ), array( /* WHERE */
222 'old_id' => $primaryOldid
226 # Store the stub objects
227 for ( $j = 1; $j < $thisChunkSize; $j++ ) {
228 # Skip if not compressing
229 if ( $stubs[$j] !== false ) {
230 $dbw->update( 'text',
232 'old_text' => $stubs[$j],
233 'old_flags' => 'object,utf-8',
234 ), array( /* WHERE */
235 'old_id' => $revs[$i + $j]->rev_text_id
244 $i += $thisChunkSize;