Merge "messagecache: avoid caching message pages that do not override"
[lhc/web/wiklou.git] / includes / Revision / RevisionStoreRecord.php
1 <?php
2 /**
3 * A RevisionRecord representing an existing revision persisted in the revision table.
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 namespace MediaWiki\Revision;
24
25 use CommentStoreComment;
26 use InvalidArgumentException;
27 use MediaWiki\User\UserIdentity;
28 use Title;
29 use User;
30 use Wikimedia\Assert\Assert;
31
32 /**
33 * A RevisionRecord representing an existing revision persisted in the revision table.
34 * RevisionStoreRecord has no optional fields, getters will never return null.
35 *
36 * @since 1.31
37 * @since 1.32 Renamed from MediaWiki\Storage\RevisionStoreRecord
38 */
39 class RevisionStoreRecord extends RevisionRecord {
40
41 /** @var bool */
42 protected $mCurrent = false;
43
44 /**
45 * @note Avoid calling this constructor directly. Use the appropriate methods
46 * in RevisionStore instead.
47 *
48 * @param Title $title The title of the page this Revision is associated with.
49 * @param UserIdentity $user
50 * @param CommentStoreComment $comment
51 * @param object $row A row from the revision table. Use RevisionStore::getQueryInfo() to build
52 * a query that yields the required fields.
53 * @param RevisionSlots $slots The slots of this revision.
54 * @param bool|string $wikiId the wiki ID of the site this Revision belongs to,
55 * or false for the local site.
56 */
57 function __construct(
58 Title $title,
59 UserIdentity $user,
60 CommentStoreComment $comment,
61 $row,
62 RevisionSlots $slots,
63 $wikiId = false
64 ) {
65 parent::__construct( $title, $slots, $wikiId );
66 Assert::parameterType( 'object', $row, '$row' );
67
68 $this->mId = intval( $row->rev_id );
69 $this->mPageId = intval( $row->rev_page );
70 $this->mComment = $comment;
71
72 $timestamp = wfTimestamp( TS_MW, $row->rev_timestamp );
73 Assert::parameter( is_string( $timestamp ), '$row->rev_timestamp', 'must be a valid timestamp' );
74
75 $this->mUser = $user;
76 $this->mMinorEdit = boolval( $row->rev_minor_edit );
77 $this->mTimestamp = $timestamp;
78 $this->mDeleted = intval( $row->rev_deleted );
79
80 // NOTE: rev_parent_id = 0 indicates that there is no parent revision, while null
81 // indicates that the parent revision is unknown. As per MW 1.31, the database schema
82 // allows rev_parent_id to be NULL.
83 $this->mParentId = isset( $row->rev_parent_id ) ? intval( $row->rev_parent_id ) : null;
84 $this->mSize = isset( $row->rev_len ) ? intval( $row->rev_len ) : null;
85 $this->mSha1 = !empty( $row->rev_sha1 ) ? $row->rev_sha1 : null;
86
87 // NOTE: we must not call $this->mTitle->getLatestRevID() here, since the state of
88 // page_latest may be in limbo during revision creation. In that case, calling
89 // $this->mTitle->getLatestRevID() would cause a bad value to be cached in the Title
90 // object. During page creation, that bad value would be 0.
91 if ( isset( $row->page_latest ) ) {
92 $this->mCurrent = ( $row->rev_id == $row->page_latest );
93 }
94
95 // sanity check
96 if (
97 $this->mPageId && $this->mTitle->exists()
98 && $this->mPageId !== $this->mTitle->getArticleID()
99 ) {
100 throw new InvalidArgumentException(
101 'The given Title does not belong to page ID ' . $this->mPageId .
102 ' but actually belongs to ' . $this->mTitle->getArticleID()
103 );
104 }
105 }
106
107 /**
108 * MCR migration note: this replaces Revision::isCurrent
109 *
110 * @return bool
111 */
112 public function isCurrent() {
113 return $this->mCurrent;
114 }
115
116 /**
117 * MCR migration note: this replaces Revision::isDeleted
118 *
119 * @param int $field One of DELETED_* bitfield constants
120 *
121 * @return bool
122 */
123 public function isDeleted( $field ) {
124 if ( $this->isCurrent() && $field === self::DELETED_TEXT ) {
125 // Current revisions of pages cannot have the content hidden. Skipping this
126 // check is very useful for Parser as it fetches templates using newKnownCurrent().
127 // Calling getVisibility() in that case triggers a verification database query.
128 return false; // no need to check
129 }
130
131 return parent::isDeleted( $field );
132 }
133
134 protected function userCan( $field, User $user ) {
135 if ( $this->isCurrent() && $field === self::DELETED_TEXT ) {
136 // Current revisions of pages cannot have the content hidden. Skipping this
137 // check is very useful for Parser as it fetches templates using newKnownCurrent().
138 // Calling getVisibility() in that case triggers a verification database query.
139 return true; // no need to check
140 }
141
142 return parent::userCan( $field, $user );
143 }
144
145 /**
146 * @return int The revision id, never null.
147 */
148 public function getId() {
149 // overwritten just to add a guarantee to the contract
150 return parent::getId();
151 }
152
153 /**
154 * @throws RevisionAccessException if the size was unknown and could not be calculated.
155 * @return string The nominal revision size, never null. May be computed on the fly.
156 */
157 public function getSize() {
158 // If length is null, calculate and remember it (potentially SLOW!).
159 // This is for compatibility with old database rows that don't have the field set.
160 if ( $this->mSize === null ) {
161 $this->mSize = $this->mSlots->computeSize();
162 }
163
164 return $this->mSize;
165 }
166
167 /**
168 * @throws RevisionAccessException if the hash was unknown and could not be calculated.
169 * @return string The revision hash, never null. May be computed on the fly.
170 */
171 public function getSha1() {
172 // If hash is null, calculate it and remember (potentially SLOW!)
173 // This is for compatibility with old database rows that don't have the field set.
174 if ( $this->mSha1 === null ) {
175 $this->mSha1 = $this->mSlots->computeSha1();
176 }
177
178 return $this->mSha1;
179 }
180
181 /**
182 * @param int $audience
183 * @param User|null $user
184 *
185 * @return UserIdentity The identity of the revision author, null if access is forbidden.
186 */
187 public function getUser( $audience = self::FOR_PUBLIC, User $user = null ) {
188 // overwritten just to add a guarantee to the contract
189 return parent::getUser( $audience, $user );
190 }
191
192 /**
193 * @param int $audience
194 * @param User|null $user
195 *
196 * @return CommentStoreComment The revision comment, null if access is forbidden.
197 */
198 public function getComment( $audience = self::FOR_PUBLIC, User $user = null ) {
199 // overwritten just to add a guarantee to the contract
200 return parent::getComment( $audience, $user );
201 }
202
203 /**
204 * @return string timestamp, never null
205 */
206 public function getTimestamp() {
207 // overwritten just to add a guarantee to the contract
208 return parent::getTimestamp();
209 }
210
211 /**
212 * @see RevisionStore::isComplete
213 *
214 * @return bool always true.
215 */
216 public function isReadyForInsertion() {
217 return true;
218 }
219
220 }
221
222 /**
223 * Retain the old class name for backwards compatibility.
224 * @deprecated since 1.32
225 */
226 class_alias( RevisionStoreRecord::class, 'MediaWiki\Storage\RevisionStoreRecord' );