3 * A RevisionRecord representing an existing revision persisted in the revision table.
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.
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.
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
23 namespace MediaWiki\Storage
;
25 use CommentStoreComment
;
26 use InvalidArgumentException
;
27 use MediaWiki\User\UserIdentity
;
30 use Wikimedia\Assert\Assert
;
33 * A RevisionRecord representing an existing revision persisted in the revision table.
34 * RevisionStoreRecord has no optional fields, getters will never return null.
38 class RevisionStoreRecord
extends RevisionRecord
{
41 protected $mCurrent = false;
44 * @note Avoid calling this constructor directly. Use the appropriate methods
45 * in RevisionStore instead.
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.
59 CommentStoreComment
$comment,
64 parent
::__construct( $title, $slots, $wikiId );
65 Assert
::parameterType( 'object', $row, '$row' );
67 $this->mId
= intval( $row->rev_id
);
68 $this->mPageId
= intval( $row->rev_page
);
69 $this->mComment
= $comment;
71 $timestamp = wfTimestamp( TS_MW
, $row->rev_timestamp
);
72 Assert
::parameter( is_string( $timestamp ), '$row->rev_timestamp', 'must be a valid timestamp' );
75 $this->mMinorEdit
= boolval( $row->rev_minor_edit
);
76 $this->mTimestamp
= $timestamp;
77 $this->mDeleted
= intval( $row->rev_deleted
);
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;
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
);
96 $this->mPageId
&& $this->mTitle
->exists()
97 && $this->mPageId
!== $this->mTitle
->getArticleID()
99 throw new InvalidArgumentException(
100 'The given Title does not belong to page ID ' . $this->mPageId
.
101 ' but actually belongs to ' . $this->mTitle
->getArticleID()
107 * MCR migration note: this replaces Revision::isCurrent
111 public function isCurrent() {
112 return $this->mCurrent
;
116 * MCR migration note: this replaces Revision::isDeleted
118 * @param int $field One of DELETED_* bitfield constants
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
130 return parent
::isDeleted( $field );
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
141 return parent
::userCan( $field, $user );
145 * @return int The revision id, never null.
147 public function getId() {
148 // overwritten just to add a guarantee to the contract
149 return parent
::getId();
153 * @throws RevisionAccessException if the size was unknown and could not be calculated.
154 * @return string The nominal revision size, never null. May be computed on the fly.
156 public function getSize() {
157 // If length is null, calculate and remember it (potentially SLOW!).
158 // This is for compatibility with old database rows that don't have the field set.
159 if ( $this->mSize
=== null ) {
160 $this->mSize
= $this->mSlots
->computeSize();
167 * @throws RevisionAccessException if the hash was unknown and could not be calculated.
168 * @return string The revision hash, never null. May be computed on the fly.
170 public function getSha1() {
171 // If hash is null, calculate it and remember (potentially SLOW!)
172 // This is for compatibility with old database rows that don't have the field set.
173 if ( $this->mSha1
=== null ) {
174 $this->mSha1
= $this->mSlots
->computeSha1();
181 * @param int $audience
182 * @param User|null $user
184 * @return UserIdentity The identity of the revision author, null if access is forbidden.
186 public function getUser( $audience = self
::FOR_PUBLIC
, User
$user = null ) {
187 // overwritten just to add a guarantee to the contract
188 return parent
::getUser( $audience, $user );
192 * @param int $audience
193 * @param User|null $user
195 * @return CommentStoreComment The revision comment, null if access is forbidden.
197 public function getComment( $audience = self
::FOR_PUBLIC
, User
$user = null ) {
198 // overwritten just to add a guarantee to the contract
199 return parent
::getComment( $audience, $user );
203 * @return string timestamp, never null
205 public function getTimestamp() {
206 // overwritten just to add a guarantee to the contract
207 return parent
::getTimestamp();