5 * @todo document (needs a one-sentence top-level class description, that answers the question: "what is a HistoryBlob?")
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.
14 public function setMeta( $meta );
17 * setMeta and getMeta currently aren't used for anything, I just thought
18 * they might be useful in the future.
21 public function getMeta();
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
28 public function addItem( $text );
33 public function getItem( $hash );
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 );
42 * Get default text. This is called from Revision::getRevisionText()
49 * @todo document (needs one-sentence top-level class description + function descriptions).
51 class ConcatenatedGzipHistoryBlob
implements HistoryBlob
53 public $mVersion = 0, $mCompressed = false, $mItems = array(), $mDefaultHash = '';
54 public $mFast = 0, $mSize = 0;
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" );
64 # HistoryBlob implementation:
68 public function setMeta( $metaData ) {
70 $this->mItems
['meta'] = $metaData;
74 public function getMeta() {
76 return $this->mItems
['meta'];
80 public function addItem( $text ) {
83 $this->mItems
[$hash] = $text;
84 $this->mSize +
= strlen( $text );
86 $stub = new HistoryBlobStub( $hash );
91 public function getItem( $hash ) {
93 if ( array_key_exists( $hash, $this->mItems
) ) {
94 return $this->mItems
[$hash];
100 /** @todo document */
101 public function setText( $text ) {
103 $stub = $this->addItem( $text );
104 $this->mDefaultHash
= $stub->mHash
;
107 /** @todo document */
108 public function getText() {
110 return $this->getItem( $this->mDefaultHash
);
113 # HistoryBlob implemented.
116 /** @todo document */
117 public function removeItem( $hash ) {
118 $this->mSize
-= strlen( $this->mItems
[$hash] );
119 unset( $this->mItems
[$hash] );
122 /** @todo document */
123 public function compress() {
124 if ( !$this->mCompressed
) {
125 $this->mItems
= gzdeflate( serialize( $this->mItems
) );
126 $this->mCompressed
= true;
130 /** @todo document */
131 public function uncompress() {
132 if ( $this->mCompressed
) {
133 $this->mItems
= unserialize( gzinflate( $this->mItems
) );
134 $this->mCompressed
= false;
139 /** @todo document */
142 return array( 'mVersion', 'mCompressed', 'mItems', 'mDefaultHash' );
145 /** @todo document */
146 function __wakeup() {
151 * Determines if this object is happy
153 public function isHappy( $maxFactor, $factorThreshold ) {
154 if ( count( $this->mItems
) == 0 ) {
157 if ( !$this->mFast
) {
159 $record = serialize( $this->mItems
);
160 $size = strlen( $record );
161 $avgUncompressed = $size / count( $this->mItems
);
162 $compressed = strlen( gzdeflate( $record ) );
164 if ( $compressed < $factorThreshold * 1024 ) {
167 return $avgUncompressed * $maxFactor < $compressed;
170 return count( $this->mItems
) <= 10;
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.
183 $wgBlobCache = array();
187 * @todo document (needs one-sentence top-level class description + some function descriptions).
189 class HistoryBlobStub
{
190 var $mOldId, $mHash, $mRef;
192 /** @todo document */
193 function HistoryBlobStub( $hash = '', $oldid = 0 ) {
194 $this->mHash
= $hash;
198 * Sets the location (old_id) of the main object to which this object
201 function setLocation( $id ) {
206 * Sets the location (old_id) of the referring object
208 function setReferrer( $id ) {
213 * Gets the location of the referring object
215 function getReferrer() {
219 /** @todo document */
221 $fname = 'HistoryBlobStub::getText';
223 if( isset( $wgBlobCache[$this->mOldId
] ) ) {
224 $obj = $wgBlobCache[$this->mOldId
];
226 $dbr = wfGetDB( DB_SLAVE
);
227 $row = $dbr->selectRow( 'text', array( 'old_flags', 'old_text' ), array( 'old_id' => $this->mOldId
) );
231 $flags = explode( ',', $row->old_flags
);
232 if( in_array( 'external', $flags ) ) {
234 @list
( /* $proto */ ,$path)=explode('://',$url,2);
236 wfProfileOut( $fname );
239 $row->old_text
=ExternalStore
::fetchFromUrl($url);
242 if( !in_array( 'object', $flags ) ) {
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
) );
251 $obj = unserialize( $row->old_text
);
254 if( !is_object( $obj ) ) {
255 // Correct for old double-serialization bug.
256 $obj = unserialize( $obj );
259 // Save this item for reference; if pulling many
260 // items in a row we'll likely use it again.
262 $wgBlobCache = array( $this->mOldId
=> $obj );
264 return $obj->getItem( $this->mHash
);
267 /** @todo document */
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.
279 * Serialized HistoryBlobCurStub objects will be inserted into the text table
280 * on conversion if $wgFastSchemaUpgrades is set to true.
282 class HistoryBlobCurStub
{
285 /** @todo document */
286 function HistoryBlobCurStub( $curid = 0 ) {
287 $this->mCurId
= $curid;
291 * Sets the location (cur_id) of the main object to which this object
294 function setLocation( $id ) {
298 /** @todo document */
300 $dbr = wfGetDB( DB_SLAVE
);
301 $row = $dbr->selectRow( 'cur', array( 'cur_text' ), array( 'cur_id' => $this->mCurId
) );
305 return $row->cur_text
;