RevisionStoreRecord.php 7.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226
  1. <?php
  2. /**
  3. * A RevisionRecord representing an existing revision persisted in the revision table.
  4. *
  5. * This program is free software; you can redistribute it and/or modify
  6. * it under the terms of the GNU General Public License as published by
  7. * the Free Software Foundation; either version 2 of the License, or
  8. * (at your option) any later version.
  9. *
  10. * This program is distributed in the hope that it will be useful,
  11. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  12. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  13. * GNU General Public License for more details.
  14. *
  15. * You should have received a copy of the GNU General Public License along
  16. * with this program; if not, write to the Free Software Foundation, Inc.,
  17. * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
  18. * http://www.gnu.org/copyleft/gpl.html
  19. *
  20. * @file
  21. */
  22. namespace MediaWiki\Revision;
  23. use CommentStoreComment;
  24. use InvalidArgumentException;
  25. use MediaWiki\User\UserIdentity;
  26. use Title;
  27. use User;
  28. use Wikimedia\Assert\Assert;
  29. /**
  30. * A RevisionRecord representing an existing revision persisted in the revision table.
  31. * RevisionStoreRecord has no optional fields, getters will never return null.
  32. *
  33. * @since 1.31
  34. * @since 1.32 Renamed from MediaWiki\Storage\RevisionStoreRecord
  35. */
  36. class RevisionStoreRecord extends RevisionRecord {
  37. /** @var bool */
  38. protected $mCurrent = false;
  39. /**
  40. * @note Avoid calling this constructor directly. Use the appropriate methods
  41. * in RevisionStore instead.
  42. *
  43. * @param Title $title The title of the page this Revision is associated with.
  44. * @param UserIdentity $user
  45. * @param CommentStoreComment $comment
  46. * @param object $row A row from the revision table. Use RevisionStore::getQueryInfo() to build
  47. * a query that yields the required fields.
  48. * @param RevisionSlots $slots The slots of this revision.
  49. * @param bool|string $dbDomain DB domain of the relevant wiki or false for the current one.
  50. */
  51. function __construct(
  52. Title $title,
  53. UserIdentity $user,
  54. CommentStoreComment $comment,
  55. $row,
  56. RevisionSlots $slots,
  57. $dbDomain = false
  58. ) {
  59. parent::__construct( $title, $slots, $dbDomain );
  60. Assert::parameterType( 'object', $row, '$row' );
  61. $this->mId = intval( $row->rev_id );
  62. $this->mPageId = intval( $row->rev_page );
  63. $this->mComment = $comment;
  64. $timestamp = wfTimestamp( TS_MW, $row->rev_timestamp );
  65. Assert::parameter( is_string( $timestamp ), '$row->rev_timestamp', 'must be a valid timestamp' );
  66. $this->mUser = $user;
  67. $this->mMinorEdit = boolval( $row->rev_minor_edit );
  68. $this->mTimestamp = $timestamp;
  69. $this->mDeleted = intval( $row->rev_deleted );
  70. // NOTE: rev_parent_id = 0 indicates that there is no parent revision, while null
  71. // indicates that the parent revision is unknown. As per MW 1.31, the database schema
  72. // allows rev_parent_id to be NULL.
  73. $this->mParentId = isset( $row->rev_parent_id ) ? intval( $row->rev_parent_id ) : null;
  74. $this->mSize = isset( $row->rev_len ) ? intval( $row->rev_len ) : null;
  75. $this->mSha1 = !empty( $row->rev_sha1 ) ? $row->rev_sha1 : null;
  76. // NOTE: we must not call $this->mTitle->getLatestRevID() here, since the state of
  77. // page_latest may be in limbo during revision creation. In that case, calling
  78. // $this->mTitle->getLatestRevID() would cause a bad value to be cached in the Title
  79. // object. During page creation, that bad value would be 0.
  80. if ( isset( $row->page_latest ) ) {
  81. $this->mCurrent = ( $row->rev_id == $row->page_latest );
  82. }
  83. // sanity check
  84. if (
  85. $this->mPageId && $this->mTitle->exists()
  86. && $this->mPageId !== $this->mTitle->getArticleID()
  87. ) {
  88. throw new InvalidArgumentException(
  89. 'The given Title does not belong to page ID ' . $this->mPageId .
  90. ' but actually belongs to ' . $this->mTitle->getArticleID()
  91. );
  92. }
  93. }
  94. /**
  95. * MCR migration note: this replaces Revision::isCurrent
  96. *
  97. * @return bool
  98. */
  99. public function isCurrent() {
  100. return $this->mCurrent;
  101. }
  102. /**
  103. * MCR migration note: this replaces Revision::isDeleted
  104. *
  105. * @param int $field One of DELETED_* bitfield constants
  106. *
  107. * @return bool
  108. */
  109. public function isDeleted( $field ) {
  110. if ( $this->isCurrent() && $field === self::DELETED_TEXT ) {
  111. // Current revisions of pages cannot have the content hidden. Skipping this
  112. // check is very useful for Parser as it fetches templates using newKnownCurrent().
  113. // Calling getVisibility() in that case triggers a verification database query.
  114. return false; // no need to check
  115. }
  116. return parent::isDeleted( $field );
  117. }
  118. protected function userCan( $field, User $user ) {
  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 true; // no need to check
  124. }
  125. return parent::userCan( $field, $user );
  126. }
  127. /**
  128. * @return int The revision id, never null.
  129. */
  130. public function getId() {
  131. // overwritten just to add a guarantee to the contract
  132. return parent::getId();
  133. }
  134. /**
  135. * @throws RevisionAccessException if the size was unknown and could not be calculated.
  136. * @return int The nominal revision size, never null. May be computed on the fly.
  137. */
  138. public function getSize() {
  139. // If length is null, calculate and remember it (potentially SLOW!).
  140. // This is for compatibility with old database rows that don't have the field set.
  141. if ( $this->mSize === null ) {
  142. $this->mSize = $this->mSlots->computeSize();
  143. }
  144. return $this->mSize;
  145. }
  146. /**
  147. * @throws RevisionAccessException if the hash was unknown and could not be calculated.
  148. * @return string The revision hash, never null. May be computed on the fly.
  149. */
  150. public function getSha1() {
  151. // If hash is null, calculate it and remember (potentially SLOW!)
  152. // This is for compatibility with old database rows that don't have the field set.
  153. if ( $this->mSha1 === null ) {
  154. $this->mSha1 = $this->mSlots->computeSha1();
  155. }
  156. return $this->mSha1;
  157. }
  158. /**
  159. * @param int $audience
  160. * @param User|null $user
  161. *
  162. * @return UserIdentity The identity of the revision author, null if access is forbidden.
  163. */
  164. public function getUser( $audience = self::FOR_PUBLIC, User $user = null ) {
  165. // overwritten just to add a guarantee to the contract
  166. return parent::getUser( $audience, $user );
  167. }
  168. /**
  169. * @param int $audience
  170. * @param User|null $user
  171. *
  172. * @return CommentStoreComment The revision comment, null if access is forbidden.
  173. */
  174. public function getComment( $audience = self::FOR_PUBLIC, User $user = null ) {
  175. // overwritten just to add a guarantee to the contract
  176. return parent::getComment( $audience, $user );
  177. }
  178. /**
  179. * @return string timestamp, never null
  180. */
  181. public function getTimestamp() {
  182. // overwritten just to add a guarantee to the contract
  183. return parent::getTimestamp();
  184. }
  185. /**
  186. * @see RevisionStore::isComplete
  187. *
  188. * @return bool always true.
  189. */
  190. public function isReadyForInsertion() {
  191. return true;
  192. }
  193. }
  194. /**
  195. * Retain the old class name for backwards compatibility.
  196. * @deprecated since 1.32
  197. */
  198. class_alias( RevisionStoreRecord::class, 'MediaWiki\Storage\RevisionStoreRecord' );