Remove ?>'s from files. They're pointless, and just asking for people to mess with...
[lhc/web/wiklou.git] / includes / HistoryBlob.php
1 <?php
2 /**
3 *
4 */
5
6 /**
7 * Pure virtual parent
8 * @todo document (needs a one-sentence top-level class description, that answers the question: "what is a HistoryBlob?")
9 */
10 interface HistoryBlob
11 {
12 /**
13 * setMeta and getMeta currently aren't used for anything, I just thought
14 * they might be useful in the future.
15 * @param $meta String: a single string.
16 */
17 public function setMeta( $meta );
18
19 /**
20 * setMeta and getMeta currently aren't used for anything, I just thought
21 * they might be useful in the future.
22 * Gets the meta-value
23 */
24 public function getMeta();
25
26 /**
27 * Adds an item of text, returns a stub object which points to the item.
28 * You must call setLocation() on the stub object before storing it to the
29 * database
30 */
31 public function addItem( $text );
32
33 /**
34 * Get item by hash
35 */
36 public function getItem( $hash );
37
38 # Set the "default text"
39 # This concept is an odd property of the current DB schema, whereby each text item has a revision
40 # associated with it. The default text is the text of the associated revision. There may, however,
41 # be other revisions in the same object
42 public function setText( $text );
43
44 /**
45 * Get default text. This is called from Revision::getRevisionText()
46 */
47 function getText();
48 }
49
50 /**
51 * The real object
52 * @todo document (needs one-sentence top-level class description + function descriptions).
53 */
54 class ConcatenatedGzipHistoryBlob implements HistoryBlob
55 {
56 public $mVersion = 0, $mCompressed = false, $mItems = array(), $mDefaultHash = '';
57 public $mFast = 0, $mSize = 0;
58
59 /** Constructor */
60 public function ConcatenatedGzipHistoryBlob() {
61 if ( !function_exists( 'gzdeflate' ) ) {
62 throw new MWException( "Need zlib support to read or write this kind of history object (ConcatenatedGzipHistoryBlob)\n" );
63 }
64 }
65
66 #
67 # HistoryBlob implementation:
68 #
69
70 /** @todo document */
71 public function setMeta( $metaData ) {
72 $this->uncompress();
73 $this->mItems['meta'] = $metaData;
74 }
75
76 /** @todo document */
77 public function getMeta() {
78 $this->uncompress();
79 return $this->mItems['meta'];
80 }
81
82 /** @todo document */
83 public 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 public 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 public function setText( $text ) {
105 $this->uncompress();
106 $stub = $this->addItem( $text );
107 $this->mDefaultHash = $stub->mHash;
108 }
109
110 /** @todo document */
111 public function getText() {
112 $this->uncompress();
113 return $this->getItem( $this->mDefaultHash );
114 }
115
116 # HistoryBlob implemented.
117
118
119 /** @todo document */
120 public function removeItem( $hash ) {
121 $this->mSize -= strlen( $this->mItems[$hash] );
122 unset( $this->mItems[$hash] );
123 }
124
125 /** @todo document */
126 public function compress() {
127 if ( !$this->mCompressed ) {
128 $this->mItems = gzdeflate( serialize( $this->mItems ) );
129 $this->mCompressed = true;
130 }
131 }
132
133 /** @todo document */
134 public function uncompress() {
135 if ( $this->mCompressed ) {
136 $this->mItems = unserialize( gzinflate( $this->mItems ) );
137 $this->mCompressed = false;
138 }
139 }
140
141
142 /** @todo document */
143 function __sleep() {
144 $this->compress();
145 return array( 'mVersion', 'mCompressed', 'mItems', 'mDefaultHash' );
146 }
147
148 /** @todo document */
149 function __wakeup() {
150 $this->uncompress();
151 }
152
153 /**
154 * Determines if this object is happy
155 */
156 public function isHappy( $maxFactor, $factorThreshold ) {
157 if ( count( $this->mItems ) == 0 ) {
158 return true;
159 }
160 if ( !$this->mFast ) {
161 $this->uncompress();
162 $record = serialize( $this->mItems );
163 $size = strlen( $record );
164 $avgUncompressed = $size / count( $this->mItems );
165 $compressed = strlen( gzdeflate( $record ) );
166
167 if ( $compressed < $factorThreshold * 1024 ) {
168 return true;
169 } else {
170 return $avgUncompressed * $maxFactor < $compressed;
171 }
172 } else {
173 return count( $this->mItems ) <= 10;
174 }
175 }
176 }
177
178
179 /**
180 * One-step cache variable to hold base blobs; operations that
181 * pull multiple revisions may often pull multiple times from
182 * the same blob. By keeping the last-used one open, we avoid
183 * redundant unserialization and decompression overhead.
184 */
185 global $wgBlobCache;
186 $wgBlobCache = array();
187
188
189 /**
190 * @todo document (needs one-sentence top-level class description + some function descriptions).
191 */
192 class HistoryBlobStub {
193 var $mOldId, $mHash, $mRef;
194
195 /** @todo document */
196 function HistoryBlobStub( $hash = '', $oldid = 0 ) {
197 $this->mHash = $hash;
198 }
199
200 /**
201 * Sets the location (old_id) of the main object to which this object
202 * points
203 */
204 function setLocation( $id ) {
205 $this->mOldId = $id;
206 }
207
208 /**
209 * Sets the location (old_id) of the referring object
210 */
211 function setReferrer( $id ) {
212 $this->mRef = $id;
213 }
214
215 /**
216 * Gets the location of the referring object
217 */
218 function getReferrer() {
219 return $this->mRef;
220 }
221
222 /** @todo document */
223 function getText() {
224 $fname = 'HistoryBlobStub::getText';
225 global $wgBlobCache;
226 if( isset( $wgBlobCache[$this->mOldId] ) ) {
227 $obj = $wgBlobCache[$this->mOldId];
228 } else {
229 $dbr = wfGetDB( DB_SLAVE );
230 $row = $dbr->selectRow( 'text', array( 'old_flags', 'old_text' ), array( 'old_id' => $this->mOldId ) );
231 if( !$row ) {
232 return false;
233 }
234 $flags = explode( ',', $row->old_flags );
235 if( in_array( 'external', $flags ) ) {
236 $url=$row->old_text;
237 @list( /* $proto */ ,$path)=explode('://',$url,2);
238 if ($path=="") {
239 wfProfileOut( $fname );
240 return false;
241 }
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 class HistoryBlobCurStub {
286 var $mCurId;
287
288 /** @todo document */
289 function HistoryBlobCurStub( $curid = 0 ) {
290 $this->mCurId = $curid;
291 }
292
293 /**
294 * Sets the location (cur_id) of the main object to which this object
295 * points
296 */
297 function setLocation( $id ) {
298 $this->mCurId = $id;
299 }
300
301 /** @todo document */
302 function getText() {
303 $dbr = wfGetDB( DB_SLAVE );
304 $row = $dbr->selectRow( 'cur', array( 'cur_text' ), array( 'cur_id' => $this->mCurId ) );
305 if( !$row ) {
306 return false;
307 }
308 return $row->cur_text;
309 }
310 }
311
312
313