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(
74  is_string( $timestamp ),
75  '$row->rev_timestamp',
76  "must be a valid timestamp (rev_id={$this->mId}, rev_timestamp={$row->rev_timestamp})"
77  );
78 
79  $this->mUser = $user;
80  $this->mMinorEdit = boolval( $row->rev_minor_edit );
81  $this->mTimestamp = $timestamp;
82  $this->mDeleted = intval( $row->rev_deleted );
83 
84  // NOTE: rev_parent_id = 0 indicates that there is no parent revision, while null
85  // indicates that the parent revision is unknown. As per MW 1.31, the database schema
86  // allows rev_parent_id to be NULL.
87  $this->mParentId = isset( $row->rev_parent_id ) ? intval( $row->rev_parent_id ) : null;
88  $this->mSize = isset( $row->rev_len ) ? intval( $row->rev_len ) : null;
89  $this->mSha1 = !empty( $row->rev_sha1 ) ? $row->rev_sha1 : null;
90 
91  // NOTE: we must not call $this->mTitle->getLatestRevID() here, since the state of
92  // page_latest may be in limbo during revision creation. In that case, calling
93  // $this->mTitle->getLatestRevID() would cause a bad value to be cached in the Title
94  // object. During page creation, that bad value would be 0.
95  if ( isset( $row->page_latest ) ) {
96  $this->mCurrent = ( $row->rev_id == $row->page_latest );
97  }
98 
99  // sanity check
100  if (
101  $this->mPageId && $this->mTitle->exists()
102  && $this->mPageId !== $this->mTitle->getArticleID()
103  ) {
104  throw new InvalidArgumentException(
105  'The given Title (' . $this->mTitle->getPrefixedText() . ')' .
106  ' does not belong to page ID ' . $this->mPageId .
107  ' but actually belongs to ' . $this->mTitle->getArticleID()
108  );
109  }
110  }
111 
115  public function isCurrent() {
116  return $this->mCurrent;
117  }
118 
126  public function isDeleted( $field ) {
127  if ( $this->isCurrent() && $field === self::DELETED_TEXT ) {
128  // Current revisions of pages cannot have the content hidden. Skipping this
129  // check is very useful for Parser as it fetches templates using newKnownCurrent().
130  // Calling getVisibility() in that case triggers a verification database query.
131  return false; // no need to check
132  }
133 
134  return parent::isDeleted( $field );
135  }
136 
137  protected function userCan( $field, User $user ) {
138  if ( $this->isCurrent() && $field === self::DELETED_TEXT ) {
139  // Current revisions of pages cannot have the content hidden. Skipping this
140  // check is very useful for Parser as it fetches templates using newKnownCurrent().
141  // Calling getVisibility() in that case triggers a verification database query.
142  return true; // no need to check
143  }
144 
145  return parent::userCan( $field, $user );
146  }
147 
151  public function getId() {
152  // overwritten just to add a guarantee to the contract
153  return parent::getId();
154  }
155 
160  public function getSize() {
161  // If length is null, calculate and remember it (potentially SLOW!).
162  // This is for compatibility with old database rows that don't have the field set.
163  if ( $this->mSize === null ) {
164  $this->mSize = $this->mSlots->computeSize();
165  }
166 
167  return $this->mSize;
168  }
169 
174  public function getSha1() {
175  // If hash is null, calculate it and remember (potentially SLOW!)
176  // This is for compatibility with old database rows that don't have the field set.
177  if ( $this->mSha1 === null ) {
178  $this->mSha1 = $this->mSlots->computeSha1();
179  }
180 
181  return $this->mSha1;
182  }
183 
190  public function getUser( $audience = self::FOR_PUBLIC, User $user = null ) {
191  // overwritten just to add a guarantee to the contract
192  return parent::getUser( $audience, $user );
193  }
194 
201  public function getComment( $audience = self::FOR_PUBLIC, User $user = null ) {
202  // overwritten just to add a guarantee to the contract
203  return parent::getComment( $audience, $user );
204  }
205 
209  public function getTimestamp() {
210  // overwritten just to add a guarantee to the contract
211  return parent::getTimestamp();
212  }
213 
219  public function isReadyForInsertion() {
220  return true;
221  }
222 
223 }
224 
229 class_alias( RevisionStoreRecord::class, 'MediaWiki\Storage\RevisionStoreRecord' );
MWTimestamp
Library for creating and parsing MW-style timestamps.
Definition: MWTimestamp.php:34
Revision\RevisionRecord
Page revision base class.
Definition: RevisionRecord.php:46
Revision\RevisionStoreRecord\getSize
getSize()
Definition: RevisionStoreRecord.php:160
Revision\RevisionStoreRecord\getSha1
getSha1()
Definition: RevisionStoreRecord.php:174
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:190
getUser
getUser()
Revision\RevisionStoreRecord\isDeleted
isDeleted( $field)
MCR migration note: this replaces Revision::isDeleted.
Definition: RevisionStoreRecord.php:126
MediaWiki\User\UserIdentity
Interface for objects representing user identity.
Definition: UserIdentity.php:32
MediaWiki\Revision
Definition: ContributionsLookup.php:3
Revision\RevisionStoreRecord\getId
getId()
Definition: RevisionStoreRecord.php:151
$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:137
Revision\RevisionStoreRecord\getComment
getComment( $audience=self::FOR_PUBLIC, User $user=null)
Definition: RevisionStoreRecord.php:201
Revision\RevisionStoreRecord\isReadyForInsertion
isReadyForInsertion()
Definition: RevisionStoreRecord.php:219
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:115
Revision\RevisionSlots
Value object representing the set of slots belonging to a revision.
Definition: RevisionSlots.php:41
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:56
Revision\RevisionRecord\$mSha1
string null $mSha1
Definition: RevisionRecord.php:79
Revision\RevisionStoreRecord\getTimestamp
getTimestamp()
Definition: RevisionStoreRecord.php:209
CommentStoreComment
Value object for a comment stored by CommentStore.
Definition: CommentStoreComment.php:30