4 * @subpackage Maintenance
8 require_once( '../includes/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 $dbw =& wfGetDB( DB_MASTER );
69 # First get a list of all pages
70 $pageRes = $dbw->select( 'page', 'page_id', false, $fname );
72 # For each of those, get a list of revisions which fit the criteria
75 $conds[] = "rev_timestamp>'" . $beginDate . "'";
78 $conds[] = "rev_timestamp<'" . $endDate . "'";
81 $conds[] = 'rev_id>=' . $startId;
83 if ( $loadStyle == LS_CHUNKED ) {
84 $tables = array( 'revision', 'text' );
85 $fields = array( 'rev_id', 'old_flags', 'old_text' );
86 $conds[] = 'rev_id=old_id';
87 $revLoadOptions = 'FOR UPDATE';
89 $tables = array( 'revision' );
90 $fields = array( 'rev_id' );
91 $revLoadOptions = array();
94 while ( $pageRow = $dbw->fetchObject( $pageRes ) ) {
96 $titleObj = Title::makeTitle( $pageRow->cur_namespace, $pageRow->cur_title );
97 print $titleObj->getPrefixedDBkey() . " ";
100 $revRes = $dbw->select( 'old', $fields,
101 array( 'rev_page' => $pageRow->page_id ) + $conds,
106 while ( $revRow = $dbw->fetchObject( $revRes ) ) {
110 if ( count( $revs ) < 2) {
111 # No revisions matching, no further processing
118 while ( $i < count( $revs ) ) {
119 if ( $i < count( $revs ) - $maxChunkSize ) {
120 $thisChunkSize = $maxChunkSize;
122 $thisChunkSize = count( $revs ) - $i;
125 $chunk = new ConcatenatedGzipHistoryBlob();
129 $primaryOldid = $revs[$i]->rev_id;
131 # Get the text of each revision and add it to the object
132 for ( $j = 0; $j < $thisChunkSize && $chunk->isHappy( $maxChunkFactor, $factorThreshold ); $j++ ) {
133 $oldid = $revs[$i + $j]->rev_id;
136 if ( $loadStyle == LS_INDIVIDUAL ) {
137 $textRow = $dbw->selectRow( 'text',
138 array( 'old_flags', 'old_text' ),
139 array( 'old_id' => $oldid ),
143 $text = Revision::getRevisionText( $textRow );
145 $text = Revision::getRevisionText( $revs[$i + $j] );
148 if ( $text === false ) {
149 print "\nError, unable to get text in old_id $oldid\n";
150 #$dbw->delete( 'old', array( 'old_id' => $oldid ) );
154 $chunk->setText( $text );
157 # Don't make a stub if it's going to be longer than the article
158 # Stubs are typically about 100 bytes
159 if ( strlen( $text ) < 120 ) {
163 $stub = $chunk->addItem( $text );
164 $stub->setLocation( $primaryOldid );
165 $hash = $stub->getHash();
166 $stub = serialize( $stub );
175 # If we couldn't actually use any stubs because the pages were too small, do nothing
177 # Store the main object
178 $dbw->update( 'text',
180 'old_text' => serialize( $chunk ),
181 'old_flags' => 'object',
182 ), array( /* WHERE */
183 'old_id' => $primaryOldid
187 # Store the stub objects
188 for ( $j = 1; $j < $thisChunkSize; $j++ ) {
189 # Skip if not compressing
190 if ( $stubs[$j] !== false ) {
191 $dbw->update( 'text',
193 'old_text' => $stubs[$j],
194 'old_flags' => 'object',
195 ), array( /* WHERE */
196 'old_id' => $revs[$i + $j]->old_id
205 $i += $thisChunkSize;