MediaWiki  master
HistoryBlobStub.php
Go to the documentation of this file.
1 <?php
24 
35  protected static $blobCache = [];
36 
38  protected $mOldId;
39 
41  protected $mHash;
42 
44  protected $mRef;
45 
50  public function __construct( $hash = '', $oldid = 0 ) {
51  $this->mHash = $hash;
52  }
53 
59  public function setLocation( $id ) {
60  $this->mOldId = $id;
61  }
62 
67  public function setReferrer( $id ) {
68  $this->mRef = $id;
69  }
70 
75  public function getReferrer() {
76  return $this->mRef;
77  }
78 
82  public function getText() {
83  if ( isset( self::$blobCache[$this->mOldId] ) ) {
84  $obj = self::$blobCache[$this->mOldId];
85  } else {
86  $dbr = wfGetDB( DB_REPLICA );
87  $row = $dbr->selectRow(
88  'text',
89  [ 'old_flags', 'old_text' ],
90  [ 'old_id' => $this->mOldId ],
91  __METHOD__
92  );
93 
94  if ( !$row ) {
95  return false;
96  }
97 
98  $flags = explode( ',', $row->old_flags );
99  if ( in_array( 'external', $flags ) ) {
100  $url = $row->old_text;
101  $parts = explode( '://', $url, 2 );
102  if ( !isset( $parts[1] ) || $parts[1] == '' ) {
103  return false;
104  }
105  $row->old_text = MediaWikiServices::getInstance()
106  ->getExternalStoreAccess()
107  ->fetchFromURL( $url );
108  }
109 
110  if ( !in_array( 'object', $flags ) ) {
111  return false;
112  }
113 
114  if ( in_array( 'gzip', $flags ) ) {
115  // This shouldn't happen, but a bug in the compress script
116  // may at times gzip-compress a HistoryBlob object row.
117  $obj = unserialize( gzinflate( $row->old_text ) );
118  } else {
119  $obj = unserialize( $row->old_text );
120  }
121 
122  if ( !is_object( $obj ) ) {
123  // Correct for old double-serialization bug.
124  $obj = unserialize( $obj );
125  }
126 
127  // Save this item for reference; if pulling many
128  // items in a row we'll likely use it again.
129  $obj->uncompress();
130  self::$blobCache = [ $this->mOldId => $obj ];
131  }
132 
133  return $obj->getItem( $this->mHash );
134  }
135 
141  public function getHash() {
142  return $this->mHash;
143  }
144 }
145 
146 // @phan-suppress-next-next-line PhanImpossibleCondition
147 // phpcs:ignore Generic.CodeAnalysis.UnconditionalIfStatement.Found
148 if ( false ) {
149  // Blobs generated by MediaWiki < 1.5 on PHP 4 were serialized with the
150  // class name coerced to lowercase. We can improve efficiency by adding
151  // autoload entries for the lowercase variants of these classes (T166759).
152  // The code below is never executed, but it is picked up by the AutoloadGenerator
153  // parser, which scans for class_alias() calls.
154  class_alias( HistoryBlobStub::class, 'historyblobstub' );
155 }
MediaWiki\MediaWikiServices
MediaWikiServices is the service locator for the application scope of MediaWiki.
Definition: MediaWikiServices.php:154
HistoryBlobStub
Pointer object for an item within a CGZ blob stored in the text table.
Definition: HistoryBlobStub.php:28
HistoryBlobStub\getHash
getHash()
Get the content hash.
Definition: HistoryBlobStub.php:141
HistoryBlobStub\$blobCache
static array $blobCache
One-step cache variable to hold base blobs; operations that pull multiple revisions may often pull mu...
Definition: HistoryBlobStub.php:35
HistoryBlobStub\setReferrer
setReferrer( $id)
Sets the location (old_id) of the referring object.
Definition: HistoryBlobStub.php:67
$dbr
$dbr
Definition: testCompression.php:54
wfGetDB
wfGetDB( $db, $groups=[], $wiki=false)
Get a Database object.
Definition: GlobalFunctions.php:2467
DB_REPLICA
const DB_REPLICA
Definition: defines.php:25
HistoryBlobStub\__construct
__construct( $hash='', $oldid=0)
Definition: HistoryBlobStub.php:50
HistoryBlobStub\getText
getText()
Definition: HistoryBlobStub.php:82
HistoryBlobStub\$mHash
string $mHash
Definition: HistoryBlobStub.php:41
unserialize
unserialize( $serialized)
Definition: ApiMessageTrait.php:146
HistoryBlobStub\setLocation
setLocation( $id)
Sets the location (old_id) of the main object to which this object points.
Definition: HistoryBlobStub.php:59
HistoryBlobStub\$mOldId
int $mOldId
Definition: HistoryBlobStub.php:38
HistoryBlobStub\$mRef
string $mRef
Definition: HistoryBlobStub.php:44
HistoryBlobStub\getReferrer
getReferrer()
Gets the location of the referring object.
Definition: HistoryBlobStub.php:75