Fix.
[mediawiki.git] / includes / HistoryBlob.php
blob3ce4ffd94dc16adee7bc66c91094a832090a546b
1 <?php
2 /**
4 */
6 /**
7 * Pure virtual parent
8 */
9 class HistoryBlob
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.
16 function setMeta( $meta ) {}
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
23 function getMeta() {}
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
30 function addItem() {}
32 /**
33 * Get item by hash
35 function getItem( $hash ) {}
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() {}
43 /**
44 * Get default text. This is called from Revision::getRevisionText()
46 function getText() {}
49 /**
50 * The real object
52 class ConcatenatedGzipHistoryBlob extends HistoryBlob
54 /* private */ var $mVersion = 0, $mCompressed = false, $mItems = array(), $mDefaultHash = '';
55 /* private */ var $mFast = 0, $mSize = 0;
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" );
63 /** @todo document */
64 function setMeta( $metaData ) {
65 $this->uncompress();
66 $this->mItems['meta'] = $metaData;
69 /** @todo document */
70 function getMeta() {
71 $this->uncompress();
72 return $this->mItems['meta'];
75 /** @todo document */
76 function addItem( $text ) {
77 $this->uncompress();
78 $hash = md5( $text );
79 $this->mItems[$hash] = $text;
80 $this->mSize += strlen( $text );
82 $stub = new HistoryBlobStub( $hash );
83 return $stub;
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;
96 /** @todo document */
97 function removeItem( $hash ) {
98 $this->mSize -= strlen( $this->mItems[$hash] );
99 unset( $this->mItems[$hash] );
102 /** @todo document */
103 function compress() {
104 if ( !$this->mCompressed ) {
105 $this->mItems = gzdeflate( serialize( $this->mItems ) );
106 $this->mCompressed = true;
110 /** @todo document */
111 function uncompress() {
112 if ( $this->mCompressed ) {
113 $this->mItems = unserialize( gzinflate( $this->mItems ) );
114 $this->mCompressed = false;
118 /** @todo document */
119 function getText() {
120 $this->uncompress();
121 return $this->getItem( $this->mDefaultHash );
124 /** @todo document */
125 function setText( $text ) {
126 $this->uncompress();
127 $stub = $this->addItem( $text );
128 $this->mDefaultHash = $stub->mHash;
131 /** @todo document */
132 function __sleep() {
133 $this->compress();
134 return array( 'mVersion', 'mCompressed', 'mItems', 'mDefaultHash' );
137 /** @todo document */
138 function __wakeup() {
139 $this->uncompress();
143 * Determines if this object is happy
145 function isHappy( $maxFactor, $factorThreshold ) {
146 if ( count( $this->mItems ) == 0 ) {
147 return true;
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 ) );
156 if ( $compressed < $factorThreshold * 1024 ) {
157 return true;
158 } else {
159 return $avgUncompressed * $maxFactor < $compressed;
161 } else {
162 return count( $this->mItems ) <= 10;
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.
174 global $wgBlobCache;
175 $wgBlobCache = array();
180 class HistoryBlobStub {
181 var $mOldId, $mHash, $mRef;
183 /** @todo document */
184 function HistoryBlobStub( $hash = '', $oldid = 0 ) {
185 $this->mHash = $hash;
189 * Sets the location (old_id) of the main object to which this object
190 * points
192 function setLocation( $id ) {
193 $this->mOldId = $id;
197 * Sets the location (old_id) of the referring object
199 function setReferrer( $id ) {
200 $this->mRef = $id;
204 * Gets the location of the referring object
206 function getReferrer() {
207 return $this->mRef;
210 /** @todo document */
211 function getText() {
212 $fname = 'HistoryBlobStub::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;
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;
230 $row->old_text=ExternalStore::fetchFromUrl($url);
233 if( !in_array( 'object', $flags ) ) {
234 return false;
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 );
245 if( !is_object( $obj ) ) {
246 // Correct for old double-serialization bug.
247 $obj = unserialize( $obj );
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 );
255 return $obj->getItem( $this->mHash );
258 /** @todo document */
259 function getHash() {
260 return $this->mHash;
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.
270 * Serialized HistoryBlobCurStub objects will be inserted into the text table
271 * on conversion if $wgFastSchemaUpgrades is set to true.
274 class HistoryBlobCurStub {
275 var $mCurId;
277 /** @todo document */
278 function HistoryBlobCurStub( $curid = 0 ) {
279 $this->mCurId = $curid;
283 * Sets the location (cur_id) of the main object to which this object
284 * points
286 function setLocation( $id ) {
287 $this->mCurId = $id;
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;
297 return $row->cur_text;