MediaWiki
fundraising/REL1_35
MainSlotRoleHandler.php
Go to the documentation of this file.
1
<?php
23
namespace
MediaWiki\Revision
;
24
25
use
Hooks
;
26
use
MediaWiki\Content\IContentHandlerFactory
;
27
use
MediaWiki\Linker\LinkTarget
;
28
use
MWUnknownContentModelException
;
29
use
Title
;
30
42
class
MainSlotRoleHandler
extends
SlotRoleHandler
{
43
48
private
$namespaceContentModels
;
49
53
private
$contentHandlerFactory
;
54
60
public
function
__construct
(
61
array
$namespaceContentModels
,
62
IContentHandlerFactory
$contentHandlerFactory
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'
:
128
return
CONTENT_MODEL_JAVASCRIPT
;
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
140
return
CONTENT_MODEL_WIKITEXT
;
141
}
142
143
}
Hooks
Hooks class.
Definition
Hooks.php:38
MWUnknownContentModelException
Exception thrown when an unregistered content model is requested.
Definition
MWUnknownContentModelException.php:11
MediaWiki\Revision\MainSlotRoleHandler
A SlotRoleHandler for the main slot.
Definition
MainSlotRoleHandler.php:42
MediaWiki\Revision\MainSlotRoleHandler\$namespaceContentModels
string[] $namespaceContentModels
A mapping of namespaces to content models.
Definition
MainSlotRoleHandler.php:48
MediaWiki\Revision\MainSlotRoleHandler\isAllowedModel
isAllowedModel( $model, LinkTarget $page)
Definition
MainSlotRoleHandler.php:80
MediaWiki\Revision\MainSlotRoleHandler\$contentHandlerFactory
IContentHandlerFactory $contentHandlerFactory
Definition
MainSlotRoleHandler.php:53
MediaWiki\Revision\MainSlotRoleHandler\getDefaultModel
getDefaultModel(LinkTarget $page)
Definition
MainSlotRoleHandler.php:92
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:69
MediaWiki\Revision\MainSlotRoleHandler\__construct
__construct(array $namespaceContentModels, IContentHandlerFactory $contentHandlerFactory)
Definition
MainSlotRoleHandler.php:60
MediaWiki\Revision\SlotRoleHandler
SlotRoleHandler instances are used to declare the existence and behavior of slot roles.
Definition
SlotRoleHandler.php:36
Title
Represents a title within MediaWiki.
Definition
Title.php:42
NS_USER
const NS_USER
Definition
Defines.php:72
CONTENT_MODEL_CSS
const CONTENT_MODEL_CSS
Definition
Defines.php:227
NS_MEDIAWIKI
const NS_MEDIAWIKI
Definition
Defines.php:78
CONTENT_MODEL_WIKITEXT
const CONTENT_MODEL_WIKITEXT
Definition
Defines.php:225
CONTENT_MODEL_JSON
const CONTENT_MODEL_JSON
Definition
Defines.php:229
CONTENT_MODEL_TEXT
const CONTENT_MODEL_TEXT
Definition
Defines.php:228
CONTENT_MODEL_JAVASCRIPT
const CONTENT_MODEL_JAVASCRIPT
Definition
Defines.php:226
MediaWiki\Content\IContentHandlerFactory
Definition
IContentHandlerFactory.php:10
MediaWiki\Linker\LinkTarget
Definition
LinkTarget.php:26
MediaWiki\Linker\LinkTarget\getNamespace
getNamespace()
Get the namespace index.
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 Mon Nov 25 2024 16:15:33 for MediaWiki by
1.10.0