3 * Helper functions for compressOld.php script.
5 * This program is free software; you can redistribute it and/or modify
6 * it under the terms of the GNU General Public License as published by
7 * the Free Software Foundation; either version 2 of the License, or
8 * (at your option) any later version.
10 * This program is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 * GNU General Public License for more details.
15 * You should have received a copy of the GNU General Public License along
16 * with this program; if not, write to the Free Software Foundation, Inc.,
17 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
18 * http://www.gnu.org/copyleft/gpl.html
21 * @ingroup Maintenance ExternalStorage
25 function compressOldPages( $start = 0, $extdb = '' ) {
26 $fname = 'compressOldPages';
29 print "Starting from old_id $start...\n";
30 $dbw = wfGetDB( DB_MASTER );
32 $res = $dbw->select( 'text', array( 'old_id','old_flags','old_text' ),
33 "old_id>=$start", $fname, array( 'ORDER BY' => 'old_id', 'LIMIT' => $chunksize, 'FOR UPDATE' ) );
34 if( $dbw->numRows( $res ) == 0 ) {
38 foreach ( $res as $row ) {
39 # print " {$row->old_id} - {$row->old_namespace}:{$row->old_title}\n";
40 compressPage( $row, $extdb );
43 $start = $last + 1; # Deletion may leave long empty stretches
49 function compressPage( $row, $extdb ) {
50 $fname = 'compressPage';
51 if ( false !== strpos( $row->old_flags, 'gzip' ) || false !== strpos( $row->old_flags, 'object' ) ) {
52 #print "Already compressed row {$row->old_id}\n";
55 $dbw = wfGetDB( DB_MASTER );
56 $flags = $row->old_flags ? "{$row->old_flags},gzip" : "gzip";
57 $compress = gzdeflate( $row->old_text );
59 # Store in external storage if required
60 if ( $extdb !== '' ) {
61 $storeObj = new ExternalStoreDB;
62 $compress = $storeObj->store( $extdb, $compress );
63 if ( $compress === false ) {
64 print "Unable to store object\n";
72 'old_flags' => $flags,
73 'old_text' => $compress
75 'old_id' => $row->old_id
82 define( 'LS_INDIVIDUAL', 0 );
83 define( 'LS_CHUNKED', 1 );
86 function compressWithConcat( $startId, $maxChunkSize, $beginDate,
87 $endDate, $extdb="", $maxPageId = false )
89 $fname = 'compressWithConcat';
90 $loadStyle = LS_CHUNKED;
92 $dbr = wfGetDB( DB_SLAVE );
93 $dbw = wfGetDB( DB_MASTER );
95 # Set up external storage
97 $storeObj = new ExternalStoreDB;
100 # Get all articles by page_id
102 $maxPageId = $dbr->selectField( 'page', 'max(page_id)', '', $fname );
104 print "Starting from $startId of $maxPageId\n";
105 $pageConds = array();
108 if ( $exclude_ns0 ) {
109 print "Excluding main namespace\n";
110 $pageConds[] = 'page_namespace<>0';
113 $pageConds[] = $queryExtra;
117 # For each article, get a list of revisions which fit the criteria
119 # No recompression, use a condition on old_flags
120 # Don't compress object type entities, because that might produce data loss when
121 # overwriting bulk storage concat rows. Don't compress external references, because
122 # the script doesn't yet delete rows from external storage.
124 'old_flags NOT ' . $dbr->buildLike( $dbr->anyString(), 'object', $dbr->anyString() ) . ' AND old_flags NOT '
125 . $dbr->buildLike( $dbr->anyString(), 'external', $dbr->anyString() ) );
128 if ( !preg_match( '/^\d{14}$/', $beginDate ) ) {
129 print "Invalid begin date \"$beginDate\"\n";
132 $conds[] = "rev_timestamp>'" . $beginDate . "'";
135 if ( !preg_match( '/^\d{14}$/', $endDate ) ) {
136 print "Invalid end date \"$endDate\"\n";
139 $conds[] = "rev_timestamp<'" . $endDate . "'";
141 if ( $loadStyle == LS_CHUNKED ) {
142 $tables = array( 'revision', 'text' );
143 $fields = array( 'rev_id', 'rev_text_id', 'old_flags', 'old_text' );
144 $conds[] = 'rev_text_id=old_id';
145 $revLoadOptions = 'FOR UPDATE';
147 $tables = array( 'revision' );
148 $fields = array( 'rev_id', 'rev_text_id' );
149 $revLoadOptions = array();
152 # Don't work with current revisions
153 # Don't lock the page table for update either -- TS 2006-04-04
155 #$conds[] = 'page_id=rev_page AND rev_id != page_latest';
157 for ( $pageId = $startId; $pageId <= $maxPageId; $pageId++ ) {
164 $pageRes = $dbr->select( 'page',
165 array('page_id', 'page_namespace', 'page_title','page_latest'),
166 $pageConds + array('page_id' => $pageId), $fname );
167 if ( $dbr->numRows( $pageRes ) == 0 ) {
170 $pageRow = $dbr->fetchObject( $pageRes );
173 $titleObj = Title::makeTitle( $pageRow->page_namespace, $pageRow->page_title );
174 print "$pageId\t" . $titleObj->getPrefixedDBkey() . " ";
177 $revRes = $dbw->select( $tables, $fields,
179 'rev_page' => $pageRow->page_id,
180 # Don't operate on the current revision
181 # Use < instead of <> in case the current revision has changed
182 # since the page select, which wasn't locking
183 'rev_id < ' . $pageRow->page_latest
189 foreach ( $revRes as $revRow ) {
193 if ( count( $revs ) < 2) {
194 # No revisions matching, no further processing
201 while ( $i < count( $revs ) ) {
202 if ( $i < count( $revs ) - $maxChunkSize ) {
203 $thisChunkSize = $maxChunkSize;
205 $thisChunkSize = count( $revs ) - $i;
208 $chunk = new ConcatenatedGzipHistoryBlob();
212 $primaryOldid = $revs[$i]->rev_text_id;
214 # Get the text of each revision and add it to the object
215 for ( $j = 0; $j < $thisChunkSize && $chunk->isHappy(); $j++ ) {
216 $oldid = $revs[$i + $j]->rev_text_id;
219 if ( $loadStyle == LS_INDIVIDUAL ) {
220 $textRow = $dbw->selectRow( 'text',
221 array( 'old_flags', 'old_text' ),
222 array( 'old_id' => $oldid ),
226 $text = Revision::getRevisionText( $textRow );
228 $text = Revision::getRevisionText( $revs[$i + $j] );
231 if ( $text === false ) {
232 print "\nError, unable to get text in old_id $oldid\n";
233 #$dbw->delete( 'old', array( 'old_id' => $oldid ) );
236 if ( $extdb == "" && $j == 0 ) {
237 $chunk->setText( $text );
240 # Don't make a stub if it's going to be longer than the article
241 # Stubs are typically about 100 bytes
242 if ( strlen( $text ) < 120 ) {
246 $stub = new HistoryBlobStub( $chunk->addItem( $text ) );
247 $stub->setLocation( $primaryOldid );
248 $stub->setReferrer( $oldid );
257 # If we couldn't actually use any stubs because the pages were too small, do nothing
259 if ( $extdb != "" ) {
260 # Move blob objects to External Storage
261 $stored = $storeObj->store( $extdb, serialize( $chunk ));
262 if ($stored === false) {
263 print "Unable to store object\n";
266 # Store External Storage URLs instead of Stub placeholders
267 foreach ($stubs as $stub) {
270 # $stored should provide base path to a BLOB
271 $url = $stored."/".$stub->getHash();
272 $dbw->update( 'text',
275 'old_flags' => 'external,utf-8',
276 ), array ( /* WHERE */
277 'old_id' => $stub->getReferrer(),
282 # Store the main object locally
283 $dbw->update( 'text',
285 'old_text' => serialize( $chunk ),
286 'old_flags' => 'object,utf-8',
287 ), array( /* WHERE */
288 'old_id' => $primaryOldid
292 # Store the stub objects
293 for ( $j = 1; $j < $thisChunkSize; $j++ ) {
294 # Skip if not compressing and don't overwrite the first revision
295 if ( $stubs[$j] !== false && $revs[$i + $j]->rev_text_id != $primaryOldid ) {
296 $dbw->update( 'text',
298 'old_text' => serialize($stubs[$j]),
299 'old_flags' => 'object,utf-8',
300 ), array( /* WHERE */
301 'old_id' => $revs[$i + $j]->rev_text_id
311 $i += $thisChunkSize;