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 $this->mDefaultHash
= $this->addItem( $text );
78 public function getText() {
80 return $this->getItem( $this->mDefaultHash
);
86 public function removeItem( $hash ) {
87 $this->mSize
-= strlen( $this->mItems
[$hash] );
88 unset( $this->mItems
[$hash] );
92 * Compress the bulk data in the object
94 public function compress() {
95 if ( !$this->mCompressed
) {
96 $this->mItems
= gzdeflate( serialize( $this->mItems
) );
97 $this->mCompressed
= true;
102 * Uncompress bulk data
104 public function uncompress() {
105 if ( $this->mCompressed
) {
106 $this->mItems
= unserialize( gzinflate( $this->mItems
) );
107 $this->mCompressed
= false;
114 return array( 'mVersion', 'mCompressed', 'mItems', 'mDefaultHash' );
117 function __wakeup() {
122 * Helper function for compression jobs
123 * Returns true until the object is "full" and ready to be committed
125 public function isHappy( $maxFactor, $factorThreshold ) {
126 if ( count( $this->mItems
) == 0 ) {
129 if ( !$this->mFast
) {
131 $record = serialize( $this->mItems
);
132 $size = strlen( $record );
133 $avgUncompressed = $size / count( $this->mItems
);
134 $compressed = strlen( gzdeflate( $record ) );
136 if ( $compressed < $factorThreshold * 1024 ) {
139 return $avgUncompressed * $maxFactor < $compressed;
142 return count( $this->mItems
) <= 10;
149 * One-step cache variable to hold base blobs; operations that
150 * pull multiple revisions may often pull multiple times from
151 * the same blob. By keeping the last-used one open, we avoid
152 * redundant unserialization and decompression overhead.
155 $wgBlobCache = array();
159 * Pointer object for an item within a CGZ blob stored in the text table.
161 class HistoryBlobStub
{
162 var $mOldId, $mHash, $mRef;
165 * @param string $hash The content hash of the text
166 * @param integer $oldid The old_id for the CGZ object
168 function HistoryBlobStub( $hash = '', $oldid = 0 ) {
169 $this->mHash
= $hash;
173 * Sets the location (old_id) of the main object to which this object
176 function setLocation( $id ) {
181 * Sets the location (old_id) of the referring object
183 function setReferrer( $id ) {
188 * Gets the location of the referring object
190 function getReferrer() {
195 $fname = 'HistoryBlobStub::getText';
197 if( isset( $wgBlobCache[$this->mOldId
] ) ) {
198 $obj = $wgBlobCache[$this->mOldId
];
200 $dbr = wfGetDB( DB_SLAVE
);
201 $row = $dbr->selectRow( 'text', array( 'old_flags', 'old_text' ), array( 'old_id' => $this->mOldId
) );
205 $flags = explode( ',', $row->old_flags
);
206 if( in_array( 'external', $flags ) ) {
208 @list
( /* $proto */ ,$path)=explode('://',$url,2);
210 wfProfileOut( $fname );
213 $row->old_text
=ExternalStore
::fetchFromUrl($url);
216 if( !in_array( 'object', $flags ) ) {
220 if( in_array( 'gzip', $flags ) ) {
221 // This shouldn't happen, but a bug in the compress script
222 // may at times gzip-compress a HistoryBlob object row.
223 $obj = unserialize( gzinflate( $row->old_text
) );
225 $obj = unserialize( $row->old_text
);
228 if( !is_object( $obj ) ) {
229 // Correct for old double-serialization bug.
230 $obj = unserialize( $obj );
233 // Save this item for reference; if pulling many
234 // items in a row we'll likely use it again.
236 $wgBlobCache = array( $this->mOldId
=> $obj );
238 return $obj->getItem( $this->mHash
);
242 * Get the content hash
251 * To speed up conversion from 1.4 to 1.5 schema, text rows can refer to the
252 * leftover cur table as the backend. This avoids expensively copying hundreds
253 * of megabytes of data during the conversion downtime.
255 * Serialized HistoryBlobCurStub objects will be inserted into the text table
256 * on conversion if $wgFastSchemaUpgrades is set to true.
258 class HistoryBlobCurStub
{
262 * @param integer $curid The cur_id pointed to
264 function HistoryBlobCurStub( $curid = 0 ) {
265 $this->mCurId
= $curid;
269 * Sets the location (cur_id) of the main object to which this object
272 function setLocation( $id ) {
277 $dbr = wfGetDB( DB_SLAVE
);
278 $row = $dbr->selectRow( 'cur', array( 'cur_text' ), array( 'cur_id' => $this->mCurId
) );
282 return $row->cur_text
;
287 * Diff-based history compression
288 * Requires xdiff 1.5+ and zlib
290 class DiffHistoryBlob
implements HistoryBlob
{
291 /** Uncompressed item cache */
292 var $mItems = array();
295 * Array of diffs, where $this->mDiffs[0] is the diff between
296 * $this->mDiffs[0] and $this->mDiffs[1]
298 var $mDiffs = array();
301 * The key for getText()
311 * True if the object is locked against further writes
313 var $mFrozen = false;
316 function __construct() {
317 if ( !function_exists( 'xdiff_string_bdiff' ) ){
318 throw new MWException( "Need xdiff 1.5+ support to read or write DiffHistoryBlob\n" );
320 if ( !function_exists( 'gzdeflate' ) ) {
321 throw new MWException( "Need zlib support to read or write DiffHistoryBlob\n" );
325 function addItem( $text ) {
326 if ( $this->mFrozen
) {
327 throw new MWException( __METHOD__
.": Cannot add more items after sleep/wakeup" );
330 $this->mItems
[] = $text;
331 $i = count( $this->mItems
) - 1;
333 # Need to do a null concatenation with warnings off, due to bugs in the current version of xdiff
334 # "String is not zero-terminated"
335 wfSuppressWarnings();
336 $this->mDiffs
[] = xdiff_string_bdiff( $this->mItems
[$i-1], $text ) . '';
342 function getItem( $key ) {
343 if ( $key > count( $this->mDiffs
) +
1 ) {
346 $key = intval( $key );
348 return $this->mItems
[0];
351 $last = count( $this->mItems
) - 1;
352 for ( $i = $last +
1; $i <= $key; $i++
) {
353 # Need to do a null concatenation with warnings off, due to bugs in the current version of xdiff
354 # "String is not zero-terminated"
355 wfSuppressWarnings();
356 $this->mItems
[$i] = xdiff_string_bpatch( $this->mItems
[$i - 1], $this->mDiffs
[$i - 1] ) . '';
359 return $this->mItems
[$key];
362 function setText( $text ) {
363 $this->mDefaultKey
= $this->addItem( $text );
367 return $this->getItem( $this->mDefaultKey
);
371 if ( !isset( $this->mItems
[0] ) ) {
376 'base' => $this->mItems
[0],
377 'diffs' => $this->mDiffs
380 if ( isset( $this->mDefaultKey
) ) {
381 $info['default'] = $this->mDefaultKey
;
383 $this->mCompressed
= gzdeflate( serialize( $info ) );
384 return array( 'mCompressed' );
387 function __wakeup() {
388 // addItem() doesn't work if mItems is partially filled from mDiffs
389 $this->mFrozen
= true;
390 $info = unserialize( gzinflate( $this->mCompressed
) );
391 unset( $this->mCompressed
);
398 if ( isset( $info['default'] ) ) {
399 $this->mDefaultKey
= $info['default'];
401 $this->mItems
[0] = $info['base'];
402 $this->mDiffs
= $info['diffs'];