PHPDocumentor [http://en.wikipedia.org/wiki/PhpDocumentor] documentation tweaking...
[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 class 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 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 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 function addItem() {}
32
33 /**
34 * Get item by hash
35 */
36 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 function setText() {}
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 extends HistoryBlob
55 {
56 /* private */ var $mVersion = 0, $mCompressed = false, $mItems = array(), $mDefaultHash = '';
57 /* private */ var $mFast = 0, $mSize = 0;
58
59 function ConcatenatedGzipHistoryBlob() {
60 if ( !function_exists( 'gzdeflate' ) ) {
61 throw new MWException( "Need zlib support to read or write this kind of history object (ConcatenatedGzipHistoryBlob)\n" );
62 }
63 }
64
65 /** @todo document */
66 function setMeta( $metaData ) {
67 $this->uncompress();
68 $this->mItems['meta'] = $metaData;
69 }
70
71 /** @todo document */
72 function getMeta() {
73 $this->uncompress();
74 return $this->mItems['meta'];
75 }
76
77 /** @todo document */
78 function addItem( $text ) {
79 $this->uncompress();
80 $hash = md5( $text );
81 $this->mItems[$hash] = $text;
82 $this->mSize += strlen( $text );
83
84 $stub = new HistoryBlobStub( $hash );
85 return $stub;
86 }
87
88 /** @todo document */
89 function getItem( $hash ) {
90 $this->uncompress();
91 if ( array_key_exists( $hash, $this->mItems ) ) {
92 return $this->mItems[$hash];
93 } else {
94 return false;
95 }
96 }
97
98 /** @todo document */
99 function removeItem( $hash ) {
100 $this->mSize -= strlen( $this->mItems[$hash] );
101 unset( $this->mItems[$hash] );
102 }
103
104 /** @todo document */
105 function compress() {
106 if ( !$this->mCompressed ) {
107 $this->mItems = gzdeflate( serialize( $this->mItems ) );
108 $this->mCompressed = true;
109 }
110 }
111
112 /** @todo document */
113 function uncompress() {
114 if ( $this->mCompressed ) {
115 $this->mItems = unserialize( gzinflate( $this->mItems ) );
116 $this->mCompressed = false;
117 }
118 }
119
120 /** @todo document */
121 function getText() {
122 $this->uncompress();
123 return $this->getItem( $this->mDefaultHash );
124 }
125
126 /** @todo document */
127 function setText( $text ) {
128 $this->uncompress();
129 $stub = $this->addItem( $text );
130 $this->mDefaultHash = $stub->mHash;
131 }
132
133 /** @todo document */
134 function __sleep() {
135 $this->compress();
136 return array( 'mVersion', 'mCompressed', 'mItems', 'mDefaultHash' );
137 }
138
139 /** @todo document */
140 function __wakeup() {
141 $this->uncompress();
142 }
143
144 /**
145 * Determines if this object is happy
146 */
147 function isHappy( $maxFactor, $factorThreshold ) {
148 if ( count( $this->mItems ) == 0 ) {
149 return true;
150 }
151 if ( !$this->mFast ) {
152 $this->uncompress();
153 $record = serialize( $this->mItems );
154 $size = strlen( $record );
155 $avgUncompressed = $size / count( $this->mItems );
156 $compressed = strlen( gzdeflate( $record ) );
157
158 if ( $compressed < $factorThreshold * 1024 ) {
159 return true;
160 } else {
161 return $avgUncompressed * $maxFactor < $compressed;
162 }
163 } else {
164 return count( $this->mItems ) <= 10;
165 }
166 }
167 }
168
169
170 /**
171 * One-step cache variable to hold base blobs; operations that
172 * pull multiple revisions may often pull multiple times from
173 * the same blob. By keeping the last-used one open, we avoid
174 * redundant unserialization and decompression overhead.
175 */
176 global $wgBlobCache;
177 $wgBlobCache = array();
178
179
180 /**
181 * @todo document (needs one-sentence top-level class description + some function descriptions).
182 */
183 class HistoryBlobStub {
184 var $mOldId, $mHash, $mRef;
185
186 /** @todo document */
187 function HistoryBlobStub( $hash = '', $oldid = 0 ) {
188 $this->mHash = $hash;
189 }
190
191 /**
192 * Sets the location (old_id) of the main object to which this object
193 * points
194 */
195 function setLocation( $id ) {
196 $this->mOldId = $id;
197 }
198
199 /**
200 * Sets the location (old_id) of the referring object
201 */
202 function setReferrer( $id ) {
203 $this->mRef = $id;
204 }
205
206 /**
207 * Gets the location of the referring object
208 */
209 function getReferrer() {
210 return $this->mRef;
211 }
212
213 /** @todo document */
214 function getText() {
215 $fname = 'HistoryBlobStub::getText';
216 global $wgBlobCache;
217 if( isset( $wgBlobCache[$this->mOldId] ) ) {
218 $obj = $wgBlobCache[$this->mOldId];
219 } else {
220 $dbr = wfGetDB( DB_SLAVE );
221 $row = $dbr->selectRow( 'text', array( 'old_flags', 'old_text' ), array( 'old_id' => $this->mOldId ) );
222 if( !$row ) {
223 return false;
224 }
225 $flags = explode( ',', $row->old_flags );
226 if( in_array( 'external', $flags ) ) {
227 $url=$row->old_text;
228 @list( /* $proto */ ,$path)=explode('://',$url,2);
229 if ($path=="") {
230 wfProfileOut( $fname );
231 return false;
232 }
233 $row->old_text=ExternalStore::fetchFromUrl($url);
234
235 }
236 if( !in_array( 'object', $flags ) ) {
237 return false;
238 }
239
240 if( in_array( 'gzip', $flags ) ) {
241 // This shouldn't happen, but a bug in the compress script
242 // may at times gzip-compress a HistoryBlob object row.
243 $obj = unserialize( gzinflate( $row->old_text ) );
244 } else {
245 $obj = unserialize( $row->old_text );
246 }
247
248 if( !is_object( $obj ) ) {
249 // Correct for old double-serialization bug.
250 $obj = unserialize( $obj );
251 }
252
253 // Save this item for reference; if pulling many
254 // items in a row we'll likely use it again.
255 $obj->uncompress();
256 $wgBlobCache = array( $this->mOldId => $obj );
257 }
258 return $obj->getItem( $this->mHash );
259 }
260
261 /** @todo document */
262 function getHash() {
263 return $this->mHash;
264 }
265 }
266
267
268 /**
269 * To speed up conversion from 1.4 to 1.5 schema, text rows can refer to the
270 * leftover cur table as the backend. This avoids expensively copying hundreds
271 * of megabytes of data during the conversion downtime.
272 *
273 * Serialized HistoryBlobCurStub objects will be inserted into the text table
274 * on conversion if $wgFastSchemaUpgrades is set to true.
275 */
276 class HistoryBlobCurStub {
277 var $mCurId;
278
279 /** @todo document */
280 function HistoryBlobCurStub( $curid = 0 ) {
281 $this->mCurId = $curid;
282 }
283
284 /**
285 * Sets the location (cur_id) of the main object to which this object
286 * points
287 */
288 function setLocation( $id ) {
289 $this->mCurId = $id;
290 }
291
292 /** @todo document */
293 function getText() {
294 $dbr = wfGetDB( DB_SLAVE );
295 $row = $dbr->selectRow( 'cur', array( 'cur_text' ), array( 'cur_id' => $this->mCurId ) );
296 if( !$row ) {
297 return false;
298 }
299 return $row->cur_text;
300 }
301 }
302
303
304 ?>