MediaWiki REL1_39
ConcatenatedGzipHistoryBlob.php
Go to the documentation of this file.
1<?php
29 public $mVersion = 0;
31 public $mCompressed = false;
33 public $mItems = [];
35 public $mDefaultHash = '';
37 public $mSize = 0;
39 public $mMaxSize = 10000000;
41 public $mMaxCount = 100;
42
43 public function __construct() {
44 if ( !function_exists( 'gzdeflate' ) ) {
45 throw new MWException( "Need zlib support to read or write this "
46 . "kind of history object (ConcatenatedGzipHistoryBlob)\n" );
47 }
48 }
49
54 public function addItem( $text ) {
55 $this->uncompress();
56 $hash = md5( $text );
57 if ( !isset( $this->mItems[$hash] ) ) {
58 $this->mItems[$hash] = $text;
59 $this->mSize += strlen( $text );
60 }
61 return $hash;
62 }
63
68 public function getItem( $hash ) {
69 $this->uncompress();
70 if ( array_key_exists( $hash, $this->mItems ) ) {
71 return $this->mItems[$hash];
72 } else {
73 return false;
74 }
75 }
76
81 public function setText( $text ) {
82 $this->uncompress();
83 $this->mDefaultHash = $this->addItem( $text );
84 }
85
89 public function getText() {
90 $this->uncompress();
91 return $this->getItem( $this->mDefaultHash );
92 }
93
99 public function removeItem( $hash ) {
100 $this->mSize -= strlen( $this->mItems[$hash] );
101 unset( $this->mItems[$hash] );
102 }
103
107 public function compress() {
108 if ( !$this->mCompressed ) {
109 $this->mItems = gzdeflate( serialize( $this->mItems ) );
110 $this->mCompressed = true;
111 }
112 }
113
117 public function uncompress() {
118 if ( $this->mCompressed ) {
119 $this->mItems = unserialize( gzinflate( $this->mItems ) );
120 $this->mCompressed = false;
121 }
122 }
123
127 public function __sleep() {
128 $this->compress();
129 return [ 'mVersion', 'mCompressed', 'mItems', 'mDefaultHash' ];
130 }
131
132 public function __wakeup() {
133 $this->uncompress();
134 }
135
142 public function isHappy() {
143 return $this->mSize < $this->mMaxSize
144 && count( $this->mItems ) < $this->mMaxCount;
145 }
146}
147
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/*
154class_alias( ConcatenatedGzipHistoryBlob::class, 'concatenatedgziphistoryblob' );
155*/
serialize()
unserialize( $serialized)
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...
MediaWiki exception.
Base class for general text storage via the "object" flag in old_flags, or two-part external storage ...