MediaWiki  1.34.0
RevisionStoreRecord.php
Go to the documentation of this file.
1 <?php
23 namespace MediaWiki\Revision;
24 
26 use InvalidArgumentException;
28 use Title;
29 use User;
30 use Wikimedia\Assert\Assert;
31 
40 
42  protected $mCurrent = false;
43 
56  function __construct(
57  Title $title,
58  UserIdentity $user,
59  CommentStoreComment $comment,
60  $row,
61  RevisionSlots $slots,
62  $dbDomain = false
63  ) {
64  parent::__construct( $title, $slots, $dbDomain );
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 = !empty( $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 
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' );
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:56
Revision\RevisionStoreRecord\getUser
getUser( $audience=self::FOR_PUBLIC, User $user=null)
Definition: RevisionStoreRecord.php:186
wfTimestamp
wfTimestamp( $outputtype=TS_UNIX, $ts=0)
Get a timestamp string in one of various formats.
Definition: GlobalFunctions.php:1869
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
Created by PhpStorm.
Definition: FallbackSlotRoleHandler.php:23
Revision\RevisionStoreRecord\getId
getId()
Definition: RevisionStoreRecord.php:147
$title
$title
Definition: testCompression.php:34
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:39
Revision\RevisionRecord\$mSize
int null $mSize
Definition: RevisionRecord.php:77
Title
Represents a title within MediaWiki.
Definition: Title.php:42
Revision\RevisionStoreRecord\isCurrent
isCurrent()
MCR migration note: this replaces Revision::isCurrent.
Definition: RevisionStoreRecord.php:111
Revision\RevisionSlots
Value object representing the set of slots belonging to a revision.
Definition: RevisionSlots.php:35
getTimestamp
getTimestamp()
Definition: RevisionSearchResultTrait.php:154
Revision\RevisionStoreRecord\$mCurrent
bool $mCurrent
Definition: RevisionStoreRecord.php:42
User
The User object encapsulates all of the user-specific settings (user_id, name, rights,...
Definition: User.php:51
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