Merge "Improve docs for Title::getInternalURL/getCanonicalURL"
[lhc/web/wiklou.git] / includes / historyblob / HistoryBlobStub.php
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 /**
24 * Pointer object for an item within a CGZ blob stored in the text table.
25 */
26 class HistoryBlobStub {
27 /**
28 * @var array One-step cache variable to hold base blobs; operations that
29 * pull multiple revisions may often pull multiple times from the same
30 * blob. By keeping the last-used one open, we avoid redundant
31 * unserialization and decompression overhead.
32 */
33 protected static $blobCache = [];
34
35 /** @var int */
36 public $mOldId;
37
38 /** @var string */
39 public $mHash;
40
41 /** @var string */
42 public $mRef;
43
44 /**
45 * @param string $hash The content hash of the text
46 * @param int $oldid The old_id for the CGZ object
47 */
48 function __construct( $hash = '', $oldid = 0 ) {
49 $this->mHash = $hash;
50 }
51
52 /**
53 * Sets the location (old_id) of the main object to which this object
54 * points
55 * @param int $id
56 */
57 function setLocation( $id ) {
58 $this->mOldId = $id;
59 }
60
61 /**
62 * Sets the location (old_id) of the referring object
63 * @param string $id
64 */
65 function setReferrer( $id ) {
66 $this->mRef = $id;
67 }
68
69 /**
70 * Gets the location of the referring object
71 * @return string
72 */
73 function getReferrer() {
74 return $this->mRef;
75 }
76
77 /**
78 * @return string|false
79 */
80 function getText() {
81 if ( isset( self::$blobCache[$this->mOldId] ) ) {
82 $obj = self::$blobCache[$this->mOldId];
83 } else {
84 $dbr = wfGetDB( DB_REPLICA );
85 $row = $dbr->selectRow(
86 'text',
87 [ 'old_flags', 'old_text' ],
88 [ 'old_id' => $this->mOldId ]
89 );
90
91 if ( !$row ) {
92 return false;
93 }
94
95 $flags = explode( ',', $row->old_flags );
96 if ( in_array( 'external', $flags ) ) {
97 $url = $row->old_text;
98 $parts = explode( '://', $url, 2 );
99 if ( !isset( $parts[1] ) || $parts[1] == '' ) {
100 return false;
101 }
102 $row->old_text = ExternalStore::fetchFromURL( $url );
103
104 }
105
106 if ( !in_array( 'object', $flags ) ) {
107 return false;
108 }
109
110 if ( in_array( 'gzip', $flags ) ) {
111 // This shouldn't happen, but a bug in the compress script
112 // may at times gzip-compress a HistoryBlob object row.
113 $obj = unserialize( gzinflate( $row->old_text ) );
114 } else {
115 $obj = unserialize( $row->old_text );
116 }
117
118 if ( !is_object( $obj ) ) {
119 // Correct for old double-serialization bug.
120 $obj = unserialize( $obj );
121 }
122
123 // Save this item for reference; if pulling many
124 // items in a row we'll likely use it again.
125 $obj->uncompress();
126 self::$blobCache = [ $this->mOldId => $obj ];
127 }
128
129 return $obj->getItem( $this->mHash );
130 }
131
132 /**
133 * Get the content hash
134 *
135 * @return string
136 */
137 function getHash() {
138 return $this->mHash;
139 }
140 }
141
142 // phpcs:ignore Generic.CodeAnalysis.UnconditionalIfStatement.Found
143 if ( false ) {
144 // Blobs generated by MediaWiki < 1.5 on PHP 4 were serialized with the
145 // class name coerced to lowercase. We can improve efficiency by adding
146 // autoload entries for the lowercase variants of these classes (T166759).
147 // The code below is never executed, but it is picked up by the AutoloadGenerator
148 // parser, which scans for class_alias() calls.
149 class_alias( HistoryBlobStub::class, 'historyblobstub' );
150 }