MediaWiki  master
MainSlotRoleHandler.php
Go to the documentation of this file.
1 <?php
23 namespace MediaWiki\Revision;
24 
31 use TitleFactory;
32 
45 
51 
54 
56  private $hookRunner;
57 
59  private $titleFactory;
60 
68  public function __construct(
71  HookContainer $hookContainer,
73  ) {
74  parent::__construct( 'main', CONTENT_MODEL_WIKITEXT );
75  $this->namespaceContentModels = $namespaceContentModels;
76  $this->contentHandlerFactory = $contentHandlerFactory;
77  $this->hookRunner = new HookRunner( $hookContainer );
78  $this->titleFactory = $titleFactory;
79  }
80 
81  public function supportsArticleCount() {
82  return true;
83  }
84 
92  public function isAllowedModel( $model, PageIdentity $page ) {
93  $title = $this->titleFactory->castFromPageIdentity( $page );
94  $handler = $this->contentHandlerFactory->getContentHandler( $model );
95 
96  // @phan-suppress-next-line PhanTypeMismatchArgumentNullable castFrom does not return null here
97  return $handler->canBeUsedOn( $title );
98  }
99 
105  public function getDefaultModel( $page ) {
106  // NOTE: this method must not rely on $title->getContentModel() directly or indirectly,
107  // because it is used to initialize the mContentModel member.
108 
109  $ext = '';
110  $ns = $page->getNamespace();
111  $model = $this->namespaceContentModels[$ns] ?? null;
112 
113  // Hook can determine default model
114  if ( $page instanceof PageIdentity ) {
115  $title = $this->titleFactory->castFromPageIdentity( $page );
116  } else {
117  $title = $this->titleFactory->castFromLinkTarget( $page );
118  }
119  // @phan-suppress-next-line PhanTypeMismatchArgumentNullable castFrom notnull/Type mismatch on pass-by-ref args
120  if ( !$this->hookRunner->onContentHandlerDefaultModelFor( $title, $model ) && $model !== null ) {
121  return $model;
122  }
123 
124  // Could this page contain code based on the title?
125  $isCodePage = $ns === NS_MEDIAWIKI && preg_match( '!\.(css|js|json)$!u', $title->getText(), $m );
126  if ( $isCodePage ) {
127  $ext = $m[1];
128  }
129 
130  // Is this a user subpage containing code?
131  $isCodeSubpage = $ns === NS_USER
132  && !$isCodePage
133  && preg_match( "/\\/.*\\.(js|css|json)$/", $title->getText(), $m );
134 
135  if ( $isCodeSubpage ) {
136  $ext = $m[1];
137  }
138 
139  // Is this wikitext, according to $wgNamespaceContentModels or the DefaultModelFor hook?
140  $isWikitext = $model === null || $model == CONTENT_MODEL_WIKITEXT;
141  $isWikitext = $isWikitext && !$isCodePage && !$isCodeSubpage;
142 
143  if ( !$isWikitext ) {
144  switch ( $ext ) {
145  case 'js':
147  case 'css':
148  return CONTENT_MODEL_CSS;
149  case 'json':
150  return CONTENT_MODEL_JSON;
151  default:
152  return $model ?? CONTENT_MODEL_TEXT;
153  }
154  }
155 
156  // We established that it must be wikitext
157 
158  return CONTENT_MODEL_WIKITEXT;
159  }
160 
161 }
const NS_USER
Definition: Defines.php:66
const CONTENT_MODEL_CSS
Definition: Defines.php:212
const NS_MEDIAWIKI
Definition: Defines.php:72
const CONTENT_MODEL_WIKITEXT
Definition: Defines.php:210
const CONTENT_MODEL_JSON
Definition: Defines.php:214
const CONTENT_MODEL_TEXT
Definition: Defines.php:213
const CONTENT_MODEL_JAVASCRIPT
Definition: Defines.php:211
Exception thrown when an unregistered content model is requested.
This class provides an implementation of the core hook interfaces, forwarding hook calls to HookConta...
Definition: HookRunner.php:562
A SlotRoleHandler for the main slot.
string[] $namespaceContentModels
A mapping of namespaces to content models.
supportsArticleCount()
Whether this slot should be considered when determining whether a page should be counted as an "artic...
__construct(array $namespaceContentModels, IContentHandlerFactory $contentHandlerFactory, HookContainer $hookContainer, TitleFactory $titleFactory)
isAllowedModel( $model, PageIdentity $page)
SlotRoleHandler instances are used to declare the existence and behavior of slot roles.
Creates Title objects.
Interface for objects (potentially) representing an editable wiki page.
if(!is_readable( $file)) $ext
Definition: router.php:48