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