Merge "Rewrite pref cleanup script"
[lhc/web/wiklou.git] / includes / Storage / MutableRevisionSlots.php
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\Storage;
24
25 use Content;
26
27 /**
28 * Mutable version of RevisionSlots, for constructing a new revision.
29 *
30 * @since 1.31
31 */
32 class MutableRevisionSlots extends RevisionSlots {
33
34 /**
35 * Constructs a MutableRevisionSlots that inherits from the given
36 * list of slots.
37 *
38 * @param SlotRecord[] $slots
39 *
40 * @return MutableRevisionSlots
41 */
42 public static function newFromParentRevisionSlots( array $slots ) {
43 $inherited = [];
44 foreach ( $slots as $slot ) {
45 $role = $slot->getRole();
46 $inherited[$role] = SlotRecord::newInherited( $slot );
47 }
48
49 return new MutableRevisionSlots( $inherited );
50 }
51
52 /**
53 * @param SlotRecord[] $slots An array of SlotRecords.
54 */
55 public function __construct( array $slots = [] ) {
56 parent::__construct( $slots );
57 }
58
59 /**
60 * Sets the given slot.
61 * If a slot with the same role is already present, it is replaced.
62 *
63 * @note This may cause the slot meta-data for the revision to be lazy-loaded.
64 *
65 * @param SlotRecord $slot
66 */
67 public function setSlot( SlotRecord $slot ) {
68 if ( !is_array( $this->slots ) ) {
69 $this->getSlots(); // initialize $this->slots
70 }
71
72 $role = $slot->getRole();
73 $this->slots[$role] = $slot;
74 }
75
76 /**
77 * Sets the content for the slot with the given role.
78 * If a slot with the same role is already present, it is replaced.
79 *
80 * @note This may cause the slot meta-data for the revision to be lazy-loaded.
81 *
82 * @param string $role
83 * @param Content $content
84 */
85 public function setContent( $role, Content $content ) {
86 $slot = SlotRecord::newUnsaved( $role, $content );
87 $this->setSlot( $slot );
88 }
89
90 /**
91 * Remove the slot for the given role, discontinue the corresponding stream.
92 *
93 * @note This may cause the slot meta-data for the revision to be lazy-loaded.
94 *
95 * @param string $role
96 */
97 public function removeSlot( $role ) {
98 if ( !is_array( $this->slots ) ) {
99 $this->getSlots(); // initialize $this->slots
100 }
101
102 unset( $this->slots[$role] );
103 }
104
105 /**
106 * Return all slots that are not inherited.
107 *
108 * @note This may cause the slot meta-data for the revision to be lazy-loaded.
109 *
110 * @return SlotRecord[]
111 */
112 public function getTouchedSlots() {
113 return array_filter(
114 $this->getSlots(),
115 function ( SlotRecord $slot ) {
116 return !$slot->isInherited();
117 }
118 );
119 }
120
121 /**
122 * Return all slots that are inherited.
123 *
124 * @note This may cause the slot meta-data for the revision to be lazy-loaded.
125 *
126 * @return SlotRecord[]
127 */
128 public function getInheritedSlots() {
129 return array_filter(
130 $this->getSlots(),
131 function ( SlotRecord $slot ) {
132 return $slot->isInherited();
133 }
134 );
135 }
136
137 }