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