Merge "Add semantic tags to license info text"
[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 ' but actually belongs to ' . $this->mTitle->getArticleID()
102 );
103 }
104 }
105
106 /**
107 * MCR migration note: this replaces Revision::isCurrent
108 *
109 * @return bool
110 */
111 public function isCurrent() {
112 return $this->mCurrent;
113 }
114
115 /**
116 * MCR migration note: this replaces Revision::isDeleted
117 *
118 * @param int $field One of DELETED_* bitfield constants
119 *
120 * @return bool
121 */
122 public function isDeleted( $field ) {
123 if ( $this->isCurrent() && $field === self::DELETED_TEXT ) {
124 // Current revisions of pages cannot have the content hidden. Skipping this
125 // check is very useful for Parser as it fetches templates using newKnownCurrent().
126 // Calling getVisibility() in that case triggers a verification database query.
127 return false; // no need to check
128 }
129
130 return parent::isDeleted( $field );
131 }
132
133 protected function userCan( $field, User $user ) {
134 if ( $this->isCurrent() && $field === self::DELETED_TEXT ) {
135 // Current revisions of pages cannot have the content hidden. Skipping this
136 // check is very useful for Parser as it fetches templates using newKnownCurrent().
137 // Calling getVisibility() in that case triggers a verification database query.
138 return true; // no need to check
139 }
140
141 return parent::userCan( $field, $user );
142 }
143
144 /**
145 * @return int The revision id, never null.
146 */
147 public function getId() {
148 // overwritten just to add a guarantee to the contract
149 return parent::getId();
150 }
151
152 /**
153 * @return string The nominal revision size, never null. May be computed on the fly.
154 */
155 public function getSize() {
156 // If length is null, calculate and remember it (potentially SLOW!).
157 // This is for compatibility with old database rows that don't have the field set.
158 if ( $this->mSize === null ) {
159 $this->mSize = $this->mSlots->computeSize();
160 }
161
162 return $this->mSize;
163 }
164
165 /**
166 * @return string The revision hash, never null. May be computed on the fly.
167 */
168 public function getSha1() {
169 // If hash is null, calculate it and remember (potentially SLOW!)
170 // This is for compatibility with old database rows that don't have the field set.
171 if ( $this->mSha1 === null ) {
172 $this->mSha1 = $this->mSlots->computeSha1();
173 }
174
175 return $this->mSha1;
176 }
177
178 /**
179 * @param int $audience
180 * @param User|null $user
181 *
182 * @return UserIdentity The identity of the revision author, null if access is forbidden.
183 */
184 public function getUser( $audience = self::FOR_PUBLIC, User $user = null ) {
185 // overwritten just to add a guarantee to the contract
186 return parent::getUser( $audience, $user );
187 }
188
189 /**
190 * @param int $audience
191 * @param User|null $user
192 *
193 * @return CommentStoreComment The revision comment, null if access is forbidden.
194 */
195 public function getComment( $audience = self::FOR_PUBLIC, User $user = null ) {
196 // overwritten just to add a guarantee to the contract
197 return parent::getComment( $audience, $user );
198 }
199
200 /**
201 * @return string timestamp, never null
202 */
203 public function getTimestamp() {
204 // overwritten just to add a guarantee to the contract
205 return parent::getTimestamp();
206 }
207
208 }