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];
99 public function setText( $text ) {
101 $this->mDefaultHash
= $this->addItem( $text );
107 public function getText() {
109 return $this->getItem( $this->mDefaultHash
);
115 * @param $hash string
117 public function removeItem( $hash ) {
118 $this->mSize
-= strlen( $this->mItems
[$hash] );
119 unset( $this->mItems
[$hash] );
123 * Compress the bulk data in the object
125 public function compress() {
126 if ( !$this->mCompressed
) {
127 $this->mItems
= gzdeflate( serialize( $this->mItems
) );
128 $this->mCompressed
= true;
133 * Uncompress bulk data
135 public function uncompress() {
136 if ( $this->mCompressed
) {
137 $this->mItems
= unserialize( gzinflate( $this->mItems
) );
138 $this->mCompressed
= false;
147 return array( 'mVersion', 'mCompressed', 'mItems', 'mDefaultHash' );
150 function __wakeup() {
155 * Helper function for compression jobs
156 * Returns true until the object is "full" and ready to be committed
160 public function isHappy() {
161 return $this->mSize
< $this->mMaxSize
162 && count( $this->mItems
) < $this->mMaxCount
;
168 * Pointer object for an item within a CGZ blob stored in the text table.
170 class HistoryBlobStub
{
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.
177 protected static $blobCache = array();
179 var $mOldId, $mHash, $mRef;
182 * @param $hash Strng: the content hash of the text
183 * @param $oldid Integer: the old_id for the CGZ object
185 function __construct( $hash = '', $oldid = 0 ) {
186 $this->mHash
= $hash;
190 * Sets the location (old_id) of the main object to which this object
193 function setLocation( $id ) {
198 * Sets the location (old_id) of the referring object
200 function setReferrer( $id ) {
205 * Gets the location of the referring object
207 function getReferrer() {
215 $fname = 'HistoryBlobStub::getText';
217 if( isset( self
::$blobCache[$this->mOldId
] ) ) {
218 $obj = self
::$blobCache[$this->mOldId
];
220 $dbr = wfGetDB( DB_SLAVE
);
221 $row = $dbr->selectRow( 'text', array( 'old_flags', 'old_text' ), array( 'old_id' => $this->mOldId
) );
225 $flags = explode( ',', $row->old_flags
);
226 if( in_array( 'external', $flags ) ) {
228 $parts = explode( '://', $url, 2 );
229 if ( !isset( $parts[1] ) ||
$parts[1] == '' ) {
230 wfProfileOut( $fname );
233 $row->old_text
= ExternalStore
::fetchFromUrl($url);
236 if( !in_array( 'object', $flags ) ) {
240 if( in_array( 'gzip', $flags ) ) {
241 // This shouldn't happen, but a bug in the compress script
242 // may at times gzip-compress a HistoryBlob object row.
243 $obj = unserialize( gzinflate( $row->old_text
) );
245 $obj = unserialize( $row->old_text
);
248 if( !is_object( $obj ) ) {
249 // Correct for old double-serialization bug.
250 $obj = unserialize( $obj );
253 // Save this item for reference; if pulling many
254 // items in a row we'll likely use it again.
256 self
::$blobCache = array( $this->mOldId
=> $obj );
258 return $obj->getItem( $this->mHash
);
262 * Get the content hash
273 * To speed up conversion from 1.4 to 1.5 schema, text rows can refer to the
274 * leftover cur table as the backend. This avoids expensively copying hundreds
275 * of megabytes of data during the conversion downtime.
277 * Serialized HistoryBlobCurStub objects will be inserted into the text table
278 * on conversion if $wgFastSchemaUpgrades is set to true.
280 class HistoryBlobCurStub
{
284 * @param $curid Integer: the cur_id pointed to
286 function __construct( $curid = 0 ) {
287 $this->mCurId
= $curid;
291 * Sets the location (cur_id) of the main object to which this object
296 function setLocation( $id ) {
301 * @return string|false
304 $dbr = wfGetDB( DB_SLAVE
);
305 $row = $dbr->selectRow( 'cur', array( 'cur_text' ), array( 'cur_id' => $this->mCurId
) );
309 return $row->cur_text
;
314 * Diff-based history compression
315 * Requires xdiff 1.5+ and zlib
317 class DiffHistoryBlob
implements HistoryBlob
{
318 /** Uncompressed item cache */
319 var $mItems = array();
321 /** Total uncompressed size */
325 * Array of diffs. If a diff D from A to B is notated D = B - A, and Z is
328 * { item[map[i]] - item[map[i-1]] where i > 0
330 * { item[map[i]] - Z where i = 0
334 /** The diff map, see above */
338 * The key for getText()
348 * True if the object is locked against further writes
350 var $mFrozen = false;
353 * The maximum uncompressed size before the object becomes sad
354 * Should be less than max_allowed_packet
356 var $mMaxSize = 10000000;
359 * The maximum number of text items before the object becomes sad
361 var $mMaxCount = 100;
363 /** Constants from xdiff.h */
364 const XDL_BDOP_INS
= 1;
365 const XDL_BDOP_CPY
= 2;
366 const XDL_BDOP_INSB
= 3;
368 function __construct() {
369 if ( !function_exists( 'gzdeflate' ) ) {
370 throw new MWException( "Need zlib support to read or write DiffHistoryBlob\n" );
375 * @throws MWException
376 * @param $text string
379 function addItem( $text ) {
380 if ( $this->mFrozen
) {
381 throw new MWException( __METHOD__
.": Cannot add more items after sleep/wakeup" );
384 $this->mItems
[] = $text;
385 $this->mSize +
= strlen( $text );
386 $this->mDiffs
= null; // later
387 return count( $this->mItems
) - 1;
394 function getItem( $key ) {
395 return $this->mItems
[$key];
399 * @param $text string
401 function setText( $text ) {
402 $this->mDefaultKey
= $this->addItem( $text );
409 return $this->getItem( $this->mDefaultKey
);
413 * @throws MWException
415 function compress() {
416 if ( !function_exists( 'xdiff_string_rabdiff' ) ){
417 throw new MWException( "Need xdiff 1.5+ support to write DiffHistoryBlob\n" );
419 if ( isset( $this->mDiffs
) ) {
420 // Already compressed
423 if ( !count( $this->mItems
) ) {
428 // Create two diff sequences: one for main text and one for small text
443 for ( $i = 0; $i < count( $this->mItems
); $i++
) {
444 $text = $this->mItems
[$i];
448 $mainTail = $sequences['main']['tail'];
449 if ( strlen( $text ) < strlen( $mainTail ) * $smallFactor ) {
455 $seq =& $sequences[$seqName];
456 $tail = $seq['tail'];
457 $diff = $this->diff( $tail, $text );
458 $seq['diffs'][] = $diff;
460 $seq['tail'] = $text;
462 unset( $seq ); // unlink dangerous alias
464 // Knit the sequences together
466 $this->mDiffs
= array();
467 $this->mDiffMap
= array();
468 foreach ( $sequences as $seq ) {
469 if ( !count( $seq['diffs'] ) ) {
472 if ( $tail === '' ) {
473 $this->mDiffs
[] = $seq['diffs'][0];
475 $head = $this->patch( '', $seq['diffs'][0] );
476 $this->mDiffs
[] = $this->diff( $tail, $head );
478 $this->mDiffMap
[] = $seq['map'][0];
479 for ( $i = 1; $i < count( $seq['diffs'] ); $i++
) {
480 $this->mDiffs
[] = $seq['diffs'][$i];
481 $this->mDiffMap
[] = $seq['map'][$i];
483 $tail = $seq['tail'];
492 function diff( $t1, $t2 ) {
493 # Need to do a null concatenation with warnings off, due to bugs in the current version of xdiff
494 # "String is not zero-terminated"
495 wfSuppressWarnings();
496 $diff = xdiff_string_rabdiff( $t1, $t2 ) . '';
504 * @return bool|string
506 function patch( $base, $diff ) {
507 if ( function_exists( 'xdiff_string_bpatch' ) ) {
508 wfSuppressWarnings();
509 $text = xdiff_string_bpatch( $base, $diff ) . '';
514 # Pure PHP implementation
516 $header = unpack( 'Vofp/Vcsize', substr( $diff, 0, 8 ) );
518 # Check the checksum if mhash is available
519 if ( extension_loaded( 'mhash' ) ) {
520 $ofp = mhash( MHASH_ADLER32
, $base );
521 if ( $ofp !== substr( $diff, 0, 4 ) ) {
522 wfDebug( __METHOD__
. ": incorrect base checksum\n" );
526 if ( $header['csize'] != strlen( $base ) ) {
527 wfDebug( __METHOD__
. ": incorrect base length\n" );
533 while ( $p < strlen( $diff ) ) {
534 $x = unpack( 'Cop', substr( $diff, $p, 1 ) );
538 case self
::XDL_BDOP_INS
:
539 $x = unpack( 'Csize', substr( $diff, $p, 1 ) );
541 $out .= substr( $diff, $p, $x['size'] );
544 case self
::XDL_BDOP_INSB
:
545 $x = unpack( 'Vcsize', substr( $diff, $p, 4 ) );
547 $out .= substr( $diff, $p, $x['csize'] );
550 case self
::XDL_BDOP_CPY
:
551 $x = unpack( 'Voff/Vcsize', substr( $diff, $p, 8 ) );
553 $out .= substr( $base, $x['off'], $x['csize'] );
556 wfDebug( __METHOD__
.": invalid op\n" );
563 function uncompress() {
564 if ( !$this->mDiffs
) {
568 for ( $diffKey = 0; $diffKey < count( $this->mDiffs
); $diffKey++
) {
569 $textKey = $this->mDiffMap
[$diffKey];
570 $text = $this->patch( $tail, $this->mDiffs
[$diffKey] );
571 $this->mItems
[$textKey] = $text;
581 if ( !count( $this->mItems
) ) {
585 // Take forward differences to improve the compression ratio for sequences
588 foreach ( $this->mDiffMap
as $i ) {
596 'diffs' => $this->mDiffs
,
600 if ( isset( $this->mDefaultKey
) ) {
601 $info['default'] = $this->mDefaultKey
;
603 $this->mCompressed
= gzdeflate( serialize( $info ) );
604 return array( 'mCompressed' );
607 function __wakeup() {
608 // addItem() doesn't work if mItems is partially filled from mDiffs
609 $this->mFrozen
= true;
610 $info = unserialize( gzinflate( $this->mCompressed
) );
611 unset( $this->mCompressed
);
618 if ( isset( $info['default'] ) ) {
619 $this->mDefaultKey
= $info['default'];
621 $this->mDiffs
= $info['diffs'];
622 if ( isset( $info['base'] ) ) {
624 $this->mDiffMap
= range( 0, count( $this->mDiffs
) - 1 );
625 array_unshift( $this->mDiffs
,
626 pack( 'VVCV', 0, 0, self
::XDL_BDOP_INSB
, strlen( $info['base'] ) ) .
630 $map = explode( ',', $info['map'] );
632 $this->mDiffMap
= array();
633 foreach ( $map as $i ) {
635 $this->mDiffMap
[] = $cur;
642 * Helper function for compression jobs
643 * Returns true until the object is "full" and ready to be committed
648 return $this->mSize
< $this->mMaxSize
649 && count( $this->mItems
) < $this->mMaxCount
;