4 * Base class for general text storage via the "object" flag in old_flags, or
5 * two-part external storage URLs. Used for represent efficient concatenated
6 * storage, and migration-related pointer objects.
11 * Adds an item of text, returns a stub object which points to the item.
12 * You must call setLocation() on the stub object before storing it to the
14 * Returns the key for getItem()
16 public function addItem( $text );
19 * Get item by key, or false if the key is not present
21 public function getItem( $key );
24 * Set the "default text"
25 * This concept is an odd property of the current DB schema, whereby each text item has a revision
26 * associated with it. The default text is the text of the associated revision. There may, however,
27 * be other revisions in the same object.
29 * Default text is not required for two-part external storage URLs.
31 public function setText( $text );
34 * Get default text. This is called from Revision::getRevisionText()
40 * Concatenated gzip (CGZ) storage
41 * Improves compression ratio by concatenating like objects before gzipping
43 class ConcatenatedGzipHistoryBlob
implements HistoryBlob
45 public $mVersion = 0, $mCompressed = false, $mItems = array(), $mDefaultHash = '';
46 public $mFast = 0, $mSize = 0;
49 public function ConcatenatedGzipHistoryBlob() {
50 if ( !function_exists( 'gzdeflate' ) ) {
51 throw new MWException( "Need zlib support to read or write this kind of history object (ConcatenatedGzipHistoryBlob)\n" );
55 public function addItem( $text ) {
58 $this->mItems
[$hash] = $text;
59 $this->mSize +
= strlen( $text );
64 public function getItem( $hash ) {
66 if ( array_key_exists( $hash, $this->mItems
) ) {
67 return $this->mItems
[$hash];
73 public function setText( $text ) {
75 $stub = $this->addItem( $text );
76 $this->mDefaultHash
= $stub->mHash
;
79 public function getText() {
81 return $this->getItem( $this->mDefaultHash
);
87 public function removeItem( $hash ) {
88 $this->mSize
-= strlen( $this->mItems
[$hash] );
89 unset( $this->mItems
[$hash] );
93 * Compress the bulk data in the object
95 public function compress() {
96 if ( !$this->mCompressed
) {
97 $this->mItems
= gzdeflate( serialize( $this->mItems
) );
98 $this->mCompressed
= true;
103 * Uncompress bulk data
105 public function uncompress() {
106 if ( $this->mCompressed
) {
107 $this->mItems
= unserialize( gzinflate( $this->mItems
) );
108 $this->mCompressed
= false;
115 return array( 'mVersion', 'mCompressed', 'mItems', 'mDefaultHash' );
118 function __wakeup() {
123 * Helper function for compression jobs
124 * Returns true until the object is "full" and ready to be committed
126 public function isHappy( $maxFactor, $factorThreshold ) {
127 if ( count( $this->mItems
) == 0 ) {
130 if ( !$this->mFast
) {
132 $record = serialize( $this->mItems
);
133 $size = strlen( $record );
134 $avgUncompressed = $size / count( $this->mItems
);
135 $compressed = strlen( gzdeflate( $record ) );
137 if ( $compressed < $factorThreshold * 1024 ) {
140 return $avgUncompressed * $maxFactor < $compressed;
143 return count( $this->mItems
) <= 10;
150 * One-step cache variable to hold base blobs; operations that
151 * pull multiple revisions may often pull multiple times from
152 * the same blob. By keeping the last-used one open, we avoid
153 * redundant unserialization and decompression overhead.
156 $wgBlobCache = array();
160 * Pointer object for an item within a CGZ blob stored in the text table.
162 class HistoryBlobStub
{
163 var $mOldId, $mHash, $mRef;
166 * @param string $hash The content hash of the text
167 * @param integer $oldid The old_id for the CGZ object
169 function HistoryBlobStub( $hash = '', $oldid = 0 ) {
170 $this->mHash
= $hash;
174 * Sets the location (old_id) of the main object to which this object
177 function setLocation( $id ) {
182 * Sets the location (old_id) of the referring object
184 function setReferrer( $id ) {
189 * Gets the location of the referring object
191 function getReferrer() {
196 $fname = 'HistoryBlobStub::getText';
198 if( isset( $wgBlobCache[$this->mOldId
] ) ) {
199 $obj = $wgBlobCache[$this->mOldId
];
201 $dbr = wfGetDB( DB_SLAVE
);
202 $row = $dbr->selectRow( 'text', array( 'old_flags', 'old_text' ), array( 'old_id' => $this->mOldId
) );
206 $flags = explode( ',', $row->old_flags
);
207 if( in_array( 'external', $flags ) ) {
209 @list
( /* $proto */ ,$path)=explode('://',$url,2);
211 wfProfileOut( $fname );
214 $row->old_text
=ExternalStore
::fetchFromUrl($url);
217 if( !in_array( 'object', $flags ) ) {
221 if( in_array( 'gzip', $flags ) ) {
222 // This shouldn't happen, but a bug in the compress script
223 // may at times gzip-compress a HistoryBlob object row.
224 $obj = unserialize( gzinflate( $row->old_text
) );
226 $obj = unserialize( $row->old_text
);
229 if( !is_object( $obj ) ) {
230 // Correct for old double-serialization bug.
231 $obj = unserialize( $obj );
234 // Save this item for reference; if pulling many
235 // items in a row we'll likely use it again.
237 $wgBlobCache = array( $this->mOldId
=> $obj );
239 return $obj->getItem( $this->mHash
);
243 * Get the content hash
252 * To speed up conversion from 1.4 to 1.5 schema, text rows can refer to the
253 * leftover cur table as the backend. This avoids expensively copying hundreds
254 * of megabytes of data during the conversion downtime.
256 * Serialized HistoryBlobCurStub objects will be inserted into the text table
257 * on conversion if $wgFastSchemaUpgrades is set to true.
259 class HistoryBlobCurStub
{
263 * @param integer $curid The cur_id pointed to
265 function HistoryBlobCurStub( $curid = 0 ) {
266 $this->mCurId
= $curid;
270 * Sets the location (cur_id) of the main object to which this object
273 function setLocation( $id ) {
278 $dbr = wfGetDB( DB_SLAVE
);
279 $row = $dbr->selectRow( 'cur', array( 'cur_text' ), array( 'cur_id' => $this->mCurId
) );
283 return $row->cur_text
;
288 * Diff-based history compression
289 * Requires xdiff 1.5+ and zlib
291 class DiffHistoryBlob
implements HistoryBlob
{
292 /** Uncompressed item cache */
293 var $mItems = array();
296 * Array of diffs, where $this->mDiffs[0] is the diff between
297 * $this->mDiffs[0] and $this->mDiffs[1]
299 var $mDiffs = array();
302 * The key for getText()
312 * True if the object is locked against further writes
314 var $mFrozen = false;
317 function __construct() {
318 if ( !function_exists( 'xdiff_string_bdiff' ) ){
319 throw new MWException( "Need xdiff 1.5+ support to read or write DiffHistoryBlob\n" );
321 if ( !function_exists( 'gzdeflate' ) ) {
322 throw new MWException( "Need zlib support to read or write DiffHistoryBlob\n" );
326 function addItem( $text ) {
327 if ( $this->mFrozen
) {
328 throw new MWException( __METHOD__
.": Cannot add more items after sleep/wakeup" );
331 $this->mItems
[] = $text;
332 $i = count( $this->mItems
) - 1;
334 # Need to do a null concatenation with warnings off, due to bugs in the current version of xdiff
335 # "String is not zero-terminated"
336 wfSuppressWarnings();
337 $this->mDiffs
[] = xdiff_string_bdiff( $this->mItems
[$i-1], $text ) . '';
343 function getItem( $key ) {
344 if ( $key > count( $this->mDiffs
) +
1 ) {
347 $key = intval( $key );
349 return $this->mItems
[0];
352 $last = count( $this->mItems
) - 1;
353 for ( $i = $last +
1; $i <= $key; $i++
) {
354 # Need to do a null concatenation with warnings off, due to bugs in the current version of xdiff
355 # "String is not zero-terminated"
356 wfSuppressWarnings();
357 $this->mItems
[$i] = xdiff_string_bpatch( $this->mItems
[$i - 1], $this->mDiffs
[$i - 1] ) . '';
360 return $this->mItems
[$key];
363 function setText( $text ) {
364 $this->mDefaultKey
= $this->addItem( $text );
368 return $this->getItem( $this->mDefaultKey
);
372 if ( !isset( $this->mItems
[0] ) ) {
377 'base' => $this->mItems
[0],
378 'diffs' => $this->mDiffs
381 if ( isset( $this->mDefaultKey
) ) {
382 $info['default'] = $this->mDefaultKey
;
384 $this->mCompressed
= gzdeflate( serialize( $info ) );
385 return array( 'mCompressed' );
388 function __wakeup() {
389 // addItem() doesn't work if mItems is partially filled from mDiffs
390 $this->mFrozen
= true;
391 $info = unserialize( gzinflate( $this->mCompressed
) );
392 unset( $this->mCompressed
);
399 if ( isset( $info['default'] ) ) {
400 $this->mDefaultKey
= $info['default'];
402 $this->mItems
[0] = $info['base'];
403 $this->mDiffs
= $info['diffs'];