MediaWiki  master
RevisionStoreRecord.php
Go to the documentation of this file.
1 <?php
23 namespace MediaWiki\Revision;
24 
26 use InvalidArgumentException;
28 use MWTimestamp;
29 use Title;
30 use User;
31 use Wikimedia\Assert\Assert;
32 
41 
43  protected $mCurrent = false;
44 
57  public function __construct(
58  Title $title,
59  UserIdentity $user,
60  CommentStoreComment $comment,
61  $row,
62  RevisionSlots $slots,
63  $dbDomain = false
64  ) {
65  parent::__construct( $title, $slots, $dbDomain );
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 = MWTimestamp::convert( 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 (' . $this->mTitle->getPrefixedText() . ')' .
102  ' does not belong to page ID ' . $this->mPageId .
103  ' but actually belongs to ' . $this->mTitle->getArticleID()
104  );
105  }
106  }
107 
111  public function isCurrent() {
112  return $this->mCurrent;
113  }
114 
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 
147  public function getId() {
148  // overwritten just to add a guarantee to the contract
149  return parent::getId();
150  }
151 
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();
161  }
162 
163  return $this->mSize;
164  }
165 
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();
175  }
176 
177  return $this->mSha1;
178  }
179 
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 );
189  }
190 
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 );
200  }
201 
205  public function getTimestamp() {
206  // overwritten just to add a guarantee to the contract
207  return parent::getTimestamp();
208  }
209 
215  public function isReadyForInsertion() {
216  return true;
217  }
218 
219 }
220 
225 class_alias( RevisionStoreRecord::class, 'MediaWiki\Storage\RevisionStoreRecord' );
MWTimestamp
Library for creating and parsing MW-style timestamps.
Definition: MWTimestamp.php:32
Revision\RevisionRecord
Page revision base class.
Definition: RevisionRecord.php:46
Revision\RevisionStoreRecord\getSize
getSize()
Definition: RevisionStoreRecord.php:156
Revision\RevisionStoreRecord\getSha1
getSha1()
Definition: RevisionStoreRecord.php:170
Revision\RevisionStoreRecord\__construct
__construct(Title $title, UserIdentity $user, CommentStoreComment $comment, $row, RevisionSlots $slots, $dbDomain=false)
Definition: RevisionStoreRecord.php:57
Revision\RevisionStoreRecord\getUser
getUser( $audience=self::FOR_PUBLIC, User $user=null)
Definition: RevisionStoreRecord.php:186
getUser
getUser()
Revision\RevisionStoreRecord\isDeleted
isDeleted( $field)
MCR migration note: this replaces Revision::isDeleted.
Definition: RevisionStoreRecord.php:122
MediaWiki\User\UserIdentity
Interface for objects representing user identity.
Definition: UserIdentity.php:32
MediaWiki\Revision
Definition: FallbackSlotRoleHandler.php:23
Revision\RevisionStoreRecord\getId
getId()
Definition: RevisionStoreRecord.php:147
$title
$title
Definition: testCompression.php:38
Revision\RevisionStoreRecord\userCan
userCan( $field, User $user)
Determine if the current user is allowed to view a particular field of this revision,...
Definition: RevisionStoreRecord.php:133
Revision\RevisionStoreRecord\getComment
getComment( $audience=self::FOR_PUBLIC, User $user=null)
Definition: RevisionStoreRecord.php:197
Revision\RevisionStoreRecord\isReadyForInsertion
isReadyForInsertion()
Definition: RevisionStoreRecord.php:215
Revision\RevisionStoreRecord
A RevisionRecord representing an existing revision persisted in the revision table.
Definition: RevisionStoreRecord.php:40
Revision\RevisionRecord\$mSize
int null $mSize
Definition: RevisionRecord.php:77
Title
Represents a title within MediaWiki.
Definition: Title.php:42
Revision\RevisionStoreRecord\isCurrent
isCurrent()
Checks whether the revision record is a stored current revision.1.35 bool
Definition: RevisionStoreRecord.php:111
Revision\RevisionSlots
Value object representing the set of slots belonging to a revision.
Definition: RevisionSlots.php:39
getTimestamp
getTimestamp()
Definition: RevisionSearchResultTrait.php:161
Revision\RevisionStoreRecord\$mCurrent
bool $mCurrent
Definition: RevisionStoreRecord.php:43
User
The User object encapsulates all of the user-specific settings (user_id, name, rights,...
Definition: User.php:55
Revision\RevisionRecord\$mSha1
string null $mSha1
Definition: RevisionRecord.php:79
Revision\RevisionStoreRecord\getTimestamp
getTimestamp()
Definition: RevisionStoreRecord.php:205
CommentStoreComment
CommentStoreComment represents a comment stored by CommentStore.
Definition: CommentStoreComment.php:29