MediaWiki  master
MutableRevisionSlots.php
Go to the documentation of this file.
1 <?php
23 namespace MediaWiki\Revision;
24 
25 use Content;
26 
37  private $resetCallback;
38 
49  public static function newFromParentRevisionSlots(
50  array $slots,
51  ?callable $resetCallback = null
52  ) {
53  $inherited = [];
54  foreach ( $slots as $slot ) {
55  $role = $slot->getRole();
56  $inherited[$role] = SlotRecord::newInherited( $slot );
57  }
58 
59  return new MutableRevisionSlots( $inherited, $resetCallback );
60  }
61 
67  public function __construct( array $slots = [], ?callable $resetCallback = null ) {
68  // @phan-suppress-next-line PhanTypeInvalidCallableArraySize
69  parent::__construct( $slots );
70  $this->resetCallback = $resetCallback;
71  }
72 
79  public function setSlot( SlotRecord $slot ) {
80  if ( !is_array( $this->slots ) ) {
81  $this->getSlots(); // initialize $this->slots
82  }
83 
84  $role = $slot->getRole();
85  $this->slots[$role] = $slot;
86  $this->triggerResetCallback();
87  }
88 
95  public function inheritSlot( SlotRecord $slot ) {
96  $this->setSlot( SlotRecord::newInherited( $slot ) );
97  }
98 
106  public function setContent( $role, Content $content ) {
107  $slot = SlotRecord::newUnsaved( $role, $content );
108  $this->setSlot( $slot );
109  }
110 
116  public function removeSlot( $role ) {
117  if ( !is_array( $this->slots ) ) {
118  $this->getSlots(); // initialize $this->slots
119  }
120 
121  unset( $this->slots[$role] );
122  $this->triggerResetCallback();
123  }
124 
128  private function triggerResetCallback() {
129  if ( $this->resetCallback ) {
130  ( $this->resetCallback )( $this );
131  }
132  }
133 
134 }
135 
140 class_alias( MutableRevisionSlots::class, 'MediaWiki\Storage\MutableRevisionSlots' );
Revision\RevisionSlots\$slots
SlotRecord[] callable $slots
Definition: RevisionSlots.php:45
Revision\MutableRevisionSlots\triggerResetCallback
triggerResetCallback()
Trigger the reset callback supplied to the constructor, if any.
Definition: MutableRevisionSlots.php:128
Revision\MutableRevisionSlots\newFromParentRevisionSlots
static newFromParentRevisionSlots(array $slots, ?callable $resetCallback=null)
Constructs a MutableRevisionSlots that inherits from the given list of slots.
Definition: MutableRevisionSlots.php:49
Revision\SlotRecord\newInherited
static newInherited(SlotRecord $slot)
Constructs a new SlotRecord for a new revision, inheriting the content of the given SlotRecord of a p...
Definition: SlotRecord.php:105
Revision\MutableRevisionSlots\setContent
setContent( $role, Content $content)
Sets the content for the slot with the given role.
Definition: MutableRevisionSlots.php:106
Revision\MutableRevisionSlots
Mutable version of RevisionSlots, for constructing a new revision.
Definition: MutableRevisionSlots.php:33
Revision\MutableRevisionSlots\$resetCallback
callable null $resetCallback
Definition: MutableRevisionSlots.php:37
MediaWiki\Revision
Definition: ContributionsLookup.php:3
Revision\MutableRevisionSlots\inheritSlot
inheritSlot(SlotRecord $slot)
Sets the given slot to an inherited version of $slot.
Definition: MutableRevisionSlots.php:95
Revision\MutableRevisionSlots\setSlot
setSlot(SlotRecord $slot)
Sets the given slot.
Definition: MutableRevisionSlots.php:79
Revision\SlotRecord\getRole
getRole()
Returns the role of the slot.
Definition: SlotRecord.php:482
Revision\SlotRecord\newUnsaved
static newUnsaved( $role, Content $content)
Constructs a new Slot from a Content object for a new revision.
Definition: SlotRecord.php:131
$content
$content
Definition: router.php:76
Revision\MutableRevisionSlots\__construct
__construct(array $slots=[], ?callable $resetCallback=null)
Definition: MutableRevisionSlots.php:67
Revision\RevisionSlots\getSlots
getSlots()
Returns an associative array that maps role names to SlotRecords.
Definition: RevisionSlots.php:165
Content
Base interface for content objects.
Definition: Content.php:35
Revision\RevisionSlots
Value object representing the set of slots belonging to a revision.
Definition: RevisionSlots.php:41
Revision\MutableRevisionSlots\removeSlot
removeSlot( $role)
Remove the slot for the given role, discontinue the corresponding stream.
Definition: MutableRevisionSlots.php:116
Revision\SlotRecord
Value object representing a content slot associated with a page revision.
Definition: SlotRecord.php:40