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  parent::__construct( $slots );
69  $this->resetCallback = $resetCallback;
70  }
71 
78  public function setSlot( SlotRecord $slot ) {
79  if ( !is_array( $this->slots ) ) {
80  $this->getSlots(); // initialize $this->slots
81  }
82 
83  $role = $slot->getRole();
84  $this->slots[$role] = $slot;
85  $this->triggerResetCallback();
86  }
87 
94  public function inheritSlot( SlotRecord $slot ) {
95  $this->setSlot( SlotRecord::newInherited( $slot ) );
96  }
97 
105  public function setContent( $role, Content $content ) {
106  $slot = SlotRecord::newUnsaved( $role, $content );
107  $this->setSlot( $slot );
108  }
109 
115  public function removeSlot( $role ) {
116  if ( !is_array( $this->slots ) ) {
117  $this->getSlots(); // initialize $this->slots
118  }
119 
120  unset( $this->slots[$role] );
121  $this->triggerResetCallback();
122  }
123 
127  private function triggerResetCallback() {
128  if ( $this->resetCallback ) {
129  ( $this->resetCallback )( $this );
130  }
131  }
132 
133 }
134 
139 class_alias( MutableRevisionSlots::class, 'MediaWiki\Storage\MutableRevisionSlots' );
Revision\RevisionSlots\$slots
SlotRecord[] callable $slots
Definition: RevisionSlots.php:42
Revision\MutableRevisionSlots\triggerResetCallback
triggerResetCallback()
Trigger the reset callback supplied to the constructor, if any.
Definition: MutableRevisionSlots.php:127
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:103
Revision\MutableRevisionSlots\setContent
setContent( $role, Content $content)
Sets the content for the slot with the given role.
Definition: MutableRevisionSlots.php:105
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: FallbackSlotRoleHandler.php:23
Revision\MutableRevisionSlots\inheritSlot
inheritSlot(SlotRecord $slot)
Sets the given slot to an inherited version of $slot.
Definition: MutableRevisionSlots.php:94
Revision\MutableRevisionSlots\setSlot
setSlot(SlotRecord $slot)
Sets the given slot.
Definition: MutableRevisionSlots.php:78
Revision\SlotRecord\getRole
getRole()
Returns the role of the slot.
Definition: SlotRecord.php:489
Revision\SlotRecord\newUnsaved
static newUnsaved( $role, Content $content)
Constructs a new Slot from a Content object for a new revision.
Definition: SlotRecord.php:129
$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:169
Content
Base interface for content objects.
Definition: Content.php:34
Revision\RevisionSlots
Value object representing the set of slots belonging to a revision.
Definition: RevisionSlots.php:39
Revision\MutableRevisionSlots\removeSlot
removeSlot( $role)
Remove the slot for the given role, discontinue the corresponding stream.
Definition: MutableRevisionSlots.php:115
Revision\SlotRecord
Value object representing a content slot associated with a page revision.
Definition: SlotRecord.php:39