MediaWiki master
ConcatenatedGzipHistoryBlob.php
Go to the documentation of this file.
1<?php
32 public $mVersion = 0;
34 public $mCompressed = false;
36 public $mItems = [];
38 public $mDefaultHash = '';
40 public $mSize = 0;
42 public $mMaxSize = 10_000_000;
44 public $mMaxCount = 100;
45
46 public function __construct() {
47 if ( !function_exists( 'gzdeflate' ) ) {
48 throw new RuntimeException( "Need zlib support to read or write this "
49 . "kind of history object (ConcatenatedGzipHistoryBlob)\n" );
50 }
51 }
52
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 );
63 }
64 return $hash;
65 }
66
71 public function getItem( $hash ) {
72 $this->uncompress();
73 if ( array_key_exists( $hash, $this->mItems ) ) {
74 return $this->mItems[$hash];
75 } else {
76 return false;
77 }
78 }
79
84 public function setText( $text ) {
85 $this->uncompress();
86 $this->mDefaultHash = $this->addItem( $text );
87 }
88
92 public function getText() {
93 $this->uncompress();
94 return $this->getItem( $this->mDefaultHash );
95 }
96
102 public function removeItem( $hash ) {
103 $this->mSize -= strlen( $this->mItems[$hash] );
104 unset( $this->mItems[$hash] );
105 }
106
110 public function compress() {
111 if ( !$this->mCompressed ) {
112 $this->mItems = gzdeflate( serialize( $this->mItems ) );
113 $this->mCompressed = true;
114 }
115 }
116
120 public function uncompress() {
121 if ( $this->mCompressed ) {
122 $this->mItems = HistoryBlobUtils::unserializeArray( gzinflate( $this->mItems ) );
123 $this->mCompressed = false;
124 }
125 }
126
130 public function __sleep() {
131 $this->compress();
132 return [ 'mVersion', 'mCompressed', 'mItems', 'mDefaultHash' ];
133 }
134
135 public function __wakeup() {
136 $this->uncompress();
137 }
138
145 public function isHappy() {
146 return $this->mSize < $this->mMaxSize
147 && count( $this->mItems ) < $this->mMaxCount;
148 }
149}
150
151// Blobs generated by MediaWiki < 1.5 on PHP 4 were serialized with the
152// class name coerced to lowercase. We can improve efficiency by adding
153// autoload entries for the lowercase variants of these classes (T166759).
154// The code below is never executed, but it is picked up by the AutoloadGenerator
155// parser, which scans for class_alias() calls.
156/*
157class_alias( ConcatenatedGzipHistoryBlob::class, 'concatenatedgziphistoryblob' );
158*/
Concatenated gzip (CGZ) storage Improves compression ratio by concatenating like objects before gzipp...
compress()
Compress the bulk data in the object.
string[] string $mItems
Array if uncompressed, string if compressed.
isHappy()
Helper function for compression jobs Returns true until the object is "full" and ready to be committe...
Base class for general text storage via the "object" flag in old_flags, or two-part external storage ...