MediaWiki  master
SlotRoleRegistry.php
Go to the documentation of this file.
1 <?php
23 namespace MediaWiki\Revision;
24 
25 use InvalidArgumentException;
26 use LogicException;
29 use Wikimedia\Assert\Assert;
30 
49 
53  private $roleNamesStore;
54 
58  private $instantiators = [];
59 
63  private $handlers;
64 
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 }
Page\PageIdentity
Interface for objects (potentially) representing an editable wiki page.
Definition: PageIdentity.php:64
MediaWiki\Revision\SlotRoleRegistry\getKnownRoles
getKnownRoles()
Returns the list of known roles, including the ones returned by getDefinedRoles(),...
Definition: SlotRoleRegistry.php:214
MediaWiki\Revision\SlotRoleRegistry\isDefinedRole
isDefinedRole( $role)
Whether the given role is defined, that is, it was defined by calling defineRole().
Definition: SlotRoleRegistry.php:227
MediaWiki\Revision\SlotRoleRegistry\__construct
__construct(NameTableStore $roleNamesStore)
Definition: SlotRoleRegistry.php:68
MediaWiki\Revision\SlotRoleRegistry\getAllowedRoles
getAllowedRoles(PageIdentity $page)
Returns the list of roles allowed when creating a new revision on the given page.
Definition: SlotRoleRegistry.php:171
MediaWiki\Revision\SlotRoleRegistry\getDefinedRoles
getDefinedRoles()
Returns the list of roles defined by calling defineRole().
Definition: SlotRoleRegistry.php:201
MediaWiki\Revision\SlotRoleRegistry\$roleNamesStore
NameTableStore $roleNamesStore
Definition: SlotRoleRegistry.php:53
MediaWiki\Revision\SlotRoleHandler
SlotRoleHandler instances are used to declare the existence and behavior of slot roles.
Definition: SlotRoleHandler.php:37
MediaWiki\Revision
Definition: ArchivedRevisionLookup.php:21
MediaWiki\Revision\SlotRoleRegistry\defineRoleWithModel
defineRoleWithModel( $role, $model, $layout=[], bool $derived=false)
Defines a slot role that allows only the given content model, and has no special behavior.
Definition: SlotRoleRegistry.php:110
MediaWiki\Revision\SlotRoleRegistry\isKnownRole
isKnownRole( $role)
Whether the given role is known, that is, it's either defined or exist according to the NameTableStor...
Definition: SlotRoleRegistry.php:239
MediaWiki\Revision\SlotRoleRegistry\getRequiredRoles
getRequiredRoles(PageIdentity $page)
Returns the list of roles required when creating a new revision on the given page.
Definition: SlotRoleRegistry.php:189
MediaWiki\Revision\SlotRoleRegistry\$handlers
SlotRoleHandler[] $handlers
Definition: SlotRoleRegistry.php:63
MediaWiki\Storage\NameTableStore
Definition: NameTableStore.php:36
MediaWiki\Revision\FallbackSlotRoleHandler
A SlotRoleHandler for providing basic functionality for undefined slot roles.
Definition: FallbackSlotRoleHandler.php:37
MediaWiki\Revision\SlotRoleRegistry\getRoleHandler
getRoleHandler( $role)
Gets the SlotRoleHandler that should be used when processing content of the given role.
Definition: SlotRoleRegistry.php:130
MediaWiki\Revision\SlotRoleRegistry
A registry service for SlotRoleHandlers, used to define which slot roles are available on which page.
Definition: SlotRoleRegistry.php:48
wfWarn
wfWarn( $msg, $callerOffset=1, $level=E_USER_NOTICE)
Send a warning either to the debug log or in a PHP error depending on $wgDevelopmentWarnings.
Definition: GlobalFunctions.php:1043
MediaWiki\Revision\SlotRoleRegistry\$instantiators
callable[] $instantiators
Definition: SlotRoleRegistry.php:58
MediaWiki\Revision\SlotRoleRegistry\defineRole
defineRole( $role, callable $instantiator)
Defines a slot role.
Definition: SlotRoleRegistry.php:84