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