MediaWiki  master
RevisionRecord.php
Go to the documentation of this file.
1 <?php
23 namespace MediaWiki\Revision;
24 
26 use Content;
27 use InvalidArgumentException;
35 use Title;
36 use Wikimedia\NonSerializable\NonSerializableTrait;
37 
47 abstract class RevisionRecord implements WikiAwareEntity {
49  use NonSerializableTrait;
51 
52  // RevisionRecord deletion constants
53  public const DELETED_TEXT = 1;
54  public const DELETED_COMMENT = 2;
55  public const DELETED_USER = 4;
56  public const DELETED_RESTRICTED = 8;
57  public const SUPPRESSED_USER = self::DELETED_USER | self::DELETED_RESTRICTED; // convenience
58  public const SUPPRESSED_ALL = self::DELETED_TEXT | self::DELETED_COMMENT | self::DELETED_USER |
59  self::DELETED_RESTRICTED; // convenience
60 
61  // Audience options for accessors
62  public const FOR_PUBLIC = 1;
63  public const FOR_THIS_USER = 2;
64  public const RAW = 3;
65 
67  protected $wikiId = false;
69  protected $mId;
71  protected $mPageId;
73  protected $mUser;
75  protected $mMinorEdit = false;
77  protected $mTimestamp;
79  protected $mDeleted = 0;
81  protected $mSize;
83  protected $mSha1;
85  protected $mParentId;
87  protected $mComment;
88 
90  protected $mPage;
91 
93  protected $mSlots;
94 
103  public function __construct( PageIdentity $page, RevisionSlots $slots, $wikiId = self::LOCAL ) {
104  $this->assertWikiIdParam( $wikiId );
105 
106  $this->mPage = $page;
107  $this->mSlots = $slots;
108  $this->wikiId = $wikiId;
109  $this->mPageId = $this->getArticleId( $page );
110  }
111 
118  public function hasSameContent( RevisionRecord $rec ) {
119  if ( $rec === $this ) {
120  return true;
121  }
122 
123  if ( $this->getId() !== null && $this->getId() === $rec->getId() ) {
124  return true;
125  }
126 
127  // check size before hash, since size is quicker to compute
128  if ( $this->getSize() !== $rec->getSize() ) {
129  return false;
130  }
131 
132  // instead of checking the hash, we could also check the content addresses of all slots.
133 
134  if ( $this->getSha1() === $rec->getSha1() ) {
135  return true;
136  }
137 
138  return false;
139  }
140 
156  public function getContent( $role, $audience = self::FOR_PUBLIC, Authority $performer = null ) {
157  // XXX: throwing an exception would be nicer, but would a further
158  // departure from the signature of Revision::getContent(), and thus
159  // more complex and error prone refactoring.
160  if ( !$this->audienceCan( self::DELETED_TEXT, $audience, $performer ) ) {
161  return null;
162  }
163 
164  $content = $this->getSlot( $role, $audience, $performer )->getContent();
165  return $content->copy();
166  }
167 
180  public function getSlot( $role, $audience = self::FOR_PUBLIC, Authority $performer = null ) {
181  $slot = $this->mSlots->getSlot( $role );
182 
183  if ( !$this->audienceCan( self::DELETED_TEXT, $audience, $performer ) ) {
184  return SlotRecord::newWithSuppressedContent( $slot );
185  }
186 
187  return $slot;
188  }
189 
197  public function hasSlot( $role ) {
198  return $this->mSlots->hasSlot( $role );
199  }
200 
207  public function getSlotRoles() {
208  return $this->mSlots->getSlotRoles();
209  }
210 
222  public function getSlots() {
223  return $this->mSlots;
224  }
225 
240  public function getOriginalSlots() {
241  return new RevisionSlots( $this->mSlots->getOriginalSlots() );
242  }
243 
255  public function getInheritedSlots() {
256  return new RevisionSlots( $this->mSlots->getInheritedSlots() );
257  }
258 
269  public function getId( $wikiId = self::LOCAL ) {
270  $this->deprecateInvalidCrossWiki( $wikiId, '1.36' );
271  return $this->mId;
272  }
273 
287  public function getParentId( $wikiId = self::LOCAL ) {
288  $this->deprecateInvalidCrossWiki( $wikiId, '1.36' );
289  return $this->mParentId;
290  }
291 
302  abstract public function getSize();
303 
315  abstract public function getSha1();
316 
325  public function getPageId( $wikiId = self::LOCAL ) {
326  $this->deprecateInvalidCrossWiki( $wikiId, '1.36' );
327  return $this->mPageId;
328  }
329 
335  public function getWikiId() {
336  return $this->wikiId;
337  }
338 
345  public function getPageAsLinkTarget() {
346  // TODO: Should be TitleValue::newFromPage( $this->mPage ),
347  // but Title is used too much still, so let's keep propagating it
348  return Title::castFromPageIdentity( $this->mPage );
349  }
350 
360  public function getPage() {
361  return $this->mPage;
362  }
363 
379  public function getUser( $audience = self::FOR_PUBLIC, Authority $performer = null ) {
380  if ( !$this->audienceCan( self::DELETED_USER, $audience, $performer ) ) {
381  return null;
382  } else {
383  return $this->mUser;
384  }
385  }
386 
403  public function getComment( $audience = self::FOR_PUBLIC, Authority $performer = null ) {
404  if ( !$this->audienceCan( self::DELETED_COMMENT, $audience, $performer ) ) {
405  return null;
406  } else {
407  return $this->mComment;
408  }
409  }
410 
416  public function isMinor() {
417  return (bool)$this->mMinorEdit;
418  }
419 
427  public function isDeleted( $field ) {
428  return ( $this->getVisibility() & $field ) == $field;
429  }
430 
438  public function getVisibility() {
439  return (int)$this->mDeleted;
440  }
441 
449  public function getTimestamp() {
450  return $this->mTimestamp;
451  }
452 
469  public function audienceCan( $field, $audience, Authority $performer = null ) {
470  if ( $audience == self::FOR_PUBLIC && $this->isDeleted( $field ) ) {
471  return false;
472  } elseif ( $audience == self::FOR_THIS_USER ) {
473  if ( !$performer ) {
474  throw new InvalidArgumentException(
475  'An Authority object must be given when checking FOR_THIS_USER audience.'
476  );
477  }
478 
479  if ( !$this->userCan( $field, $performer ) ) {
480  return false;
481  }
482  }
483 
484  return true;
485  }
486 
499  public function userCan( $field, Authority $performer ) {
500  return self::userCanBitfield( $this->getVisibility(), $field, $performer, $this->mPage );
501  }
502 
519  public static function userCanBitfield( $bitfield, $field, Authority $performer, PageIdentity $page = null ) {
520  if ( $bitfield & $field ) { // aspect is deleted
521  if ( $bitfield & self::DELETED_RESTRICTED ) {
522  $permissions = [ 'suppressrevision', 'viewsuppressed' ];
523  } elseif ( $field & self::DELETED_TEXT ) {
524  $permissions = [ 'deletedtext' ];
525  } else {
526  $permissions = [ 'deletedhistory' ];
527  }
528 
529  $permissionlist = implode( ', ', $permissions );
530  if ( $page === null ) {
531  wfDebug( "Checking for $permissionlist due to $field match on $bitfield" );
532  return $performer->isAllowedAny( ...$permissions );
533  } else {
534  wfDebug( "Checking for $permissionlist on $page due to $field match on $bitfield" );
535  foreach ( $permissions as $perm ) {
536  if ( $performer->authorizeRead( $perm, $page ) ) {
537  return true;
538  }
539  }
540  return false;
541  }
542  } else {
543  return true;
544  }
545  }
546 
558  public function isReadyForInsertion() {
559  // NOTE: don't check getSize() and getSha1(), since that may cause the full content to
560  // be loaded in order to calculate the values. Just assume these methods will not return
561  // null if mSlots is not empty.
562 
563  // NOTE: getId() and getPageId() may return null before a revision is saved, so don't
564  // check them.
565 
566  return $this->getTimestamp() !== null
567  && $this->getComment( self::RAW ) !== null
568  && $this->getUser( self::RAW ) !== null
569  && $this->mSlots->getSlotRoles() !== [];
570  }
571 
577  public function isCurrent() {
578  return false;
579  }
580 }
581 
586 class_alias( RevisionRecord::class, 'MediaWiki\Storage\RevisionRecord' );
Page\PageIdentity
Interface for objects (potentially) representing an editable wiki page.
Definition: PageIdentity.php:65
MediaWiki\DAO\WikiAwareEntityTrait
trait WikiAwareEntityTrait
Definition: WikiAwareEntityTrait.php:32
Revision\RevisionRecord
Page revision base class.
Definition: RevisionRecord.php:47
MediaWiki\DAO\deprecateInvalidCrossWiki
deprecateInvalidCrossWiki( $wikiId, string $since)
Emits a deprecation warning $since version if $wikiId is not the same as this wiki.
Definition: WikiAwareEntityTrait.php:70
Revision\RevisionRecord\getPageId
getPageId( $wikiId=self::LOCAL)
Get the page ID.
Definition: RevisionRecord.php:325
Revision\RevisionRecord\userCan
userCan( $field, Authority $performer)
Determine if the give authority is allowed to view a particular field of this revision,...
Definition: RevisionRecord.php:499
Revision\RevisionRecord\getOriginalSlots
getOriginalSlots()
Returns the slots that originate in this revision.
Definition: RevisionRecord.php:240
Revision\RevisionRecord\isDeleted
isDeleted( $field)
MCR migration note: this replaces Revision::isDeleted.
Definition: RevisionRecord.php:427
Revision\RevisionRecord\getPage
getPage()
Returns the page this revision belongs to.
Definition: RevisionRecord.php:360
Revision\RevisionRecord\$mSlots
RevisionSlots $mSlots
Definition: RevisionRecord.php:93
Revision\RevisionRecord\$mComment
CommentStoreComment null $mComment
Definition: RevisionRecord.php:87
Revision\RevisionRecord\$mPage
PageIdentity $mPage
Definition: RevisionRecord.php:90
Revision\RevisionRecord\audienceCan
audienceCan( $field, $audience, Authority $performer=null)
Check that the given audience has access to the given field.
Definition: RevisionRecord.php:469
Revision\RevisionRecord\getTimestamp
getTimestamp()
MCR migration note: this replaces Revision::getTimestamp.
Definition: RevisionRecord.php:449
Revision\RevisionRecord\$mMinorEdit
bool $mMinorEdit
Definition: RevisionRecord.php:75
Revision\RevisionRecord\hasSlot
hasSlot( $role)
Returns whether the given slot is defined in this revision.
Definition: RevisionRecord.php:197
Revision\RevisionRecord\getParentId
getParentId( $wikiId=self::LOCAL)
Get parent revision ID (the original previous page revision).
Definition: RevisionRecord.php:287
MediaWiki\DAO\WikiAwareEntity
Marker interface for entities aware of the wiki they belong to.
Definition: WikiAwareEntity.php:34
MediaWiki\User\UserIdentity
Interface for objects representing user identity.
Definition: UserIdentity.php:35
Revision\RevisionRecord\$mUser
UserIdentity null $mUser
Definition: RevisionRecord.php:73
Title\castFromPageIdentity
static castFromPageIdentity(?PageIdentity $pageIdentity)
Return a Title for a given PageIdentity.
Definition: Title.php:329
MediaWiki\Revision
Definition: ContributionsLookup.php:3
MediaWiki\Page\LegacyArticleIdAccess
trait LegacyArticleIdAccess
Convenience trait for conversion to PageIdentity.
Definition: LegacyArticleIdAccess.php:26
Revision\RevisionRecord\getUser
getUser( $audience=self::FOR_PUBLIC, Authority $performer=null)
Fetch revision's author's user identity, if it's available to the specified audience.
Definition: RevisionRecord.php:379
Revision\RevisionRecord\getInheritedSlots
getInheritedSlots()
Returns slots inherited from some previous revision.
Definition: RevisionRecord.php:255
Revision\RevisionRecord\$mTimestamp
string null $mTimestamp
Definition: RevisionRecord.php:77
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\$wikiId
string false $wikiId
Wiki ID; false means the current wiki.
Definition: RevisionRecord.php:67
Revision\RevisionRecord\isMinor
isMinor()
MCR migration note: this replaces Revision::isMinor.
Definition: RevisionRecord.php:416
Revision\RevisionRecord\isReadyForInsertion
isReadyForInsertion()
Returns whether this RevisionRecord is ready for insertion, that is, whether it contains all informat...
Definition: RevisionRecord.php:558
Revision\RevisionRecord\RAW
const RAW
Definition: RevisionRecord.php:64
Revision\RevisionRecord\getSlotRoles
getSlotRoles()
Returns the slot names (roles) of all slots present in this revision.
Definition: RevisionRecord.php:207
Revision\RevisionRecord\$mParentId
int null $mParentId
Definition: RevisionRecord.php:85
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:915
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:65
MediaWiki\Permissions\Authority
@unstable
Definition: Authority.php:30
Revision\RevisionRecord\SUPPRESSED_USER
const SUPPRESSED_USER
Definition: RevisionRecord.php:57
$content
$content
Definition: router.php:76
Revision\RevisionRecord\DELETED_USER
const DELETED_USER
Definition: RevisionRecord.php:55
Revision\RevisionRecord\getId
getId( $wikiId=self::LOCAL)
Get revision ID.
Definition: RevisionRecord.php:269
MediaWiki\DAO\assertWikiIdParam
assertWikiIdParam( $wikiId)
Asserts correct $wikiId parameter was passed.
Definition: WikiAwareEntityTrait.php:88
Revision\RevisionRecord\__construct
__construct(PageIdentity $page, RevisionSlots $slots, $wikiId=self::LOCAL)
Definition: RevisionRecord.php:103
Revision\RevisionRecord\$mSize
int null $mSize
Definition: RevisionRecord.php:81
Revision\RevisionRecord\getSlots
getSlots()
Returns the slots defined for this revision.
Definition: RevisionRecord.php:222
Content
Base interface for content objects.
Definition: Content.php:35
Revision\RevisionRecord\FOR_PUBLIC
const FOR_PUBLIC
Definition: RevisionRecord.php:62
Revision\RevisionRecord\$mPageId
int $mPageId
Definition: RevisionRecord.php:71
Revision\RevisionRecord\getVisibility
getVisibility()
Get the deletion bitfield of the revision.
Definition: RevisionRecord.php:438
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:47
Revision\RevisionRecord\DELETED_COMMENT
const DELETED_COMMENT
Definition: RevisionRecord.php:54
Revision\RevisionRecord\DELETED_TEXT
const DELETED_TEXT
Definition: RevisionRecord.php:53
Revision\RevisionSlots
Value object representing the set of slots belonging to a revision.
Definition: RevisionSlots.php:41
Revision\RevisionRecord\getComment
getComment( $audience=self::FOR_PUBLIC, Authority $performer=null)
Fetch revision comment, if it's available to the specified audience.
Definition: RevisionRecord.php:403
Revision\RevisionRecord\hasSameContent
hasSameContent(RevisionRecord $rec)
Definition: RevisionRecord.php:118
Revision\RevisionRecord\userCanBitfield
static userCanBitfield( $bitfield, $field, Authority $performer, PageIdentity $page=null)
Determine if the current user is allowed to view a particular field of this revision,...
Definition: RevisionRecord.php:519
Revision\RevisionRecord\isCurrent
isCurrent()
Checks whether the revision record is a stored current revision.
Definition: RevisionRecord.php:577
Revision\RevisionRecord\SUPPRESSED_ALL
const SUPPRESSED_ALL
Definition: RevisionRecord.php:58
Revision\RevisionRecord\DELETED_RESTRICTED
const DELETED_RESTRICTED
Definition: RevisionRecord.php:56
MediaWiki\Linker\LinkTarget
Definition: LinkTarget.php:26
Revision\RevisionRecord\FOR_THIS_USER
const FOR_THIS_USER
Definition: RevisionRecord.php:63
Revision\RevisionRecord\$mSha1
string null $mSha1
Definition: RevisionRecord.php:83
Revision\RevisionRecord\getWikiId
getWikiId()
Get the ID of the wiki this revision belongs to.
Definition: RevisionRecord.php:335
Revision\RevisionRecord\$mDeleted
int $mDeleted
using the DELETED_XXX and SUPPRESSED_XXX flags
Definition: RevisionRecord.php:79
Revision\RevisionRecord\$mId
int null $mId
Definition: RevisionRecord.php:69
CommentStoreComment
Value object for a comment stored by CommentStore.
Definition: CommentStoreComment.php:30
Revision\RevisionRecord\getContent
getContent( $role, $audience=self::FOR_PUBLIC, Authority $performer=null)
Returns the Content of the given slot of this revision.
Definition: RevisionRecord.php:156
Revision\RevisionRecord\getSlot
getSlot( $role, $audience=self::FOR_PUBLIC, Authority $performer=null)
Returns meta-data for the given slot.
Definition: RevisionRecord.php:180