MediaWiki
REL1_37
MainSlotRoleHandler.php
Go to the documentation of this file.
1
<?php
23
namespace
MediaWiki\Revision
;
24
25
use
MediaWiki\Content\IContentHandlerFactory
;
26
use
MediaWiki\HookContainer\HookContainer
;
27
use
MediaWiki\HookContainer\HookRunner
;
28
use
MediaWiki\Linker\LinkTarget
;
29
use
MediaWiki\Page\PageIdentity
;
30
use
MWUnknownContentModelException
;
31
use
TitleFactory
;
32
44
class
MainSlotRoleHandler
extends
SlotRoleHandler
{
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(
'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
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->castFromPageIdentity( $page );
115
}
else
{
116
$title
= $this->titleFactory->castFromLinkTarget( $page );
117
}
118
if
( !$this->hookRunner->onContentHandlerDefaultModelFor(
$title
, $model ) && $model !==
null
) {
119
return
$model;
120
}
121
122
// Could this page contain code based on the title?
123
$isCodePage = $ns ===
NS_MEDIAWIKI
&& preg_match(
'!\.(css|js|json)$!u'
,
$title
->getText(), $m );
124
if
( $isCodePage ) {
125
$ext
= $m[1];
126
}
127
128
// Is this a user subpage containing code?
129
$isCodeSubpage = $ns ===
NS_USER
130
&& !$isCodePage
131
&& preg_match(
"/\\/.*\\.(js|css|json)$/"
,
$title
->getText(), $m );
132
133
if
( $isCodeSubpage ) {
134
$ext
= $m[1];
135
}
136
137
// Is this wikitext, according to $wgNamespaceContentModels or the DefaultModelFor hook?
138
$isWikitext = $model ===
null
|| $model ==
CONTENT_MODEL_WIKITEXT
;
139
$isWikitext = $isWikitext && !$isCodePage && !$isCodeSubpage;
140
141
if
( !$isWikitext ) {
142
switch
(
$ext
) {
143
case
'js'
:
144
return
CONTENT_MODEL_JAVASCRIPT
;
145
case
'css'
:
146
return
CONTENT_MODEL_CSS
;
147
case
'json'
:
148
return
CONTENT_MODEL_JSON
;
149
default
:
150
return
$model ??
CONTENT_MODEL_TEXT
;
151
}
152
}
153
154
// We established that it must be wikitext
155
156
return
CONTENT_MODEL_WIKITEXT
;
157
}
158
159
}
NS_USER
const NS_USER
Definition
Defines.php:66
CONTENT_MODEL_CSS
const CONTENT_MODEL_CSS
Definition
Defines.php:210
NS_MEDIAWIKI
const NS_MEDIAWIKI
Definition
Defines.php:72
CONTENT_MODEL_WIKITEXT
const CONTENT_MODEL_WIKITEXT
Definition
Defines.php:208
CONTENT_MODEL_JSON
const CONTENT_MODEL_JSON
Definition
Defines.php:212
CONTENT_MODEL_TEXT
const CONTENT_MODEL_TEXT
Definition
Defines.php:211
CONTENT_MODEL_JAVASCRIPT
const CONTENT_MODEL_JAVASCRIPT
Definition
Defines.php:209
MWUnknownContentModelException
Exception thrown when an unregistered content model is requested.
Definition
MWUnknownContentModelException.php:11
MediaWiki\HookContainer\HookContainer
HookContainer class.
Definition
HookContainer.php:45
MediaWiki\HookContainer\HookRunner
This class provides an implementation of the core hook interfaces, forwarding hook calls to HookConta...
Definition
HookRunner.php:559
MediaWiki\Revision\MainSlotRoleHandler
A SlotRoleHandler for the main slot.
Definition
MainSlotRoleHandler.php:44
MediaWiki\Revision\MainSlotRoleHandler\getDefaultModel
getDefaultModel( $page)
Definition
MainSlotRoleHandler.php:104
MediaWiki\Revision\MainSlotRoleHandler\$namespaceContentModels
string[] $namespaceContentModels
A mapping of namespaces to content models.
Definition
MainSlotRoleHandler.php:50
MediaWiki\Revision\MainSlotRoleHandler\$hookRunner
HookRunner $hookRunner
Definition
MainSlotRoleHandler.php:56
MediaWiki\Revision\MainSlotRoleHandler\$contentHandlerFactory
IContentHandlerFactory $contentHandlerFactory
Definition
MainSlotRoleHandler.php:53
MediaWiki\Revision\MainSlotRoleHandler\supportsArticleCount
supportsArticleCount()
Whether this slot should be considered when determining whether a page should be counted as an "artic...
Definition
MainSlotRoleHandler.php:81
MediaWiki\Revision\MainSlotRoleHandler\__construct
__construct(array $namespaceContentModels, IContentHandlerFactory $contentHandlerFactory, HookContainer $hookContainer, TitleFactory $titleFactory)
Definition
MainSlotRoleHandler.php:68
MediaWiki\Revision\MainSlotRoleHandler\$titleFactory
TitleFactory $titleFactory
Definition
MainSlotRoleHandler.php:59
MediaWiki\Revision\MainSlotRoleHandler\isAllowedModel
isAllowedModel( $model, PageIdentity $page)
Definition
MainSlotRoleHandler.php:92
MediaWiki\Revision\SlotRoleHandler
SlotRoleHandler instances are used to declare the existence and behavior of slot roles.
Definition
SlotRoleHandler.php:37
TitleFactory
Creates Title objects.
Definition
TitleFactory.php:35
MediaWiki\Content\IContentHandlerFactory
Definition
IContentHandlerFactory.php:10
MediaWiki\Linker\LinkTarget
Definition
LinkTarget.php:26
MediaWiki\Page\PageIdentity
Interface for objects (potentially) representing an editable wiki page.
Definition
PageIdentity.php:64
MediaWiki\Revision
Definition
ContributionsLookup.php:3
$ext
if(!is_readable( $file)) $ext
Definition
router.php:48
$title
$title
Definition
testCompression.php:38
includes
Revision
MainSlotRoleHandler.php
Generated on Fri Apr 5 2024 23:40:48 for MediaWiki by
1.9.8