MediaWiki master
MainSlotRoleHandler.php
Go to the documentation of this file.
1<?php
23namespace MediaWiki\Revision;
24
32
45
50 private $namespaceContentModels;
51
53 private $contentHandlerFactory;
54
56 private $hookRunner;
57
59 private $titleFactory;
60
68 public function __construct(
69 array $namespaceContentModels,
70 IContentHandlerFactory $contentHandlerFactory,
71 HookContainer $hookContainer,
72 TitleFactory $titleFactory
73 ) {
74 parent::__construct( SlotRecord::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->newFromPageIdentity( $page );
94 $handler = $this->contentHandlerFactory->getContentHandler( $model );
95
96 return $handler->canBeUsedOn( $title );
97 }
98
104 public function getDefaultModel( $page ) {
105 // NOTE: this method must not rely on $title->getContentModel() directly or indirectly,
106 // because it is used to initialize the mContentModel member.
107
108 $ext = '';
109 $ns = $page->getNamespace();
110 $model = $this->namespaceContentModels[$ns] ?? null;
111
112 // Hook can determine default model
113 if ( $page instanceof PageIdentity ) {
114 $title = $this->titleFactory->newFromPageIdentity( $page );
115 } else {
116 $title = $this->titleFactory->newFromLinkTarget( $page );
117 }
118 // @phan-suppress-next-line PhanTypeMismatchArgument Type mismatch on pass-by-ref args
119 if ( !$this->hookRunner->onContentHandlerDefaultModelFor( $title, $model ) && $model !== null ) {
120 return $model;
121 }
122
123 // Could this page contain code based on the title?
124 $isCodePage = $ns === NS_MEDIAWIKI && preg_match( '!\.(css|js|json)$!u', $title->getText(), $m );
125 if ( $isCodePage ) {
126 $ext = $m[1];
127 }
128
129 // Is this a user subpage containing code?
130 $isCodeSubpage = $ns === NS_USER
131 && !$isCodePage
132 && preg_match( "/\\/.*\\.(js|css|json)$/", $title->getText(), $m );
133
134 if ( $isCodeSubpage ) {
135 $ext = $m[1];
136 }
137
138 // Is this wikitext, according to $wgNamespaceContentModels or the DefaultModelFor hook?
139 $isWikitext = $model === null || $model == CONTENT_MODEL_WIKITEXT;
140 $isWikitext = $isWikitext && !$isCodePage && !$isCodeSubpage;
141
142 if ( !$isWikitext ) {
143 switch ( $ext ) {
144 case 'js':
146 case 'css':
147 return CONTENT_MODEL_CSS;
148 case 'json':
149 return CONTENT_MODEL_JSON;
150 default:
151 return $model ?? CONTENT_MODEL_TEXT;
152 }
153 }
154
155 // We established that it must be wikitext
156
158 }
159
160}
const NS_USER
Definition Defines.php:67
const CONTENT_MODEL_CSS
Definition Defines.php:230
const NS_MEDIAWIKI
Definition Defines.php:73
const CONTENT_MODEL_WIKITEXT
Definition Defines.php:228
const CONTENT_MODEL_JSON
Definition Defines.php:232
const CONTENT_MODEL_TEXT
Definition Defines.php:231
const CONTENT_MODEL_JAVASCRIPT
Definition Defines.php:229
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.