Revert previous commit, breaks cache.
[mediawiki.git] / includes / HistoryBlob.php
blobb051adf143dd241ac6649d3806b020607e9e8ac6
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 $mFast = 0, $mSize = 0;
48 /** Constructor */
49 public function ConcatenatedGzipHistoryBlob() {
50 if ( !function_exists( 'gzdeflate' ) ) {
51 throw new MWException( "Need zlib support to read or write this kind of history object (ConcatenatedGzipHistoryBlob)\n" );
55 public function addItem( $text ) {
56 $this->uncompress();
57 $hash = md5( $text );
58 $this->mItems[$hash] = $text;
59 $this->mSize += strlen( $text );
61 return $hash;
64 public function getItem( $hash ) {
65 $this->uncompress();
66 if ( array_key_exists( $hash, $this->mItems ) ) {
67 return $this->mItems[$hash];
68 } else {
69 return false;
73 public function setText( $text ) {
74 $this->uncompress();
75 $this->mDefaultHash = $this->addItem( $text );
78 public function getText() {
79 $this->uncompress();
80 return $this->getItem( $this->mDefaultHash );
83 /**
84 * Remove an item
86 public function removeItem( $hash ) {
87 $this->mSize -= strlen( $this->mItems[$hash] );
88 unset( $this->mItems[$hash] );
91 /**
92 * Compress the bulk data in the object
94 public function compress() {
95 if ( !$this->mCompressed ) {
96 $this->mItems = gzdeflate( serialize( $this->mItems ) );
97 $this->mCompressed = true;
102 * Uncompress bulk data
104 public function uncompress() {
105 if ( $this->mCompressed ) {
106 $this->mItems = unserialize( gzinflate( $this->mItems ) );
107 $this->mCompressed = false;
112 function __sleep() {
113 $this->compress();
114 return array( 'mVersion', 'mCompressed', 'mItems', 'mDefaultHash' );
117 function __wakeup() {
118 $this->uncompress();
122 * Helper function for compression jobs
123 * Returns true until the object is "full" and ready to be committed
125 public function isHappy( $maxFactor, $factorThreshold ) {
126 if ( count( $this->mItems ) == 0 ) {
127 return true;
129 if ( !$this->mFast ) {
130 $this->uncompress();
131 $record = serialize( $this->mItems );
132 $size = strlen( $record );
133 $avgUncompressed = $size / count( $this->mItems );
134 $compressed = strlen( gzdeflate( $record ) );
136 if ( $compressed < $factorThreshold * 1024 ) {
137 return true;
138 } else {
139 return $avgUncompressed * $maxFactor < $compressed;
141 } else {
142 return count( $this->mItems ) <= 10;
149 * One-step cache variable to hold base blobs; operations that
150 * pull multiple revisions may often pull multiple times from
151 * the same blob. By keeping the last-used one open, we avoid
152 * redundant unserialization and decompression overhead.
154 global $wgBlobCache;
155 $wgBlobCache = array();
159 * Pointer object for an item within a CGZ blob stored in the text table.
161 class HistoryBlobStub {
162 var $mOldId, $mHash, $mRef;
165 * @param string $hash The content hash of the text
166 * @param integer $oldid The old_id for the CGZ object
168 function HistoryBlobStub( $hash = '', $oldid = 0 ) {
169 $this->mHash = $hash;
173 * Sets the location (old_id) of the main object to which this object
174 * points
176 function setLocation( $id ) {
177 $this->mOldId = $id;
181 * Sets the location (old_id) of the referring object
183 function setReferrer( $id ) {
184 $this->mRef = $id;
188 * Gets the location of the referring object
190 function getReferrer() {
191 return $this->mRef;
194 function getText() {
195 $fname = 'HistoryBlobStub::getText';
196 global $wgBlobCache;
197 if( isset( $wgBlobCache[$this->mOldId] ) ) {
198 $obj = $wgBlobCache[$this->mOldId];
199 } else {
200 $dbr = wfGetDB( DB_SLAVE );
201 $row = $dbr->selectRow( 'text', array( 'old_flags', 'old_text' ), array( 'old_id' => $this->mOldId ) );
202 if( !$row ) {
203 return false;
205 $flags = explode( ',', $row->old_flags );
206 if( in_array( 'external', $flags ) ) {
207 $url=$row->old_text;
208 @list( /* $proto */ ,$path)=explode('://',$url,2);
209 if ($path=="") {
210 wfProfileOut( $fname );
211 return false;
213 $row->old_text=ExternalStore::fetchFromUrl($url);
216 if( !in_array( 'object', $flags ) ) {
217 return false;
220 if( in_array( 'gzip', $flags ) ) {
221 // This shouldn't happen, but a bug in the compress script
222 // may at times gzip-compress a HistoryBlob object row.
223 $obj = unserialize( gzinflate( $row->old_text ) );
224 } else {
225 $obj = unserialize( $row->old_text );
228 if( !is_object( $obj ) ) {
229 // Correct for old double-serialization bug.
230 $obj = unserialize( $obj );
233 // Save this item for reference; if pulling many
234 // items in a row we'll likely use it again.
235 $obj->uncompress();
236 $wgBlobCache = array( $this->mOldId => $obj );
238 return $obj->getItem( $this->mHash );
242 * Get the content hash
244 function getHash() {
245 return $this->mHash;
251 * To speed up conversion from 1.4 to 1.5 schema, text rows can refer to the
252 * leftover cur table as the backend. This avoids expensively copying hundreds
253 * of megabytes of data during the conversion downtime.
255 * Serialized HistoryBlobCurStub objects will be inserted into the text table
256 * on conversion if $wgFastSchemaUpgrades is set to true.
258 class HistoryBlobCurStub {
259 var $mCurId;
262 * @param integer $curid The cur_id pointed to
264 function HistoryBlobCurStub( $curid = 0 ) {
265 $this->mCurId = $curid;
269 * Sets the location (cur_id) of the main object to which this object
270 * points
272 function setLocation( $id ) {
273 $this->mCurId = $id;
276 function getText() {
277 $dbr = wfGetDB( DB_SLAVE );
278 $row = $dbr->selectRow( 'cur', array( 'cur_text' ), array( 'cur_id' => $this->mCurId ) );
279 if( !$row ) {
280 return false;
282 return $row->cur_text;
287 * Diff-based history compression
288 * Requires xdiff 1.5+ and zlib
290 class DiffHistoryBlob implements HistoryBlob {
291 /** Uncompressed item cache */
292 var $mItems = array();
294 /**
295 * Array of diffs, where $this->mDiffs[0] is the diff between
296 * $this->mDiffs[0] and $this->mDiffs[1]
298 var $mDiffs = array();
301 * The key for getText()
303 var $mDefaultKey;
306 * Compressed storage
308 var $mCompressed;
311 * True if the object is locked against further writes
313 var $mFrozen = false;
316 function __construct() {
317 if ( !function_exists( 'xdiff_string_bdiff' ) ){
318 throw new MWException( "Need xdiff 1.5+ support to read or write DiffHistoryBlob\n" );
320 if ( !function_exists( 'gzdeflate' ) ) {
321 throw new MWException( "Need zlib support to read or write DiffHistoryBlob\n" );
325 function addItem( $text ) {
326 if ( $this->mFrozen ) {
327 throw new MWException( __METHOD__.": Cannot add more items after sleep/wakeup" );
330 $this->mItems[] = $text;
331 $i = count( $this->mItems ) - 1;
332 if ( $i > 0 ) {
333 # Need to do a null concatenation with warnings off, due to bugs in the current version of xdiff
334 # "String is not zero-terminated"
335 wfSuppressWarnings();
336 $this->mDiffs[] = xdiff_string_bdiff( $this->mItems[$i-1], $text ) . '';
337 wfRestoreWarnings();
339 return $i;
342 function getItem( $key ) {
343 if ( $key > count( $this->mDiffs ) + 1 ) {
344 return false;
346 $key = intval( $key );
347 if ( $key == 0 ) {
348 return $this->mItems[0];
351 $last = count( $this->mItems ) - 1;
352 for ( $i = $last + 1; $i <= $key; $i++ ) {
353 # Need to do a null concatenation with warnings off, due to bugs in the current version of xdiff
354 # "String is not zero-terminated"
355 wfSuppressWarnings();
356 $this->mItems[$i] = xdiff_string_bpatch( $this->mItems[$i - 1], $this->mDiffs[$i - 1] ) . '';
357 wfRestoreWarnings();
359 return $this->mItems[$key];
362 function setText( $text ) {
363 $this->mDefaultKey = $this->addItem( $text );
366 function getText() {
367 return $this->getItem( $this->mDefaultKey );
370 function __sleep() {
371 if ( !isset( $this->mItems[0] ) ) {
372 // Empty object
373 $info = false;
374 } else {
375 $info = array(
376 'base' => $this->mItems[0],
377 'diffs' => $this->mDiffs
380 if ( isset( $this->mDefaultKey ) ) {
381 $info['default'] = $this->mDefaultKey;
383 $this->mCompressed = gzdeflate( serialize( $info ) );
384 return array( 'mCompressed' );
387 function __wakeup() {
388 // addItem() doesn't work if mItems is partially filled from mDiffs
389 $this->mFrozen = true;
390 $info = unserialize( gzinflate( $this->mCompressed ) );
391 unset( $this->mCompressed );
393 if ( !$info ) {
394 // Empty object
395 return;
398 if ( isset( $info['default'] ) ) {
399 $this->mDefaultKey = $info['default'];
401 $this->mItems[0] = $info['base'];
402 $this->mDiffs = $info['diffs'];