MediaWiki  1.34.0
ConcatenatedGzipHistoryBlob.php
Go to the documentation of this file.
1 <?php
28  public $mVersion = 0;
29  public $mCompressed = false;
34  public $mItems = [];
35  public $mDefaultHash = '';
36  public $mSize = 0;
37  public $mMaxSize = 10000000;
38  public $mMaxCount = 100;
39 
40  public function __construct() {
41  if ( !function_exists( 'gzdeflate' ) ) {
42  throw new MWException( "Need zlib support to read or write this "
43  . "kind of history object (ConcatenatedGzipHistoryBlob)\n" );
44  }
45  }
46 
51  public function addItem( $text ) {
52  $this->uncompress();
53  $hash = md5( $text );
54  if ( !isset( $this->mItems[$hash] ) ) {
55  $this->mItems[$hash] = $text;
56  $this->mSize += strlen( $text );
57  }
58  return $hash;
59  }
60 
65  public function getItem( $hash ) {
66  $this->uncompress();
67  if ( array_key_exists( $hash, $this->mItems ) ) {
68  return $this->mItems[$hash];
69  } else {
70  return false;
71  }
72  }
73 
78  public function setText( $text ) {
79  $this->uncompress();
80  $this->mDefaultHash = $this->addItem( $text );
81  }
82 
86  public function getText() {
87  $this->uncompress();
88  return $this->getItem( $this->mDefaultHash );
89  }
90 
96  public function removeItem( $hash ) {
97  $this->mSize -= strlen( $this->mItems[$hash] );
98  unset( $this->mItems[$hash] );
99  }
100 
104  public function compress() {
105  if ( !$this->mCompressed ) {
106  $this->mItems = gzdeflate( serialize( $this->mItems ) );
107  $this->mCompressed = true;
108  }
109  }
110 
114  public function uncompress() {
115  if ( $this->mCompressed ) {
116  $this->mItems = unserialize( gzinflate( $this->mItems ) );
117  $this->mCompressed = false;
118  }
119  }
120 
124  function __sleep() {
125  $this->compress();
126  return [ 'mVersion', 'mCompressed', 'mItems', 'mDefaultHash' ];
127  }
128 
129  function __wakeup() {
130  $this->uncompress();
131  }
132 
139  public function isHappy() {
140  return $this->mSize < $this->mMaxSize
141  && count( $this->mItems ) < $this->mMaxCount;
142  }
143 }
144 
145 // phpcs:ignore Generic.CodeAnalysis.UnconditionalIfStatement.Found
146 if ( false ) {
147  // Blobs generated by MediaWiki < 1.5 on PHP 4 were serialized with the
148  // class name coerced to lowercase. We can improve efficiency by adding
149  // autoload entries for the lowercase variants of these classes (T166759).
150  // The code below is never executed, but it is picked up by the AutoloadGenerator
151  // parser, which scans for class_alias() calls.
152  class_alias( ConcatenatedGzipHistoryBlob::class, 'concatenatedgziphistoryblob' );
153 }
HistoryBlob
Base class for general text storage via the "object" flag in old_flags, or two-part external storage ...
Definition: HistoryBlob.php:28
ConcatenatedGzipHistoryBlob\$mMaxSize
$mMaxSize
Definition: ConcatenatedGzipHistoryBlob.php:37
ConcatenatedGzipHistoryBlob\__sleep
__sleep()
Definition: ConcatenatedGzipHistoryBlob.php:124
ConcatenatedGzipHistoryBlob\isHappy
isHappy()
Helper function for compression jobs Returns true until the object is "full" and ready to be committe...
Definition: ConcatenatedGzipHistoryBlob.php:139
ConcatenatedGzipHistoryBlob\$mItems
array string $mItems
@fixme Why are some methods treating it as an array, and others as a string, unconditionally?
Definition: ConcatenatedGzipHistoryBlob.php:34
ConcatenatedGzipHistoryBlob
Concatenated gzip (CGZ) storage Improves compression ratio by concatenating like objects before gzipp...
Definition: ConcatenatedGzipHistoryBlob.php:27
ConcatenatedGzipHistoryBlob\removeItem
removeItem( $hash)
Remove an item.
Definition: ConcatenatedGzipHistoryBlob.php:96
serialize
serialize()
Definition: ApiMessageTrait.php:138
ConcatenatedGzipHistoryBlob\$mMaxCount
$mMaxCount
Definition: ConcatenatedGzipHistoryBlob.php:38
ConcatenatedGzipHistoryBlob\$mVersion
$mVersion
Definition: ConcatenatedGzipHistoryBlob.php:28
MWException
MediaWiki exception.
Definition: MWException.php:26
ConcatenatedGzipHistoryBlob\getText
getText()
Definition: ConcatenatedGzipHistoryBlob.php:86
ConcatenatedGzipHistoryBlob\__construct
__construct()
Definition: ConcatenatedGzipHistoryBlob.php:40
ConcatenatedGzipHistoryBlob\$mDefaultHash
$mDefaultHash
Definition: ConcatenatedGzipHistoryBlob.php:35
ConcatenatedGzipHistoryBlob\$mCompressed
$mCompressed
Definition: ConcatenatedGzipHistoryBlob.php:29
ConcatenatedGzipHistoryBlob\uncompress
uncompress()
Uncompress bulk data.
Definition: ConcatenatedGzipHistoryBlob.php:114
ConcatenatedGzipHistoryBlob\$mSize
$mSize
Definition: ConcatenatedGzipHistoryBlob.php:36
ConcatenatedGzipHistoryBlob\getItem
getItem( $hash)
Definition: ConcatenatedGzipHistoryBlob.php:65
unserialize
unserialize( $serialized)
Definition: ApiMessageTrait.php:146
ConcatenatedGzipHistoryBlob\__wakeup
__wakeup()
Definition: ConcatenatedGzipHistoryBlob.php:129
ConcatenatedGzipHistoryBlob\addItem
addItem( $text)
Definition: ConcatenatedGzipHistoryBlob.php:51
ConcatenatedGzipHistoryBlob\compress
compress()
Compress the bulk data in the object.
Definition: ConcatenatedGzipHistoryBlob.php:104
ConcatenatedGzipHistoryBlob\setText
setText( $text)
Definition: ConcatenatedGzipHistoryBlob.php:78