MediaWiki REL1_35
MainSlotRoleHandler.php
Go to the documentation of this file.
1<?php
23namespace MediaWiki\Revision;
24
25use Hooks;
29use Title;
30
43
49
54
60 public function __construct(
63 ) {
64 parent::__construct( 'main', CONTENT_MODEL_WIKITEXT );
65 $this->namespaceContentModels = $namespaceContentModels;
66 $this->contentHandlerFactory = $contentHandlerFactory;
67 }
68
69 public function supportsArticleCount() {
70 return true;
71 }
72
80 public function isAllowedModel( $model, LinkTarget $page ) {
81 $title = Title::newFromLinkTarget( $page );
82 $handler = $this->contentHandlerFactory->getContentHandler( $model );
83
84 return $handler->canBeUsedOn( $title );
85 }
86
92 public function getDefaultModel( LinkTarget $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 $title = Title::newFromLinkTarget( $page );
102 if ( !Hooks::runner()->onContentHandlerDefaultModelFor( $title, $model ) && $model !== null ) {
103 return $model;
104 }
105
106 // Could this page contain code based on the title?
107 $isCodePage = $ns === NS_MEDIAWIKI && preg_match( '!\.(css|js|json)$!u', $title->getText(), $m );
108 if ( $isCodePage ) {
109 $ext = $m[1];
110 }
111
112 // Is this a user subpage containing code?
113 $isCodeSubpage = $ns === NS_USER
114 && !$isCodePage
115 && preg_match( "/\\/.*\\.(js|css|json)$/", $title->getText(), $m );
116
117 if ( $isCodeSubpage ) {
118 $ext = $m[1];
119 }
120
121 // Is this wikitext, according to $wgNamespaceContentModels or the DefaultModelFor hook?
122 $isWikitext = $model === null || $model == CONTENT_MODEL_WIKITEXT;
123 $isWikitext = $isWikitext && !$isCodePage && !$isCodeSubpage;
124
125 if ( !$isWikitext ) {
126 switch ( $ext ) {
127 case 'js':
129 case 'css':
130 return CONTENT_MODEL_CSS;
131 case 'json':
132 return CONTENT_MODEL_JSON;
133 default:
134 return $model ?? CONTENT_MODEL_TEXT;
135 }
136 }
137
138 // We established that it must be wikitext
139
141 }
142
143}
Hooks class.
Definition Hooks.php:38
Exception thrown when an unregistered content model is requested.
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)
SlotRoleHandler instances are used to declare the existence and behavior of slot roles.
Represents a title within MediaWiki.
Definition Title.php:42
const NS_USER
Definition Defines.php:72
const CONTENT_MODEL_CSS
Definition Defines.php:227
const NS_MEDIAWIKI
Definition Defines.php:78
const CONTENT_MODEL_WIKITEXT
Definition Defines.php:225
const CONTENT_MODEL_JSON
Definition Defines.php:229
const CONTENT_MODEL_TEXT
Definition Defines.php:228
const CONTENT_MODEL_JAVASCRIPT
Definition Defines.php:226
getNamespace()
Get the namespace index.
if(!is_readable( $file)) $ext
Definition router.php:48