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