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