Replacing var keyword with private / public as we now require PHP5.
[lhc/web/wiklou.git] / includes / HistoryBlob.php
1 <?php
2 /**
3 *
4 * @package MediaWiki
5 */
6
7 /**
8 * Pure virtual parent
9 * @package MediaWiki
10 */
11 class HistoryBlob
12 {
13 /**
14 * setMeta and getMeta currently aren't used for anything, I just thought
15 * they might be useful in the future.
16 * @param $meta String: a single string.
17 */
18 function setMeta( $meta ) {}
19
20 /**
21 * setMeta and getMeta currently aren't used for anything, I just thought
22 * they might be useful in the future.
23 * Gets the meta-value
24 */
25 function getMeta() {}
26
27 /**
28 * Adds an item of text, returns a stub object which points to the item.
29 * You must call setLocation() on the stub object before storing it to the
30 * database
31 */
32 function addItem() {}
33
34 /**
35 * Get item by hash
36 */
37 function getItem( $hash ) {}
38
39 # Set the "default text"
40 # This concept is an odd property of the current DB schema, whereby each text item has a revision
41 # associated with it. The default text is the text of the associated revision. There may, however,
42 # be other revisions in the same object
43 function setText() {}
44
45 /**
46 * Get default text. This is called from Revision::getRevisionText()
47 */
48 function getText() {}
49 }
50
51 /**
52 * The real object
53 * @package MediaWiki
54 */
55 class ConcatenatedGzipHistoryBlob extends HistoryBlob {
56 private
57 $mCompressed = false,
58 $mDefaultHash = '',
59 $mFast = 0,
60 $mItems = array(),
61 $mSize = 0,
62 $mVersion = 0 ;
63
64 function ConcatenatedGzipHistoryBlob() {
65 if ( !function_exists( 'gzdeflate' ) ) {
66 wfDie( "Need zlib support to read or write this kind of history object (ConcatenatedGzipHistoryBlob)\n" );
67 }
68 }
69
70 /** @todo document */
71 function setMeta( $metaData ) {
72 $this->uncompress();
73 $this->mItems['meta'] = $metaData;
74 }
75
76 /** @todo document */
77 function getMeta() {
78 $this->uncompress();
79 return $this->mItems['meta'];
80 }
81
82 /** @todo document */
83 function addItem( $text ) {
84 $this->uncompress();
85 $hash = md5( $text );
86 $this->mItems[$hash] = $text;
87 $this->mSize += strlen( $text );
88
89 $stub = new HistoryBlobStub( $hash );
90 return $stub;
91 }
92
93 /** @todo document */
94 function getItem( $hash ) {
95 $this->uncompress();
96 if ( array_key_exists( $hash, $this->mItems ) ) {
97 return $this->mItems[$hash];
98 } else {
99 return false;
100 }
101 }
102
103 /** @todo document */
104 function removeItem( $hash ) {
105 $this->mSize -= strlen( $this->mItems[$hash] );
106 unset( $this->mItems[$hash] );
107 }
108
109 /** @todo document */
110 function compress() {
111 if ( !$this->mCompressed ) {
112 $this->mItems = gzdeflate( serialize( $this->mItems ) );
113 $this->mCompressed = true;
114 }
115 }
116
117 /** @todo document */
118 function uncompress() {
119 if ( $this->mCompressed ) {
120 $this->mItems = unserialize( gzinflate( $this->mItems ) );
121 $this->mCompressed = false;
122 }
123 }
124
125 /** @todo document */
126 function getText() {
127 $this->uncompress();
128 return $this->getItem( $this->mDefaultHash );
129 }
130
131 /** @todo document */
132 function setText( $text ) {
133 $this->uncompress();
134 $stub = $this->addItem( $text );
135 $this->mDefaultHash = $stub->mHash;
136 }
137
138 /** @todo document */
139 function __sleep() {
140 $this->compress();
141 return array( 'mVersion', 'mCompressed', 'mItems', 'mDefaultHash' );
142 }
143
144 /** @todo document */
145 function __wakeup() {
146 $this->uncompress();
147 }
148
149 /**
150 * Determines if this object is happy
151 */
152 function isHappy( $maxFactor, $factorThreshold ) {
153 if ( count( $this->mItems ) == 0 ) {
154 return true;
155 }
156 if ( !$this->mFast ) {
157 $this->uncompress();
158 $record = serialize( $this->mItems );
159 $size = strlen( $record );
160 $avgUncompressed = $size / count( $this->mItems );
161 $compressed = strlen( gzdeflate( $record ) );
162
163 if ( $compressed < $factorThreshold * 1024 ) {
164 return true;
165 } else {
166 return $avgUncompressed * $maxFactor < $compressed;
167 }
168 } else {
169 return count( $this->mItems ) <= 10;
170 }
171 }
172 }
173
174
175 /**
176 * One-step cache variable to hold base blobs; operations that
177 * pull multiple revisions may often pull multiple times from
178 * the same blob. By keeping the last-used one open, we avoid
179 * redundant unserialization and decompression overhead.
180 */
181 global $wgBlobCache;
182 $wgBlobCache = array();
183
184
185 /**
186 * @package MediaWiki
187 */
188 class HistoryBlobStub {
189 private
190 $mHash,
191 $mOldId,
192 $mRef;
193
194 /** @todo document */
195 function HistoryBlobStub( $hash = '', $oldid = 0 ) {
196 $this->mHash = $hash;
197 }
198
199 /**
200 * Sets the location (old_id) of the main object to which this object
201 * points
202 */
203 function setLocation( $id ) {
204 $this->mOldId = $id;
205 }
206
207 /**
208 * Sets the location (old_id) of the referring object
209 */
210 function setReferrer( $id ) {
211 $this->mRef = $id;
212 }
213
214 /**
215 * Gets the location of the referring object
216 */
217 function getReferrer() {
218 return $this->mRef;
219 }
220
221 /** @todo document */
222 function getText() {
223 $fname = 'HistoryBlob::getText';
224 global $wgBlobCache;
225 if( isset( $wgBlobCache[$this->mOldId] ) ) {
226 $obj = $wgBlobCache[$this->mOldId];
227 } else {
228 $dbr =& wfGetDB( DB_SLAVE );
229 $row = $dbr->selectRow( 'text', array( 'old_flags', 'old_text' ), array( 'old_id' => $this->mOldId ) );
230 if( !$row ) {
231 return false;
232 }
233 $flags = explode( ',', $row->old_flags );
234 if( in_array( 'external', $flags ) ) {
235 $url=$row->old_text;
236 @list($proto,$path)=explode('://',$url,2);
237 if ($path=="") {
238 wfProfileOut( $fname );
239 return false;
240 }
241 require_once('ExternalStore.php');
242 $row->old_text=ExternalStore::fetchFromUrl($url);
243
244 }
245 if( !in_array( 'object', $flags ) ) {
246 return false;
247 }
248
249 if( in_array( 'gzip', $flags ) ) {
250 // This shouldn't happen, but a bug in the compress script
251 // may at times gzip-compress a HistoryBlob object row.
252 $obj = unserialize( gzinflate( $row->old_text ) );
253 } else {
254 $obj = unserialize( $row->old_text );
255 }
256
257 if( !is_object( $obj ) ) {
258 // Correct for old double-serialization bug.
259 $obj = unserialize( $obj );
260 }
261
262 // Save this item for reference; if pulling many
263 // items in a row we'll likely use it again.
264 $obj->uncompress();
265 $wgBlobCache = array( $this->mOldId => $obj );
266 }
267 return $obj->getItem( $this->mHash );
268 }
269
270 /** @todo document */
271 function getHash() {
272 return $this->mHash;
273 }
274 }
275
276
277 /**
278 * To speed up conversion from 1.4 to 1.5 schema, text rows can refer to the
279 * leftover cur table as the backend. This avoids expensively copying hundreds
280 * of megabytes of data during the conversion downtime.
281 *
282 * Serialized HistoryBlobCurStub objects will be inserted into the text table
283 * on conversion if $wgFastSchemaUpgrades is set to true.
284 *
285 * @package MediaWiki
286 */
287 class HistoryBlobCurStub {
288 private
289 $mCurId;
290
291 /** @todo document */
292 function HistoryBlobCurStub( $curid = 0 ) {
293 $this->mCurId = $curid;
294 }
295
296 /**
297 * Sets the location (cur_id) of the main object to which this object
298 * points
299 */
300 function setLocation( $id ) {
301 $this->mCurId = $id;
302 }
303
304 /** @todo document */
305 function getText() {
306 $dbr =& wfGetDB( DB_SLAVE );
307 $row = $dbr->selectRow( 'cur', array( 'cur_text' ), array( 'cur_id' => $this->mCurId ) );
308 if( !$row ) {
309 return false;
310 }
311 return $row->cur_text;
312 }
313 }
314
315
316 ?>