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
17 * @return String: the key for getItem()
19 function addItem( $text );
22 * Get item by key, or false if the key is not present
26 * @return String or false
28 function getItem( $key );
31 * Set the "default text"
32 * This concept is an odd property of the current DB schema, whereby each text item has a revision
33 * associated with it. The default text is the text of the associated revision. There may, however,
34 * be other revisions in the same object.
36 * Default text is not required for two-part external storage URLs.
40 function setText( $text );
43 * Get default text. This is called from Revision::getRevisionText()
51 * Concatenated gzip (CGZ) storage
52 * Improves compression ratio by concatenating like objects before gzipping
54 class ConcatenatedGzipHistoryBlob
implements HistoryBlob
56 public $mVersion = 0, $mCompressed = false, $mItems = array(), $mDefaultHash = '';
58 public $mMaxSize = 10000000;
59 public $mMaxCount = 100;
62 public function __construct() {
63 if ( !function_exists( 'gzdeflate' ) ) {
64 throw new MWException( "Need zlib support to read or write this kind of history object (ConcatenatedGzipHistoryBlob)\n" );
72 public function addItem( $text ) {
75 if ( !isset( $this->mItems
[$hash] ) ) {
76 $this->mItems
[$hash] = $text;
77 $this->mSize +
= strlen( $text );
86 public function getItem( $hash ) {
88 if ( array_key_exists( $hash, $this->mItems
) ) {
89 return $this->mItems
[$hash];
98 public function setText( $text ) {
100 $this->mDefaultHash
= $this->addItem( $text );
106 public function getText() {
108 return $this->getItem( $this->mDefaultHash
);
114 * @param $hash string
116 public function removeItem( $hash ) {
117 $this->mSize
-= strlen( $this->mItems
[$hash] );
118 unset( $this->mItems
[$hash] );
122 * Compress the bulk data in the object
124 public function compress() {
125 if ( !$this->mCompressed
) {
126 $this->mItems
= gzdeflate( serialize( $this->mItems
) );
127 $this->mCompressed
= true;
132 * Uncompress bulk data
134 public function uncompress() {
135 if ( $this->mCompressed
) {
136 $this->mItems
= unserialize( gzinflate( $this->mItems
) );
137 $this->mCompressed
= false;
146 return array( 'mVersion', 'mCompressed', 'mItems', 'mDefaultHash' );
149 function __wakeup() {
154 * Helper function for compression jobs
155 * Returns true until the object is "full" and ready to be committed
159 public function isHappy() {
160 return $this->mSize
< $this->mMaxSize
161 && count( $this->mItems
) < $this->mMaxCount
;
167 * Pointer object for an item within a CGZ blob stored in the text table.
169 class HistoryBlobStub
{
171 * One-step cache variable to hold base blobs; operations that
172 * pull multiple revisions may often pull multiple times from
173 * the same blob. By keeping the last-used one open, we avoid
174 * redundant unserialization and decompression overhead.
176 protected static $blobCache = array();
178 var $mOldId, $mHash, $mRef;
181 * @param $hash Strng: the content hash of the text
182 * @param $oldid Integer: the old_id for the CGZ object
184 function __construct( $hash = '', $oldid = 0 ) {
185 $this->mHash
= $hash;
189 * Sets the location (old_id) of the main object to which this object
192 function setLocation( $id ) {
197 * Sets the location (old_id) of the referring object
199 function setReferrer( $id ) {
204 * Gets the location of the referring object
206 function getReferrer() {
214 $fname = 'HistoryBlobStub::getText';
216 if( isset( self
::$blobCache[$this->mOldId
] ) ) {
217 $obj = self
::$blobCache[$this->mOldId
];
219 $dbr = wfGetDB( DB_SLAVE
);
220 $row = $dbr->selectRow( 'text', array( 'old_flags', 'old_text' ), array( 'old_id' => $this->mOldId
) );
224 $flags = explode( ',', $row->old_flags
);
225 if( in_array( 'external', $flags ) ) {
227 $parts = explode( '://', $url, 2 );
228 if ( !isset( $parts[1] ) ||
$parts[1] == '' ) {
229 wfProfileOut( $fname );
232 $row->old_text
= ExternalStore
::fetchFromUrl($url);
235 if( !in_array( 'object', $flags ) ) {
239 if( in_array( 'gzip', $flags ) ) {
240 // This shouldn't happen, but a bug in the compress script
241 // may at times gzip-compress a HistoryBlob object row.
242 $obj = unserialize( gzinflate( $row->old_text
) );
244 $obj = unserialize( $row->old_text
);
247 if( !is_object( $obj ) ) {
248 // Correct for old double-serialization bug.
249 $obj = unserialize( $obj );
252 // Save this item for reference; if pulling many
253 // items in a row we'll likely use it again.
255 self
::$blobCache = array( $this->mOldId
=> $obj );
257 return $obj->getItem( $this->mHash
);
261 * Get the content hash
272 * To speed up conversion from 1.4 to 1.5 schema, text rows can refer to the
273 * leftover cur table as the backend. This avoids expensively copying hundreds
274 * of megabytes of data during the conversion downtime.
276 * Serialized HistoryBlobCurStub objects will be inserted into the text table
277 * on conversion if $wgFastSchemaUpgrades is set to true.
279 class HistoryBlobCurStub
{
283 * @param $curid Integer: the cur_id pointed to
285 function __construct( $curid = 0 ) {
286 $this->mCurId
= $curid;
290 * Sets the location (cur_id) of the main object to which this object
295 function setLocation( $id ) {
300 * @return string|false
303 $dbr = wfGetDB( DB_SLAVE
);
304 $row = $dbr->selectRow( 'cur', array( 'cur_text' ), array( 'cur_id' => $this->mCurId
) );
308 return $row->cur_text
;
313 * Diff-based history compression
314 * Requires xdiff 1.5+ and zlib
316 class DiffHistoryBlob
implements HistoryBlob
{
317 /** Uncompressed item cache */
318 var $mItems = array();
320 /** Total uncompressed size */
324 * Array of diffs. If a diff D from A to B is notated D = B - A, and Z is
327 * { item[map[i]] - item[map[i-1]] where i > 0
329 * { item[map[i]] - Z where i = 0
333 /** The diff map, see above */
337 * The key for getText()
347 * True if the object is locked against further writes
349 var $mFrozen = false;
352 * The maximum uncompressed size before the object becomes sad
353 * Should be less than max_allowed_packet
355 var $mMaxSize = 10000000;
358 * The maximum number of text items before the object becomes sad
360 var $mMaxCount = 100;
362 /** Constants from xdiff.h */
363 const XDL_BDOP_INS
= 1;
364 const XDL_BDOP_CPY
= 2;
365 const XDL_BDOP_INSB
= 3;
367 function __construct() {
368 if ( !function_exists( 'gzdeflate' ) ) {
369 throw new MWException( "Need zlib support to read or write DiffHistoryBlob\n" );
374 * @throws MWException
375 * @param $text string
378 function addItem( $text ) {
379 if ( $this->mFrozen
) {
380 throw new MWException( __METHOD__
.": Cannot add more items after sleep/wakeup" );
383 $this->mItems
[] = $text;
384 $this->mSize +
= strlen( $text );
385 $this->mDiffs
= null; // later
386 return count( $this->mItems
) - 1;
393 function getItem( $key ) {
394 return $this->mItems
[$key];
398 * @param $text string
400 function setText( $text ) {
401 $this->mDefaultKey
= $this->addItem( $text );
408 return $this->getItem( $this->mDefaultKey
);
412 * @throws MWException
414 function compress() {
415 if ( !function_exists( 'xdiff_string_rabdiff' ) ){
416 throw new MWException( "Need xdiff 1.5+ support to write DiffHistoryBlob\n" );
418 if ( isset( $this->mDiffs
) ) {
419 // Already compressed
422 if ( !count( $this->mItems
) ) {
427 // Create two diff sequences: one for main text and one for small text
442 for ( $i = 0; $i < count( $this->mItems
); $i++
) {
443 $text = $this->mItems
[$i];
447 $mainTail = $sequences['main']['tail'];
448 if ( strlen( $text ) < strlen( $mainTail ) * $smallFactor ) {
454 $seq =& $sequences[$seqName];
455 $tail = $seq['tail'];
456 $diff = $this->diff( $tail, $text );
457 $seq['diffs'][] = $diff;
459 $seq['tail'] = $text;
461 unset( $seq ); // unlink dangerous alias
463 // Knit the sequences together
465 $this->mDiffs
= array();
466 $this->mDiffMap
= array();
467 foreach ( $sequences as $seq ) {
468 if ( !count( $seq['diffs'] ) ) {
471 if ( $tail === '' ) {
472 $this->mDiffs
[] = $seq['diffs'][0];
474 $head = $this->patch( '', $seq['diffs'][0] );
475 $this->mDiffs
[] = $this->diff( $tail, $head );
477 $this->mDiffMap
[] = $seq['map'][0];
478 for ( $i = 1; $i < count( $seq['diffs'] ); $i++
) {
479 $this->mDiffs
[] = $seq['diffs'][$i];
480 $this->mDiffMap
[] = $seq['map'][$i];
482 $tail = $seq['tail'];
491 function diff( $t1, $t2 ) {
492 # Need to do a null concatenation with warnings off, due to bugs in the current version of xdiff
493 # "String is not zero-terminated"
494 wfSuppressWarnings();
495 $diff = xdiff_string_rabdiff( $t1, $t2 ) . '';
503 * @return bool|string
505 function patch( $base, $diff ) {
506 if ( function_exists( 'xdiff_string_bpatch' ) ) {
507 wfSuppressWarnings();
508 $text = xdiff_string_bpatch( $base, $diff ) . '';
513 # Pure PHP implementation
515 $header = unpack( 'Vofp/Vcsize', substr( $diff, 0, 8 ) );
517 # Check the checksum if mhash is available
518 if ( extension_loaded( 'mhash' ) ) {
519 $ofp = mhash( MHASH_ADLER32
, $base );
520 if ( $ofp !== substr( $diff, 0, 4 ) ) {
521 wfDebug( __METHOD__
. ": incorrect base checksum\n" );
525 if ( $header['csize'] != strlen( $base ) ) {
526 wfDebug( __METHOD__
. ": incorrect base length\n" );
532 while ( $p < strlen( $diff ) ) {
533 $x = unpack( 'Cop', substr( $diff, $p, 1 ) );
537 case self
::XDL_BDOP_INS
:
538 $x = unpack( 'Csize', substr( $diff, $p, 1 ) );
540 $out .= substr( $diff, $p, $x['size'] );
543 case self
::XDL_BDOP_INSB
:
544 $x = unpack( 'Vcsize', substr( $diff, $p, 4 ) );
546 $out .= substr( $diff, $p, $x['csize'] );
549 case self
::XDL_BDOP_CPY
:
550 $x = unpack( 'Voff/Vcsize', substr( $diff, $p, 8 ) );
552 $out .= substr( $base, $x['off'], $x['csize'] );
555 wfDebug( __METHOD__
.": invalid op\n" );
562 function uncompress() {
563 if ( !$this->mDiffs
) {
567 for ( $diffKey = 0; $diffKey < count( $this->mDiffs
); $diffKey++
) {
568 $textKey = $this->mDiffMap
[$diffKey];
569 $text = $this->patch( $tail, $this->mDiffs
[$diffKey] );
570 $this->mItems
[$textKey] = $text;
580 if ( !count( $this->mItems
) ) {
584 // Take forward differences to improve the compression ratio for sequences
587 foreach ( $this->mDiffMap
as $i ) {
595 'diffs' => $this->mDiffs
,
599 if ( isset( $this->mDefaultKey
) ) {
600 $info['default'] = $this->mDefaultKey
;
602 $this->mCompressed
= gzdeflate( serialize( $info ) );
603 return array( 'mCompressed' );
606 function __wakeup() {
607 // addItem() doesn't work if mItems is partially filled from mDiffs
608 $this->mFrozen
= true;
609 $info = unserialize( gzinflate( $this->mCompressed
) );
610 unset( $this->mCompressed
);
617 if ( isset( $info['default'] ) ) {
618 $this->mDefaultKey
= $info['default'];
620 $this->mDiffs
= $info['diffs'];
621 if ( isset( $info['base'] ) ) {
623 $this->mDiffMap
= range( 0, count( $this->mDiffs
) - 1 );
624 array_unshift( $this->mDiffs
,
625 pack( 'VVCV', 0, 0, self
::XDL_BDOP_INSB
, strlen( $info['base'] ) ) .
629 $map = explode( ',', $info['map'] );
631 $this->mDiffMap
= array();
632 foreach ( $map as $i ) {
634 $this->mDiffMap
[] = $cur;
641 * Helper function for compression jobs
642 * Returns true until the object is "full" and ready to be committed
647 return $this->mSize
< $this->mMaxSize
648 && count( $this->mItems
) < $this->mMaxCount
;