| 1 |
<?php |
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
|
| 6 |
|
| 7 |
|
| 8 |
|
| 9 |
|
| 10 |
|
| 11 |
|
| 12 |
|
| 13 |
|
| 14 |
|
| 15 |
|
| 16 |
|
| 17 |
|
| 18 |
|
| 19 |
|
| 20 |
|
| 21 |
|
| 22 |
|
| 23 |
|
| 24 |
|
| 25 |
|
| 26 |
|
| 27 |
|
| 28 |
|
| 29 |
|
| 30 |
class DiffHistoryBlob implements HistoryBlob { |
| 31 |
|
| 32 |
public $mItems = []; |
| 33 |
|
| 34 |
|
| 35 |
public $mSize = 0; |
| 36 |
|
| 37 |
|
| 38 |
|
| 39 |
|
| 40 |
|
| 41 |
|
| 42 |
|
| 43 |
|
| 44 |
|
| 45 |
public $mDiffs; |
| 46 |
|
| 47 |
|
| 48 |
public $mDiffMap; |
| 49 |
|
| 50 |
|
| 51 |
|
| 52 |
public $mDefaultKey; |
| 53 |
|
| 54 |
|
| 55 |
public $mCompressed; |
| 56 |
|
| 57 |
|
| 58 |
public $mFrozen = false; |
| 59 |
|
| 60 |
|
| 61 |
|
| 62 |
|
| 63 |
|
| 64 |
public $mMaxSize = 10_000_000; |
| 65 |
|
| 66 |
|
| 67 |
public $mMaxCount = 100; |
| 68 |
|
| 69 |
|
| 70 |
private const XDL_BDOP_INS = 1; |
| 71 |
private const XDL_BDOP_CPY = 2; |
| 72 |
private const XDL_BDOP_INSB = 3; |
| 73 |
|
| 74 |
public function __construct() { |
| 75 |
if ( !function_exists( 'gzdeflate' ) ) { |
| 76 |
throw new RuntimeException( "Need zlib support to read or write DiffHistoryBlob\n" ); |
| 77 |
} |
| 78 |
} |
| 79 |
|
| 80 |
|
| 81 |
|
| 82 |
|
| 83 |
|
| 84 |
public function addItem( $text ) { |
| 85 |
if ( $this->mFrozen ) { |
| 86 |
throw new BadMethodCallException( __METHOD__ . ": Cannot add more items after sleep/wakeup" ); |
| 87 |
} |
| 88 |
|
| 89 |
$this->mItems[] = $text; |
| 90 |
$this->mSize += strlen( $text ); |
| 91 |
$this->mDiffs = null; |
| 92 |
return (string)( count( $this->mItems ) - 1 ); |
| 93 |
} |
| 94 |
|
| 95 |
|
| 96 |
|
| 97 |
|
| 98 |
|
| 99 |
public function getItem( $key ) { |
| 100 |
return $this->mItems[(int)$key]; |
| 101 |
} |
| 102 |
|
| 103 |
|
| 104 |
|
| 105 |
|
| 106 |
public function setText( $text ) { |
| 107 |
$this->mDefaultKey = $this->addItem( $text ); |
| 108 |
} |
| 109 |
|
| 110 |
|
| 111 |
|
| 112 |
|
| 113 |
public function getText() { |
| 114 |
return $this->getItem( $this->mDefaultKey ); |
| 115 |
} |
| 116 |
|
| 117 |
private function compress() { |
| 118 |
if ( !function_exists( 'xdiff_string_rabdiff' ) ) { |
| 119 |
throw new RuntimeException( "Need xdiff support to write DiffHistoryBlob\n" ); |
| 120 |
} |
| 121 |
if ( isset( $this->mDiffs ) ) { |
| 122 |
|
| 123 |
return; |
| 124 |
} |
| 125 |
if ( $this->mItems === [] ) { |
| 126 |
return; |
| 127 |
} |
| 128 |
|
| 129 |
|
| 130 |
$sequences = [ |
| 131 |
'small' => [ |
| 132 |
'tail' => '', |
| 133 |
'diffs' => [], |
| 134 |
'map' => [], |
| 135 |
], |
| 136 |
'main' => [ |
| 137 |
'tail' => '', |
| 138 |
'diffs' => [], |
| 139 |
'map' => [], |
| 140 |
], |
| 141 |
]; |
| 142 |
$smallFactor = 0.5; |
| 143 |
|
| 144 |
$mItemsCount = count( $this->mItems ); |
| 145 |
for ( $i = 0; $i < $mItemsCount; $i++ ) { |
| 146 |
$text = $this->mItems[$i]; |
| 147 |
if ( $i == 0 ) { |
| 148 |
$seqName = 'main'; |
| 149 |
} else { |
| 150 |
$mainTail = $sequences['main']['tail']; |
| 151 |
if ( strlen( $text ) < strlen( $mainTail ) * $smallFactor ) { |
| 152 |
$seqName = 'small'; |
| 153 |
} else { |
| 154 |
$seqName = 'main'; |
| 155 |
} |
| 156 |
} |
| 157 |
|
| 158 |
$tail = $sequences[$seqName]['tail']; |
| 159 |
$diff = $this->diff( $tail, $text ); |
| 160 |
$sequences[$seqName]['diffs'][] = $diff; |
| 161 |
$sequences[$seqName]['map'][] = $i; |
| 162 |
$sequences[$seqName]['tail'] = $text; |
| 163 |
} |
| 164 |
|
| 165 |
|
| 166 |
$tail = ''; |
| 167 |
$this->mDiffs = []; |
| 168 |
$this->mDiffMap = []; |
| 169 |
foreach ( $sequences as $seq ) { |
| 170 |
if ( $seq['diffs'] === [] ) { |
| 171 |
continue; |
| 172 |
} |
| 173 |
if ( $tail === '' ) { |
| 174 |
$this->mDiffs[] = $seq['diffs'][0]; |
| 175 |
} else { |
| 176 |
$head = $this->patch( '', $seq['diffs'][0] ); |
| 177 |
$this->mDiffs[] = $this->diff( $tail, $head ); |
| 178 |
} |
| 179 |
$this->mDiffMap[] = $seq['map'][0]; |
| 180 |
$diffsCount = count( $seq['diffs'] ); |
| 181 |
for ( $i = 1; $i < $diffsCount; $i++ ) { |
| 182 |
$this->mDiffs[] = $seq['diffs'][$i]; |
| 183 |
$this->mDiffMap[] = $seq['map'][$i]; |
| 184 |
} |
| 185 |
$tail = $seq['tail']; |
| 186 |
} |
| 187 |
} |
| 188 |
|
| 189 |
|
| 190 |
|
| 191 |
|
| 192 |
|
| 193 |
|
| 194 |
private function diff( $t1, $t2 ) { |
| 195 |
return xdiff_string_rabdiff( $t1, $t2 ); |
| 196 |
} |
| 197 |
|
| 198 |
|
| 199 |
|
| 200 |
|
| 201 |
|
| 202 |
|
| 203 |
private function patch( $base, $diff ) { |
| 204 |
if ( function_exists( 'xdiff_string_bpatch' ) ) { |
| 205 |
return xdiff_string_bpatch( $base, $diff ); |
| 206 |
} |
| 207 |
|
| 208 |
|
| 209 |
|
| 210 |
$header = unpack( 'Vofp/Vcsize', substr( $diff, 0, 8 ) ); |
| 211 |
|
| 212 |
|
| 213 |
$ofp = $this->xdiffAdler32( $base ); |
| 214 |
if ( $ofp !== substr( $diff, 0, 4 ) ) { |
| 215 |
wfDebug( __METHOD__ . ": incorrect base checksum" ); |
| 216 |
return false; |
| 217 |
} |
| 218 |
if ( $header['csize'] != strlen( $base ) ) { |
| 219 |
wfDebug( __METHOD__ . ": incorrect base length" ); |
| 220 |
return false; |
| 221 |
} |
| 222 |
|
| 223 |
$p = 8; |
| 224 |
$out = ''; |
| 225 |
while ( $p < strlen( $diff ) ) { |
| 226 |
$x = unpack( 'Cop', substr( $diff, $p, 1 ) ); |
| 227 |
$op = $x['op']; |
| 228 |
++$p; |
| 229 |
switch ( $op ) { |
| 230 |
case self::XDL_BDOP_INS: |
| 231 |
$x = unpack( 'Csize', substr( $diff, $p, 1 ) ); |
| 232 |
$p++; |
| 233 |
$out .= substr( $diff, $p, $x['size'] ); |
| 234 |
$p += $x['size']; |
| 235 |
break; |
| 236 |
case self::XDL_BDOP_INSB: |
| 237 |
$x = unpack( 'Vcsize', substr( $diff, $p, 4 ) ); |
| 238 |
$p += 4; |
| 239 |
$out .= substr( $diff, $p, $x['csize'] ); |
| 240 |
$p += $x['csize']; |
| 241 |
break; |
| 242 |
case self::XDL_BDOP_CPY: |
| 243 |
$x = unpack( 'Voff/Vcsize', substr( $diff, $p, 8 ) ); |
| 244 |
$p += 8; |
| 245 |
$out .= substr( $base, $x['off'], $x['csize'] ); |
| 246 |
break; |
| 247 |
default: |
| 248 |
wfDebug( __METHOD__ . ": invalid op" ); |
| 249 |
return false; |
| 250 |
} |
| 251 |
} |
| 252 |
return $out; |
| 253 |
} |
| 254 |
|
| 255 |
|
| 256 |
|
| 257 |
|
| 258 |
|
| 259 |
|
| 260 |
|
| 261 |
|
| 262 |
public function xdiffAdler32( $s ) { |
| 263 |
static $init = null; |
| 264 |
$init ??= str_repeat( "\xf0", 205 ) . "\xee" . str_repeat( "\xf0", 67 ) . "\x02"; |
| 265 |
|
| 266 |
|
| 267 |
|
| 268 |
|
| 269 |
return strrev( hash( 'adler32', $init . $s, true ) ); |
| 270 |
} |
| 271 |
|
| 272 |
private function uncompress() { |
| 273 |
if ( !$this->mDiffs ) { |
| 274 |
return; |
| 275 |
} |
| 276 |
$tail = ''; |
| 277 |
$mDiffsCount = count( $this->mDiffs ); |
| 278 |
for ( $diffKey = 0; $diffKey < $mDiffsCount; $diffKey++ ) { |
| 279 |
$textKey = $this->mDiffMap[$diffKey]; |
| 280 |
$text = $this->patch( $tail, $this->mDiffs[$diffKey] ); |
| 281 |
$this->mItems[$textKey] = $text; |
| 282 |
$tail = $text; |
| 283 |
} |
| 284 |
} |
| 285 |
|
| 286 |
|
| 287 |
|
| 288 |
|
| 289 |
public function __sleep() { |
| 290 |
$this->compress(); |
| 291 |
if ( $this->mItems === [] ) { |
| 292 |
$info = false; |
| 293 |
} else { |
| 294 |
|
| 295 |
$map = ''; |
| 296 |
$prev = 0; |
| 297 |
foreach ( $this->mDiffMap as $i ) { |
| 298 |
if ( $map !== '' ) { |
| 299 |
$map .= ','; |
| 300 |
} |
| 301 |
$map .= $i - $prev; |
| 302 |
$prev = $i; |
| 303 |
} |
| 304 |
$info = [ |
| 305 |
'diffs' => $this->mDiffs, |
| 306 |
'map' => $map |
| 307 |
]; |
| 308 |
} |
| 309 |
if ( isset( $this->mDefaultKey ) ) { |
| 310 |
$info['default'] = $this->mDefaultKey; |
| 311 |
} |
| 312 |
$this->mCompressed = gzdeflate( serialize( $info ) ); |
| 313 |
return [ 'mCompressed' ]; |
| 314 |
} |
| 315 |
|
| 316 |
public function __wakeup() { |
| 317 |
|
| 318 |
$this->mFrozen = true; |
| 319 |
$info = HistoryBlobUtils::unserializeArray( gzinflate( $this->mCompressed ) ); |
| 320 |
$this->mCompressed = null; |
| 321 |
|
| 322 |
if ( !$info ) { |
| 323 |
|
| 324 |
return; |
| 325 |
} |
| 326 |
|
| 327 |
if ( isset( $info['default'] ) ) { |
| 328 |
$this->mDefaultKey = $info['default']; |
| 329 |
} |
| 330 |
$this->mDiffs = $info['diffs']; |
| 331 |
if ( isset( $info['base'] ) ) { |
| 332 |
|
| 333 |
$this->mDiffMap = range( 0, count( $this->mDiffs ) - 1 ); |
| 334 |
array_unshift( $this->mDiffs, |
| 335 |
pack( 'VVCV', 0, 0, self::XDL_BDOP_INSB, strlen( $info['base'] ) ) . |
| 336 |
$info['base'] ); |
| 337 |
} else { |
| 338 |
|
| 339 |
$map = explode( ',', $info['map'] ); |
| 340 |
$cur = 0; |
| 341 |
$this->mDiffMap = []; |
| 342 |
foreach ( $map as $i ) { |
| 343 |
$cur += $i; |
| 344 |
$this->mDiffMap[] = $cur; |
| 345 |
} |
| 346 |
} |
| 347 |
$this->uncompress(); |
| 348 |
} |
| 349 |
|
| 350 |
|
| 351 |
|
| 352 |
|
| 353 |
|
| 354 |
|
| 355 |
|
| 356 |
public function isHappy() { |
| 357 |
return $this->mSize < $this->mMaxSize |
| 358 |
&& count( $this->mItems ) < $this->mMaxCount; |
| 359 |
} |
| 360 |
|
| 361 |
} |