ConcatenatedGzipHistoryBlob.php 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154
  1. <?php
  2. /**
  3. * Efficient concatenated text storage.
  4. *
  5. * This program is free software; you can redistribute it and/or modify
  6. * it under the terms of the GNU General Public License as published by
  7. * the Free Software Foundation; either version 2 of the License, or
  8. * (at your option) any later version.
  9. *
  10. * This program is distributed in the hope that it will be useful,
  11. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  12. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  13. * GNU General Public License for more details.
  14. *
  15. * You should have received a copy of the GNU General Public License along
  16. * with this program; if not, write to the Free Software Foundation, Inc.,
  17. * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
  18. * http://www.gnu.org/copyleft/gpl.html
  19. *
  20. * @file
  21. */
  22. /**
  23. * Concatenated gzip (CGZ) storage
  24. * Improves compression ratio by concatenating like objects before gzipping
  25. */
  26. class ConcatenatedGzipHistoryBlob implements HistoryBlob {
  27. public $mVersion = 0;
  28. public $mCompressed = false;
  29. /**
  30. * @var array|string
  31. * @fixme Why are some methods treating it as an array, and others as a string, unconditionally?
  32. */
  33. public $mItems = [];
  34. public $mDefaultHash = '';
  35. public $mSize = 0;
  36. public $mMaxSize = 10000000;
  37. public $mMaxCount = 100;
  38. public function __construct() {
  39. if ( !function_exists( 'gzdeflate' ) ) {
  40. throw new MWException( "Need zlib support to read or write this "
  41. . "kind of history object (ConcatenatedGzipHistoryBlob)\n" );
  42. }
  43. }
  44. /**
  45. * @param string $text
  46. * @return string
  47. */
  48. public function addItem( $text ) {
  49. $this->uncompress();
  50. $hash = md5( $text );
  51. if ( !isset( $this->mItems[$hash] ) ) {
  52. $this->mItems[$hash] = $text;
  53. $this->mSize += strlen( $text );
  54. }
  55. return $hash;
  56. }
  57. /**
  58. * @param string $hash
  59. * @return array|bool
  60. */
  61. public function getItem( $hash ) {
  62. $this->uncompress();
  63. if ( array_key_exists( $hash, $this->mItems ) ) {
  64. return $this->mItems[$hash];
  65. } else {
  66. return false;
  67. }
  68. }
  69. /**
  70. * @param string $text
  71. * @return void
  72. */
  73. public function setText( $text ) {
  74. $this->uncompress();
  75. $this->mDefaultHash = $this->addItem( $text );
  76. }
  77. /**
  78. * @return array|bool
  79. */
  80. public function getText() {
  81. $this->uncompress();
  82. return $this->getItem( $this->mDefaultHash );
  83. }
  84. /**
  85. * Remove an item
  86. *
  87. * @param string $hash
  88. */
  89. public function removeItem( $hash ) {
  90. $this->mSize -= strlen( $this->mItems[$hash] );
  91. unset( $this->mItems[$hash] );
  92. }
  93. /**
  94. * Compress the bulk data in the object
  95. */
  96. public function compress() {
  97. if ( !$this->mCompressed ) {
  98. $this->mItems = gzdeflate( serialize( $this->mItems ) );
  99. $this->mCompressed = true;
  100. }
  101. }
  102. /**
  103. * Uncompress bulk data
  104. */
  105. public function uncompress() {
  106. if ( $this->mCompressed ) {
  107. $this->mItems = unserialize( gzinflate( $this->mItems ) );
  108. $this->mCompressed = false;
  109. }
  110. }
  111. /**
  112. * @return array
  113. */
  114. function __sleep() {
  115. $this->compress();
  116. return [ 'mVersion', 'mCompressed', 'mItems', 'mDefaultHash' ];
  117. }
  118. function __wakeup() {
  119. $this->uncompress();
  120. }
  121. /**
  122. * Helper function for compression jobs
  123. * Returns true until the object is "full" and ready to be committed
  124. *
  125. * @return bool
  126. */
  127. public function isHappy() {
  128. return $this->mSize < $this->mMaxSize
  129. && count( $this->mItems ) < $this->mMaxCount;
  130. }
  131. }
  132. // phpcs:ignore Generic.CodeAnalysis.UnconditionalIfStatement.Found
  133. if ( false ) {
  134. // Blobs generated by MediaWiki < 1.5 on PHP 4 were serialized with the
  135. // class name coerced to lowercase. We can improve efficiency by adding
  136. // autoload entries for the lowercase variants of these classes (T166759).
  137. // The code below is never executed, but it is picked up by the AutoloadGenerator
  138. // parser, which scans for class_alias() calls.
  139. class_alias( ConcatenatedGzipHistoryBlob::class, 'concatenatedgziphistoryblob' );
  140. }