* Fixed E_NOTICE..
[mediawiki.git] / includes / HistoryBlob.php
blob984ee2d4f808d6e429a4f9ea4d1acb68565fbe97
1 <?php
2 /**
4 */
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 interface HistoryBlob
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.
17 public function setMeta( $meta );
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
24 public function getMeta();
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
31 public function addItem( $text );
33 /**
34 * Get item by hash
36 public function getItem( $hash );
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 public function setText( $text );
44 /**
45 * Get default text. This is called from Revision::getRevisionText()
47 function getText();
50 /**
51 * The real object
52 * @todo document (needs one-sentence top-level class description + function descriptions).
54 class ConcatenatedGzipHistoryBlob implements HistoryBlob
56 public $mVersion = 0, $mCompressed = false, $mItems = array(), $mDefaultHash = '';
57 public $mFast = 0, $mSize = 0;
59 /** Constructor */
60 public function ConcatenatedGzipHistoryBlob() {
61 if ( !function_exists( 'gzdeflate' ) ) {
62 throw new MWException( "Need zlib support to read or write this kind of history object (ConcatenatedGzipHistoryBlob)\n" );
67 # HistoryBlob implementation:
70 /** @todo document */
71 public function setMeta( $metaData ) {
72 $this->uncompress();
73 $this->mItems['meta'] = $metaData;
76 /** @todo document */
77 public function getMeta() {
78 $this->uncompress();
79 return $this->mItems['meta'];
82 /** @todo document */
83 public function addItem( $text ) {
84 $this->uncompress();
85 $hash = md5( $text );
86 $this->mItems[$hash] = $text;
87 $this->mSize += strlen( $text );
89 $stub = new HistoryBlobStub( $hash );
90 return $stub;
93 /** @todo document */
94 public function getItem( $hash ) {
95 $this->uncompress();
96 if ( array_key_exists( $hash, $this->mItems ) ) {
97 return $this->mItems[$hash];
98 } else {
99 return false;
103 /** @todo document */
104 public function setText( $text ) {
105 $this->uncompress();
106 $stub = $this->addItem( $text );
107 $this->mDefaultHash = $stub->mHash;
110 /** @todo document */
111 public function getText() {
112 $this->uncompress();
113 return $this->getItem( $this->mDefaultHash );
116 # HistoryBlob implemented.
119 /** @todo document */
120 public function removeItem( $hash ) {
121 $this->mSize -= strlen( $this->mItems[$hash] );
122 unset( $this->mItems[$hash] );
125 /** @todo document */
126 public function compress() {
127 if ( !$this->mCompressed ) {
128 $this->mItems = gzdeflate( serialize( $this->mItems ) );
129 $this->mCompressed = true;
133 /** @todo document */
134 public function uncompress() {
135 if ( $this->mCompressed ) {
136 $this->mItems = unserialize( gzinflate( $this->mItems ) );
137 $this->mCompressed = false;
142 /** @todo document */
143 function __sleep() {
144 $this->compress();
145 return array( 'mVersion', 'mCompressed', 'mItems', 'mDefaultHash' );
148 /** @todo document */
149 function __wakeup() {
150 $this->uncompress();
154 * Determines if this object is happy
156 public function isHappy( $maxFactor, $factorThreshold ) {
157 if ( count( $this->mItems ) == 0 ) {
158 return true;
160 if ( !$this->mFast ) {
161 $this->uncompress();
162 $record = serialize( $this->mItems );
163 $size = strlen( $record );
164 $avgUncompressed = $size / count( $this->mItems );
165 $compressed = strlen( gzdeflate( $record ) );
167 if ( $compressed < $factorThreshold * 1024 ) {
168 return true;
169 } else {
170 return $avgUncompressed * $maxFactor < $compressed;
172 } else {
173 return count( $this->mItems ) <= 10;
180 * One-step cache variable to hold base blobs; operations that
181 * pull multiple revisions may often pull multiple times from
182 * the same blob. By keeping the last-used one open, we avoid
183 * redundant unserialization and decompression overhead.
185 global $wgBlobCache;
186 $wgBlobCache = array();
190 * @todo document (needs one-sentence top-level class description + some function descriptions).
192 class HistoryBlobStub {
193 var $mOldId, $mHash, $mRef;
195 /** @todo document */
196 function HistoryBlobStub( $hash = '', $oldid = 0 ) {
197 $this->mHash = $hash;
201 * Sets the location (old_id) of the main object to which this object
202 * points
204 function setLocation( $id ) {
205 $this->mOldId = $id;
209 * Sets the location (old_id) of the referring object
211 function setReferrer( $id ) {
212 $this->mRef = $id;
216 * Gets the location of the referring object
218 function getReferrer() {
219 return $this->mRef;
222 /** @todo document */
223 function getText() {
224 $fname = 'HistoryBlobStub::getText';
225 global $wgBlobCache;
226 if( isset( $wgBlobCache[$this->mOldId] ) ) {
227 $obj = $wgBlobCache[$this->mOldId];
228 } else {
229 $dbr = wfGetDB( DB_SLAVE );
230 $row = $dbr->selectRow( 'text', array( 'old_flags', 'old_text' ), array( 'old_id' => $this->mOldId ) );
231 if( !$row ) {
232 return false;
234 $flags = explode( ',', $row->old_flags );
235 if( in_array( 'external', $flags ) ) {
236 $url=$row->old_text;
237 @list( /* $proto */ ,$path)=explode('://',$url,2);
238 if ($path=="") {
239 wfProfileOut( $fname );
240 return false;
242 $row->old_text=ExternalStore::fetchFromUrl($url);
245 if( !in_array( 'object', $flags ) ) {
246 return false;
249 if( in_array( 'gzip', $flags ) ) {
250 // This shouldn't happen, but a bug in the compress script
251 // may at times gzip-compress a HistoryBlob object row.
252 $obj = unserialize( gzinflate( $row->old_text ) );
253 } else {
254 $obj = unserialize( $row->old_text );
257 if( !is_object( $obj ) ) {
258 // Correct for old double-serialization bug.
259 $obj = unserialize( $obj );
262 // Save this item for reference; if pulling many
263 // items in a row we'll likely use it again.
264 $obj->uncompress();
265 $wgBlobCache = array( $this->mOldId => $obj );
267 return $obj->getItem( $this->mHash );
270 /** @todo document */
271 function getHash() {
272 return $this->mHash;
278 * To speed up conversion from 1.4 to 1.5 schema, text rows can refer to the
279 * leftover cur table as the backend. This avoids expensively copying hundreds
280 * of megabytes of data during the conversion downtime.
282 * Serialized HistoryBlobCurStub objects will be inserted into the text table
283 * on conversion if $wgFastSchemaUpgrades is set to true.
285 class HistoryBlobCurStub {
286 var $mCurId;
288 /** @todo document */
289 function HistoryBlobCurStub( $curid = 0 ) {
290 $this->mCurId = $curid;
294 * Sets the location (cur_id) of the main object to which this object
295 * points
297 function setLocation( $id ) {
298 $this->mCurId = $id;
301 /** @todo document */
302 function getText() {
303 $dbr = wfGetDB( DB_SLAVE );
304 $row = $dbr->selectRow( 'cur', array( 'cur_text' ), array( 'cur_id' => $this->mCurId ) );
305 if( !$row ) {
306 return false;
308 return $row->cur_text;