MediaWiki  1.34.0
RevisionRecord.php
Go to the documentation of this file.
1 <?php
23 namespace MediaWiki\Revision;
24 
26 use Content;
27 use InvalidArgumentException;
28 use LogicException;
32 use MWException;
33 use Title;
34 use User;
35 use Wikimedia\Assert\Assert;
36 
46 abstract class RevisionRecord {
47 
48  // RevisionRecord deletion constants
49  const DELETED_TEXT = 1;
50  const DELETED_COMMENT = 2;
51  const DELETED_USER = 4;
52  const DELETED_RESTRICTED = 8;
53  const SUPPRESSED_USER = self::DELETED_USER | self::DELETED_RESTRICTED; // convenience
54  const SUPPRESSED_ALL = self::DELETED_TEXT | self::DELETED_COMMENT | self::DELETED_USER |
55  self::DELETED_RESTRICTED; // convenience
56 
57  // Audience options for accessors
58  const FOR_PUBLIC = 1;
59  const FOR_THIS_USER = 2;
60  const RAW = 3;
61 
63  protected $mWiki = false;
65  protected $mId;
67  protected $mPageId;
69  protected $mUser;
71  protected $mMinorEdit = false;
73  protected $mTimestamp;
75  protected $mDeleted = 0;
77  protected $mSize;
79  protected $mSha1;
81  protected $mParentId;
83  protected $mComment;
84 
86  protected $mTitle; // TODO: we only need the title for permission checks!
87 
89  protected $mSlots;
90 
101  function __construct( Title $title, RevisionSlots $slots, $dbDomain = false ) {
102  Assert::parameterType( 'string|boolean', $dbDomain, '$dbDomain' );
103 
104  $this->mTitle = $title;
105  $this->mSlots = $slots;
106  $this->mWiki = $dbDomain;
107 
108  // XXX: this is a sensible default, but we may not have a Title object here in the future.
109  $this->mPageId = $title->getArticleID();
110  }
111 
117  public function __sleep() {
118  throw new LogicException( __CLASS__ . ' is not serializable.' );
119  }
120 
127  public function hasSameContent( RevisionRecord $rec ) {
128  if ( $rec === $this ) {
129  return true;
130  }
131 
132  if ( $this->getId() !== null && $this->getId() === $rec->getId() ) {
133  return true;
134  }
135 
136  // check size before hash, since size is quicker to compute
137  if ( $this->getSize() !== $rec->getSize() ) {
138  return false;
139  }
140 
141  // instead of checking the hash, we could also check the content addresses of all slots.
142 
143  if ( $this->getSha1() === $rec->getSha1() ) {
144  return true;
145  }
146 
147  return false;
148  }
149 
167  public function getContent( $role, $audience = self::FOR_PUBLIC, User $user = null ) {
168  // XXX: throwing an exception would be nicer, but would a further
169  // departure from the signature of Revision::getContent(), and thus
170  // more complex and error prone refactoring.
171  if ( !$this->audienceCan( self::DELETED_TEXT, $audience, $user ) ) {
172  return null;
173  }
174 
175  $content = $this->getSlot( $role, $audience, $user )->getContent();
176  return $content->copy();
177  }
178 
191  public function getSlot( $role, $audience = self::FOR_PUBLIC, User $user = null ) {
192  $slot = $this->mSlots->getSlot( $role );
193 
194  if ( !$this->audienceCan( self::DELETED_TEXT, $audience, $user ) ) {
195  return SlotRecord::newWithSuppressedContent( $slot );
196  }
197 
198  return $slot;
199  }
200 
208  public function hasSlot( $role ) {
209  return $this->mSlots->hasSlot( $role );
210  }
211 
218  public function getSlotRoles() {
219  return $this->mSlots->getSlotRoles();
220  }
221 
227  public function getSlots() {
228  return $this->mSlots;
229  }
230 
245  public function getOriginalSlots() {
246  return new RevisionSlots( $this->mSlots->getOriginalSlots() );
247  }
248 
260  public function getInheritedSlots() {
261  return new RevisionSlots( $this->mSlots->getInheritedSlots() );
262  }
263 
273  public function getId() {
274  return $this->mId;
275  }
276 
289  public function getParentId() {
290  return $this->mParentId;
291  }
292 
303  abstract public function getSize();
304 
316  abstract public function getSha1();
317 
325  public function getPageId() {
326  return $this->mPageId;
327  }
328 
334  public function getWikiId() {
335  return $this->mWiki;
336  }
337 
345  public function getPageAsLinkTarget() {
346  return $this->mTitle;
347  }
348 
365  public function getUser( $audience = self::FOR_PUBLIC, User $user = null ) {
366  if ( !$this->audienceCan( self::DELETED_USER, $audience, $user ) ) {
367  return null;
368  } else {
369  return $this->mUser;
370  }
371  }
372 
390  public function getComment( $audience = self::FOR_PUBLIC, User $user = null ) {
391  if ( !$this->audienceCan( self::DELETED_COMMENT, $audience, $user ) ) {
392  return null;
393  } else {
394  return $this->mComment;
395  }
396  }
397 
403  public function isMinor() {
404  return (bool)$this->mMinorEdit;
405  }
406 
414  public function isDeleted( $field ) {
415  return ( $this->getVisibility() & $field ) == $field;
416  }
417 
425  public function getVisibility() {
426  return (int)$this->mDeleted;
427  }
428 
436  public function getTimestamp() {
437  return $this->mTimestamp;
438  }
439 
457  public function audienceCan( $field, $audience, User $user = null ) {
458  if ( $audience == self::FOR_PUBLIC && $this->isDeleted( $field ) ) {
459  return false;
460  } elseif ( $audience == self::FOR_THIS_USER ) {
461  if ( !$user ) {
462  throw new InvalidArgumentException(
463  'A User object must be given when checking FOR_THIS_USER audience.'
464  );
465  }
466 
467  if ( !$this->userCan( $field, $user ) ) {
468  return false;
469  }
470  }
471 
472  return true;
473  }
474 
487  protected function userCan( $field, User $user ) {
488  // TODO: use callback for permission checks, so we don't need to know a Title object!
489  return self::userCanBitfield( $this->getVisibility(), $field, $user, $this->mTitle );
490  }
491 
508  public static function userCanBitfield( $bitfield, $field, User $user, Title $title = null ) {
509  if ( $bitfield & $field ) { // aspect is deleted
510  if ( $bitfield & self::DELETED_RESTRICTED ) {
511  $permissions = [ 'suppressrevision', 'viewsuppressed' ];
512  } elseif ( $field & self::DELETED_TEXT ) {
513  $permissions = [ 'deletedtext' ];
514  } else {
515  $permissions = [ 'deletedhistory' ];
516  }
517 
518  // XXX: How can we avoid global scope here?
519  // Perhaps the audience check should be done in a callback.
520  $permissionManager = MediaWikiServices::getInstance()->getPermissionManager();
521  $permissionlist = implode( ', ', $permissions );
522  if ( $title === null ) {
523  wfDebug( "Checking for $permissionlist due to $field match on $bitfield\n" );
524  foreach ( $permissions as $perm ) {
525  if ( $permissionManager->userHasRight( $user, $perm ) ) {
526  return true;
527  }
528  }
529  return false;
530  } else {
531  $text = $title->getPrefixedText();
532  wfDebug( "Checking for $permissionlist on $text due to $field match on $bitfield\n" );
533 
534  foreach ( $permissions as $perm ) {
535  if ( $permissionManager->userCan( $perm, $user, $title ) ) {
536  return true;
537  }
538  }
539  return false;
540  }
541  } else {
542  return true;
543  }
544  }
545 
557  public function isReadyForInsertion() {
558  // NOTE: don't check getSize() and getSha1(), since that may cause the full content to
559  // be loaded in order to calculate the values. Just assume these methods will not return
560  // null if mSlots is not empty.
561 
562  // NOTE: getId() and getPageId() may return null before a revision is saved, so don't
563  // check them.
564 
565  return $this->getTimestamp() !== null
566  && $this->getComment( self::RAW ) !== null
567  && $this->getUser( self::RAW ) !== null
568  && $this->mSlots->getSlotRoles() !== [];
569  }
570 
571 }
572 
577 class_alias( RevisionRecord::class, 'MediaWiki\Storage\RevisionRecord' );
Revision\RevisionRecord
Page revision base class.
Definition: RevisionRecord.php:46
Revision\RevisionRecord\$mWiki
string false $mWiki
Wiki ID; false means the current wiki.
Definition: RevisionRecord.php:63
Revision\RevisionRecord\$mTitle
Title $mTitle
Definition: RevisionRecord.php:86
MediaWiki\MediaWikiServices
MediaWikiServices is the service locator for the application scope of MediaWiki.
Definition: MediaWikiServices.php:117
Revision\RevisionRecord\getOriginalSlots
getOriginalSlots()
Returns the slots that originate in this revision.
Definition: RevisionRecord.php:245
Revision\RevisionRecord\isDeleted
isDeleted( $field)
MCR migration note: this replaces Revision::isDeleted.
Definition: RevisionRecord.php:414
Revision\RevisionRecord\$mSlots
RevisionSlots $mSlots
Definition: RevisionRecord.php:89
Revision\RevisionRecord\$mComment
CommentStoreComment null $mComment
Definition: RevisionRecord.php:83
Revision\RevisionRecord\getTimestamp
getTimestamp()
MCR migration note: this replaces Revision::getTimestamp.
Definition: RevisionRecord.php:436
Revision\RevisionRecord\$mMinorEdit
bool $mMinorEdit
Definition: RevisionRecord.php:71
Revision\RevisionRecord\audienceCan
audienceCan( $field, $audience, User $user=null)
Check that the given audience has access to the given field.
Definition: RevisionRecord.php:457
Revision\RevisionRecord\getSlot
getSlot( $role, $audience=self::FOR_PUBLIC, User $user=null)
Returns meta-data for the given slot.
Definition: RevisionRecord.php:191
Revision\RevisionRecord\hasSlot
hasSlot( $role)
Returns whether the given slot is defined in this revision.
Definition: RevisionRecord.php:208
Revision\RevisionRecord\userCan
userCan( $field, User $user)
Determine if the current user is allowed to view a particular field of this revision,...
Definition: RevisionRecord.php:487
MediaWiki\User\UserIdentity
Interface for objects representing user identity.
Definition: UserIdentity.php:32
Revision\RevisionRecord\$mUser
UserIdentity null $mUser
Definition: RevisionRecord.php:69
MediaWiki\Revision
Created by PhpStorm.
Definition: FallbackSlotRoleHandler.php:23
Revision\RevisionRecord\getInheritedSlots
getInheritedSlots()
Returns slots inherited from some previous revision.
Definition: RevisionRecord.php:260
MediaWiki\MediaWikiServices\getInstance
static getInstance()
Returns the global default instance of the top level service locator.
Definition: MediaWikiServices.php:138
Revision\RevisionRecord\$mTimestamp
string null $mTimestamp
Definition: RevisionRecord.php:73
MWException
MediaWiki exception.
Definition: MWException.php:26
Revision\RevisionRecord\getSize
getSize()
Returns the nominal size of this revision, in bogo-bytes.
Revision\RevisionRecord\getSha1
getSha1()
Returns the base36 sha1 of this revision.
Revision\RevisionRecord\getUser
getUser( $audience=self::FOR_PUBLIC, User $user=null)
Fetch revision's author's user identity, if it's available to the specified audience.
Definition: RevisionRecord.php:365
Revision\RevisionRecord\isMinor
isMinor()
MCR migration note: this replaces Revision::isMinor.
Definition: RevisionRecord.php:403
Revision\RevisionRecord\isReadyForInsertion
isReadyForInsertion()
Returns whether this RevisionRecord is ready for insertion, that is, whether it contains all informat...
Definition: RevisionRecord.php:557
Revision\RevisionRecord\userCanBitfield
static userCanBitfield( $bitfield, $field, User $user, Title $title=null)
Determine if the current user is allowed to view a particular field of this revision,...
Definition: RevisionRecord.php:508
Revision\RevisionRecord\RAW
const RAW
Definition: RevisionRecord.php:60
$title
$title
Definition: testCompression.php:34
Revision\RevisionRecord\getSlotRoles
getSlotRoles()
Returns the slot names (roles) of all slots present in this revision.
Definition: RevisionRecord.php:218
Revision\RevisionRecord\$mParentId
int null $mParentId
Definition: RevisionRecord.php:81
wfDebug
wfDebug( $text, $dest='all', array $context=[])
Sends a line to the debug log if enabled or, optionally, to a comment in output.
Definition: GlobalFunctions.php:913
Revision\RevisionRecord\getPageId
getPageId()
Get the page ID.
Definition: RevisionRecord.php:325
Revision\SlotRecord\newWithSuppressedContent
static newWithSuppressedContent(SlotRecord $slot)
Returns a new SlotRecord just like the given $slot, except that calling getContent() will fail with a...
Definition: SlotRecord.php:63
Revision\RevisionRecord\getId
getId()
Get revision ID.
Definition: RevisionRecord.php:273
Revision\RevisionRecord\getParentId
getParentId()
Get parent revision ID (the original previous page revision).
Definition: RevisionRecord.php:289
Revision\RevisionRecord\SUPPRESSED_USER
const SUPPRESSED_USER
Definition: RevisionRecord.php:53
Revision\RevisionRecord\__sleep
__sleep()
Implemented to defy serialization.
Definition: RevisionRecord.php:117
$content
$content
Definition: router.php:78
Revision\RevisionRecord\__construct
__construct(Title $title, RevisionSlots $slots, $dbDomain=false)
Definition: RevisionRecord.php:101
Revision\RevisionRecord\DELETED_USER
const DELETED_USER
Definition: RevisionRecord.php:51
Revision\RevisionRecord\getComment
getComment( $audience=self::FOR_PUBLIC, User $user=null)
Fetch revision comment, if it's available to the specified audience.
Definition: RevisionRecord.php:390
Revision\RevisionRecord\$mSize
int null $mSize
Definition: RevisionRecord.php:77
Revision\RevisionRecord\getSlots
getSlots()
Returns the slots defined for this revision.
Definition: RevisionRecord.php:227
Content
Base interface for content objects.
Definition: Content.php:34
Revision\RevisionRecord\FOR_PUBLIC
const FOR_PUBLIC
Definition: RevisionRecord.php:58
Revision\RevisionRecord\$mPageId
int $mPageId
Definition: RevisionRecord.php:67
Revision\RevisionRecord\getVisibility
getVisibility()
Get the deletion bitfield of the revision.
Definition: RevisionRecord.php:425
Revision\RevisionRecord\getPageAsLinkTarget
getPageAsLinkTarget()
Returns the title of the page this revision is associated with as a LinkTarget object.
Definition: RevisionRecord.php:345
Title
Represents a title within MediaWiki.
Definition: Title.php:42
Revision\RevisionRecord\DELETED_COMMENT
const DELETED_COMMENT
Definition: RevisionRecord.php:50
Revision\RevisionRecord\DELETED_TEXT
const DELETED_TEXT
Definition: RevisionRecord.php:49
Revision\RevisionSlots
Value object representing the set of slots belonging to a revision.
Definition: RevisionSlots.php:35
Revision\RevisionRecord\getContent
getContent( $role, $audience=self::FOR_PUBLIC, User $user=null)
Returns the Content of the given slot of this revision.
Definition: RevisionRecord.php:167
Revision\RevisionRecord\hasSameContent
hasSameContent(RevisionRecord $rec)
Definition: RevisionRecord.php:127
Revision\RevisionRecord\SUPPRESSED_ALL
const SUPPRESSED_ALL
Definition: RevisionRecord.php:54
Revision\RevisionRecord\DELETED_RESTRICTED
const DELETED_RESTRICTED
Definition: RevisionRecord.php:52
MediaWiki\Linker\LinkTarget
Definition: LinkTarget.php:26
User
The User object encapsulates all of the user-specific settings (user_id, name, rights,...
Definition: User.php:51
Revision\RevisionRecord\FOR_THIS_USER
const FOR_THIS_USER
Definition: RevisionRecord.php:59
Revision\RevisionRecord\$mSha1
string null $mSha1
Definition: RevisionRecord.php:79
Revision\RevisionRecord\getWikiId
getWikiId()
Get the ID of the wiki this revision belongs to.
Definition: RevisionRecord.php:334
Revision\RevisionRecord\$mDeleted
int $mDeleted
using the DELETED_XXX and SUPPRESSED_XXX flags
Definition: RevisionRecord.php:75
Revision\RevisionRecord\$mId
int null $mId
Definition: RevisionRecord.php:65
CommentStoreComment
CommentStoreComment represents a comment stored by CommentStore.
Definition: CommentStoreComment.php:29