Improved $wgRC2UDPInterwikiPrefix feature, so that it can actually be a prefix and...
[mediawiki.git] / includes / HistoryBlob.php
blobdda50781077d30903e3c3f70f0c13c15652776dc
1 <?php
3 /**
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.
7 */
8 interface HistoryBlob
10 /**
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
13 * database
14 * Returns the key for getItem()
16 public function addItem( $text );
18 /**
19 * Get item by key, or false if the key is not present
21 public function getItem( $key );
23 /**
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 );
33 /**
34 * Get default text. This is called from Revision::getRevisionText()
36 function getText();
39 /**
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 $mSize = 0;
47 public $mMaxSize = 10000000;
48 public $mMaxCount = 100;
50 /** Constructor */
51 public function ConcatenatedGzipHistoryBlob() {
52 if ( !function_exists( 'gzdeflate' ) ) {
53 throw new MWException( "Need zlib support to read or write this kind of history object (ConcatenatedGzipHistoryBlob)\n" );
57 public function addItem( $text ) {
58 $this->uncompress();
59 $hash = md5( $text );
60 if ( !isset( $this->mItems[$hash] ) ) {
61 $this->mItems[$hash] = $text;
62 $this->mSize += strlen( $text );
64 return $hash;
67 public function getItem( $hash ) {
68 $this->uncompress();
69 if ( array_key_exists( $hash, $this->mItems ) ) {
70 return $this->mItems[$hash];
71 } else {
72 return false;
76 public function setText( $text ) {
77 $this->uncompress();
78 $this->mDefaultHash = $this->addItem( $text );
81 public function getText() {
82 $this->uncompress();
83 return $this->getItem( $this->mDefaultHash );
86 /**
87 * Remove an item
89 public function removeItem( $hash ) {
90 $this->mSize -= strlen( $this->mItems[$hash] );
91 unset( $this->mItems[$hash] );
94 /**
95 * Compress the bulk data in the object
97 public function compress() {
98 if ( !$this->mCompressed ) {
99 $this->mItems = gzdeflate( serialize( $this->mItems ) );
100 $this->mCompressed = true;
105 * Uncompress bulk data
107 public function uncompress() {
108 if ( $this->mCompressed ) {
109 $this->mItems = unserialize( gzinflate( $this->mItems ) );
110 $this->mCompressed = false;
115 function __sleep() {
116 $this->compress();
117 return array( 'mVersion', 'mCompressed', 'mItems', 'mDefaultHash' );
120 function __wakeup() {
121 $this->uncompress();
125 * Helper function for compression jobs
126 * Returns true until the object is "full" and ready to be committed
128 public function isHappy() {
129 return $this->mSize < $this->mMaxSize
130 && count( $this->mItems ) < $this->mMaxCount;
136 * One-step cache variable to hold base blobs; operations that
137 * pull multiple revisions may often pull multiple times from
138 * the same blob. By keeping the last-used one open, we avoid
139 * redundant unserialization and decompression overhead.
141 global $wgBlobCache;
142 $wgBlobCache = array();
146 * Pointer object for an item within a CGZ blob stored in the text table.
148 class HistoryBlobStub {
149 var $mOldId, $mHash, $mRef;
152 * @param string $hash The content hash of the text
153 * @param integer $oldid The old_id for the CGZ object
155 function HistoryBlobStub( $hash = '', $oldid = 0 ) {
156 $this->mHash = $hash;
160 * Sets the location (old_id) of the main object to which this object
161 * points
163 function setLocation( $id ) {
164 $this->mOldId = $id;
168 * Sets the location (old_id) of the referring object
170 function setReferrer( $id ) {
171 $this->mRef = $id;
175 * Gets the location of the referring object
177 function getReferrer() {
178 return $this->mRef;
181 function getText() {
182 $fname = 'HistoryBlobStub::getText';
183 global $wgBlobCache;
184 if( isset( $wgBlobCache[$this->mOldId] ) ) {
185 $obj = $wgBlobCache[$this->mOldId];
186 } else {
187 $dbr = wfGetDB( DB_SLAVE );
188 $row = $dbr->selectRow( 'text', array( 'old_flags', 'old_text' ), array( 'old_id' => $this->mOldId ) );
189 if( !$row ) {
190 return false;
192 $flags = explode( ',', $row->old_flags );
193 if( in_array( 'external', $flags ) ) {
194 $url=$row->old_text;
195 @list( /* $proto */ ,$path)=explode('://',$url,2);
196 if ($path=="") {
197 wfProfileOut( $fname );
198 return false;
200 $row->old_text=ExternalStore::fetchFromUrl($url);
203 if( !in_array( 'object', $flags ) ) {
204 return false;
207 if( in_array( 'gzip', $flags ) ) {
208 // This shouldn't happen, but a bug in the compress script
209 // may at times gzip-compress a HistoryBlob object row.
210 $obj = unserialize( gzinflate( $row->old_text ) );
211 } else {
212 $obj = unserialize( $row->old_text );
215 if( !is_object( $obj ) ) {
216 // Correct for old double-serialization bug.
217 $obj = unserialize( $obj );
220 // Save this item for reference; if pulling many
221 // items in a row we'll likely use it again.
222 $obj->uncompress();
223 $wgBlobCache = array( $this->mOldId => $obj );
225 return $obj->getItem( $this->mHash );
229 * Get the content hash
231 function getHash() {
232 return $this->mHash;
238 * To speed up conversion from 1.4 to 1.5 schema, text rows can refer to the
239 * leftover cur table as the backend. This avoids expensively copying hundreds
240 * of megabytes of data during the conversion downtime.
242 * Serialized HistoryBlobCurStub objects will be inserted into the text table
243 * on conversion if $wgFastSchemaUpgrades is set to true.
245 class HistoryBlobCurStub {
246 var $mCurId;
249 * @param integer $curid The cur_id pointed to
251 function HistoryBlobCurStub( $curid = 0 ) {
252 $this->mCurId = $curid;
256 * Sets the location (cur_id) of the main object to which this object
257 * points
259 function setLocation( $id ) {
260 $this->mCurId = $id;
263 function getText() {
264 $dbr = wfGetDB( DB_SLAVE );
265 $row = $dbr->selectRow( 'cur', array( 'cur_text' ), array( 'cur_id' => $this->mCurId ) );
266 if( !$row ) {
267 return false;
269 return $row->cur_text;
274 * Diff-based history compression
275 * Requires xdiff 1.5+ and zlib
277 class DiffHistoryBlob implements HistoryBlob {
278 /** Uncompressed item cache */
279 var $mItems = array();
281 /** Total uncompressed size */
282 var $mSize = 0;
284 /**
285 * Array of diffs. If a diff D from A to B is notated D = B - A, and Z is
286 * an empty string:
288 * { item[map[i]] - item[map[i-1]] where i > 0
289 * diff[i] = {
290 * { item[map[i]] - Z where i = 0
292 var $mDiffs;
294 /** The diff map, see above */
295 var $mDiffMap;
298 * The key for getText()
300 var $mDefaultKey;
303 * Compressed storage
305 var $mCompressed;
308 * True if the object is locked against further writes
310 var $mFrozen = false;
313 * The maximum uncompressed size before the object becomes sad
314 * Should be less than max_allowed_packet
316 var $mMaxSize = 10000000;
319 * The maximum number of text items before the object becomes sad
321 var $mMaxCount = 100;
323 /** Constants from xdiff.h */
324 const XDL_BDOP_INS = 1;
325 const XDL_BDOP_CPY = 2;
326 const XDL_BDOP_INSB = 3;
328 function __construct() {
329 if ( !function_exists( 'gzdeflate' ) ) {
330 throw new MWException( "Need zlib support to read or write DiffHistoryBlob\n" );
334 function addItem( $text ) {
335 if ( $this->mFrozen ) {
336 throw new MWException( __METHOD__.": Cannot add more items after sleep/wakeup" );
339 $this->mItems[] = $text;
340 $this->mSize += strlen( $text );
341 $this->mDiffs = null; // later
342 return count( $this->mItems ) - 1;
345 function getItem( $key ) {
346 return $this->mItems[$key];
349 function setText( $text ) {
350 $this->mDefaultKey = $this->addItem( $text );
353 function getText() {
354 return $this->getItem( $this->mDefaultKey );
357 function compress() {
358 if ( !function_exists( 'xdiff_string_bdiff' ) ){
359 throw new MWException( "Need xdiff 1.5+ support to write DiffHistoryBlob\n" );
361 if ( isset( $this->mDiffs ) ) {
362 // Already compressed
363 return;
365 if ( !count( $this->mItems ) ) {
366 // Empty
367 return;
370 // Create two diff sequences: one for main text and one for small text
371 $sequences = array(
372 'small' => array(
373 'tail' => '',
374 'diffs' => array(),
375 'map' => array(),
377 'main' => array(
378 'tail' => '',
379 'diffs' => array(),
380 'map' => array(),
383 $smallFactor = 0.5;
385 for ( $i = 0; $i < count( $this->mItems ); $i++ ) {
386 $text = $this->mItems[$i];
387 if ( $i == 0 ) {
388 $seqName = 'main';
389 } else {
390 $mainTail = $sequences['main']['tail'];
391 if ( strlen( $text ) < strlen( $mainTail ) * $smallFactor ) {
392 $seqName = 'small';
393 } else {
394 $seqName = 'main';
397 $seq =& $sequences[$seqName];
398 $tail = $seq['tail'];
399 $diff = $this->diff( $tail, $text );
400 $seq['diffs'][] = $diff;
401 $seq['map'][] = $i;
402 $seq['tail'] = $text;
404 unset( $seq ); // unlink dangerous alias
406 // Knit the sequences together
407 $tail = '';
408 $this->mDiffs = array();
409 $this->mDiffMap = array();
410 foreach ( $sequences as $seq ) {
411 if ( !count( $seq['diffs'] ) ) {
412 continue;
414 if ( $tail === '' ) {
415 $this->mDiffs[] = $seq['diffs'][0];
416 } else {
417 $head = $this->patch( '', $seq['diffs'][0] );
418 $this->mDiffs[] = $this->diff( $tail, $head );
420 $this->mDiffMap[] = $seq['map'][0];
421 for ( $i = 1; $i < count( $seq['diffs'] ); $i++ ) {
422 $this->mDiffs[] = $seq['diffs'][$i];
423 $this->mDiffMap[] = $seq['map'][$i];
425 $tail = $seq['tail'];
429 function diff( $t1, $t2 ) {
430 # Need to do a null concatenation with warnings off, due to bugs in the current version of xdiff
431 # "String is not zero-terminated"
432 wfSuppressWarnings();
433 $diff = xdiff_string_bdiff( $t1, $t2 ) . '';
434 wfRestoreWarnings();
435 return $diff;
438 function patch( $base, $diff ) {
439 if ( function_exists( 'xdiff_string_bpatch' ) ) {
440 wfSuppressWarnings();
441 $text = xdiff_string_bpatch( $base, $diff ) . '';
442 wfRestoreWarnings();
443 return $text;
446 # Pure PHP implementation
448 $header = unpack( 'Vofp/Vcsize', substr( $diff, 0, 8 ) );
450 # Check the checksum if mhash is available
451 if ( extension_loaded( 'mhash' ) ) {
452 $ofp = mhash( MHASH_ADLER32, $base );
453 if ( $ofp !== substr( $diff, 0, 4 ) ) {
454 wfDebug( __METHOD__. ": incorrect base checksum\n" );
455 return false;
458 if ( $header['csize'] != strlen( $base ) ) {
459 wfDebug( __METHOD__. ": incorrect base length\n" );
460 return false;
463 $p = 8;
464 $out = '';
465 while ( $p < strlen( $diff ) ) {
466 $x = unpack( 'Cop', substr( $diff, $p, 1 ) );
467 $op = $x['op'];
468 ++$p;
469 switch ( $op ) {
470 case self::XDL_BDOP_INS:
471 $x = unpack( 'Csize', substr( $diff, $p, 1 ) );
472 $p++;
473 $out .= substr( $diff, $p, $x['size'] );
474 $p += $x['size'];
475 break;
476 case self::XDL_BDOP_INSB:
477 $x = unpack( 'Vcsize', substr( $diff, $p, 4 ) );
478 $p += 4;
479 $out .= substr( $diff, $p, $x['csize'] );
480 $p += $x['csize'];
481 break;
482 case self::XDL_BDOP_CPY:
483 $x = unpack( 'Voff/Vcsize', substr( $diff, $p, 8 ) );
484 $p += 8;
485 $out .= substr( $base, $x['off'], $x['csize'] );
486 break;
487 default:
488 wfDebug( __METHOD__.": invalid op\n" );
489 return false;
492 return $out;
495 function uncompress() {
496 if ( !$this->mDiffs ) {
497 return;
499 $tail = '';
500 for ( $diffKey = 0; $diffKey < count( $this->mDiffs ); $diffKey++ ) {
501 $textKey = $this->mDiffMap[$diffKey];
502 $text = $this->patch( $tail, $this->mDiffs[$diffKey] );
503 $this->mItems[$textKey] = $text;
504 $tail = $text;
508 function __sleep() {
509 $this->compress();
510 if ( !count( $this->mItems ) ) {
511 // Empty object
512 $info = false;
513 } else {
514 // Take forward differences to improve the compression ratio for sequences
515 $map = '';
516 $prev = 0;
517 foreach ( $this->mDiffMap as $i ) {
518 if ( $map !== '' ) {
519 $map .= ',';
521 $map .= $i - $prev;
522 $prev = $i;
524 $info = array(
525 'diffs' => $this->mDiffs,
526 'map' => $map
529 if ( isset( $this->mDefaultKey ) ) {
530 $info['default'] = $this->mDefaultKey;
532 $this->mCompressed = gzdeflate( serialize( $info ) );
533 return array( 'mCompressed' );
536 function __wakeup() {
537 // addItem() doesn't work if mItems is partially filled from mDiffs
538 $this->mFrozen = true;
539 $info = unserialize( gzinflate( $this->mCompressed ) );
540 unset( $this->mCompressed );
542 if ( !$info ) {
543 // Empty object
544 return;
547 if ( isset( $info['default'] ) ) {
548 $this->mDefaultKey = $info['default'];
550 $this->mDiffs = $info['diffs'];
551 if ( isset( $info['base'] ) ) {
552 // Old format
553 $this->mDiffMap = range( 0, count( $this->mDiffs ) - 1 );
554 array_unshift( $this->mDiffs,
555 pack( 'VVCV', 0, 0, self::XDL_BDOP_INSB, strlen( $info['base'] ) ) .
556 $info['base'] );
557 } else {
558 // New format
559 $map = explode( ',', $info['map'] );
560 $cur = 0;
561 $this->mDiffMap = array();
562 foreach ( $map as $i ) {
563 $cur += $i;
564 $this->mDiffMap[] = $cur;
567 $this->uncompress();
571 * Helper function for compression jobs
572 * Returns true until the object is "full" and ready to be committed
574 function isHappy() {
575 return $this->mSize < $this->mMaxSize
576 && count( $this->mItems ) < $this->mMaxCount;