3 * This file is part of MediaWiki.
5 * This program is free software; you can redistribute it and/or modify
6 * it under the terms of the GNU General Public License as published by
7 * the Free Software Foundation; either version 2 of the License, or
8 * (at your option) any later version.
10 * This program is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 * GNU General Public License for more details.
15 * You should have received a copy of the GNU General Public License along
16 * with this program; if not, write to the Free Software Foundation, Inc.,
17 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
18 * http://www.gnu.org/copyleft/gpl.html
23 namespace MediaWiki\Revision
;
27 use MediaWiki\Linker\LinkTarget
;
31 * A SlotRoleHandler for the main slot. While most slot roles serve a specific purpose and
32 * thus typically exhibit the same behaviour on all pages, the main slot is used for different
33 * things in different pages, typically depending on the namespace, a "file extension" in
34 * the page name, or the content model of the slot's content.
36 * MainSlotRoleHandler implements some of the per-namespace and per-model behavior that was
37 * supported prior to MediaWiki Version 1.33.
41 class MainSlotRoleHandler
extends SlotRoleHandler
{
44 * @var string[] A mapping of namespaces to content models.
45 * @see $wgNamespaceContentModels
47 private $namespaceContentModels;
50 * @param string[] $namespaceContentModels A mapping of namespaces to content models,
51 * typically from $wgNamespaceContentModels.
53 public function __construct( array $namespaceContentModels ) {
54 parent
::__construct( 'main', CONTENT_MODEL_WIKITEXT
);
55 $this->namespaceContentModels
= $namespaceContentModels;
58 public function supportsArticleCount() {
63 * @param string $model
64 * @param LinkTarget $page
68 public function isAllowedModel( $model, LinkTarget
$page ) {
69 $title = Title
::newFromLinkTarget( $page );
70 $handler = ContentHandler
::getForModelID( $model );
71 return $handler->canBeUsedOn( $title );
75 * @param LinkTarget $page
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.
84 $ns = $page->getNamespace();
85 $model = $this->namespaceContentModels
[$ns] ??
null;
87 // Hook can determine default model
88 $title = Title
::newFromLinkTarget( $page );
89 if ( !Hooks
::run( 'ContentHandlerDefaultModelFor', [ $title, &$model ] ) ) {
90 if ( !is_null( $model ) ) {
95 // Could this page contain code based on the title?
96 $isCodePage = $ns === NS_MEDIAWIKI
&& preg_match( '!\.(css|js|json)$!u', $title->getText(), $m );
101 // Is this a user subpage containing code?
102 $isCodeSubpage = $ns === NS_USER
104 && preg_match( "/\\/.*\\.(js|css|json)$/", $title->getText(), $m );
106 if ( $isCodeSubpage ) {
110 // Is this wikitext, according to $wgNamespaceContentModels or the DefaultModelFor hook?
111 $isWikitext = is_null( $model ) ||
$model == CONTENT_MODEL_WIKITEXT
;
112 $isWikitext = $isWikitext && !$isCodePage && !$isCodeSubpage;
114 if ( !$isWikitext ) {
117 return CONTENT_MODEL_JAVASCRIPT
;
119 return CONTENT_MODEL_CSS
;
121 return CONTENT_MODEL_JSON
;
123 return $model ?? CONTENT_MODEL_TEXT
;
127 // We established that it must be wikitext
129 return CONTENT_MODEL_WIKITEXT
;