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 public function setMeta( $meta );
20 * setMeta and getMeta currently aren't used for anything, I just thought
21 * they might be useful in the future.
24 public function getMeta();
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
31 public function addItem( $text );
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 );
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
implements HistoryBlob
56 public $mVersion = 0, $mCompressed = false, $mItems = array(), $mDefaultHash = '';
57 public $mFast = 0, $mSize = 0;
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:
71 public function setMeta( $metaData ) {
73 $this->mItems
['meta'] = $metaData;
77 public function getMeta() {
79 return $this->mItems
['meta'];
83 public function addItem( $text ) {
86 $this->mItems
[$hash] = $text;
87 $this->mSize +
= strlen( $text );
89 $stub = new HistoryBlobStub( $hash );
94 public function getItem( $hash ) {
96 if ( array_key_exists( $hash, $this->mItems
) ) {
97 return $this->mItems
[$hash];
103 /** @todo document */
104 public function setText( $text ) {
106 $stub = $this->addItem( $text );
107 $this->mDefaultHash
= $stub->mHash
;
110 /** @todo document */
111 public function getText() {
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 */
145 return array( 'mVersion', 'mCompressed', 'mItems', 'mDefaultHash' );
148 /** @todo document */
149 function __wakeup() {
154 * Determines if this object is happy
156 public function isHappy( $maxFactor, $factorThreshold ) {
157 if ( count( $this->mItems
) == 0 ) {
160 if ( !$this->mFast
) {
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 ) {
170 return $avgUncompressed * $maxFactor < $compressed;
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.
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
204 function setLocation( $id ) {
209 * Sets the location (old_id) of the referring object
211 function setReferrer( $id ) {
216 * Gets the location of the referring object
218 function getReferrer() {
222 /** @todo document */
224 $fname = 'HistoryBlobStub::getText';
226 if( isset( $wgBlobCache[$this->mOldId
] ) ) {
227 $obj = $wgBlobCache[$this->mOldId
];
229 $dbr = wfGetDB( DB_SLAVE
);
230 $row = $dbr->selectRow( 'text', array( 'old_flags', 'old_text' ), array( 'old_id' => $this->mOldId
) );
234 $flags = explode( ',', $row->old_flags
);
235 if( in_array( 'external', $flags ) ) {
237 @list
( /* $proto */ ,$path)=explode('://',$url,2);
239 wfProfileOut( $fname );
242 $row->old_text
=ExternalStore
::fetchFromUrl($url);
245 if( !in_array( 'object', $flags ) ) {
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
) );
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.
265 $wgBlobCache = array( $this->mOldId
=> $obj );
267 return $obj->getItem( $this->mHash
);
270 /** @todo document */
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
{
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
297 function setLocation( $id ) {
301 /** @todo document */
303 $dbr = wfGetDB( DB_SLAVE
);
304 $row = $dbr->selectRow( 'cur', array( 'cur_text' ), array( 'cur_id' => $this->mCurId
) );
308 return $row->cur_text
;