8 * @todo document (needs a one-sentence top-level class description, that answers the question: "what is a HistoryBlob?")
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 function setMeta( $meta ) {}
20 * setMeta and getMeta currently aren't used for anything, I just thought
21 * they might be useful in the future.
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
36 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
45 * Get default text. This is called from Revision::getRevisionText()
52 * @todo document (needs one-sentence top-level class description + function descriptions).
54 class ConcatenatedGzipHistoryBlob
extends HistoryBlob
56 /* private */ var $mVersion = 0, $mCompressed = false, $mItems = array(), $mDefaultHash = '';
57 /* private */ var $mFast = 0, $mSize = 0;
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" );
66 function setMeta( $metaData ) {
68 $this->mItems
['meta'] = $metaData;
74 return $this->mItems
['meta'];
78 function addItem( $text ) {
81 $this->mItems
[$hash] = $text;
82 $this->mSize +
= strlen( $text );
84 $stub = new HistoryBlobStub( $hash );
89 function getItem( $hash ) {
91 if ( array_key_exists( $hash, $this->mItems
) ) {
92 return $this->mItems
[$hash];
99 function removeItem( $hash ) {
100 $this->mSize
-= strlen( $this->mItems
[$hash] );
101 unset( $this->mItems
[$hash] );
104 /** @todo document */
105 function compress() {
106 if ( !$this->mCompressed
) {
107 $this->mItems
= gzdeflate( serialize( $this->mItems
) );
108 $this->mCompressed
= true;
112 /** @todo document */
113 function uncompress() {
114 if ( $this->mCompressed
) {
115 $this->mItems
= unserialize( gzinflate( $this->mItems
) );
116 $this->mCompressed
= false;
120 /** @todo document */
123 return $this->getItem( $this->mDefaultHash
);
126 /** @todo document */
127 function setText( $text ) {
129 $stub = $this->addItem( $text );
130 $this->mDefaultHash
= $stub->mHash
;
133 /** @todo document */
136 return array( 'mVersion', 'mCompressed', 'mItems', 'mDefaultHash' );
139 /** @todo document */
140 function __wakeup() {
145 * Determines if this object is happy
147 function isHappy( $maxFactor, $factorThreshold ) {
148 if ( count( $this->mItems
) == 0 ) {
151 if ( !$this->mFast
) {
153 $record = serialize( $this->mItems
);
154 $size = strlen( $record );
155 $avgUncompressed = $size / count( $this->mItems
);
156 $compressed = strlen( gzdeflate( $record ) );
158 if ( $compressed < $factorThreshold * 1024 ) {
161 return $avgUncompressed * $maxFactor < $compressed;
164 return count( $this->mItems
) <= 10;
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.
177 $wgBlobCache = array();
181 * @todo document (needs one-sentence top-level class description + some function descriptions).
183 class HistoryBlobStub
{
184 var $mOldId, $mHash, $mRef;
186 /** @todo document */
187 function HistoryBlobStub( $hash = '', $oldid = 0 ) {
188 $this->mHash
= $hash;
192 * Sets the location (old_id) of the main object to which this object
195 function setLocation( $id ) {
200 * Sets the location (old_id) of the referring object
202 function setReferrer( $id ) {
207 * Gets the location of the referring object
209 function getReferrer() {
213 /** @todo document */
215 $fname = 'HistoryBlobStub::getText';
217 if( isset( $wgBlobCache[$this->mOldId
] ) ) {
218 $obj = $wgBlobCache[$this->mOldId
];
220 $dbr = wfGetDB( DB_SLAVE
);
221 $row = $dbr->selectRow( 'text', array( 'old_flags', 'old_text' ), array( 'old_id' => $this->mOldId
) );
225 $flags = explode( ',', $row->old_flags
);
226 if( in_array( 'external', $flags ) ) {
228 @list
( /* $proto */ ,$path)=explode('://',$url,2);
230 wfProfileOut( $fname );
233 $row->old_text
=ExternalStore
::fetchFromUrl($url);
236 if( !in_array( 'object', $flags ) ) {
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
) );
245 $obj = unserialize( $row->old_text
);
248 if( !is_object( $obj ) ) {
249 // Correct for old double-serialization bug.
250 $obj = unserialize( $obj );
253 // Save this item for reference; if pulling many
254 // items in a row we'll likely use it again.
256 $wgBlobCache = array( $this->mOldId
=> $obj );
258 return $obj->getItem( $this->mHash
);
261 /** @todo document */
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.
273 * Serialized HistoryBlobCurStub objects will be inserted into the text table
274 * on conversion if $wgFastSchemaUpgrades is set to true.
276 class HistoryBlobCurStub
{
279 /** @todo document */
280 function HistoryBlobCurStub( $curid = 0 ) {
281 $this->mCurId
= $curid;
285 * Sets the location (cur_id) of the main object to which this object
288 function setLocation( $id ) {
292 /** @todo document */
294 $dbr = wfGetDB( DB_SLAVE
);
295 $row = $dbr->selectRow( 'cur', array( 'cur_text' ), array( 'cur_id' => $this->mCurId
) );
299 return $row->cur_text
;