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