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  if ( $page->getWikiId() !== $wikiId ) {
112  $expected = $wikiId ?: 'local';
113  $actual = $page->getWikiId() ?: 'local';
115  "Invalid cross-wiki page {$page}. Expected: {$expected}, got {$actual}",
116  '1.36'
117  );
118  }
119 
120  if ( !$page->canExist() ) {
122  "Constructing RevisionRecord for a page that can't exist: {$page}",
123  '1.36'
124  );
125  }
126  }
127 
134  public function hasSameContent( RevisionRecord $rec ) {
135  if ( $rec === $this ) {
136  return true;
137  }
138 
139  if ( $this->getId() !== null && $this->getId() === $rec->getId() ) {
140  return true;
141  }
142 
143  // check size before hash, since size is quicker to compute
144  if ( $this->getSize() !== $rec->getSize() ) {
145  return false;
146  }
147 
148  // instead of checking the hash, we could also check the content addresses of all slots.
149 
150  if ( $this->getSha1() === $rec->getSha1() ) {
151  return true;
152  }
153 
154  return false;
155  }
156 
172  public function getContent( $role, $audience = self::FOR_PUBLIC, Authority $performer = null ) {
173  // XXX: throwing an exception would be nicer, but would a further
174  // departure from the signature of Revision::getContent(), and thus
175  // more complex and error prone refactoring.
176  if ( !$this->audienceCan( self::DELETED_TEXT, $audience, $performer ) ) {
177  return null;
178  }
179 
180  $content = $this->getSlot( $role, $audience, $performer )->getContent();
181  return $content->copy();
182  }
183 
196  public function getSlot( $role, $audience = self::FOR_PUBLIC, Authority $performer = null ) {
197  $slot = $this->mSlots->getSlot( $role );
198 
199  if ( !$this->audienceCan( self::DELETED_TEXT, $audience, $performer ) ) {
200  return SlotRecord::newWithSuppressedContent( $slot );
201  }
202 
203  return $slot;
204  }
205 
213  public function hasSlot( $role ) {
214  return $this->mSlots->hasSlot( $role );
215  }
216 
223  public function getSlotRoles() {
224  return $this->mSlots->getSlotRoles();
225  }
226 
238  public function getSlots() {
239  return $this->mSlots;
240  }
241 
256  public function getOriginalSlots() {
257  return new RevisionSlots( $this->mSlots->getOriginalSlots() );
258  }
259 
271  public function getInheritedSlots() {
272  return new RevisionSlots( $this->mSlots->getInheritedSlots() );
273  }
274 
281  public function getPrimarySlots() : RevisionSlots {
282  return new RevisionSlots( $this->mSlots->getPrimarySlots() );
283  }
284 
295  public function getId( $wikiId = self::LOCAL ) {
296  $this->deprecateInvalidCrossWiki( $wikiId, '1.36' );
297  return $this->mId;
298  }
299 
313  public function getParentId( $wikiId = self::LOCAL ) {
314  $this->deprecateInvalidCrossWiki( $wikiId, '1.36' );
315  return $this->mParentId;
316  }
317 
328  abstract public function getSize();
329 
341  abstract public function getSha1();
342 
351  public function getPageId( $wikiId = self::LOCAL ) {
352  $this->deprecateInvalidCrossWiki( $wikiId, '1.36' );
353  return $this->mPageId;
354  }
355 
361  public function getWikiId() {
362  return $this->wikiId;
363  }
364 
371  public function getPageAsLinkTarget() {
372  // TODO: Should be TitleValue::newFromPage( $this->mPage ),
373  // but Title is used too much still, so let's keep propagating it
374  return Title::castFromPageIdentity( $this->mPage );
375  }
376 
386  public function getPage() {
387  return $this->mPage;
388  }
389 
405  public function getUser( $audience = self::FOR_PUBLIC, Authority $performer = null ) {
406  if ( !$this->audienceCan( self::DELETED_USER, $audience, $performer ) ) {
407  return null;
408  } else {
409  return $this->mUser;
410  }
411  }
412 
429  public function getComment( $audience = self::FOR_PUBLIC, Authority $performer = null ) {
430  if ( !$this->audienceCan( self::DELETED_COMMENT, $audience, $performer ) ) {
431  return null;
432  } else {
433  return $this->mComment;
434  }
435  }
436 
442  public function isMinor() {
443  return (bool)$this->mMinorEdit;
444  }
445 
453  public function isDeleted( $field ) {
454  return ( $this->getVisibility() & $field ) == $field;
455  }
456 
464  public function getVisibility() {
465  return (int)$this->mDeleted;
466  }
467 
475  public function getTimestamp() {
476  return $this->mTimestamp;
477  }
478 
495  public function audienceCan( $field, $audience, Authority $performer = null ) {
496  if ( $audience == self::FOR_PUBLIC && $this->isDeleted( $field ) ) {
497  return false;
498  } elseif ( $audience == self::FOR_THIS_USER ) {
499  if ( !$performer ) {
500  throw new InvalidArgumentException(
501  'An Authority object must be given when checking FOR_THIS_USER audience.'
502  );
503  }
504 
505  if ( !$this->userCan( $field, $performer ) ) {
506  return false;
507  }
508  }
509 
510  return true;
511  }
512 
525  public function userCan( $field, Authority $performer ) {
526  return self::userCanBitfield( $this->getVisibility(), $field, $performer, $this->mPage );
527  }
528 
545  public static function userCanBitfield( $bitfield, $field, Authority $performer, PageIdentity $page = null ) {
546  if ( $bitfield & $field ) { // aspect is deleted
547  if ( $bitfield & self::DELETED_RESTRICTED ) {
548  $permissions = [ 'suppressrevision', 'viewsuppressed' ];
549  } elseif ( $field & self::DELETED_TEXT ) {
550  $permissions = [ 'deletedtext' ];
551  } else {
552  $permissions = [ 'deletedhistory' ];
553  }
554 
555  $permissionlist = implode( ', ', $permissions );
556  if ( $page === null ) {
557  wfDebug( "Checking for $permissionlist due to $field match on $bitfield" );
558  return $performer->isAllowedAny( ...$permissions );
559  } else {
560  wfDebug( "Checking for $permissionlist on $page due to $field match on $bitfield" );
561  foreach ( $permissions as $perm ) {
562  if ( $performer->authorizeRead( $perm, $page ) ) {
563  return true;
564  }
565  }
566  return false;
567  }
568  } else {
569  return true;
570  }
571  }
572 
584  public function isReadyForInsertion() {
585  // NOTE: don't check getSize() and getSha1(), since that may cause the full content to
586  // be loaded in order to calculate the values. Just assume these methods will not return
587  // null if mSlots is not empty.
588 
589  // NOTE: getId() and getPageId() may return null before a revision is saved, so don't
590  // check them.
591 
592  return $this->getTimestamp() !== null
593  && $this->getComment( self::RAW ) !== null
594  && $this->getUser( self::RAW ) !== null
595  && $this->mSlots->getSlotRoles() !== [];
596  }
597 
603  public function isCurrent() {
604  return false;
605  }
606 }
607 
612 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:351
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:525
Revision\RevisionRecord\getPrimarySlots
getPrimarySlots()
Returns primary slots (those that are not derived).
Definition: RevisionRecord.php:281
Revision\RevisionRecord\getOriginalSlots
getOriginalSlots()
Returns the slots that originate in this revision.
Definition: RevisionRecord.php:256
Revision\RevisionRecord\isDeleted
isDeleted( $field)
MCR migration note: this replaces Revision::isDeleted.
Definition: RevisionRecord.php:453
Revision\RevisionRecord\getPage
getPage()
Returns the page this revision belongs to.
Definition: RevisionRecord.php:386
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:495
Revision\RevisionRecord\getTimestamp
getTimestamp()
MCR migration note: this replaces Revision::getTimestamp.
Definition: RevisionRecord.php:475
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:213
Revision\RevisionRecord\getParentId
getParentId( $wikiId=self::LOCAL)
Get parent revision ID (the original previous page revision).
Definition: RevisionRecord.php:313
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:39
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:328
MediaWiki\Revision
Definition: ContributionsLookup.php:3
MediaWiki\Page\LegacyArticleIdAccess
trait LegacyArticleIdAccess
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:405
Revision\RevisionRecord\getInheritedSlots
getInheritedSlots()
Returns slots inherited from some previous revision.
Definition: RevisionRecord.php:271
Revision\RevisionRecord\$mTimestamp
string null $mTimestamp
Definition: RevisionRecord.php:77
wfDeprecatedMsg
wfDeprecatedMsg( $msg, $version=false, $component=false, $callerOffset=2)
Log a deprecation warning with arbitrary message text.
Definition: GlobalFunctions.php:1066
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:442
Revision\RevisionRecord\isReadyForInsertion
isReadyForInsertion()
Returns whether this RevisionRecord is ready for insertion, that is, whether it contains all informat...
Definition: RevisionRecord.php:584
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:223
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:70
MediaWiki\Permissions\Authority
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:295
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:238
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:464
Revision\RevisionRecord\getPageAsLinkTarget
getPageAsLinkTarget()
Returns the title of the page this revision is associated with as a LinkTarget object.
Definition: RevisionRecord.php:371
Title
Represents a title within MediaWiki.
Definition: Title.php:46
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:429
Revision\RevisionRecord\hasSameContent
hasSameContent(RevisionRecord $rec)
Definition: RevisionRecord.php:134
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:545
Revision\RevisionRecord\isCurrent
isCurrent()
Checks whether the revision record is a stored current revision.
Definition: RevisionRecord.php:603
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:361
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:172
Revision\RevisionRecord\getSlot
getSlot( $role, $audience=self::FOR_PUBLIC, Authority $performer=null)
Returns meta-data for the given slot.
Definition: RevisionRecord.php:196