Add the other existing $skin.css/.js to the message files too to be consistent
[mediawiki.git] / includes / HistoryBlob.php
blob3772926dfdb553d784a5d2d21dcaaabe1004f714
1 <?php
3 /**
4 * Pure virtual parent
5 * @todo document (needs a one-sentence top-level class description, that answers the question: "what is a HistoryBlob?")
6 */
7 interface HistoryBlob
9 /**
10 * setMeta and getMeta currently aren't used for anything, I just thought
11 * they might be useful in the future.
12 * @param $meta String: a single string.
14 public function setMeta( $meta );
16 /**
17 * setMeta and getMeta currently aren't used for anything, I just thought
18 * they might be useful in the future.
19 * Gets the meta-value
21 public function getMeta();
23 /**
24 * Adds an item of text, returns a stub object which points to the item.
25 * You must call setLocation() on the stub object before storing it to the
26 * database
28 public function addItem( $text );
30 /**
31 * Get item by hash
33 public function getItem( $hash );
35 # Set the "default text"
36 # This concept is an odd property of the current DB schema, whereby each text item has a revision
37 # associated with it. The default text is the text of the associated revision. There may, however,
38 # be other revisions in the same object
39 public function setText( $text );
41 /**
42 * Get default text. This is called from Revision::getRevisionText()
44 function getText();
47 /**
48 * The real object
49 * @todo document (needs one-sentence top-level class description + function descriptions).
51 class ConcatenatedGzipHistoryBlob implements HistoryBlob
53 public $mVersion = 0, $mCompressed = false, $mItems = array(), $mDefaultHash = '';
54 public $mFast = 0, $mSize = 0;
56 /** Constructor */
57 public function ConcatenatedGzipHistoryBlob() {
58 if ( !function_exists( 'gzdeflate' ) ) {
59 throw new MWException( "Need zlib support to read or write this kind of history object (ConcatenatedGzipHistoryBlob)\n" );
64 # HistoryBlob implementation:
67 /** @todo document */
68 public function setMeta( $metaData ) {
69 $this->uncompress();
70 $this->mItems['meta'] = $metaData;
73 /** @todo document */
74 public function getMeta() {
75 $this->uncompress();
76 return $this->mItems['meta'];
79 /** @todo document */
80 public function addItem( $text ) {
81 $this->uncompress();
82 $hash = md5( $text );
83 $this->mItems[$hash] = $text;
84 $this->mSize += strlen( $text );
86 $stub = new HistoryBlobStub( $hash );
87 return $stub;
90 /** @todo document */
91 public function getItem( $hash ) {
92 $this->uncompress();
93 if ( array_key_exists( $hash, $this->mItems ) ) {
94 return $this->mItems[$hash];
95 } else {
96 return false;
100 /** @todo document */
101 public function setText( $text ) {
102 $this->uncompress();
103 $stub = $this->addItem( $text );
104 $this->mDefaultHash = $stub->mHash;
107 /** @todo document */
108 public function getText() {
109 $this->uncompress();
110 return $this->getItem( $this->mDefaultHash );
113 # HistoryBlob implemented.
116 /** @todo document */
117 public function removeItem( $hash ) {
118 $this->mSize -= strlen( $this->mItems[$hash] );
119 unset( $this->mItems[$hash] );
122 /** @todo document */
123 public function compress() {
124 if ( !$this->mCompressed ) {
125 $this->mItems = gzdeflate( serialize( $this->mItems ) );
126 $this->mCompressed = true;
130 /** @todo document */
131 public function uncompress() {
132 if ( $this->mCompressed ) {
133 $this->mItems = unserialize( gzinflate( $this->mItems ) );
134 $this->mCompressed = false;
139 /** @todo document */
140 function __sleep() {
141 $this->compress();
142 return array( 'mVersion', 'mCompressed', 'mItems', 'mDefaultHash' );
145 /** @todo document */
146 function __wakeup() {
147 $this->uncompress();
151 * Determines if this object is happy
153 public function isHappy( $maxFactor, $factorThreshold ) {
154 if ( count( $this->mItems ) == 0 ) {
155 return true;
157 if ( !$this->mFast ) {
158 $this->uncompress();
159 $record = serialize( $this->mItems );
160 $size = strlen( $record );
161 $avgUncompressed = $size / count( $this->mItems );
162 $compressed = strlen( gzdeflate( $record ) );
164 if ( $compressed < $factorThreshold * 1024 ) {
165 return true;
166 } else {
167 return $avgUncompressed * $maxFactor < $compressed;
169 } else {
170 return count( $this->mItems ) <= 10;
177 * One-step cache variable to hold base blobs; operations that
178 * pull multiple revisions may often pull multiple times from
179 * the same blob. By keeping the last-used one open, we avoid
180 * redundant unserialization and decompression overhead.
182 global $wgBlobCache;
183 $wgBlobCache = array();
187 * @todo document (needs one-sentence top-level class description + some function descriptions).
189 class HistoryBlobStub {
190 var $mOldId, $mHash, $mRef;
192 /** @todo document */
193 function HistoryBlobStub( $hash = '', $oldid = 0 ) {
194 $this->mHash = $hash;
198 * Sets the location (old_id) of the main object to which this object
199 * points
201 function setLocation( $id ) {
202 $this->mOldId = $id;
206 * Sets the location (old_id) of the referring object
208 function setReferrer( $id ) {
209 $this->mRef = $id;
213 * Gets the location of the referring object
215 function getReferrer() {
216 return $this->mRef;
219 /** @todo document */
220 function getText() {
221 $fname = 'HistoryBlobStub::getText';
222 global $wgBlobCache;
223 if( isset( $wgBlobCache[$this->mOldId] ) ) {
224 $obj = $wgBlobCache[$this->mOldId];
225 } else {
226 $dbr = wfGetDB( DB_SLAVE );
227 $row = $dbr->selectRow( 'text', array( 'old_flags', 'old_text' ), array( 'old_id' => $this->mOldId ) );
228 if( !$row ) {
229 return false;
231 $flags = explode( ',', $row->old_flags );
232 if( in_array( 'external', $flags ) ) {
233 $url=$row->old_text;
234 @list( /* $proto */ ,$path)=explode('://',$url,2);
235 if ($path=="") {
236 wfProfileOut( $fname );
237 return false;
239 $row->old_text=ExternalStore::fetchFromUrl($url);
242 if( !in_array( 'object', $flags ) ) {
243 return false;
246 if( in_array( 'gzip', $flags ) ) {
247 // This shouldn't happen, but a bug in the compress script
248 // may at times gzip-compress a HistoryBlob object row.
249 $obj = unserialize( gzinflate( $row->old_text ) );
250 } else {
251 $obj = unserialize( $row->old_text );
254 if( !is_object( $obj ) ) {
255 // Correct for old double-serialization bug.
256 $obj = unserialize( $obj );
259 // Save this item for reference; if pulling many
260 // items in a row we'll likely use it again.
261 $obj->uncompress();
262 $wgBlobCache = array( $this->mOldId => $obj );
264 return $obj->getItem( $this->mHash );
267 /** @todo document */
268 function getHash() {
269 return $this->mHash;
275 * To speed up conversion from 1.4 to 1.5 schema, text rows can refer to the
276 * leftover cur table as the backend. This avoids expensively copying hundreds
277 * of megabytes of data during the conversion downtime.
279 * Serialized HistoryBlobCurStub objects will be inserted into the text table
280 * on conversion if $wgFastSchemaUpgrades is set to true.
282 class HistoryBlobCurStub {
283 var $mCurId;
285 /** @todo document */
286 function HistoryBlobCurStub( $curid = 0 ) {
287 $this->mCurId = $curid;
291 * Sets the location (cur_id) of the main object to which this object
292 * points
294 function setLocation( $id ) {
295 $this->mCurId = $id;
298 /** @todo document */
299 function getText() {
300 $dbr = wfGetDB( DB_SLAVE );
301 $row = $dbr->selectRow( 'cur', array( 'cur_text' ), array( 'cur_id' => $this->mCurId ) );
302 if( !$row ) {
303 return false;
305 return $row->cur_text;