MediaWiki REL1_39
SlotRoleRegistry.php
Go to the documentation of this file.
1<?php
23namespace MediaWiki\Revision;
24
25use InvalidArgumentException;
26use LogicException;
29use Wikimedia\Assert\Assert;
30
49
53 private $roleNamesStore;
54
58 private $instantiators = [];
59
63 private $handlers;
64
68 public function __construct( NameTableStore $roleNamesStore ) {
69 $this->roleNamesStore = $roleNamesStore;
70 }
71
84 public function defineRole( $role, callable $instantiator ) {
85 $role = strtolower( $role );
86
87 if ( $this->isDefinedRole( $role ) ) {
88 throw new LogicException( "Role $role is already defined" );
89 }
90
91 $this->instantiators[$role] = $instantiator;
92 }
93
110 public function defineRoleWithModel( $role, $model, $layout = [], bool $derived = false ) {
111 $role = strtolower( $role );
112
113 $this->defineRole(
114 $role,
115 static function ( $role ) use ( $model, $layout, $derived ) {
116 return new SlotRoleHandler( $role, $model, $layout, $derived );
117 }
118 );
119 }
120
130 public function getRoleHandler( $role ) {
131 $role = strtolower( $role );
132
133 if ( !isset( $this->handlers[$role] ) ) {
134 if ( !$this->isDefinedRole( $role ) ) {
135 if ( $this->isKnownRole( $role ) ) {
136 // The role has no handler defined, but is represented in the database.
137 // This may happen e.g. when the extension that defined the role was uninstalled.
138 wfWarn( __METHOD__ . ": known but undefined slot role $role" );
139 $this->handlers[$role] = new FallbackSlotRoleHandler( $role );
140 } else {
141 // The role doesn't have a handler defined, and is not represented in
142 // the database. Something must be quite wrong.
143 throw new InvalidArgumentException( "Unknown role $role" );
144 }
145 } else {
146 $handler = call_user_func( $this->instantiators[$role], $role );
147
148 Assert::postcondition(
149 $handler instanceof SlotRoleHandler,
150 "Instantiator for $role role must return a SlotRoleHandler"
151 );
152
153 $this->handlers[$role] = $handler;
154 }
155 }
156
157 return $this->handlers[$role];
158 }
159
171 public function getAllowedRoles( PageIdentity $page ) {
172 // TODO: allow this to be overwritten per namespace (or page type)
173 // TODO: decide how to control which slots are offered for editing per default (T209927)
174 return $this->getDefinedRoles();
175 }
176
189 public function getRequiredRoles( PageIdentity $page ) {
190 // TODO: allow this to be overwritten per namespace (or page type)
191 return [ 'main' ];
192 }
193
201 public function getDefinedRoles() {
202 return array_keys( $this->instantiators );
203 }
204
214 public function getKnownRoles() {
215 return array_unique( array_merge(
216 $this->getDefinedRoles(),
217 $this->roleNamesStore->getMap()
218 ) );
219 }
220
227 public function isDefinedRole( $role ) {
228 $role = strtolower( $role );
229 return in_array( $role, $this->getDefinedRoles(), true );
230 }
231
239 public function isKnownRole( $role ) {
240 $role = strtolower( $role );
241 return in_array( $role, $this->getKnownRoles(), true );
242 }
243
244}
wfWarn( $msg, $callerOffset=1, $level=E_USER_NOTICE)
Send a warning either to the debug log or in a PHP error depending on $wgDevelopmentWarnings.
A SlotRoleHandler for providing basic functionality for undefined slot roles.
SlotRoleHandler instances are used to declare the existence and behavior of slot roles.
A registry service for SlotRoleHandlers, used to define which slot roles are available on which page.
getRequiredRoles(PageIdentity $page)
Returns the list of roles required when creating a new revision on the given page.
isDefinedRole( $role)
Whether the given role is defined, that is, it was defined by calling defineRole().
getDefinedRoles()
Returns the list of roles defined by calling defineRole().
defineRoleWithModel( $role, $model, $layout=[], bool $derived=false)
Defines a slot role that allows only the given content model, and has no special behavior.
__construct(NameTableStore $roleNamesStore)
isKnownRole( $role)
Whether the given role is known, that is, it's either defined or exist according to the NameTableStor...
getKnownRoles()
Returns the list of known roles, including the ones returned by getDefinedRoles(),...
getAllowedRoles(PageIdentity $page)
Returns the list of roles allowed when creating a new revision on the given page.
defineRole( $role, callable $instantiator)
Defines a slot role.
getRoleHandler( $role)
Gets the SlotRoleHandler that should be used when processing content of the given role.
Interface for objects (potentially) representing an editable wiki page.