14 * setMeta and getMeta currently aren't used for anything, I just thought
15 * they might be useful in the future.
16 * @param string $meta a single string
18 function setMeta( $meta ) {}
21 * setMeta and getMeta currently aren't used for anything, I just thought
22 * they might be useful in the future.
28 * Adds an item of text, returns a stub object which points to the item.
29 * You must call setLocation() on the stub object before storing it to the
37 function getItem( $hash ) {}
39 # Set the "default text"
40 # This concept is an odd property of the current DB schema, whereby each text item has a revision
41 # associated with it. The default text is the text of the associated revision. There may, however,
42 # be other revisions in the same object
46 * Get default text. This is called from Revision::getRevisionText()
55 class ConcatenatedGzipHistoryBlob
extends HistoryBlob
57 /* private */ var $mVersion = 0, $mCompressed = false, $mItems = array(), $mDefaultHash = '';
58 /* private */ var $mFast = 0, $mSize = 0;
60 function ConcatenatedGzipHistoryBlob() {
61 if ( !function_exists( 'gzdeflate' ) ) {
62 wfDie( "Need zlib support to read or write this kind of history object (ConcatenatedGzipHistoryBlob)\n" );
67 function setMeta( $metaData ) {
69 $this->mItems
['meta'] = $metaData;
75 return $this->mItems
['meta'];
79 function addItem( $text ) {
82 $this->mItems
[$hash] = $text;
83 $this->mSize +
= strlen( $text );
85 $stub = new HistoryBlobStub( $hash );
90 function getItem( $hash ) {
92 if ( array_key_exists( $hash, $this->mItems
) ) {
93 return $this->mItems
[$hash];
100 function removeItem( $hash ) {
101 $this->mSize
-= strlen( $this->mItems
[$hash] );
102 unset( $this->mItems
[$hash] );
105 /** @todo document */
106 function compress() {
107 if ( !$this->mCompressed
) {
108 $this->mItems
= gzdeflate( serialize( $this->mItems
) );
109 $this->mCompressed
= true;
113 /** @todo document */
114 function uncompress() {
115 if ( $this->mCompressed
) {
116 $this->mItems
= unserialize( gzinflate( $this->mItems
) );
117 $this->mCompressed
= false;
121 /** @todo document */
124 return $this->getItem( $this->mDefaultHash
);
127 /** @todo document */
128 function setText( $text ) {
130 $stub = $this->addItem( $text );
131 $this->mDefaultHash
= $stub->mHash
;
134 /** @todo document */
137 return array( 'mVersion', 'mCompressed', 'mItems', 'mDefaultHash' );
140 /** @todo document */
141 function __wakeup() {
146 * Determines if this object is happy
148 function isHappy( $maxFactor, $factorThreshold ) {
149 if ( count( $this->mItems
) == 0 ) {
152 if ( !$this->mFast
) {
154 $record = serialize( $this->mItems
);
155 $size = strlen( $record );
156 $avgUncompressed = $size / count( $this->mItems
);
157 $compressed = strlen( gzdeflate( $record ) );
159 if ( $compressed < $factorThreshold * 1024 ) {
162 return $avgUncompressed * $maxFactor < $compressed;
165 return count( $this->mItems
) <= 10;
172 * One-step cache variable to hold base blobs; operations that
173 * pull multiple revisions may often pull multiple times from
174 * the same blob. By keeping the last-used one open, we avoid
175 * redundant unserialization and decompression overhead.
178 $wgBlobCache = array();
184 class HistoryBlobStub
{
185 var $mOldId, $mHash, $mRef;
187 /** @todo document */
188 function HistoryBlobStub( $hash = '', $oldid = 0 ) {
189 $this->mHash
= $hash;
193 * Sets the location (old_id) of the main object to which this object
196 function setLocation( $id ) {
201 * Sets the location (old_id) of the referring object
203 function setReferrer( $id ) {
208 * Gets the location of the referring object
210 function getReferrer() {
214 /** @todo document */
216 $fname = 'HistoryBlob::getText';
218 if( isset( $wgBlobCache[$this->mOldId
] ) ) {
219 $obj = $wgBlobCache[$this->mOldId
];
221 $dbr =& wfGetDB( DB_SLAVE
);
222 $row = $dbr->selectRow( 'text', array( 'old_flags', 'old_text' ), array( 'old_id' => $this->mOldId
) );
226 $flags = explode( ',', $row->old_flags
);
227 if( in_array( 'external', $flags ) ) {
229 @list
($proto,$path)=explode('://',$url,2);
231 wfProfileOut( $fname );
234 require_once('ExternalStore.php');
235 $row->old_text
=ExternalStore
::fetchFromUrl($url);
238 if( !in_array( 'object', $flags ) ) {
242 if( in_array( 'gzip', $flags ) ) {
243 // This shouldn't happen, but a bug in the compress script
244 // may at times gzip-compress a HistoryBlob object row.
245 $obj = unserialize( gzinflate( $row->old_text
) );
247 $obj = unserialize( $row->old_text
);
250 if( !is_object( $obj ) ) {
251 // Correct for old double-serialization bug.
252 $obj = unserialize( $obj );
255 // Save this item for reference; if pulling many
256 // items in a row we'll likely use it again.
258 $wgBlobCache = array( $this->mOldId
=> $obj );
260 return $obj->getItem( $this->mHash
);
263 /** @todo document */
271 * To speed up conversion from 1.4 to 1.5 schema, text rows can refer to the
272 * leftover cur table as the backend. This avoids expensively copying hundreds
273 * of megabytes of data during the conversion downtime.
275 * Serialized HistoryBlobCurStub objects will be inserted into the text table
276 * on conversion if $wgFastSchemaUpgrades is set to true.
280 class HistoryBlobCurStub
{
283 /** @todo document */
284 function HistoryBlobCurStub( $curid = 0 ) {
285 $this->mCurId
= $curid;
289 * Sets the location (cur_id) of the main object to which this object
292 function setLocation( $id ) {
296 /** @todo document */
298 $dbr =& wfGetDB( DB_SLAVE
);
299 $row = $dbr->selectRow( 'cur', array( 'cur_text' ), array( 'cur_id' => $this->mCurId
) );
303 return $row->cur_text
;