MediaWiki
REL1_34
MainSlotRoleHandler.php
Go to the documentation of this file.
1
<?php
23
namespace
MediaWiki\Revision
;
24
25
use
ContentHandler
;
26
use
Hooks
;
27
use
MediaWiki\Linker\LinkTarget
;
28
use
Title
;
29
41
class
MainSlotRoleHandler
extends
SlotRoleHandler
{
42
47
private
$namespaceContentModels
;
48
53
public
function
__construct
( array
$namespaceContentModels
) {
54
parent::__construct(
'main'
,
CONTENT_MODEL_WIKITEXT
);
55
$this->namespaceContentModels =
$namespaceContentModels
;
56
}
57
58
public
function
supportsArticleCount
() {
59
return
true
;
60
}
61
68
public
function
isAllowedModel
( $model,
LinkTarget
$page ) {
69
$title
= Title::newFromLinkTarget( $page );
70
$handler = ContentHandler::getForModelID( $model );
71
return
$handler->canBeUsedOn(
$title
);
72
}
73
79
public
function
getDefaultModel
(
LinkTarget
$page ) {
80
// NOTE: this method must not rely on $title->getContentModel() directly or indirectly,
81
// because it is used to initialize the mContentModel member.
82
83
$ext
=
''
;
84
$ns = $page->
getNamespace
();
85
$model = $this->namespaceContentModels[$ns] ??
null
;
86
87
// Hook can determine default model
88
$title
= Title::newFromLinkTarget( $page );
89
if
( !Hooks::run(
'ContentHandlerDefaultModelFor'
, [
$title
, &$model ] ) && !is_null( $model ) ) {
90
return
$model;
91
}
92
93
// Could this page contain code based on the title?
94
$isCodePage = $ns ===
NS_MEDIAWIKI
&& preg_match(
'!\.(css|js|json)$!u'
,
$title
->getText(), $m );
95
if
( $isCodePage ) {
96
$ext
= $m[1];
97
}
98
99
// Is this a user subpage containing code?
100
$isCodeSubpage = $ns ===
NS_USER
101
&& !$isCodePage
102
&& preg_match(
"/\\/.*\\.(js|css|json)$/"
,
$title
->getText(), $m );
103
104
if
( $isCodeSubpage ) {
105
$ext
= $m[1];
106
}
107
108
// Is this wikitext, according to $wgNamespaceContentModels or the DefaultModelFor hook?
109
$isWikitext = is_null( $model ) || $model ==
CONTENT_MODEL_WIKITEXT
;
110
$isWikitext = $isWikitext && !$isCodePage && !$isCodeSubpage;
111
112
if
( !$isWikitext ) {
113
switch
(
$ext
) {
114
case
'js'
:
115
return
CONTENT_MODEL_JAVASCRIPT
;
116
case
'css'
:
117
return
CONTENT_MODEL_CSS
;
118
case
'json'
:
119
return
CONTENT_MODEL_JSON
;
120
default
:
121
return
$model ??
CONTENT_MODEL_TEXT
;
122
}
123
}
124
125
// We established that it must be wikitext
126
127
return
CONTENT_MODEL_WIKITEXT
;
128
}
129
130
}
ContentHandler
A content handler knows how do deal with a specific type of content on a wiki page.
Definition
ContentHandler.php:55
Hooks
Hooks class.
Definition
Hooks.php:34
MediaWiki\Revision\MainSlotRoleHandler
A SlotRoleHandler for the main slot.
Definition
MainSlotRoleHandler.php:41
MediaWiki\Revision\MainSlotRoleHandler\$namespaceContentModels
string[] $namespaceContentModels
A mapping of namespaces to content models.
Definition
MainSlotRoleHandler.php:47
MediaWiki\Revision\MainSlotRoleHandler\isAllowedModel
isAllowedModel( $model, LinkTarget $page)
Definition
MainSlotRoleHandler.php:68
MediaWiki\Revision\MainSlotRoleHandler\getDefaultModel
getDefaultModel(LinkTarget $page)
Definition
MainSlotRoleHandler.php:79
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:58
MediaWiki\Revision\MainSlotRoleHandler\__construct
__construct(array $namespaceContentModels)
Definition
MainSlotRoleHandler.php:53
MediaWiki\Revision\SlotRoleHandler
SlotRoleHandler instances are used to declare the existence and behavior of slot roles.
Definition
SlotRoleHandler.php:34
Title
Represents a title within MediaWiki.
Definition
Title.php:42
NS_USER
const NS_USER
Definition
Defines.php:71
CONTENT_MODEL_CSS
const CONTENT_MODEL_CSS
Definition
Defines.php:226
NS_MEDIAWIKI
const NS_MEDIAWIKI
Definition
Defines.php:77
CONTENT_MODEL_WIKITEXT
const CONTENT_MODEL_WIKITEXT
Definition
Defines.php:224
CONTENT_MODEL_JSON
const CONTENT_MODEL_JSON
Definition
Defines.php:228
CONTENT_MODEL_TEXT
const CONTENT_MODEL_TEXT
Definition
Defines.php:227
CONTENT_MODEL_JAVASCRIPT
const CONTENT_MODEL_JAVASCRIPT
Definition
Defines.php:225
MediaWiki\Linker\LinkTarget
Definition
LinkTarget.php:26
MediaWiki\Linker\LinkTarget\getNamespace
getNamespace()
Get the namespace index.
MediaWiki\Revision
Created by PhpStorm.
Definition
FallbackSlotRoleHandler.php:23
$ext
if(!is_readable( $file)) $ext
Definition
router.php:48
$title
$title
Definition
testCompression.php:34
includes
Revision
MainSlotRoleHandler.php
Generated on Fri Apr 5 2024 23:10:27 for MediaWiki by
1.9.8