Extension to r89419, I was too excited fixing the bug
[mediawiki.git] / maintenance / storage / compressOld.inc
blob02e85cbd5785f2902381f871dc40c456129cf2af
1 <?php
2 /**
3  * Helper functions for compressOld.php script.
4  *
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.
9  *
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.
14  *
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
19  *
20  * @file
21  * @ingroup Maintenance ExternalStorage
22  */
24 /** @todo document */
25 function compressOldPages( $start = 0, $extdb = '' ) {
26         $fname = 'compressOldPages';
28         $chunksize = 50;
29         print "Starting from old_id $start...\n";
30         $dbw = wfGetDB( DB_MASTER );
31         do {
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 ) {
35                         break;
36                 }
37                 $last = $start;
38                 foreach ( $res as $row ) {
39                         # print "  {$row->old_id} - {$row->old_namespace}:{$row->old_title}\n";
40                         compressPage( $row, $extdb );
41                         $last = $row->old_id;
42                 }
43                 $start = $last + 1; # Deletion may leave long empty stretches
44                 print "$start...\n";
45         } while( true );
48 /** @todo document */
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";
53                 return false;
54         }
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";
65                         return false;
66                 }
67         }
69         # Update text row
70         $dbw->update( 'text',
71                 array( /* SET */
72                         'old_flags' => $flags,
73                         'old_text' => $compress
74                 ), array( /* WHERE */
75                         'old_id' => $row->old_id
76                 ), $fname,
77                 array( 'LIMIT' => 1 )
78         );
79         return true;
82 define( 'LS_INDIVIDUAL', 0 );
83 define( 'LS_CHUNKED', 1 );
85 /** @todo document */
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
96         if ( $extdb != '' ) {
97                 $storeObj = new ExternalStoreDB;
98         }
100         # Get all articles by page_id
101         if ( !$maxPageId ) {
102                 $maxPageId = $dbr->selectField( 'page', 'max(page_id)', '', $fname );
103         }
104         print "Starting from $startId of $maxPageId\n";
105         $pageConds = array();
107         /*
108         if ( $exclude_ns0 ) {
109                 print "Excluding main namespace\n";
110                 $pageConds[] = 'page_namespace<>0';
111         }
112         if ( $queryExtra ) {
113                                 $pageConds[] = $queryExtra;
114         }
115          */
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.
123         $conds = array(
124                 'old_flags NOT ' . $dbr->buildLike( $dbr->anyString(), 'object', $dbr->anyString() ) . ' AND old_flags NOT '
125                         . $dbr->buildLike( $dbr->anyString(), 'external', $dbr->anyString() ) );
127         if ( $beginDate ) {
128                 if ( !preg_match( '/^\d{14}$/', $beginDate ) ) {
129                         print "Invalid begin date \"$beginDate\"\n";
130                         return false;
131                 }
132                 $conds[] = "rev_timestamp>'" . $beginDate . "'";
133         }
134         if ( $endDate )  {
135                 if ( !preg_match( '/^\d{14}$/', $endDate ) ) {
136                         print "Invalid end date \"$endDate\"\n";
137                         return false;
138                 }
139                 $conds[] = "rev_timestamp<'" . $endDate . "'";
140         }
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';
146         } else {
147                 $tables = array( 'revision' );
148                 $fields = array( 'rev_id', 'rev_text_id' );
149                 $revLoadOptions = array();
150         }
152         # Don't work with current revisions
153         # Don't lock the page table for update either -- TS 2006-04-04
154         #$tables[] = 'page';
155         #$conds[] = 'page_id=rev_page AND rev_id != page_latest';
157         for ( $pageId = $startId; $pageId <= $maxPageId; $pageId++ ) {
158                 wfWaitForSlaves();
160                 # Wake up
161                 $dbr->ping();
163                 # Get the page row
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 ) {
168                         continue;
169                 }
170                 $pageRow = $dbr->fetchObject( $pageRes );
172                 # Display progress
173                 $titleObj = Title::makeTitle( $pageRow->page_namespace, $pageRow->page_title );
174                 print "$pageId\t" . $titleObj->getPrefixedDBkey() . " ";
176                 # Load revisions
177                 $revRes = $dbw->select( $tables, $fields,
178                         array_merge( array(
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
184                         ), $conds ),
185                         $fname,
186                         $revLoadOptions
187                 );
188                 $revs = array();
189                 foreach ( $revRes as $revRow ) {
190                         $revs[] = $revRow;
191                 }
193                 if ( count( $revs ) < 2) {
194                         # No revisions matching, no further processing
195                         print "\n";
196                         continue;
197                 }
199                 # For each chunk
200                 $i = 0;
201                 while ( $i < count( $revs ) ) {
202                         if ( $i < count( $revs ) - $maxChunkSize ) {
203                                 $thisChunkSize = $maxChunkSize;
204                         } else {
205                                 $thisChunkSize = count( $revs ) - $i;
206                         }
208                         $chunk = new ConcatenatedGzipHistoryBlob();
209                         $stubs = array();
210                         $dbw->begin();
211                         $usedChunk = false;
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;
218                                 # Get text
219                                 if ( $loadStyle == LS_INDIVIDUAL ) {
220                                         $textRow = $dbw->selectRow( 'text',
221                                                 array( 'old_flags', 'old_text' ),
222                                                 array( 'old_id' => $oldid ),
223                                                 $fname,
224                                                 'FOR UPDATE'
225                                         );
226                                         $text = Revision::getRevisionText( $textRow );
227                                 } else {
228                                         $text = Revision::getRevisionText( $revs[$i + $j] );
229                                 }
231                                 if ( $text === false ) {
232                                         print "\nError, unable to get text in old_id $oldid\n";
233                                         #$dbw->delete( 'old', array( 'old_id' => $oldid ) );
234                                 }
236                                 if ( $extdb == "" && $j == 0 ) {
237                                         $chunk->setText( $text );
238                                         print '.';
239                                 } else {
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 ) {
243                                                 $stub = false;
244                                                 print 'x';
245                                         } else {
246                                                 $stub = new HistoryBlobStub( $chunk->addItem( $text ) );
247                                                 $stub->setLocation( $primaryOldid );
248                                                 $stub->setReferrer( $oldid );
249                                                 print '.';
250                                                 $usedChunk = true;
251                                         }
252                                         $stubs[$j] = $stub;
253                                 }
254                         }
255                         $thisChunkSize = $j;
257                         # If we couldn't actually use any stubs because the pages were too small, do nothing
258                         if ( $usedChunk ) {
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";
264                                                 return false;
265                                         }
266                                         # Store External Storage URLs instead of Stub placeholders
267                                         foreach ($stubs as $stub) {
268                                                 if ($stub===false)
269                                                         continue;
270                                                 # $stored should provide base path to a BLOB
271                                                 $url = $stored."/".$stub->getHash();
272                                                 $dbw->update( 'text',
273                                                         array( /* SET */
274                                                                 'old_text' => $url,
275                                                                 'old_flags' => 'external,utf-8',
276                                                         ), array ( /* WHERE */
277                                                                 'old_id' => $stub->getReferrer(),
278                                                         )
279                                                 );
280                                         }
281                                 } else {
282                                         # Store the main object locally
283                                         $dbw->update( 'text',
284                                                 array( /* SET */
285                                                         'old_text' => serialize( $chunk ),
286                                                         'old_flags' => 'object,utf-8',
287                                                 ), array( /* WHERE */
288                                                         'old_id' => $primaryOldid
289                                                 )
290                                         );
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',
297                                                                 array( /* SET */
298                                                                         'old_text' => serialize($stubs[$j]),
299                                                                         'old_flags' => 'object,utf-8',
300                                                                 ), array( /* WHERE */
301                                                                         'old_id' => $revs[$i + $j]->rev_text_id
302                                                                 )
303                                                         );
304                                                 }
305                                         }
306                                 }
307                         }
308                         # Done, next
309                         print "/";
310                         $dbw->commit();
311                         $i += $thisChunkSize;
312                         wfWaitForSlaves();
313                 }
314                 print "\n";
315         }
316         return true;