MediaWiki  master
RevisionStoreRecord.php
Go to the documentation of this file.
1 <?php
23 namespace MediaWiki\Revision;
24 
26 use InvalidArgumentException;
30 use MWTimestamp;
31 use Wikimedia\Assert\Assert;
32 
41 
43  protected $mCurrent = false;
44 
57  public function __construct(
58  PageIdentity $page,
59  UserIdentity $user,
60  CommentStoreComment $comment,
61  $row,
62  RevisionSlots $slots,
63  $wikiId = self::LOCAL
64  ) {
65  parent::__construct( $page, $slots, $wikiId );
66  Assert::parameterType( \stdClass::class, $row, '$row' );
67  $this->mId = intval( $row->rev_id );
68  $this->mPageId = intval( $row->rev_page );
69  $this->mComment = $comment;
70 
71  $timestamp = MWTimestamp::convert( TS_MW, $row->rev_timestamp );
72  Assert::parameter(
73  is_string( $timestamp ),
74  '$row->rev_timestamp',
75  "must be a valid timestamp (rev_id={$this->mId}, rev_timestamp={$row->rev_timestamp})"
76  );
77 
78  $this->mUser = $user;
79  $this->mMinorEdit = boolval( $row->rev_minor_edit );
80  $this->mTimestamp = $timestamp;
81  $this->mDeleted = intval( $row->rev_deleted );
82 
83  // NOTE: rev_parent_id = 0 indicates that there is no parent revision, while null
84  // indicates that the parent revision is unknown. As per MW 1.31, the database schema
85  // allows rev_parent_id to be NULL.
86  $this->mParentId = isset( $row->rev_parent_id ) ? intval( $row->rev_parent_id ) : null;
87  $this->mSize = isset( $row->rev_len ) ? intval( $row->rev_len ) : null;
88  $this->mSha1 = !empty( $row->rev_sha1 ) ? $row->rev_sha1 : null;
89 
90  // NOTE: we must not call $this->mTitle->getLatestRevID() here, since the state of
91  // page_latest may be in limbo during revision creation. In that case, calling
92  // $this->mTitle->getLatestRevID() would cause a bad value to be cached in the Title
93  // object. During page creation, that bad value would be 0.
94  if ( isset( $row->page_latest ) ) {
95  $this->mCurrent = ( $row->rev_id == $row->page_latest );
96  }
97 
98  // sanity check
99  $pageIdBasedOnPage = $this->getArticleId( $this->mPage );
100  if ( $this->mPageId && $pageIdBasedOnPage && $this->mPageId !== $pageIdBasedOnPage ) {
101  throw new InvalidArgumentException(
102  'The given page (' . $this->mPage . ')' .
103  ' does not belong to page ID ' . $this->mPageId .
104  ' but actually belongs to ' . $this->getArticleId( $this->mPage )
105  );
106  }
107  }
108 
112  public function isCurrent() {
113  return $this->mCurrent;
114  }
115 
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
129  }
130 
131  return parent::isDeleted( $field );
132  }
133 
134  public function userCan( $field, Authority $performer ) {
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
140  }
141 
142  return parent::userCan( $field, $performer );
143  }
144 
149  public function getId( $wikiId = self::LOCAL ) {
150  // overwritten just to add a guarantee to the contract
151  return parent::getId( $wikiId );
152  }
153 
158  public function getSize() {
159  // If length is null, calculate and remember it (potentially SLOW!).
160  // This is for compatibility with old database rows that don't have the field set.
161  if ( $this->mSize === null ) {
162  $this->mSize = $this->mSlots->computeSize();
163  }
164 
165  return $this->mSize;
166  }
167 
172  public function getSha1() {
173  // If hash is null, calculate it and remember (potentially SLOW!)
174  // This is for compatibility with old database rows that don't have the field set.
175  if ( $this->mSha1 === null ) {
176  $this->mSha1 = $this->mSlots->computeSha1();
177  }
178 
179  return $this->mSha1;
180  }
181 
188  public function getUser( $audience = self::FOR_PUBLIC, Authority $performer = null ) {
189  // overwritten just to add a guarantee to the contract
190  return parent::getUser( $audience, $performer );
191  }
192 
199  public function getComment( $audience = self::FOR_PUBLIC, Authority $performer = null ) {
200  // overwritten just to add a guarantee to the contract
201  return parent::getComment( $audience, $performer );
202  }
203 
207  public function getTimestamp() {
208  // overwritten just to add a guarantee to the contract
209  return parent::getTimestamp();
210  }
211 
217  public function isReadyForInsertion() {
218  return true;
219  }
220 
221 }
222 
227 class_alias( RevisionStoreRecord::class, 'MediaWiki\Storage\RevisionStoreRecord' );
Page\PageIdentity
Interface for objects (potentially) representing an editable wiki page.
Definition: PageIdentity.php:65
MWTimestamp
Library for creating and parsing MW-style timestamps.
Definition: MWTimestamp.php:37
Revision\RevisionRecord
Page revision base class.
Definition: RevisionRecord.php:47
Revision\RevisionStoreRecord\getSize
getSize()
Definition: RevisionStoreRecord.php:158
Revision\RevisionStoreRecord\getSha1
getSha1()
Definition: RevisionStoreRecord.php:172
Revision\RevisionStoreRecord\getId
getId( $wikiId=self::LOCAL)
Definition: RevisionStoreRecord.php:149
Revision\RevisionStoreRecord\isDeleted
isDeleted( $field)
MCR migration note: this replaces Revision::isDeleted.
Definition: RevisionStoreRecord.php:123
MediaWiki\User\UserIdentity
Interface for objects representing user identity.
Definition: UserIdentity.php:39
MediaWiki\Revision
Definition: ContributionsLookup.php:3
Revision\RevisionStoreRecord\userCan
userCan( $field, Authority $performer)
Determine if the give authority is allowed to view a particular field of this revision,...
Definition: RevisionStoreRecord.php:134
Revision\RevisionStoreRecord\__construct
__construct(PageIdentity $page, UserIdentity $user, CommentStoreComment $comment, $row, RevisionSlots $slots, $wikiId=self::LOCAL)
Definition: RevisionStoreRecord.php:57
Revision\RevisionRecord\$wikiId
string false $wikiId
Wiki ID; false means the current wiki.
Definition: RevisionRecord.php:67
MediaWiki\Permissions\Authority
Definition: Authority.php:30
Revision\RevisionStoreRecord\getComment
getComment( $audience=self::FOR_PUBLIC, Authority $performer=null)
Definition: RevisionStoreRecord.php:199
Revision\RevisionStoreRecord\isReadyForInsertion
isReadyForInsertion()
Definition: RevisionStoreRecord.php:217
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:81
Revision\RevisionStoreRecord\isCurrent
isCurrent()
Checks whether the revision record is a stored current revision.1.35 bool
Definition: RevisionStoreRecord.php:112
Revision\RevisionStoreRecord\getUser
getUser( $audience=self::FOR_PUBLIC, Authority $performer=null)
Definition: RevisionStoreRecord.php:188
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
Revision\RevisionRecord\$mSha1
string null $mSha1
Definition: RevisionRecord.php:83
Revision\RevisionStoreRecord\getTimestamp
getTimestamp()
Definition: RevisionStoreRecord.php:207
CommentStoreComment
Value object for a comment stored by CommentStore.
Definition: CommentStoreComment.php:30