MediaWiki master
MainSlotRoleHandler.php
Go to the documentation of this file.
1<?php
9namespace MediaWiki\Revision;
10
19
32
37 private $namespaceContentModels;
38
40 private $contentHandlerFactory;
41
43 private $hookRunner;
44
46 private $titleFactory;
47
55 public function __construct(
56 array $namespaceContentModels,
57 IContentHandlerFactory $contentHandlerFactory,
58 HookContainer $hookContainer,
59 TitleFactory $titleFactory
60 ) {
61 parent::__construct( SlotRecord::MAIN, CONTENT_MODEL_WIKITEXT );
62 $this->namespaceContentModels = $namespaceContentModels;
63 $this->contentHandlerFactory = $contentHandlerFactory;
64 $this->hookRunner = new HookRunner( $hookContainer );
65 $this->titleFactory = $titleFactory;
66 }
67
69 public function supportsArticleCount() {
70 return true;
71 }
72
80 public function isAllowedModel( $model, PageIdentity $page ) {
81 $title = $this->titleFactory->newFromPageIdentity( $page );
82 $handler = $this->contentHandlerFactory->getContentHandler( $model );
83
84 return $handler->canBeUsedOn( $title );
85 }
86
92 public function getDefaultModel( $page ) {
93 // NOTE: this method must not rely on $title->getContentModel() directly or indirectly,
94 // because it is used to initialize the mContentModel member.
95
96 $ext = '';
97 $ns = $page->getNamespace();
98 $model = $this->namespaceContentModels[$ns] ?? null;
99
100 // Hook can determine default model
101 if ( $page instanceof PageReference ) {
102 $title = $this->titleFactory->newFromPageReference( $page );
103 } else {
104 $title = $this->titleFactory->newFromLinkTarget( $page );
105 }
106 // @phan-suppress-next-line PhanTypeMismatchArgument Type mismatch on pass-by-ref args
107 if ( !$this->hookRunner->onContentHandlerDefaultModelFor( $title, $model ) && $model !== null ) {
108 return $model;
109 }
110
111 // Could this page contain code based on the title?
112 $isCodePage = $ns === NS_MEDIAWIKI && preg_match( '!\.(css|js|json|vue)$!u', $title->getText(), $m );
113 if ( $isCodePage ) {
114 $ext = $m[1];
115 }
116
117 // Is this a user subpage containing code?
118 $isCodeSubpage = $ns === NS_USER
119 && !$isCodePage
120 && preg_match( "/\\/.*\\.(js|css|json|vue)$/", $title->getText(), $m );
121
122 if ( $isCodeSubpage ) {
123 $ext = $m[1];
124 }
125
126 // Is this wikitext, according to $wgNamespaceContentModels or the DefaultModelFor hook?
127 $isWikitext = $model === null || $model == CONTENT_MODEL_WIKITEXT;
128 $isWikitext = $isWikitext && !$isCodePage && !$isCodeSubpage;
129
130 if ( !$isWikitext ) {
131 switch ( $ext ) {
132 case 'js':
134 case 'css':
135 return CONTENT_MODEL_CSS;
136 case 'json':
137 return CONTENT_MODEL_JSON;
138 case 'vue':
139 return CONTENT_MODEL_VUE;
140 default:
141 return $model ?? CONTENT_MODEL_TEXT;
142 }
143 }
144
145 // We established that it must be wikitext
146
148 }
149
150}
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.
Interface for objects (potentially) representing a page that can be viewable and linked to on a wiki.