Localisation updates for cpre messages from Betawiki (2008-07-11 12:26 CEST)
[mediawiki.git] / maintenance / storage / compressOld.inc
blob52b9c40b2e952bc8c0d101334c3e35ce93316c56
1 <?php
2 /**
3  * @file
4  * @ingroup Maintenance ExternalStorage
5  */
7 /** */
8 require_once( 'Revision.php' );
9 require_once( 'ExternalStoreDB.php' );
11 /** @todo document */
12 function compressOldPages( $start = 0, $extdb = '' ) {
13         $fname = 'compressOldPages';
15         $chunksize = 50;
16         print "Starting from old_id $start...\n";
17         $dbw = wfGetDB( DB_MASTER );
18         do {
19                 $res = $dbw->select( 'text', array( 'old_id','old_flags','old_text' ),
20                         "old_id>=$start", $fname, array( 'ORDER BY' => 'old_id', 'LIMIT' => $chunksize, 'FOR UPDATE' ) );
21                 if( $dbw->numRows( $res ) == 0 ) {
22                         break;
23                 }
24                 $last = $start;
25                 while( $row = $dbw->fetchObject( $res ) ) {
26                         # print "  {$row->old_id} - {$row->old_namespace}:{$row->old_title}\n";
27                         compressPage( $row, $extdb );
28                         $last = $row->old_id;
29                 }
30                 $dbw->freeResult( $res );
31                 $start = $last + 1; # Deletion may leave long empty stretches
32                 print "$start...\n";
33         } while( true );
36 /** @todo document */
37 function compressPage( $row, $extdb ) {
38         $fname = 'compressPage';
39         if ( false !== strpos( $row->old_flags, 'gzip' ) || false !== strpos( $row->old_flags, 'object' ) ) {
40                 #print "Already compressed row {$row->old_id}\n";
41                 return false;
42         }
43         $dbw = wfGetDB( DB_MASTER );
44         $flags = $row->old_flags ? "{$row->old_flags},gzip" : "gzip";
45         $compress = gzdeflate( $row->old_text );
47         # Store in external storage if required
48         if ( $extdb !== '' ) {
49                 $storeObj = new ExternalStoreDB;
50                 $compress = $storeObj->store( $extdb, $compress );
51                 if ( $compress === false ) {
52                         print "Unable to store object\n";
53                         return false;
54                 }
55         }
57         # Update text row
58         $dbw->update( 'text',
59                 array( /* SET */
60                         'old_flags' => $flags,
61                         'old_text' => $compress
62                 ), array( /* WHERE */
63                         'old_id' => $row->old_id
64                 ), $fname, 'LIMIT 1'
65         );
66         return true;
69 define( 'LS_INDIVIDUAL', 0 );
70 define( 'LS_CHUNKED', 1 );
72 /** @todo document */
73 function compressWithConcat( $startId, $maxChunkSize, $maxChunkFactor, $factorThreshold, $beginDate,
74         $endDate, $extdb="", $maxPageId = false )
76         $fname = 'compressWithConcat';
77         $loadStyle = LS_CHUNKED;
79         $dbr = wfGetDB( DB_SLAVE );
80         $dbw = wfGetDB( DB_MASTER );
82         # Set up external storage
83         if ( $extdb != '' ) {
84                 $storeObj = new ExternalStoreDB;
85         }
87         # Get all articles by page_id
88         if ( !$maxPageId ) {
89                 $maxPageId = $dbr->selectField( 'page', 'max(page_id)', '', $fname );
90         }
91         print "Starting from $startId of $maxPageId\n";
92         $pageConds = array();
94         /*
95         if ( $exclude_ns0 ) {
96                 print "Excluding main namespace\n";
97                 $pageConds[] = 'page_namespace<>0';
98         }
99         if ( $queryExtra ) {
100                 $pageConds[] = $queryExtra;
101         }
102          */
104         # For each article, get a list of revisions which fit the criteria
105         
106         # No recompression, use a condition on old_flags
107         # Don't compress object type entities, because that might produce data loss when
108         # overwriting bulk storage concat rows. Don't compress external references, because
109         # the script doesn't yet delete rows from external storage.
110         $conds = array(
111                 "old_flags NOT LIKE '%object%' AND old_flags NOT LIKE '%external%'");
113         if ( $beginDate ) {
114                 if ( !preg_match( '/^\d{14}$/', $beginDate ) ) {
115                         print "Invalid begin date \"$beginDate\"\n";
116                         return false;
117                 }
118                 $conds[] = "rev_timestamp>'" . $beginDate . "'";
119         }
120         if ( $endDate )  {
121                 if ( !preg_match( '/^\d{14}$/', $endDate ) ) {
122                         print "Invalid end date \"$endDate\"\n";
123                         return false;
124                 }
125                 $conds[] = "rev_timestamp<'" . $endDate . "'";
126         }
127         if ( $loadStyle == LS_CHUNKED ) {
128                 $tables = array( 'revision', 'text' );
129                 $fields = array( 'rev_id', 'rev_text_id', 'old_flags', 'old_text' );
130                 $conds[] = 'rev_text_id=old_id';
131                 $revLoadOptions = 'FOR UPDATE';
132         } else {
133                 $tables = array( 'revision' );
134                 $fields = array( 'rev_id', 'rev_text_id' );
135                 $revLoadOptions = array();
136         }
138         # Don't work with current revisions
139         # Don't lock the page table for update either -- TS 2006-04-04
140         #$tables[] = 'page';
141         #$conds[] = 'page_id=rev_page AND rev_id != page_latest';
143         for ( $pageId = $startId; $pageId <= $maxPageId; $pageId++ ) {
144                 wfWaitForSlaves( 5 );
146                 # Wake up
147                 $dbr->ping();           
149                 # Get the page row
150                 $pageRes = $dbr->select( 'page', 
151                         array('page_id', 'page_namespace', 'page_title','page_latest'),
152                         $pageConds + array('page_id' => $pageId), $fname );
153                 if ( $dbr->numRows( $pageRes ) == 0 ) {
154                         continue;
155                 }
156                 $pageRow = $dbr->fetchObject( $pageRes );
158                 # Display progress
159                 $titleObj = Title::makeTitle( $pageRow->page_namespace, $pageRow->page_title );
160                 print "$pageId\t" . $titleObj->getPrefixedDBkey() . " ";
162                 # Load revisions
163                 $revRes = $dbw->select( $tables, $fields,
164                         array_merge( array( 
165                                 'rev_page' => $pageRow->page_id, 
166                                 # Don't operate on the current revision
167                                 # Use < instead of <> in case the current revision has changed 
168                                 # since the page select, which wasn't locking
169                                 'rev_id < ' . $pageRow->page_latest
170                         ), $conds ),
171                         $fname,
172                         $revLoadOptions
173                 );
174                 $revs = array();
175                 while ( $revRow = $dbw->fetchObject( $revRes ) ) {
176                         $revs[] = $revRow;
177                 }
179                 if ( count( $revs ) < 2) {
180                         # No revisions matching, no further processing
181                         print "\n";
182                         continue;
183                 }
185                 # For each chunk
186                 $i = 0;
187                 while ( $i < count( $revs ) ) {
188                         if ( $i < count( $revs ) - $maxChunkSize ) {
189                                 $thisChunkSize = $maxChunkSize;
190                         } else {
191                                 $thisChunkSize = count( $revs ) - $i;
192                         }
194                         $chunk = new ConcatenatedGzipHistoryBlob();
195                         $stubs = array();
196                         $dbw->begin();
197                         $usedChunk = false;
198                         $primaryOldid = $revs[$i]->rev_text_id;
200                         # Get the text of each revision and add it to the object
201                         for ( $j = 0; $j < $thisChunkSize && $chunk->isHappy( $maxChunkFactor, $factorThreshold ); $j++ ) {
202                                 $oldid = $revs[$i + $j]->rev_text_id;
204                                 # Get text
205                                 if ( $loadStyle == LS_INDIVIDUAL ) {
206                                         $textRow = $dbw->selectRow( 'text',
207                                                 array( 'old_flags', 'old_text' ),
208                                                 array( 'old_id' => $oldid ),
209                                                 $fname,
210                                                 'FOR UPDATE'
211                                         );
212                                         $text = Revision::getRevisionText( $textRow );
213                                 } else {
214                                         $text = Revision::getRevisionText( $revs[$i + $j] );
215                                 }
217                                 if ( $text === false ) {
218                                         print "\nError, unable to get text in old_id $oldid\n";
219                                         #$dbw->delete( 'old', array( 'old_id' => $oldid ) );
220                                 }
222                                 if ( $extdb == "" && $j == 0 ) {
223                                         $chunk->setText( $text );
224                                         print '.';
225                                 } else {
226                                         # Don't make a stub if it's going to be longer than the article
227                                         # Stubs are typically about 100 bytes
228                                         if ( strlen( $text ) < 120 ) {
229                                                 $stub = false;
230                                                 print 'x';
231                                         } else {
232                                                 $stub = $chunk->addItem( $text );
233                                                 $stub->setLocation( $primaryOldid );
234                                                 $stub->setReferrer( $oldid );
235                                                 print '.';
236                                                 $usedChunk = true;
237                                         }
238                                         $stubs[$j] = $stub;
239                                 }
240                         }
241                         $thisChunkSize = $j;
243                         # If we couldn't actually use any stubs because the pages were too small, do nothing
244                         if ( $usedChunk ) {
245                                 if ( $extdb != "" ) {
246                                         # Move blob objects to External Storage
247                                         $stored = $storeObj->store( $extdb, serialize( $chunk ));
248                                         if ($stored === false) {
249                                                 print "Unable to store object\n";
250                                                 return false;
251                                         }
252                                         # Store External Storage URLs instead of Stub placeholders
253                                         foreach ($stubs as $stub) {
254                                                 if ($stub===false)
255                                                         continue;
256                                                 # $stored should provide base path to a BLOB
257                                                 $url = $stored."/".$stub->getHash();
258                                                 $dbw->update( 'text',
259                                                         array( /* SET */
260                                                                 'old_text' => $url,
261                                                                 'old_flags' => 'external,utf-8',
262                                                         ), array ( /* WHERE */
263                                                                 'old_id' => $stub->getReferrer(),
264                                                         )
265                                                 );
266                                         }
267                                 } else {
268                                         # Store the main object locally
269                                         $dbw->update( 'text',
270                                                 array( /* SET */
271                                                         'old_text' => serialize( $chunk ),
272                                                         'old_flags' => 'object,utf-8',
273                                                 ), array( /* WHERE */
274                                                         'old_id' => $primaryOldid
275                                                 )
276                                         );
278                                         # Store the stub objects
279                                         for ( $j = 1; $j < $thisChunkSize; $j++ ) {
280                                                 # Skip if not compressing and don't overwrite the first revision
281                                                 if ( $stubs[$j] !== false && $revs[$i + $j]->rev_text_id != $primaryOldid ) {
282                                                         $dbw->update( 'text',
283                                                                 array( /* SET */
284                                                                         'old_text' => serialize($stubs[$j]),
285                                                                         'old_flags' => 'object,utf-8',
286                                                                 ), array( /* WHERE */
287                                                                         'old_id' => $revs[$i + $j]->rev_text_id
288                                                                 )
289                                                         );
290                                                 }
291                                         }
292                                 }
293                         }
294                         # Done, next
295                         print "/";
296                         $dbw->commit();
297                         $i += $thisChunkSize;
298                         wfWaitForSlaves( 5 );
299                 }
300                 print "\n";
301         }
302         return true;