MediaWiki REL1_35
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 public function __sleep() {
125 $this->compress();
126 return [ 'mVersion', 'mCompressed', 'mItems', 'mDefaultHash' ];
127 }
128
129 public 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// @phan-suppress-next-next-line PhanImpossibleCondition
146// phpcs:ignore Generic.CodeAnalysis.UnconditionalIfStatement.Found
147if ( false ) {
148 // Blobs generated by MediaWiki < 1.5 on PHP 4 were serialized with the
149 // class name coerced to lowercase. We can improve efficiency by adding
150 // autoload entries for the lowercase variants of these classes (T166759).
151 // The code below is never executed, but it is picked up by the AutoloadGenerator
152 // parser, which scans for class_alias() calls.
153 class_alias( ConcatenatedGzipHistoryBlob::class, 'concatenatedgziphistoryblob' );
154}
serialize()
unserialize( $serialized)
Concatenated gzip (CGZ) storage Improves compression ratio by concatenating like objects before gzipp...
compress()
Compress the bulk data in the object.
array string $mItems
@fixme Why are some methods treating it as an array, and others as a string, unconditionally?
isHappy()
Helper function for compression jobs Returns true until the object is "full" and ready to be committe...
MediaWiki exception.
Base class for general text storage via the "object" flag in old_flags, or two-part external storage ...