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 @list
( /* $proto */ ,$path)=explode('://',$url,2);
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
294 function setLocation( $id ) {
299 * @return string|false
302 $dbr = wfGetDB( DB_SLAVE
);
303 $row = $dbr->selectRow( 'cur', array( 'cur_text' ), array( 'cur_id' => $this->mCurId
) );
307 return $row->cur_text
;
312 * Diff-based history compression
313 * Requires xdiff 1.5+ and zlib
315 class DiffHistoryBlob
implements HistoryBlob
{
316 /** Uncompressed item cache */
317 var $mItems = array();
319 /** Total uncompressed size */
323 * Array of diffs. If a diff D from A to B is notated D = B - A, and Z is
326 * { item[map[i]] - item[map[i-1]] where i > 0
328 * { item[map[i]] - Z where i = 0
332 /** The diff map, see above */
336 * The key for getText()
346 * True if the object is locked against further writes
348 var $mFrozen = false;
351 * The maximum uncompressed size before the object becomes sad
352 * Should be less than max_allowed_packet
354 var $mMaxSize = 10000000;
357 * The maximum number of text items before the object becomes sad
359 var $mMaxCount = 100;
361 /** Constants from xdiff.h */
362 const XDL_BDOP_INS
= 1;
363 const XDL_BDOP_CPY
= 2;
364 const XDL_BDOP_INSB
= 3;
366 function __construct() {
367 if ( !function_exists( 'gzdeflate' ) ) {
368 throw new MWException( "Need zlib support to read or write DiffHistoryBlob\n" );
373 * @throws MWException
374 * @param $text string
377 function addItem( $text ) {
378 if ( $this->mFrozen
) {
379 throw new MWException( __METHOD__
.": Cannot add more items after sleep/wakeup" );
382 $this->mItems
[] = $text;
383 $this->mSize +
= strlen( $text );
384 $this->mDiffs
= null; // later
385 return count( $this->mItems
) - 1;
392 function getItem( $key ) {
393 return $this->mItems
[$key];
397 * @param $text string
399 function setText( $text ) {
400 $this->mDefaultKey
= $this->addItem( $text );
407 return $this->getItem( $this->mDefaultKey
);
411 * @throws MWException
413 function compress() {
414 if ( !function_exists( 'xdiff_string_rabdiff' ) ){
415 throw new MWException( "Need xdiff 1.5+ support to write DiffHistoryBlob\n" );
417 if ( isset( $this->mDiffs
) ) {
418 // Already compressed
421 if ( !count( $this->mItems
) ) {
426 // Create two diff sequences: one for main text and one for small text
441 for ( $i = 0; $i < count( $this->mItems
); $i++
) {
442 $text = $this->mItems
[$i];
446 $mainTail = $sequences['main']['tail'];
447 if ( strlen( $text ) < strlen( $mainTail ) * $smallFactor ) {
453 $seq =& $sequences[$seqName];
454 $tail = $seq['tail'];
455 $diff = $this->diff( $tail, $text );
456 $seq['diffs'][] = $diff;
458 $seq['tail'] = $text;
460 unset( $seq ); // unlink dangerous alias
462 // Knit the sequences together
464 $this->mDiffs
= array();
465 $this->mDiffMap
= array();
466 foreach ( $sequences as $seq ) {
467 if ( !count( $seq['diffs'] ) ) {
470 if ( $tail === '' ) {
471 $this->mDiffs
[] = $seq['diffs'][0];
473 $head = $this->patch( '', $seq['diffs'][0] );
474 $this->mDiffs
[] = $this->diff( $tail, $head );
476 $this->mDiffMap
[] = $seq['map'][0];
477 for ( $i = 1; $i < count( $seq['diffs'] ); $i++
) {
478 $this->mDiffs
[] = $seq['diffs'][$i];
479 $this->mDiffMap
[] = $seq['map'][$i];
481 $tail = $seq['tail'];
485 function diff( $t1, $t2 ) {
486 # Need to do a null concatenation with warnings off, due to bugs in the current version of xdiff
487 # "String is not zero-terminated"
488 wfSuppressWarnings();
489 $diff = xdiff_string_rabdiff( $t1, $t2 ) . '';
494 function patch( $base, $diff ) {
495 if ( function_exists( 'xdiff_string_bpatch' ) ) {
496 wfSuppressWarnings();
497 $text = xdiff_string_bpatch( $base, $diff ) . '';
502 # Pure PHP implementation
504 $header = unpack( 'Vofp/Vcsize', substr( $diff, 0, 8 ) );
506 # Check the checksum if mhash is available
507 if ( extension_loaded( 'mhash' ) ) {
508 $ofp = mhash( MHASH_ADLER32
, $base );
509 if ( $ofp !== substr( $diff, 0, 4 ) ) {
510 wfDebug( __METHOD__
. ": incorrect base checksum\n" );
514 if ( $header['csize'] != strlen( $base ) ) {
515 wfDebug( __METHOD__
. ": incorrect base length\n" );
521 while ( $p < strlen( $diff ) ) {
522 $x = unpack( 'Cop', substr( $diff, $p, 1 ) );
526 case self
::XDL_BDOP_INS
:
527 $x = unpack( 'Csize', substr( $diff, $p, 1 ) );
529 $out .= substr( $diff, $p, $x['size'] );
532 case self
::XDL_BDOP_INSB
:
533 $x = unpack( 'Vcsize', substr( $diff, $p, 4 ) );
535 $out .= substr( $diff, $p, $x['csize'] );
538 case self
::XDL_BDOP_CPY
:
539 $x = unpack( 'Voff/Vcsize', substr( $diff, $p, 8 ) );
541 $out .= substr( $base, $x['off'], $x['csize'] );
544 wfDebug( __METHOD__
.": invalid op\n" );
551 function uncompress() {
552 if ( !$this->mDiffs
) {
556 for ( $diffKey = 0; $diffKey < count( $this->mDiffs
); $diffKey++
) {
557 $textKey = $this->mDiffMap
[$diffKey];
558 $text = $this->patch( $tail, $this->mDiffs
[$diffKey] );
559 $this->mItems
[$textKey] = $text;
566 if ( !count( $this->mItems
) ) {
570 // Take forward differences to improve the compression ratio for sequences
573 foreach ( $this->mDiffMap
as $i ) {
581 'diffs' => $this->mDiffs
,
585 if ( isset( $this->mDefaultKey
) ) {
586 $info['default'] = $this->mDefaultKey
;
588 $this->mCompressed
= gzdeflate( serialize( $info ) );
589 return array( 'mCompressed' );
592 function __wakeup() {
593 // addItem() doesn't work if mItems is partially filled from mDiffs
594 $this->mFrozen
= true;
595 $info = unserialize( gzinflate( $this->mCompressed
) );
596 unset( $this->mCompressed
);
603 if ( isset( $info['default'] ) ) {
604 $this->mDefaultKey
= $info['default'];
606 $this->mDiffs
= $info['diffs'];
607 if ( isset( $info['base'] ) ) {
609 $this->mDiffMap
= range( 0, count( $this->mDiffs
) - 1 );
610 array_unshift( $this->mDiffs
,
611 pack( 'VVCV', 0, 0, self
::XDL_BDOP_INSB
, strlen( $info['base'] ) ) .
615 $map = explode( ',', $info['map'] );
617 $this->mDiffMap
= array();
618 foreach ( $map as $i ) {
620 $this->mDiffMap
[] = $cur;
627 * Helper function for compression jobs
628 * Returns true until the object is "full" and ready to be committed
631 return $this->mSize
< $this->mMaxSize
632 && count( $this->mItems
) < $this->mMaxCount
;