MediaWiki master
MutableRevisionSlots.php
Go to the documentation of this file.
1<?php
23namespace MediaWiki\Revision;
24
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
77 public function setSlot( SlotRecord $slot ) {
78 if ( !is_array( $this->slots ) ) {
79 $this->getSlots(); // initialize $this->slots
80 }
81
82 $role = $slot->getRole();
83 $this->slots[$role] = $slot;
84 $this->triggerResetCallback();
85 }
86
91 public function inheritSlot( SlotRecord $slot ) {
92 $this->setSlot( SlotRecord::newInherited( $slot ) );
93 }
94
102 public function setContent( $role, Content $content ) {
103 $slot = SlotRecord::newUnsaved( $role, $content );
104 $this->setSlot( $slot );
105 }
106
112 public function removeSlot( $role ) {
113 if ( !is_array( $this->slots ) ) {
114 $this->getSlots(); // initialize $this->slots
115 }
116
117 unset( $this->slots[$role] );
118 $this->triggerResetCallback();
119 }
120
124 private function triggerResetCallback() {
125 if ( $this->resetCallback ) {
126 ( $this->resetCallback )( $this );
127 }
128 }
129
130}
Mutable version of RevisionSlots, for constructing a new revision.
setSlot(SlotRecord $slot)
Sets the given slot.
inheritSlot(SlotRecord $slot)
Sets the given slot to an inherited version of $slot.
static newFromParentRevisionSlots(array $slots, ?callable $resetCallback=null)
Constructs a MutableRevisionSlots that inherits from the given list of slots.
__construct(array $slots=[], ?callable $resetCallback=null)
setContent( $role, Content $content)
Sets the content for the slot with the given role.
removeSlot( $role)
Remove the slot for the given role, discontinue the corresponding stream.
Value object representing the set of slots belonging to a revision.
getSlots()
Returns an associative array that maps role names to SlotRecords.
Value object representing a content slot associated with a page revision.
getRole()
Returns the role of the slot.
static newInherited(SlotRecord $slot)
Constructs a new SlotRecord for a new revision, inheriting the content of the given SlotRecord of a p...
static newUnsaved(string $role, Content $content, bool $derived=false)
Constructs a new Slot from a Content object for a new revision.
Content objects represent page content, e.g.
Definition Content.php:42