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  $pageIdBasedOnPage = $this->getArticleId( $this->mPage );
94  if ( $this->mPageId && $pageIdBasedOnPage && $this->mPageId !== $pageIdBasedOnPage ) {
95  throw new InvalidArgumentException(
96  'The given page (' . $this->mPage . ')' .
97  ' does not belong to page ID ' . $this->mPageId .
98  ' but actually belongs to ' . $this->getArticleId( $this->mPage )
99  );
100  }
101  }
102 
106  public function isCurrent() {
107  return $this->mCurrent;
108  }
109 
117  public function isDeleted( $field ) {
118  if ( $this->isCurrent() && $field === self::DELETED_TEXT ) {
119  // Current revisions of pages cannot have the content hidden. Skipping this
120  // check is very useful for Parser as it fetches templates using newKnownCurrent().
121  // Calling getVisibility() in that case triggers a verification database query.
122  return false; // no need to check
123  }
124 
125  return parent::isDeleted( $field );
126  }
127 
128  public function userCan( $field, Authority $performer ) {
129  if ( $this->isCurrent() && $field === self::DELETED_TEXT ) {
130  // Current revisions of pages cannot have the content hidden. Skipping this
131  // check is very useful for Parser as it fetches templates using newKnownCurrent().
132  // Calling getVisibility() in that case triggers a verification database query.
133  return true; // no need to check
134  }
135 
136  return parent::userCan( $field, $performer );
137  }
138 
143  public function getId( $wikiId = self::LOCAL ) {
144  // overwritten just to add a guarantee to the contract
145  return parent::getId( $wikiId );
146  }
147 
152  public function getSize() {
153  // If length is null, calculate and remember it (potentially SLOW!).
154  // This is for compatibility with old database rows that don't have the field set.
155  if ( $this->mSize === null ) {
156  $this->mSize = $this->mSlots->computeSize();
157  }
158 
159  return $this->mSize;
160  }
161 
166  public function getSha1() {
167  // If hash is null, calculate it and remember (potentially SLOW!)
168  // This is for compatibility with old database rows that don't have the field set.
169  if ( $this->mSha1 === null ) {
170  $this->mSha1 = $this->mSlots->computeSha1();
171  }
172 
173  return $this->mSha1;
174  }
175 
182  public function getUser( $audience = self::FOR_PUBLIC, Authority $performer = null ) {
183  // overwritten just to add a guarantee to the contract
184  return parent::getUser( $audience, $performer );
185  }
186 
193  public function getComment( $audience = self::FOR_PUBLIC, Authority $performer = null ) {
194  // overwritten just to add a guarantee to the contract
195  return parent::getComment( $audience, $performer );
196  }
197 
201  public function getTimestamp() {
202  // overwritten just to add a guarantee to the contract
203  return parent::getTimestamp();
204  }
205 
211  public function isReadyForInsertion() {
212  return true;
213  }
214 
215 }
getUser()
Value object for a comment stored by CommentStore.
Library for creating and parsing MW-style timestamps.
Definition: MWTimestamp.php:39
Page revision base class.
string false $wikiId
Wiki ID; false means the current wiki.
Value object representing the set of slots belonging to a revision.
A RevisionRecord representing an existing revision persisted in the revision table.
isDeleted( $field)
MCR migration note: this replaced Revision::isDeleted.
userCan( $field, Authority $performer)
Determine if the give authority is allowed to view a particular field of this revision,...
isCurrent()
Checks whether the revision record is a stored current revision.1.35 bool
__construct(PageIdentity $page, UserIdentity $user, CommentStoreComment $comment, \stdClass $row, RevisionSlots $slots, $wikiId=self::LOCAL)
getComment( $audience=self::FOR_PUBLIC, Authority $performer=null)
getUser( $audience=self::FOR_PUBLIC, Authority $performer=null)
Interface for objects (potentially) representing an editable wiki page.
This interface represents the authority associated the current execution context, such as a web reque...
Definition: Authority.php:37
Interface for objects representing user identity.