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