4 * @subpackage Maintenance
8 require_once( 'Revision.php' );
11 function compressOldPages( $start = 0 ) {
12 $fname = 'compressOldPages';
15 print "Starting from old_id $start...\n";
16 $dbw =& wfGetDB( DB_MASTER );
17 $old = $dbw->tableName( 'old' );
19 $end = $start + $chunksize;
20 $res = $dbw->select( 'old', 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";
31 $dbw->freeResult( $res );
32 $start = $last + 1; # Deletion may leave long empty stretches
38 function compressPage( $row ) {
39 $fname = 'compressPage';
40 if( false !== strpos( $row->old_flags, "gzip" ) ) {
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 );
49 'old_flags' => $flags,
50 'old_text' => $compress
52 'old_id' => $row->old_id
58 define( 'LS_INDIVIDUAL', 0 );
59 define( 'LS_CHUNKED', 1 );
62 function compressWithConcat( $startId, $maxChunkSize, $maxChunkFactor, $factorThreshold, $beginDate, $endDate )
64 $fname = 'compressWithConcat';
65 $loadStyle = LS_CHUNKED;
67 $dbr =& wfGetDB( DB_SLAVE );
68 $dbw =& wfGetDB( DB_MASTER );
70 # Get all articles by page_id
71 $maxPageId = $dbr->selectField( 'page', 'max(page_id)', '', $fname );
75 print "Excluding main namespace\n";
76 $pageConds[] = 'page_namespace<>0';
79 $pageConds[] = $queryExtra;
82 # For each article, get a list of revisions which fit the criteria
83 # No recompression, use a condition on old_flags
84 $conds = array("old_flags NOT LIKE '%object%'");
87 $conds[] = "rev_timestamp>'" . $beginDate . "'";
90 $conds[] = "rev_timestamp<'" . $endDate . "'";
92 if ( $loadStyle == LS_CHUNKED ) {
93 $tables = array( 'revision', 'text' );
94 $fields = array( 'rev_id', 'rev_text_id', 'old_flags', 'old_text' );
95 $conds[] = 'rev_text_id=old_id';
96 $revLoadOptions = 'FOR UPDATE';
98 $tables = array( 'revision' );
99 $fields = array( 'rev_id', 'rev_text_id' );
100 $revLoadOptions = array();
103 $oldReadsSinceLastSlaveWait = 0; #check slave lag periodically
104 $totalMatchingRevisions = 0;
106 for ( $pageId = $startId; $pageId <= $maxPageId; $pageId++ ) {
107 $pageRes = $dbr->select( 'page', array('page_id', 'page_namespace', 'page_title'),
108 $pageConds + array('page_id' => $pageId), $fname );
109 if ( $dbr->numRows( $pageRes ) == 0 ) {
112 $pageRow = $dbr->fetchObject( $pageRes );
115 $titleObj = Title::makeTitle( $pageRow->page_namespace, $pageRow->page_title );
116 print "$pageId\t" . $titleObj->getPrefixedDBkey() . " ";
119 $revRes = $dbw->select( $tables, $fields,
120 array( 'rev_page' => $pageRow->page_id ) + $conds,
125 while ( $revRow = $dbw->fetchObject( $revRes ) ) {
129 if ( count( $revs ) < 2) {
130 # No revisions matching, no further processing
137 while ( $i < count( $revs ) ) {
138 if ( $i < count( $revs ) - $maxChunkSize ) {
139 $thisChunkSize = $maxChunkSize;
141 $thisChunkSize = count( $revs ) - $i;
144 $chunk = new ConcatenatedGzipHistoryBlob();
148 $primaryOldid = $revs[$i]->rev_text_id;
150 # Get the text of each revision and add it to the object
151 for ( $j = 0; $j < $thisChunkSize && $chunk->isHappy( $maxChunkFactor, $factorThreshold ); $j++ ) {
152 $oldid = $revs[$i + $j]->rev_text_id;
155 if ( $loadStyle == LS_INDIVIDUAL ) {
156 $textRow = $dbw->selectRow( 'text',
157 array( 'old_flags', 'old_text' ),
158 array( 'old_id' => $oldid ),
162 $text = Revision::getRevisionText( $textRow );
164 $text = Revision::getRevisionText( $revs[$i + $j] );
167 if ( $text === false ) {
168 print "\nError, unable to get text in old_id $oldid\n";
169 #$dbw->delete( 'old', array( 'old_id' => $oldid ) );
173 $chunk->setText( $text );
176 # Don't make a stub if it's going to be longer than the article
177 # Stubs are typically about 100 bytes
178 if ( strlen( $text ) < 120 ) {
182 $stub = $chunk->addItem( $text );
183 $stub->setLocation( $primaryOldid );
184 $hash = $stub->getHash();
185 $stub = serialize( $stub );
194 # If we couldn't actually use any stubs because the pages were too small, do nothing
196 # Store the main object
197 $dbw->update( 'text',
199 'old_text' => serialize( $chunk ),
200 'old_flags' => 'object',
201 ), array( /* WHERE */
202 'old_id' => $primaryOldid
206 # Store the stub objects
207 for ( $j = 1; $j < $thisChunkSize; $j++ ) {
208 # Skip if not compressing
209 if ( $stubs[$j] !== false ) {
210 $dbw->update( 'text',
212 'old_text' => $stubs[$j],
213 'old_flags' => 'object',
214 ), array( /* WHERE */
215 'old_id' => $revs[$i + $j]->rev_text_id
224 $i += $thisChunkSize;