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\Revision
;
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.
37 * @since 1.32 Renamed from MediaWiki\Storage\RevisionStoreRecord
39 class RevisionStoreRecord
extends RevisionRecord
{
42 protected $mCurrent = false;
45 * @note Avoid calling this constructor directly. Use the appropriate methods
46 * in RevisionStore instead.
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.
60 CommentStoreComment
$comment,
65 parent
::__construct( $title, $slots, $wikiId );
66 Assert
::parameterType( 'object', $row, '$row' );
68 $this->mId
= intval( $row->rev_id
);
69 $this->mPageId
= intval( $row->rev_page
);
70 $this->mComment
= $comment;
72 $timestamp = wfTimestamp( TS_MW
, $row->rev_timestamp
);
73 Assert
::parameter( is_string( $timestamp ), '$row->rev_timestamp', 'must be a valid timestamp' );
76 $this->mMinorEdit
= boolval( $row->rev_minor_edit
);
77 $this->mTimestamp
= $timestamp;
78 $this->mDeleted
= intval( $row->rev_deleted
);
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;
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
);
97 $this->mPageId
&& $this->mTitle
->exists()
98 && $this->mPageId
!== $this->mTitle
->getArticleID()
100 throw new InvalidArgumentException(
101 'The given Title does not belong to page ID ' . $this->mPageId
.
102 ' but actually belongs to ' . $this->mTitle
->getArticleID()
108 * MCR migration note: this replaces Revision::isCurrent
112 public function isCurrent() {
113 return $this->mCurrent
;
117 * MCR migration note: this replaces Revision::isDeleted
119 * @param int $field One of DELETED_* bitfield constants
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
131 return parent
::isDeleted( $field );
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
142 return parent
::userCan( $field, $user );
146 * @return int The revision id, never null.
148 public function getId() {
149 // overwritten just to add a guarantee to the contract
150 return parent
::getId();
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.
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();
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.
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();
182 * @param int $audience
183 * @param User|null $user
185 * @return UserIdentity The identity of the revision author, null if access is forbidden.
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 );
193 * @param int $audience
194 * @param User|null $user
196 * @return CommentStoreComment The revision comment, null if access is forbidden.
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 );
204 * @return string timestamp, never null
206 public function getTimestamp() {
207 // overwritten just to add a guarantee to the contract
208 return parent
::getTimestamp();
212 * @see RevisionStore::isComplete
214 * @return bool always true.
216 public function isReadyForInsertion() {
223 * Retain the old class name for backwards compatibility.
224 * @deprecated since 1.32
226 class_alias( RevisionStoreRecord
::class, 'MediaWiki\Storage\RevisionStoreRecord' );