MediaWiki  master
MainSlotRoleHandler.php
Go to the documentation of this file.
1 <?php
23 namespace MediaWiki\Revision;
24 
25 use Hooks;
29 use Title;
30 
43 
49 
54 
60  public function __construct(
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 ) {
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 
140  return CONTENT_MODEL_WIKITEXT;
141  }
142 
143 }
CONTENT_MODEL_JSON
const CONTENT_MODEL_JSON
Definition: Defines.php:228
Revision\MainSlotRoleHandler\isAllowedModel
isAllowedModel( $model, LinkTarget $page)
Definition: MainSlotRoleHandler.php:80
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
Revision\MainSlotRoleHandler
A SlotRoleHandler for the main slot.
Definition: MainSlotRoleHandler.php:42
Revision\MainSlotRoleHandler\$namespaceContentModels
string[] $namespaceContentModels
A mapping of namespaces to content models.
Definition: MainSlotRoleHandler.php:48
CONTENT_MODEL_CSS
const CONTENT_MODEL_CSS
Definition: Defines.php:226
Revision\SlotRoleHandler
SlotRoleHandler instances are used to declare the existence and behavior of slot roles.
Definition: SlotRoleHandler.php:36
CONTENT_MODEL_WIKITEXT
const CONTENT_MODEL_WIKITEXT
Definition: Defines.php:224
MediaWiki\Revision
Definition: ContributionsLookup.php:3
$title
$title
Definition: testCompression.php:38
MediaWiki\Content\IContentHandlerFactory
Definition: IContentHandlerFactory.php:10
Hooks\runner
static runner()
Get a HookRunner instance for calling hooks using the new interfaces.
Definition: Hooks.php:172
Title\newFromLinkTarget
static newFromLinkTarget(LinkTarget $linkTarget, $forceClone='')
Returns a Title given a LinkTarget.
Definition: Title.php:281
Title
Represents a title within MediaWiki.
Definition: Title.php:42
NS_USER
const NS_USER
Definition: Defines.php:71
MWUnknownContentModelException
Exception thrown when an unregistered content model is requested.
Definition: MWUnknownContentModelException.php:11
$ext
if(!is_readable( $file)) $ext
Definition: router.php:48
NS_MEDIAWIKI
const NS_MEDIAWIKI
Definition: Defines.php:77
CONTENT_MODEL_JAVASCRIPT
const CONTENT_MODEL_JAVASCRIPT
Definition: Defines.php:225
MediaWiki\Linker\LinkTarget
Definition: LinkTarget.php:26
CONTENT_MODEL_TEXT
const CONTENT_MODEL_TEXT
Definition: Defines.php:227
Revision\MainSlotRoleHandler\$contentHandlerFactory
IContentHandlerFactory $contentHandlerFactory
Definition: MainSlotRoleHandler.php:53
Revision\MainSlotRoleHandler\getDefaultModel
getDefaultModel(LinkTarget $page)
Definition: MainSlotRoleHandler.php:92
Revision\MainSlotRoleHandler\__construct
__construct(array $namespaceContentModels, IContentHandlerFactory $contentHandlerFactory)
Definition: MainSlotRoleHandler.php:60
Hooks
Hooks class.
Definition: Hooks.php:38