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