| 1 | <?php |
| 2 | /** |
| 3 | * Mutable version of RevisionSlots, for constructing a new revision. |
| 4 | * |
| 5 | * This program is free software; you can redistribute it and/or modify |
| 6 | * it under the terms of the GNU General Public License as published by |
| 7 | * the Free Software Foundation; either version 2 of the License, or |
| 8 | * (at your option) any later version. |
| 9 | * |
| 10 | * This program is distributed in the hope that it will be useful, |
| 11 | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 12 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 13 | * GNU General Public License for more details. |
| 14 | * |
| 15 | * You should have received a copy of the GNU General Public License along |
| 16 | * with this program; if not, write to the Free Software Foundation, Inc., |
| 17 | * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. |
| 18 | * http://www.gnu.org/copyleft/gpl.html |
| 19 | * |
| 20 | * @file |
| 21 | */ |
| 22 | |
| 23 | namespace MediaWiki\Revision; |
| 24 | |
| 25 | use MediaWiki\Content\Content; |
| 26 | |
| 27 | /** |
| 28 | * Mutable version of RevisionSlots, for constructing a new revision. |
| 29 | * |
| 30 | * @since 1.31 |
| 31 | * @since 1.32 Renamed from MediaWiki\Storage\MutableRevisionSlots |
| 32 | */ |
| 33 | class MutableRevisionSlots extends RevisionSlots { |
| 34 | /** |
| 35 | * @var callable|null |
| 36 | */ |
| 37 | private $resetCallback; |
| 38 | |
| 39 | /** |
| 40 | * Constructs a MutableRevisionSlots that inherits from the given |
| 41 | * list of slots. |
| 42 | * |
| 43 | * @param SlotRecord[] $slots |
| 44 | * @param callable|null $resetCallback Callback to be triggered whenever slots change. |
| 45 | * Signature: function ( MutableRevisionSlots ): void. |
| 46 | * |
| 47 | * @return MutableRevisionSlots |
| 48 | */ |
| 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 | |
| 62 | /** |
| 63 | * @param SlotRecord[] $slots An array of SlotRecords. |
| 64 | * @param callable|null $resetCallback Callback to be triggered whenever slots change. |
| 65 | * Signature: function ( MutableRevisionSlots ): void. |
| 66 | */ |
| 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 | |
| 73 | /** |
| 74 | * Sets the given slot. |
| 75 | * If a slot with the same role is already present, it is replaced. |
| 76 | * |
| 77 | * @param SlotRecord $slot |
| 78 | */ |
| 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 | |
| 89 | /** |
| 90 | * Sets the given slot to an inherited version of $slot. |
| 91 | * If a slot with the same role is already present, it is replaced. |
| 92 | * |
| 93 | * @param SlotRecord $slot |
| 94 | */ |
| 95 | public function inheritSlot( SlotRecord $slot ) { |
| 96 | $this->setSlot( SlotRecord::newInherited( $slot ) ); |
| 97 | } |
| 98 | |
| 99 | /** |
| 100 | * Sets the content for the slot with the given role. |
| 101 | * If a slot with the same role is already present, it is replaced. |
| 102 | * |
| 103 | * @param string $role |
| 104 | * @param Content $content |
| 105 | */ |
| 106 | public function setContent( $role, Content $content ) { |
| 107 | $slot = SlotRecord::newUnsaved( $role, $content ); |
| 108 | $this->setSlot( $slot ); |
| 109 | } |
| 110 | |
| 111 | /** |
| 112 | * Remove the slot for the given role, discontinue the corresponding stream. |
| 113 | * |
| 114 | * @param string $role |
| 115 | */ |
| 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 | |
| 125 | /** |
| 126 | * Trigger the reset callback supplied to the constructor, if any. |
| 127 | */ |
| 128 | private function triggerResetCallback() { |
| 129 | if ( $this->resetCallback ) { |
| 130 | ( $this->resetCallback )( $this ); |
| 131 | } |
| 132 | } |
| 133 | |
| 134 | } |