MediaWiki master
MainSlotRoleHandler.php
Go to the documentation of this file.
1<?php
9namespace MediaWiki\Revision;
10
18
31
36 private $namespaceContentModels;
37
39 private $contentHandlerFactory;
40
42 private $hookRunner;
43
45 private $titleFactory;
46
54 public function __construct(
55 array $namespaceContentModels,
56 IContentHandlerFactory $contentHandlerFactory,
57 HookContainer $hookContainer,
58 TitleFactory $titleFactory
59 ) {
60 parent::__construct( SlotRecord::MAIN, CONTENT_MODEL_WIKITEXT );
61 $this->namespaceContentModels = $namespaceContentModels;
62 $this->contentHandlerFactory = $contentHandlerFactory;
63 $this->hookRunner = new HookRunner( $hookContainer );
64 $this->titleFactory = $titleFactory;
65 }
66
68 public function supportsArticleCount() {
69 return true;
70 }
71
79 public function isAllowedModel( $model, PageIdentity $page ) {
80 $title = $this->titleFactory->newFromPageIdentity( $page );
81 $handler = $this->contentHandlerFactory->getContentHandler( $model );
82
83 return $handler->canBeUsedOn( $title );
84 }
85
91 public function getDefaultModel( $page ) {
92 // NOTE: this method must not rely on $title->getContentModel() directly or indirectly,
93 // because it is used to initialize the mContentModel member.
94
95 $ext = '';
96 $ns = $page->getNamespace();
97 $model = $this->namespaceContentModels[$ns] ?? null;
98
99 // Hook can determine default model
100 if ( $page instanceof PageIdentity ) {
101 $title = $this->titleFactory->newFromPageIdentity( $page );
102 } else {
103 $title = $this->titleFactory->newFromLinkTarget( $page );
104 }
105 // @phan-suppress-next-line PhanTypeMismatchArgument Type mismatch on pass-by-ref args
106 if ( !$this->hookRunner->onContentHandlerDefaultModelFor( $title, $model ) && $model !== null ) {
107 return $model;
108 }
109
110 // Could this page contain code based on the title?
111 $isCodePage = $ns === NS_MEDIAWIKI && preg_match( '!\.(css|js|json|vue)$!u', $title->getText(), $m );
112 if ( $isCodePage ) {
113 $ext = $m[1];
114 }
115
116 // Is this a user subpage containing code?
117 $isCodeSubpage = $ns === NS_USER
118 && !$isCodePage
119 && preg_match( "/\\/.*\\.(js|css|json|vue)$/", $title->getText(), $m );
120
121 if ( $isCodeSubpage ) {
122 $ext = $m[1];
123 }
124
125 // Is this wikitext, according to $wgNamespaceContentModels or the DefaultModelFor hook?
126 $isWikitext = $model === null || $model == CONTENT_MODEL_WIKITEXT;
127 $isWikitext = $isWikitext && !$isCodePage && !$isCodeSubpage;
128
129 if ( !$isWikitext ) {
130 switch ( $ext ) {
131 case 'js':
133 case 'css':
134 return CONTENT_MODEL_CSS;
135 case 'json':
136 return CONTENT_MODEL_JSON;
137 case 'vue':
138 return CONTENT_MODEL_VUE;
139 default:
140 return $model ?? CONTENT_MODEL_TEXT;
141 }
142 }
143
144 // We established that it must be wikitext
145
147 }
148
149}
const CONTENT_MODEL_VUE
Definition Defines.php:241
const NS_USER
Definition Defines.php:53
const CONTENT_MODEL_CSS
Definition Defines.php:237
const NS_MEDIAWIKI
Definition Defines.php:59
const CONTENT_MODEL_WIKITEXT
Definition Defines.php:235
const CONTENT_MODEL_JSON
Definition Defines.php:239
const CONTENT_MODEL_TEXT
Definition Defines.php:238
const CONTENT_MODEL_JAVASCRIPT
Definition Defines.php:236
Exception thrown when an unregistered content model is requested.
This class provides an implementation of the core hook interfaces, forwarding hook calls to HookConta...
A SlotRoleHandler for the main slot.
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.
Represents the target of a wiki link.
Interface for objects (potentially) representing an editable wiki page.