4 * @subpackage Maintenance
7 require_once( '../includes/Revision.php' );
10 function compressOldPages( $start = 0 ) {
11 $fname = 'compressOldPages';
14 print "Starting from old_id $start...\n";
15 $dbw =& wfGetDB( DB_MASTER );
16 $old = $dbw->tableName( 'old' );
18 $end = $start + $chunksize;
19 $res = $dbw->select( 'old', array( 'old_id','old_flags','old_namespace','old_title','old_text' ),
20 "old_id>=$start", $fname, array( 'ORDER BY' => 'old_id', 'LIMIT' => $chunksize, 'FOR UPDATE' ) );
21 if( $dbw->numRows( $res ) == 0 ) {
25 while( $row = $dbw->fetchObject( $res ) ) {
26 # print " {$row->old_id} - {$row->old_namespace}:{$row->old_title}\n";
30 $dbw->freeResult( $res );
31 $start = $last + 1; # Deletion may leave long empty stretches
36 function compressPage( $row ) {
37 $fname = 'compressPage';
38 if( false !== strpos( $row->old_flags, "gzip" ) ) {
39 print "Already compressed row {$row->old_id}?\n";
42 $dbw =& wfGetDB( DB_MASTER );
43 $flags = $row->old_flags ? "{$row->old_flags},gzip" : "gzip";
44 $compress = gzdeflate( $row->old_text );
47 'old_flags' => $flags,
48 'old_text' => $compress
50 'old_id' => $row->old_id
56 define( 'LS_INDIVIDUAL', 0 );
57 define( 'LS_CHUNKED', 1 );
59 function compressWithConcat( $startId, $maxChunkSize, $maxChunkFactor, $factorThreshold, $beginDate, $endDate )
61 $fname = 'compressWithConcat';
62 $loadStyle = LS_CHUNKED;
64 $dbw =& wfGetDB( DB_MASTER );
66 # First get a list of all pages
67 $pageRes = $dbw->select( 'page', 'page_id', false, $fname );
69 # For each of those, get a list of revisions which fit the criteria
72 $conds[] = "rev_timestamp>'" . $beginDate . "'";
75 $conds[] = "rev_timestamp<'" . $endDate . "'";
78 $conds[] = 'rev_id>=' . $startId;
80 if ( $loadStyle == LS_CHUNKED ) {
81 $tables = array( 'revision', 'text' );
82 $fields = array( 'rev_id', 'old_flags', 'old_text' );
83 $conds[] = 'rev_id=old_id';
84 $revLoadOptions = 'FOR UPDATE';
86 $tables = array( 'revision' );
87 $fields = array( 'rev_id' );
88 $revLoadOptions = array();
91 while ( $pageRow = $dbw->fetchObject( $pageRes ) ) {
93 $titleObj = Title::makeTitle( $pageRow->cur_namespace, $pageRow->cur_title );
94 print $titleObj->getPrefixedDBkey() . " ";
97 $revRes = $dbw->select( 'old', $fields,
98 array( 'rev_page' => $pageRow->page_id ) + $conds,
103 while ( $revRow = $dbw->fetchObject( $revRes ) ) {
107 if ( count( $revs ) < 2) {
108 # No revisions matching, no further processing
115 while ( $i < count( $revs ) ) {
116 if ( $i < count( $revs ) - $maxChunkSize ) {
117 $thisChunkSize = $maxChunkSize;
119 $thisChunkSize = count( $revs ) - $i;
122 $chunk = new ConcatenatedGzipHistoryBlob();
126 $primaryOldid = $revs[$i]->rev_id;
128 # Get the text of each revision and add it to the object
129 for ( $j = 0; $j < $thisChunkSize && $chunk->isHappy( $maxChunkFactor, $factorThreshold ); $j++ ) {
130 $oldid = $revs[$i + $j]->rev_id;
133 if ( $loadStyle == LS_INDIVIDUAL ) {
134 $textRow = $dbw->selectRow( 'text',
135 array( 'old_flags', 'old_text' ),
136 array( 'old_id' => $oldid ),
140 $text = Revision::getRevisionText( $textRow );
142 $text = Revision::getRevisionText( $revs[$i + $j] );
145 if ( $text === false ) {
146 print "\nError, unable to get text in old_id $oldid\n";
147 #$dbw->delete( 'old', array( 'old_id' => $oldid ) );
151 $chunk->setText( $text );
154 # Don't make a stub if it's going to be longer than the article
155 # Stubs are typically about 100 bytes
156 if ( strlen( $text ) < 120 ) {
160 $stub = $chunk->addItem( $text );
161 $stub->setLocation( $primaryOldid );
162 $hash = $stub->getHash();
163 $stub = serialize( $stub );
172 # If we couldn't actually use any stubs because the pages were too small, do nothing
174 # Store the main object
175 $dbw->update( 'text',
177 'old_text' => serialize( $chunk ),
178 'old_flags' => 'object',
179 ), array( /* WHERE */
180 'old_id' => $primaryOldid
184 # Store the stub objects
185 for ( $j = 1; $j < $thisChunkSize; $j++ ) {
186 # Skip if not compressing
187 if ( $stubs[$j] !== false ) {
188 $dbw->update( 'text',
190 'old_text' => $stubs[$j],
191 'old_flags' => 'object',
192 ), array( /* WHERE */
193 'old_id' => $revs[$i + $j]->old_id
202 $i += $thisChunkSize;