Live fixes: lots of scary magic runes; php 5 fixes; reporting; etc
[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 $tables[] = 'page';
133 $conds[] = 'page_id=rev_page AND rev_id != page_latest';
134
135 $oldReadsSinceLastSlaveWait = 0; #check slave lag periodically
136 $totalMatchingRevisions = 0;
137 $masterPos = false;
138 for ( $pageId = $startId; $pageId <= $maxPageId; $pageId++ ) {
139 wfWaitForSlaves( 5 );
140
141 # Wake up
142 $dbr->ping();
143
144 # Get the page row
145 $pageRes = $dbr->select( 'page', array('page_id', 'page_namespace', 'page_title'),
146 $pageConds + array('page_id' => $pageId), $fname );
147 if ( $dbr->numRows( $pageRes ) == 0 ) {
148 continue;
149 }
150 $pageRow = $dbr->fetchObject( $pageRes );
151
152 # Display progress
153 $titleObj = Title::makeTitle( $pageRow->page_namespace, $pageRow->page_title );
154 print "$pageId\t" . $titleObj->getPrefixedDBkey() . " ";
155
156 # Load revisions
157 $revRes = $dbw->select( $tables, $fields,
158 array( 'rev_page' => $pageRow->page_id ) + $conds,
159 $fname,
160 $revLoadOptions
161 );
162 $revs = array();
163 while ( $revRow = $dbw->fetchObject( $revRes ) ) {
164 $revs[] = $revRow;
165 }
166
167 if ( count( $revs ) < 2) {
168 # No revisions matching, no further processing
169 print "\n";
170 continue;
171 }
172
173 # For each chunk
174 $i = 0;
175 while ( $i < count( $revs ) ) {
176 if ( $i < count( $revs ) - $maxChunkSize ) {
177 $thisChunkSize = $maxChunkSize;
178 } else {
179 $thisChunkSize = count( $revs ) - $i;
180 }
181
182 $chunk = new ConcatenatedGzipHistoryBlob();
183 $stubs = array();
184 $dbw->begin();
185 $usedChunk = false;
186 $primaryOldid = $revs[$i]->rev_text_id;
187
188 # Get the text of each revision and add it to the object
189 for ( $j = 0; $j < $thisChunkSize && $chunk->isHappy( $maxChunkFactor, $factorThreshold ); $j++ ) {
190 $oldid = $revs[$i + $j]->rev_text_id;
191
192 # Get text
193 if ( $loadStyle == LS_INDIVIDUAL ) {
194 $textRow = $dbw->selectRow( 'text',
195 array( 'old_flags', 'old_text' ),
196 array( 'old_id' => $oldid ),
197 $fname,
198 'FOR UPDATE'
199 );
200 $text = Revision::getRevisionText( $textRow );
201 } else {
202 $text = Revision::getRevisionText( $revs[$i + $j] );
203 }
204
205 if ( $text === false ) {
206 print "\nError, unable to get text in old_id $oldid\n";
207 #$dbw->delete( 'old', array( 'old_id' => $oldid ) );
208 }
209
210 if ( $extdb == "" && $j == 0 ) {
211 $chunk->setText( $text );
212 print '.';
213 } else {
214 # Don't make a stub if it's going to be longer than the article
215 # Stubs are typically about 100 bytes
216 if ( strlen( $text ) < 120 ) {
217 $stub = false;
218 print 'x';
219 } else {
220 $stub = $chunk->addItem( $text );
221 $stub->setLocation( $primaryOldid );
222 $stub->setReferrer( $oldid );
223 print '.';
224 $usedChunk = true;
225 }
226 $stubs[$j] = $stub;
227 }
228 }
229 $thisChunkSize = $j;
230
231 # If we couldn't actually use any stubs because the pages were too small, do nothing
232 if ( $usedChunk ) {
233 if ( $extdb != "" ) {
234 # Move blob objects to External Storage
235 $stored = $storeObj->store( $extdb, serialize( $chunk ));
236 if ($stored === false) {
237 print "Unable to store object\n";
238 return false;
239 }
240 # Store External Storage URLs instead of Stub placeholders
241 foreach ($stubs as $stub) {
242 if ($stub===false)
243 continue;
244 # $stored should provide base path to a BLOB
245 $url = $stored."/".$stub->getHash();
246 $dbw->update( 'text',
247 array( /* SET */
248 'old_text' => $url,
249 'old_flags' => 'external,utf-8',
250 ), array ( /* WHERE */
251 'old_id' => $stub->getReferrer(),
252 )
253 );
254 }
255 } else {
256 # Store the main object locally
257 $dbw->update( 'text',
258 array( /* SET */
259 'old_text' => serialize( $chunk ),
260 'old_flags' => 'object,utf-8',
261 ), array( /* WHERE */
262 'old_id' => $primaryOldid
263 )
264 );
265
266 # Store the stub objects
267 for ( $j = 1; $j < $thisChunkSize; $j++ ) {
268 # Skip if not compressing
269 if ( $stubs[$j] !== false ) {
270 $dbw->update( 'text',
271 array( /* SET */
272 'old_text' => serialize($stubs[$j]),
273 'old_flags' => 'object,utf-8',
274 ), array( /* WHERE */
275 'old_id' => $revs[$i + $j]->rev_text_id
276 )
277 );
278 }
279 }
280 }
281 }
282 # Done, next
283 print "/";
284 $dbw->commit();
285 $i += $thisChunkSize;
286 wfWaitForSlaves( 5 );
287 }
288 print "\n";
289 }
290 return true;
291 }
292 ?>