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
15 * @return String: the key for getItem()
17 public function addItem( $text );
20 * Get item by key, or false if the key is not present
22 * @return String or false
24 public function getItem( $key );
27 * Set the "default text"
28 * This concept is an odd property of the current DB schema, whereby each text item has a revision
29 * associated with it. The default text is the text of the associated revision. There may, however,
30 * be other revisions in the same object.
32 * Default text is not required for two-part external storage URLs.
34 public function setText( $text );
37 * Get default text. This is called from Revision::getRevisionText()
45 * Concatenated gzip (CGZ) storage
46 * Improves compression ratio by concatenating like objects before gzipping
48 class ConcatenatedGzipHistoryBlob
implements HistoryBlob
50 public $mVersion = 0, $mCompressed = false, $mItems = array(), $mDefaultHash = '';
52 public $mMaxSize = 10000000;
53 public $mMaxCount = 100;
56 public function ConcatenatedGzipHistoryBlob() {
57 if ( !function_exists( 'gzdeflate' ) ) {
58 throw new MWException( "Need zlib support to read or write this kind of history object (ConcatenatedGzipHistoryBlob)\n" );
62 public function addItem( $text ) {
65 if ( !isset( $this->mItems
[$hash] ) ) {
66 $this->mItems
[$hash] = $text;
67 $this->mSize +
= strlen( $text );
72 public function getItem( $hash ) {
74 if ( array_key_exists( $hash, $this->mItems
) ) {
75 return $this->mItems
[$hash];
81 public function setText( $text ) {
83 $this->mDefaultHash
= $this->addItem( $text );
86 public function getText() {
88 return $this->getItem( $this->mDefaultHash
);
94 public function removeItem( $hash ) {
95 $this->mSize
-= strlen( $this->mItems
[$hash] );
96 unset( $this->mItems
[$hash] );
100 * Compress the bulk data in the object
102 public function compress() {
103 if ( !$this->mCompressed
) {
104 $this->mItems
= gzdeflate( serialize( $this->mItems
) );
105 $this->mCompressed
= true;
110 * Uncompress bulk data
112 public function uncompress() {
113 if ( $this->mCompressed
) {
114 $this->mItems
= unserialize( gzinflate( $this->mItems
) );
115 $this->mCompressed
= false;
122 return array( 'mVersion', 'mCompressed', 'mItems', 'mDefaultHash' );
125 function __wakeup() {
130 * Helper function for compression jobs
131 * Returns true until the object is "full" and ready to be committed
133 public function isHappy() {
134 return $this->mSize
< $this->mMaxSize
135 && count( $this->mItems
) < $this->mMaxCount
;
143 * Pointer object for an item within a CGZ blob stored in the text table.
145 class HistoryBlobStub
{
147 * One-step cache variable to hold base blobs; operations that
148 * pull multiple revisions may often pull multiple times from
149 * the same blob. By keeping the last-used one open, we avoid
150 * redundant unserialization and decompression overhead.
152 protected static $blobCache = array();
154 var $mOldId, $mHash, $mRef;
157 * @param $hash Strng: the content hash of the text
158 * @param $oldid Integer: the old_id for the CGZ object
160 function HistoryBlobStub( $hash = '', $oldid = 0 ) {
161 $this->mHash
= $hash;
165 * Sets the location (old_id) of the main object to which this object
168 function setLocation( $id ) {
173 * Sets the location (old_id) of the referring object
175 function setReferrer( $id ) {
180 * Gets the location of the referring object
182 function getReferrer() {
187 $fname = 'HistoryBlobStub::getText';
189 if( isset( self
::$blobCache[$this->mOldId
] ) ) {
190 $obj = self
::$blobCache[$this->mOldId
];
192 $dbr = wfGetDB( DB_SLAVE
);
193 $row = $dbr->selectRow( 'text', array( 'old_flags', 'old_text' ), array( 'old_id' => $this->mOldId
) );
197 $flags = explode( ',', $row->old_flags
);
198 if( in_array( 'external', $flags ) ) {
200 @list
( /* $proto */ ,$path)=explode('://',$url,2);
202 wfProfileOut( $fname );
205 $row->old_text
=ExternalStore
::fetchFromUrl($url);
208 if( !in_array( 'object', $flags ) ) {
212 if( in_array( 'gzip', $flags ) ) {
213 // This shouldn't happen, but a bug in the compress script
214 // may at times gzip-compress a HistoryBlob object row.
215 $obj = unserialize( gzinflate( $row->old_text
) );
217 $obj = unserialize( $row->old_text
);
220 if( !is_object( $obj ) ) {
221 // Correct for old double-serialization bug.
222 $obj = unserialize( $obj );
225 // Save this item for reference; if pulling many
226 // items in a row we'll likely use it again.
228 self
::$blobCache = array( $this->mOldId
=> $obj );
230 return $obj->getItem( $this->mHash
);
234 * Get the content hash
243 * To speed up conversion from 1.4 to 1.5 schema, text rows can refer to the
244 * leftover cur table as the backend. This avoids expensively copying hundreds
245 * of megabytes of data during the conversion downtime.
247 * Serialized HistoryBlobCurStub objects will be inserted into the text table
248 * on conversion if $wgFastSchemaUpgrades is set to true.
250 class HistoryBlobCurStub
{
254 * @param $curid Integer: the cur_id pointed to
256 function HistoryBlobCurStub( $curid = 0 ) {
257 $this->mCurId
= $curid;
261 * Sets the location (cur_id) of the main object to which this object
264 function setLocation( $id ) {
269 $dbr = wfGetDB( DB_SLAVE
);
270 $row = $dbr->selectRow( 'cur', array( 'cur_text' ), array( 'cur_id' => $this->mCurId
) );
274 return $row->cur_text
;
279 * Diff-based history compression
280 * Requires xdiff 1.5+ and zlib
282 class DiffHistoryBlob
implements HistoryBlob
{
283 /** Uncompressed item cache */
284 var $mItems = array();
286 /** Total uncompressed size */
290 * Array of diffs. If a diff D from A to B is notated D = B - A, and Z is
293 * { item[map[i]] - item[map[i-1]] where i > 0
295 * { item[map[i]] - Z where i = 0
299 /** The diff map, see above */
303 * The key for getText()
313 * True if the object is locked against further writes
315 var $mFrozen = false;
318 * The maximum uncompressed size before the object becomes sad
319 * Should be less than max_allowed_packet
321 var $mMaxSize = 10000000;
324 * The maximum number of text items before the object becomes sad
326 var $mMaxCount = 100;
328 /** Constants from xdiff.h */
329 const XDL_BDOP_INS
= 1;
330 const XDL_BDOP_CPY
= 2;
331 const XDL_BDOP_INSB
= 3;
333 function __construct() {
334 if ( !function_exists( 'gzdeflate' ) ) {
335 throw new MWException( "Need zlib support to read or write DiffHistoryBlob\n" );
339 function addItem( $text ) {
340 if ( $this->mFrozen
) {
341 throw new MWException( __METHOD__
.": Cannot add more items after sleep/wakeup" );
344 $this->mItems
[] = $text;
345 $this->mSize +
= strlen( $text );
346 $this->mDiffs
= null; // later
347 return count( $this->mItems
) - 1;
350 function getItem( $key ) {
351 return $this->mItems
[$key];
354 function setText( $text ) {
355 $this->mDefaultKey
= $this->addItem( $text );
359 return $this->getItem( $this->mDefaultKey
);
362 function compress() {
363 if ( !function_exists( 'xdiff_string_rabdiff' ) ){
364 throw new MWException( "Need xdiff 1.5+ support to write DiffHistoryBlob\n" );
366 if ( isset( $this->mDiffs
) ) {
367 // Already compressed
370 if ( !count( $this->mItems
) ) {
375 // Create two diff sequences: one for main text and one for small text
390 for ( $i = 0; $i < count( $this->mItems
); $i++
) {
391 $text = $this->mItems
[$i];
395 $mainTail = $sequences['main']['tail'];
396 if ( strlen( $text ) < strlen( $mainTail ) * $smallFactor ) {
402 $seq =& $sequences[$seqName];
403 $tail = $seq['tail'];
404 $diff = $this->diff( $tail, $text );
405 $seq['diffs'][] = $diff;
407 $seq['tail'] = $text;
409 unset( $seq ); // unlink dangerous alias
411 // Knit the sequences together
413 $this->mDiffs
= array();
414 $this->mDiffMap
= array();
415 foreach ( $sequences as $seq ) {
416 if ( !count( $seq['diffs'] ) ) {
419 if ( $tail === '' ) {
420 $this->mDiffs
[] = $seq['diffs'][0];
422 $head = $this->patch( '', $seq['diffs'][0] );
423 $this->mDiffs
[] = $this->diff( $tail, $head );
425 $this->mDiffMap
[] = $seq['map'][0];
426 for ( $i = 1; $i < count( $seq['diffs'] ); $i++
) {
427 $this->mDiffs
[] = $seq['diffs'][$i];
428 $this->mDiffMap
[] = $seq['map'][$i];
430 $tail = $seq['tail'];
434 function diff( $t1, $t2 ) {
435 # Need to do a null concatenation with warnings off, due to bugs in the current version of xdiff
436 # "String is not zero-terminated"
437 wfSuppressWarnings();
438 $diff = xdiff_string_rabdiff( $t1, $t2 ) . '';
443 function patch( $base, $diff ) {
444 if ( function_exists( 'xdiff_string_bpatch' ) ) {
445 wfSuppressWarnings();
446 $text = xdiff_string_bpatch( $base, $diff ) . '';
451 # Pure PHP implementation
453 $header = unpack( 'Vofp/Vcsize', substr( $diff, 0, 8 ) );
455 # Check the checksum if mhash is available
456 if ( extension_loaded( 'mhash' ) ) {
457 $ofp = mhash( MHASH_ADLER32
, $base );
458 if ( $ofp !== substr( $diff, 0, 4 ) ) {
459 wfDebug( __METHOD__
. ": incorrect base checksum\n" );
463 if ( $header['csize'] != strlen( $base ) ) {
464 wfDebug( __METHOD__
. ": incorrect base length\n" );
470 while ( $p < strlen( $diff ) ) {
471 $x = unpack( 'Cop', substr( $diff, $p, 1 ) );
475 case self
::XDL_BDOP_INS
:
476 $x = unpack( 'Csize', substr( $diff, $p, 1 ) );
478 $out .= substr( $diff, $p, $x['size'] );
481 case self
::XDL_BDOP_INSB
:
482 $x = unpack( 'Vcsize', substr( $diff, $p, 4 ) );
484 $out .= substr( $diff, $p, $x['csize'] );
487 case self
::XDL_BDOP_CPY
:
488 $x = unpack( 'Voff/Vcsize', substr( $diff, $p, 8 ) );
490 $out .= substr( $base, $x['off'], $x['csize'] );
493 wfDebug( __METHOD__
.": invalid op\n" );
500 function uncompress() {
501 if ( !$this->mDiffs
) {
505 for ( $diffKey = 0; $diffKey < count( $this->mDiffs
); $diffKey++
) {
506 $textKey = $this->mDiffMap
[$diffKey];
507 $text = $this->patch( $tail, $this->mDiffs
[$diffKey] );
508 $this->mItems
[$textKey] = $text;
515 if ( !count( $this->mItems
) ) {
519 // Take forward differences to improve the compression ratio for sequences
522 foreach ( $this->mDiffMap
as $i ) {
530 'diffs' => $this->mDiffs
,
534 if ( isset( $this->mDefaultKey
) ) {
535 $info['default'] = $this->mDefaultKey
;
537 $this->mCompressed
= gzdeflate( serialize( $info ) );
538 return array( 'mCompressed' );
541 function __wakeup() {
542 // addItem() doesn't work if mItems is partially filled from mDiffs
543 $this->mFrozen
= true;
544 $info = unserialize( gzinflate( $this->mCompressed
) );
545 unset( $this->mCompressed
);
552 if ( isset( $info['default'] ) ) {
553 $this->mDefaultKey
= $info['default'];
555 $this->mDiffs
= $info['diffs'];
556 if ( isset( $info['base'] ) ) {
558 $this->mDiffMap
= range( 0, count( $this->mDiffs
) - 1 );
559 array_unshift( $this->mDiffs
,
560 pack( 'VVCV', 0, 0, self
::XDL_BDOP_INSB
, strlen( $info['base'] ) ) .
564 $map = explode( ',', $info['map'] );
566 $this->mDiffMap
= array();
567 foreach ( $map as $i ) {
569 $this->mDiffMap
[] = $cur;
576 * Helper function for compression jobs
577 * Returns true until the object is "full" and ready to be committed
580 return $this->mSize
< $this->mMaxSize
581 && count( $this->mItems
) < $this->mMaxCount
;