| 1 |
<?php |
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
|
| 6 |
|
| 7 |
|
| 8 |
|
| 9 |
|
| 10 |
|
| 11 |
|
| 12 |
|
| 13 |
|
| 14 |
|
| 15 |
|
| 16 |
|
| 17 |
|
| 18 |
|
| 19 |
|
| 20 |
|
| 21 |
|
| 22 |
|
| 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 MediaWiki\Title\TitleFactory; |
| 31 |
use MWUnknownContentModelException; |
| 32 |
|
| 33 |
|
| 34 |
|
| 35 |
|
| 36 |
|
| 37 |
|
| 38 |
|
| 39 |
|
| 40 |
|
| 41 |
|
| 42 |
|
| 43 |
|
| 44 |
class MainSlotRoleHandler extends SlotRoleHandler { |
| 45 |
|
| 46 |
|
| 47 |
|
| 48 |
|
| 49 |
|
| 50 |
private $namespaceContentModels; |
| 51 |
|
| 52 |
|
| 53 |
private $contentHandlerFactory; |
| 54 |
|
| 55 |
|
| 56 |
private $hookRunner; |
| 57 |
|
| 58 |
|
| 59 |
private $titleFactory; |
| 60 |
|
| 61 |
|
| 62 |
|
| 63 |
|
| 64 |
|
| 65 |
|
| 66 |
|
| 67 |
|
| 68 |
public function __construct( |
| 69 |
array $namespaceContentModels, |
| 70 |
IContentHandlerFactory $contentHandlerFactory, |
| 71 |
HookContainer $hookContainer, |
| 72 |
TitleFactory $titleFactory |
| 73 |
) { |
| 74 |
parent::__construct( SlotRecord::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 |
|
| 85 |
|
| 86 |
|
| 87 |
|
| 88 |
|
| 89 |
|
| 90 |
|
| 91 |
|
| 92 |
public function isAllowedModel( $model, PageIdentity $page ) { |
| 93 |
$title = $this->titleFactory->newFromPageIdentity( $page ); |
| 94 |
$handler = $this->contentHandlerFactory->getContentHandler( $model ); |
| 95 |
|
| 96 |
return $handler->canBeUsedOn( $title ); |
| 97 |
} |
| 98 |
|
| 99 |
|
| 100 |
|
| 101 |
|
| 102 |
|
| 103 |
|
| 104 |
public function getDefaultModel( $page ) { |
| 105 |
|
| 106 |
|
| 107 |
|
| 108 |
$ext = ''; |
| 109 |
$ns = $page->getNamespace(); |
| 110 |
$model = $this->namespaceContentModels[$ns] ?? null; |
| 111 |
|
| 112 |
|
| 113 |
if ( $page instanceof PageIdentity ) { |
| 114 |
$title = $this->titleFactory->newFromPageIdentity( $page ); |
| 115 |
} else { |
| 116 |
$title = $this->titleFactory->newFromLinkTarget( $page ); |
| 117 |
} |
| 118 |
|
| 119 |
if ( !$this->hookRunner->onContentHandlerDefaultModelFor( $title, $model ) && $model !== null ) { |
| 120 |
return $model; |
| 121 |
} |
| 122 |
|
| 123 |
|
| 124 |
$isCodePage = $ns === NS_MEDIAWIKI && preg_match( '!\.(css|js|json)$!u', $title->getText(), $m ); |
| 125 |
if ( $isCodePage ) { |
| 126 |
$ext = $m[1]; |
| 127 |
} |
| 128 |
|
| 129 |
|
| 130 |
$isCodeSubpage = $ns === NS_USER |
| 131 |
&& !$isCodePage |
| 132 |
&& preg_match( "/\\/.*\\.(js|css|json)$/", $title->getText(), $m ); |
| 133 |
|
| 134 |
if ( $isCodeSubpage ) { |
| 135 |
$ext = $m[1]; |
| 136 |
} |
| 137 |
|
| 138 |
|
| 139 |
$isWikitext = $model === null || $model == CONTENT_MODEL_WIKITEXT; |
| 140 |
$isWikitext = $isWikitext && !$isCodePage && !$isCodeSubpage; |
| 141 |
|
| 142 |
if ( !$isWikitext ) { |
| 143 |
switch ( $ext ) { |
| 144 |
case 'js': |
| 145 |
return CONTENT_MODEL_JAVASCRIPT; |
| 146 |
case 'css': |
| 147 |
return CONTENT_MODEL_CSS; |
| 148 |
case 'json': |
| 149 |
return CONTENT_MODEL_JSON; |
| 150 |
default: |
| 151 |
return $model ?? CONTENT_MODEL_TEXT; |
| 152 |
} |
| 153 |
} |
| 154 |
|
| 155 |
|
| 156 |
|
| 157 |
return CONTENT_MODEL_WIKITEXT; |
| 158 |
} |
| 159 |
|
| 160 |
} |