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