MutableRevisionSlots.php 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117
  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. namespace MediaWiki\Revision;
  23. use Content;
  24. /**
  25. * Mutable version of RevisionSlots, for constructing a new revision.
  26. *
  27. * @since 1.31
  28. * @since 1.32 Renamed from MediaWiki\Storage\MutableRevisionSlots
  29. */
  30. class MutableRevisionSlots extends RevisionSlots {
  31. /**
  32. * Constructs a MutableRevisionSlots that inherits from the given
  33. * list of slots.
  34. *
  35. * @param SlotRecord[] $slots
  36. *
  37. * @return MutableRevisionSlots
  38. */
  39. public static function newFromParentRevisionSlots( array $slots ) {
  40. $inherited = [];
  41. foreach ( $slots as $slot ) {
  42. $role = $slot->getRole();
  43. $inherited[$role] = SlotRecord::newInherited( $slot );
  44. }
  45. return new MutableRevisionSlots( $inherited );
  46. }
  47. /**
  48. * @param SlotRecord[] $slots An array of SlotRecords.
  49. */
  50. public function __construct( array $slots = [] ) {
  51. parent::__construct( $slots );
  52. }
  53. /**
  54. * Sets the given slot.
  55. * If a slot with the same role is already present, it is replaced.
  56. *
  57. * @param SlotRecord $slot
  58. */
  59. public function setSlot( SlotRecord $slot ) {
  60. if ( !is_array( $this->slots ) ) {
  61. $this->getSlots(); // initialize $this->slots
  62. }
  63. $role = $slot->getRole();
  64. $this->slots[$role] = $slot;
  65. }
  66. /**
  67. * Sets the given slot to an inherited version of $slot.
  68. * If a slot with the same role is already present, it is replaced.
  69. *
  70. * @param SlotRecord $slot
  71. */
  72. public function inheritSlot( SlotRecord $slot ) {
  73. $this->setSlot( SlotRecord::newInherited( $slot ) );
  74. }
  75. /**
  76. * Sets the content for the slot with the given role.
  77. * If a slot with the same role is already present, it is replaced.
  78. *
  79. * @param string $role
  80. * @param Content $content
  81. */
  82. public function setContent( $role, Content $content ) {
  83. $slot = SlotRecord::newUnsaved( $role, $content );
  84. $this->setSlot( $slot );
  85. }
  86. /**
  87. * Remove the slot for the given role, discontinue the corresponding stream.
  88. *
  89. * @param string $role
  90. */
  91. public function removeSlot( $role ) {
  92. if ( !is_array( $this->slots ) ) {
  93. $this->getSlots(); // initialize $this->slots
  94. }
  95. unset( $this->slots[$role] );
  96. }
  97. }
  98. /**
  99. * Retain the old class name for backwards compatibility.
  100. * @deprecated since 1.32
  101. */
  102. class_alias( MutableRevisionSlots::class, 'MediaWiki\Storage\MutableRevisionSlots' );