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 
40 
42  protected $mCurrent = false;
43 
56  public function __construct(
57  PageIdentity $page,
58  UserIdentity $user,
59  CommentStoreComment $comment,
60  \stdClass $row,
61  RevisionSlots $slots,
62  $wikiId = self::LOCAL
63  ) {
64  parent::__construct( $page, $slots, $wikiId );
65  $this->mId = intval( $row->rev_id );
66  $this->mPageId = intval( $row->rev_page );
67  $this->mComment = $comment;
68 
69  // Don't use MWTimestamp::convert, instead let any detailed exception from MWTimestamp
70  // bubble up (T254210)
71  $timestamp = ( new MWTimestamp( $row->rev_timestamp ) )->getTimestamp( TS_MW );
72 
73  $this->mUser = $user;
74  $this->mMinorEdit = boolval( $row->rev_minor_edit );
75  $this->mTimestamp = $timestamp;
76  $this->mDeleted = intval( $row->rev_deleted );
77 
78  // NOTE: rev_parent_id = 0 indicates that there is no parent revision, while null
79  // indicates that the parent revision is unknown. As per MW 1.31, the database schema
80  // allows rev_parent_id to be NULL.
81  $this->mParentId = isset( $row->rev_parent_id ) ? intval( $row->rev_parent_id ) : null;
82  $this->mSize = isset( $row->rev_len ) ? intval( $row->rev_len ) : null;
83  $this->mSha1 = !empty( $row->rev_sha1 ) ? $row->rev_sha1 : null;
84 
85  // NOTE: we must not call $this->mTitle->getLatestRevID() here, since the state of
86  // page_latest may be in limbo during revision creation. In that case, calling
87  // $this->mTitle->getLatestRevID() would cause a bad value to be cached in the Title
88  // object. During page creation, that bad value would be 0.
89  if ( isset( $row->page_latest ) ) {
90  $this->mCurrent = ( $row->rev_id == $row->page_latest );
91  }
92 
93  // sanity check
94  $pageIdBasedOnPage = $this->getArticleId( $this->mPage );
95  if ( $this->mPageId && $pageIdBasedOnPage && $this->mPageId !== $pageIdBasedOnPage ) {
96  throw new InvalidArgumentException(
97  'The given page (' . $this->mPage . ')' .
98  ' does not belong to page ID ' . $this->mPageId .
99  ' but actually belongs to ' . $this->getArticleId( $this->mPage )
100  );
101  }
102  }
103 
107  public function isCurrent() {
108  return $this->mCurrent;
109  }
110 
118  public function isDeleted( $field ) {
119  if ( $this->isCurrent() && $field === self::DELETED_TEXT ) {
120  // Current revisions of pages cannot have the content hidden. Skipping this
121  // check is very useful for Parser as it fetches templates using newKnownCurrent().
122  // Calling getVisibility() in that case triggers a verification database query.
123  return false; // no need to check
124  }
125 
126  return parent::isDeleted( $field );
127  }
128 
129  public function userCan( $field, Authority $performer ) {
130  if ( $this->isCurrent() && $field === self::DELETED_TEXT ) {
131  // Current revisions of pages cannot have the content hidden. Skipping this
132  // check is very useful for Parser as it fetches templates using newKnownCurrent().
133  // Calling getVisibility() in that case triggers a verification database query.
134  return true; // no need to check
135  }
136 
137  return parent::userCan( $field, $performer );
138  }
139 
144  public function getId( $wikiId = self::LOCAL ) {
145  // overwritten just to add a guarantee to the contract
146  return parent::getId( $wikiId );
147  }
148 
153  public function getSize() {
154  // If length is null, calculate and remember it (potentially SLOW!).
155  // This is for compatibility with old database rows that don't have the field set.
156  if ( $this->mSize === null ) {
157  $this->mSize = $this->mSlots->computeSize();
158  }
159 
160  return $this->mSize;
161  }
162 
167  public function getSha1() {
168  // If hash is null, calculate it and remember (potentially SLOW!)
169  // This is for compatibility with old database rows that don't have the field set.
170  if ( $this->mSha1 === null ) {
171  $this->mSha1 = $this->mSlots->computeSha1();
172  }
173 
174  return $this->mSha1;
175  }
176 
183  public function getUser( $audience = self::FOR_PUBLIC, Authority $performer = null ) {
184  // overwritten just to add a guarantee to the contract
185  return parent::getUser( $audience, $performer );
186  }
187 
194  public function getComment( $audience = self::FOR_PUBLIC, Authority $performer = null ) {
195  // overwritten just to add a guarantee to the contract
196  return parent::getComment( $audience, $performer );
197  }
198 
202  public function getTimestamp() {
203  // overwritten just to add a guarantee to the contract
204  return parent::getTimestamp();
205  }
206 
212  public function isReadyForInsertion() {
213  return true;
214  }
215 
216 }
217 
222 class_alias( RevisionStoreRecord::class, 'MediaWiki\Storage\RevisionStoreRecord' );
Page\PageIdentity
Interface for objects (potentially) representing an editable wiki page.
Definition: PageIdentity.php:64
MWTimestamp
Library for creating and parsing MW-style timestamps.
Definition: MWTimestamp.php:38
MediaWiki\Revision\RevisionRecord
Page revision base class.
Definition: RevisionRecord.php:47
MediaWiki\Revision\RevisionStoreRecord\isDeleted
isDeleted( $field)
MCR migration note: this replaced Revision::isDeleted.
Definition: RevisionStoreRecord.php:118
MediaWiki\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:129
MediaWiki\User\UserIdentity
Interface for objects representing user identity.
Definition: UserIdentity.php:39
MediaWiki\Revision\RevisionStoreRecord\getComment
getComment( $audience=self::FOR_PUBLIC, Authority $performer=null)
Definition: RevisionStoreRecord.php:194
MediaWiki\Revision
Definition: ContributionsLookup.php:3
MediaWiki\Revision\RevisionRecord\$wikiId
string false $wikiId
Wiki ID; false means the current wiki.
Definition: RevisionRecord.php:67
MediaWiki\Revision\RevisionStoreRecord\getTimestamp
getTimestamp()
Definition: RevisionStoreRecord.php:202
MediaWiki\Revision\RevisionRecord\$mSha1
string null $mSha1
Definition: RevisionRecord.php:83
MediaWiki\Revision\RevisionStoreRecord\getId
getId( $wikiId=self::LOCAL)
Definition: RevisionStoreRecord.php:144
MediaWiki\Permissions\Authority
This interface represents the authority associated the current execution context, such as a web reque...
Definition: Authority.php:37
MediaWiki\Revision\RevisionStoreRecord\getSha1
getSha1()
Definition: RevisionStoreRecord.php:167
MediaWiki\Revision\RevisionStoreRecord
A RevisionRecord representing an existing revision persisted in the revision table.
Definition: RevisionStoreRecord.php:39
MediaWiki\Revision\RevisionStoreRecord\__construct
__construct(PageIdentity $page, UserIdentity $user, CommentStoreComment $comment, \stdClass $row, RevisionSlots $slots, $wikiId=self::LOCAL)
Definition: RevisionStoreRecord.php:56
MediaWiki\Revision\RevisionSlots
Value object representing the set of slots belonging to a revision.
Definition: RevisionSlots.php:41
MediaWiki\Revision\RevisionStoreRecord\$mCurrent
bool $mCurrent
Definition: RevisionStoreRecord.php:42
MediaWiki\Revision\RevisionStoreRecord\getUser
getUser( $audience=self::FOR_PUBLIC, Authority $performer=null)
Definition: RevisionStoreRecord.php:183
getTimestamp
getTimestamp()
Definition: RevisionSearchResultTrait.php:161
MediaWiki\Revision\RevisionStoreRecord\getSize
getSize()
Definition: RevisionStoreRecord.php:153
MediaWiki\Revision\RevisionRecord\$mSize
int null $mSize
Definition: RevisionRecord.php:81
MediaWiki\Revision\RevisionStoreRecord\isReadyForInsertion
isReadyForInsertion()
Definition: RevisionStoreRecord.php:212
CommentStoreComment
Value object for a comment stored by CommentStore.
Definition: CommentStoreComment.php:30
MediaWiki\Revision\RevisionStoreRecord\isCurrent
isCurrent()
Checks whether the revision record is a stored current revision.1.35 bool
Definition: RevisionStoreRecord.php:107