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