Don't lock page table rows
[lhc/web/wiklou.git] / maintenance / storage / compressOld.inc
1 <?php
2 /**
3 * @package MediaWiki
4 * @subpackage Maintenance
5 */
6
7 /** */
8 require_once( 'Revision.php' );
9 require_once( 'ExternalStoreDB.php' );
10
11 /** @todo document */
12 function compressOldPages( $start = 0, $extdb = '' ) {
13 $fname = 'compressOldPages';
14
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 );
35 }
36
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 );
47
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 }
57
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;
68 }
69
70 define( 'LS_INDIVIDUAL', 0 );
71 define( 'LS_CHUNKED', 1 );
72
73 /** @todo document */
74 function compressWithConcat( $startId, $maxChunkSize, $maxChunkFactor, $factorThreshold, $beginDate,
75 $endDate, $extdb="", $maxPageId = false )
76 {
77 $fname = 'compressWithConcat';
78 $loadStyle = LS_CHUNKED;
79
80 $dbr =& wfGetDB( DB_SLAVE );
81 $dbw =& wfGetDB( DB_MASTER );
82
83 # Set up external storage
84 if ( $extdb != '' ) {
85 $storeObj = new ExternalStoreDB;
86 }
87
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();
94
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 */
104
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%'");
113
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 }
130
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';
135
136 $oldReadsSinceLastSlaveWait = 0; #check slave lag periodically
137 $totalMatchingRevisions = 0;
138 $masterPos = false;
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(
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 = $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
277 if ( $stubs[$j] !== false ) {
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 }
300 ?>