Refactored and added orphan blob search
[lhc/web/wiklou.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 while( $row = $dbw->fetchObject( $res ) ) {
22 # print " {$row->old_id} - {$row->old_namespace}:{$row->old_title}\n";
23 compressPage( $row, $extdb );
24 $last = $row->old_id;
25 }
26 $dbw->freeResult( $res );
27 $start = $last + 1; # Deletion may leave long empty stretches
28 print "$start...\n";
29 } while( true );
30 }
31
32 /** @todo document */
33 function compressPage( $row, $extdb ) {
34 $fname = 'compressPage';
35 if ( false !== strpos( $row->old_flags, 'gzip' ) || false !== strpos( $row->old_flags, 'object' ) ) {
36 #print "Already compressed row {$row->old_id}\n";
37 return false;
38 }
39 $dbw = wfGetDB( DB_MASTER );
40 $flags = $row->old_flags ? "{$row->old_flags},gzip" : "gzip";
41 $compress = gzdeflate( $row->old_text );
42
43 # Store in external storage if required
44 if ( $extdb !== '' ) {
45 $storeObj = new ExternalStoreDB;
46 $compress = $storeObj->store( $extdb, $compress );
47 if ( $compress === false ) {
48 print "Unable to store object\n";
49 return false;
50 }
51 }
52
53 # Update text row
54 $dbw->update( 'text',
55 array( /* SET */
56 'old_flags' => $flags,
57 'old_text' => $compress
58 ), array( /* WHERE */
59 'old_id' => $row->old_id
60 ), $fname, '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, $maxChunkFactor, $factorThreshold, $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 LIKE '%object%' AND old_flags NOT LIKE '%external%'");
108
109 if ( $beginDate ) {
110 if ( !preg_match( '/^\d{14}$/', $beginDate ) ) {
111 print "Invalid begin date \"$beginDate\"\n";
112 return false;
113 }
114 $conds[] = "rev_timestamp>'" . $beginDate . "'";
115 }
116 if ( $endDate ) {
117 if ( !preg_match( '/^\d{14}$/', $endDate ) ) {
118 print "Invalid end date \"$endDate\"\n";
119 return false;
120 }
121 $conds[] = "rev_timestamp<'" . $endDate . "'";
122 }
123 if ( $loadStyle == LS_CHUNKED ) {
124 $tables = array( 'revision', 'text' );
125 $fields = array( 'rev_id', 'rev_text_id', 'old_flags', 'old_text' );
126 $conds[] = 'rev_text_id=old_id';
127 $revLoadOptions = 'FOR UPDATE';
128 } else {
129 $tables = array( 'revision' );
130 $fields = array( 'rev_id', 'rev_text_id' );
131 $revLoadOptions = array();
132 }
133
134 # Don't work with current revisions
135 # Don't lock the page table for update either -- TS 2006-04-04
136 #$tables[] = 'page';
137 #$conds[] = 'page_id=rev_page AND rev_id != page_latest';
138
139 for ( $pageId = $startId; $pageId <= $maxPageId; $pageId++ ) {
140 wfWaitForSlaves( 5 );
141
142 # Wake up
143 $dbr->ping();
144
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 );
153
154 # Display progress
155 $titleObj = Title::makeTitle( $pageRow->page_namespace, $pageRow->page_title );
156 print "$pageId\t" . $titleObj->getPrefixedDBkey() . " ";
157
158 # Load revisions
159 $revRes = $dbw->select( $tables, $fields,
160 array_merge( array(
161 'rev_page' => $pageRow->page_id,
162 # Don't operate on the current revision
163 # Use < instead of <> in case the current revision has changed
164 # since the page select, which wasn't locking
165 'rev_id < ' . $pageRow->page_latest
166 ), $conds ),
167 $fname,
168 $revLoadOptions
169 );
170 $revs = array();
171 while ( $revRow = $dbw->fetchObject( $revRes ) ) {
172 $revs[] = $revRow;
173 }
174
175 if ( count( $revs ) < 2) {
176 # No revisions matching, no further processing
177 print "\n";
178 continue;
179 }
180
181 # For each chunk
182 $i = 0;
183 while ( $i < count( $revs ) ) {
184 if ( $i < count( $revs ) - $maxChunkSize ) {
185 $thisChunkSize = $maxChunkSize;
186 } else {
187 $thisChunkSize = count( $revs ) - $i;
188 }
189
190 $chunk = new ConcatenatedGzipHistoryBlob();
191 $stubs = array();
192 $dbw->begin();
193 $usedChunk = false;
194 $primaryOldid = $revs[$i]->rev_text_id;
195
196 # Get the text of each revision and add it to the object
197 for ( $j = 0; $j < $thisChunkSize && $chunk->isHappy( $maxChunkFactor, $factorThreshold ); $j++ ) {
198 $oldid = $revs[$i + $j]->rev_text_id;
199
200 # Get text
201 if ( $loadStyle == LS_INDIVIDUAL ) {
202 $textRow = $dbw->selectRow( 'text',
203 array( 'old_flags', 'old_text' ),
204 array( 'old_id' => $oldid ),
205 $fname,
206 'FOR UPDATE'
207 );
208 $text = Revision::getRevisionText( $textRow );
209 } else {
210 $text = Revision::getRevisionText( $revs[$i + $j] );
211 }
212
213 if ( $text === false ) {
214 print "\nError, unable to get text in old_id $oldid\n";
215 #$dbw->delete( 'old', array( 'old_id' => $oldid ) );
216 }
217
218 if ( $extdb == "" && $j == 0 ) {
219 $chunk->setText( $text );
220 print '.';
221 } else {
222 # Don't make a stub if it's going to be longer than the article
223 # Stubs are typically about 100 bytes
224 if ( strlen( $text ) < 120 ) {
225 $stub = false;
226 print 'x';
227 } else {
228 $stub = new HistoryBlobStub( $chunk->addItem( $text ) );
229 $stub->setLocation( $primaryOldid );
230 $stub->setReferrer( $oldid );
231 print '.';
232 $usedChunk = true;
233 }
234 $stubs[$j] = $stub;
235 }
236 }
237 $thisChunkSize = $j;
238
239 # If we couldn't actually use any stubs because the pages were too small, do nothing
240 if ( $usedChunk ) {
241 if ( $extdb != "" ) {
242 # Move blob objects to External Storage
243 $stored = $storeObj->store( $extdb, serialize( $chunk ));
244 if ($stored === false) {
245 print "Unable to store object\n";
246 return false;
247 }
248 # Store External Storage URLs instead of Stub placeholders
249 foreach ($stubs as $stub) {
250 if ($stub===false)
251 continue;
252 # $stored should provide base path to a BLOB
253 $url = $stored."/".$stub->getHash();
254 $dbw->update( 'text',
255 array( /* SET */
256 'old_text' => $url,
257 'old_flags' => 'external,utf-8',
258 ), array ( /* WHERE */
259 'old_id' => $stub->getReferrer(),
260 )
261 );
262 }
263 } else {
264 # Store the main object locally
265 $dbw->update( 'text',
266 array( /* SET */
267 'old_text' => serialize( $chunk ),
268 'old_flags' => 'object,utf-8',
269 ), array( /* WHERE */
270 'old_id' => $primaryOldid
271 )
272 );
273
274 # Store the stub objects
275 for ( $j = 1; $j < $thisChunkSize; $j++ ) {
276 # Skip if not compressing and don't overwrite the first revision
277 if ( $stubs[$j] !== false && $revs[$i + $j]->rev_text_id != $primaryOldid ) {
278 $dbw->update( 'text',
279 array( /* SET */
280 'old_text' => serialize($stubs[$j]),
281 'old_flags' => 'object,utf-8',
282 ), array( /* WHERE */
283 'old_id' => $revs[$i + $j]->rev_text_id
284 )
285 );
286 }
287 }
288 }
289 }
290 # Done, next
291 print "/";
292 $dbw->commit();
293 $i += $thisChunkSize;
294 wfWaitForSlaves( 5 );
295 }
296 print "\n";
297 }
298 return true;
299 }