updated for 1.5/1.6
[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 )
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 $oldReadsSinceLastSlaveWait = 0; #check slave lag periodically
120 $totalMatchingRevisions = 0;
121 $masterPos = false;
122 for ( $pageId = $startId; $pageId <= $maxPageId; $pageId++ ) {
123 $pageRes = $dbr->select( 'page', array('page_id', 'page_namespace', 'page_title'),
124 $pageConds + array('page_id' => $pageId), $fname );
125 if ( $dbr->numRows( $pageRes ) == 0 ) {
126 continue;
127 }
128 $pageRow = $dbr->fetchObject( $pageRes );
129
130 # Display progress
131 $titleObj = Title::makeTitle( $pageRow->page_namespace, $pageRow->page_title );
132 print "$pageId\t" . $titleObj->getPrefixedDBkey() . " ";
133
134 # Load revisions
135 $revRes = $dbw->select( $tables, $fields,
136 array( 'rev_page' => $pageRow->page_id ) + $conds,
137 $fname,
138 $revLoadOptions
139 );
140 $revs = array();
141 while ( $revRow = $dbw->fetchObject( $revRes ) ) {
142 $revs[] = $revRow;
143 }
144
145 if ( count( $revs ) < 2) {
146 # No revisions matching, no further processing
147 print "\n";
148 continue;
149 }
150
151 # For each chunk
152 $i = 0;
153 while ( $i < count( $revs ) ) {
154 if ( $i < count( $revs ) - $maxChunkSize ) {
155 $thisChunkSize = $maxChunkSize;
156 } else {
157 $thisChunkSize = count( $revs ) - $i;
158 }
159
160 $chunk = new ConcatenatedGzipHistoryBlob();
161 $stubs = array();
162 $dbw->begin();
163 $usedChunk = false;
164 $primaryOldid = $revs[$i]->rev_text_id;
165
166 # Get the text of each revision and add it to the object
167 for ( $j = 0; $j < $thisChunkSize && $chunk->isHappy( $maxChunkFactor, $factorThreshold ); $j++ ) {
168 $oldid = $revs[$i + $j]->rev_text_id;
169
170 # Get text
171 if ( $loadStyle == LS_INDIVIDUAL ) {
172 $textRow = $dbw->selectRow( 'text',
173 array( 'old_flags', 'old_text' ),
174 array( 'old_id' => $oldid ),
175 $fname,
176 'FOR UPDATE'
177 );
178 $text = Revision::getRevisionText( $textRow );
179 } else {
180 $text = Revision::getRevisionText( $revs[$i + $j] );
181 }
182
183 if ( $text === false ) {
184 print "\nError, unable to get text in old_id $oldid\n";
185 #$dbw->delete( 'old', array( 'old_id' => $oldid ) );
186 }
187
188 if ( $j == 0 ) {
189 $chunk->setText( $text );
190 print '.';
191 } else {
192 # Don't make a stub if it's going to be longer than the article
193 # Stubs are typically about 100 bytes
194 if ( strlen( $text ) < 120 ) {
195 $stub = false;
196 print 'x';
197 } else {
198 $stub = $chunk->addItem( $text );
199 $stub->setLocation( $primaryOldid );
200 $hash = $stub->getHash();
201 $stub = serialize( $stub );
202 print '.';
203 $usedChunk = true;
204 }
205 $stubs[$j] = $stub;
206 }
207 }
208 $thisChunkSize = $j;
209
210 # If we couldn't actually use any stubs because the pages were too small, do nothing
211 if ( $usedChunk ) {
212 # Store the main object
213 $dbw->update( 'text',
214 array( /* SET */
215 'old_text' => serialize( $chunk ),
216 'old_flags' => 'object,utf-8',
217 ), array( /* WHERE */
218 'old_id' => $primaryOldid
219 )
220 );
221
222 # Store the stub objects
223 for ( $j = 1; $j < $thisChunkSize; $j++ ) {
224 # Skip if not compressing
225 if ( $stubs[$j] !== false ) {
226 $dbw->update( 'text',
227 array( /* SET */
228 'old_text' => $stubs[$j],
229 'old_flags' => 'object,utf-8',
230 ), array( /* WHERE */
231 'old_id' => $revs[$i + $j]->rev_text_id
232 )
233 );
234 }
235 }
236 }
237 # Done, next
238 print "/";
239 $dbw->commit();
240 $i += $thisChunkSize;
241 }
242 print "\n";
243 }
244 return true;
245 }
246 ?>