| 1 |
<?php |
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
|
| 6 |
|
| 7 |
|
| 8 |
|
| 9 |
|
| 10 |
|
| 11 |
|
| 12 |
|
| 13 |
|
| 14 |
|
| 15 |
|
| 16 |
|
| 17 |
|
| 18 |
|
| 19 |
|
| 20 |
|
| 21 |
namespace MediaWiki\Content; |
| 22 |
|
| 23 |
use MediaWiki\Content\Renderer\ContentParseParams; |
| 24 |
use MediaWiki\Content\Transform\PreSaveTransformParams; |
| 25 |
use MediaWiki\Html\Html; |
| 26 |
use MediaWiki\MainConfigNames; |
| 27 |
use MediaWiki\MediaWikiServices; |
| 28 |
use MediaWiki\Parser\ParserOutput; |
| 29 |
use MediaWiki\Parser\ParserOutputFlags; |
| 30 |
use MediaWiki\Title\Title; |
| 31 |
use WikiPage; |
| 32 |
|
| 33 |
|
| 34 |
|
| 35 |
|
| 36 |
|
| 37 |
|
| 38 |
|
| 39 |
|
| 40 |
|
| 41 |
class JavaScriptContentHandler extends CodeContentHandler { |
| 42 |
|
| 43 |
|
| 44 |
|
| 45 |
|
| 46 |
public function __construct( $modelId = CONTENT_MODEL_JAVASCRIPT ) { |
| 47 |
parent::__construct( $modelId, [ CONTENT_FORMAT_JAVASCRIPT ] ); |
| 48 |
} |
| 49 |
|
| 50 |
|
| 51 |
|
| 52 |
|
| 53 |
protected function getContentClass() { |
| 54 |
return JavaScriptContent::class; |
| 55 |
} |
| 56 |
|
| 57 |
public function supportsRedirects() { |
| 58 |
return true; |
| 59 |
} |
| 60 |
|
| 61 |
|
| 62 |
|
| 63 |
|
| 64 |
|
| 65 |
|
| 66 |
|
| 67 |
|
| 68 |
public function makeRedirectContent( Title $destination, $text = '' ) { |
| 69 |
|
| 70 |
$url = $destination->getFullURL( 'action=raw&ctype=text/javascript', false, PROTO_RELATIVE ); |
| 71 |
$class = $this->getContentClass(); |
| 72 |
|
| 73 |
|
| 74 |
|
| 75 |
$redirectContent = '/* #REDIRECT */mw.loader.load(' |
| 76 |
. json_encode( $url, JSON_UNESCAPED_SLASHES | JSON_UNESCAPED_UNICODE ) |
| 77 |
. ');'; |
| 78 |
return new $class( $redirectContent ); |
| 79 |
} |
| 80 |
|
| 81 |
public function preSaveTransform( |
| 82 |
Content $content, |
| 83 |
PreSaveTransformParams $pstParams |
| 84 |
): Content { |
| 85 |
'@phan-var JavascriptContent $content'; |
| 86 |
|
| 87 |
$parserOptions = $pstParams->getParserOptions(); |
| 88 |
|
| 89 |
$services = MediaWikiServices::getInstance(); |
| 90 |
if ( !$services->getUserOptionsLookup()->getBoolOption( $pstParams->getUser(), 'pst-cssjs' ) ) { |
| 91 |
|
| 92 |
$parserOptions = clone $parserOptions; |
| 93 |
$parserOptions->setPreSaveTransform( false ); |
| 94 |
} |
| 95 |
|
| 96 |
$text = $content->getText(); |
| 97 |
$pst = $services->getParserFactory()->getInstance()->preSaveTransform( |
| 98 |
$text, |
| 99 |
$pstParams->getPage(), |
| 100 |
$pstParams->getUser(), |
| 101 |
$parserOptions |
| 102 |
); |
| 103 |
|
| 104 |
$contentClass = $this->getContentClass(); |
| 105 |
return new $contentClass( $pst ); |
| 106 |
} |
| 107 |
|
| 108 |
|
| 109 |
|
| 110 |
|
| 111 |
|
| 112 |
|
| 113 |
|
| 114 |
|
| 115 |
|
| 116 |
|
| 117 |
|
| 118 |
|
| 119 |
|
| 120 |
|
| 121 |
|
| 122 |
|
| 123 |
|
| 124 |
protected function fillParserOutput( |
| 125 |
Content $content, |
| 126 |
ContentParseParams $cpoParams, |
| 127 |
ParserOutput &$output |
| 128 |
) { |
| 129 |
$textModelsToParse = MediaWikiServices::getInstance()->getMainConfig()->get( |
| 130 |
MainConfigNames::TextModelsToParse ); |
| 131 |
'@phan-var JavaScriptContent $content'; |
| 132 |
if ( in_array( $content->getModel(), $textModelsToParse ) ) { |
| 133 |
|
| 134 |
$output = MediaWikiServices::getInstance()->getParserFactory()->getInstance() |
| 135 |
->parse( |
| 136 |
$content->getText(), |
| 137 |
$cpoParams->getPage(), |
| 138 |
WikiPage::makeParserOptionsFromTitleAndModel( |
| 139 |
$cpoParams->getPage(), |
| 140 |
$content->getModel(), |
| 141 |
'canonical' |
| 142 |
), |
| 143 |
true, |
| 144 |
true, |
| 145 |
$cpoParams->getRevId() |
| 146 |
); |
| 147 |
} |
| 148 |
|
| 149 |
if ( $cpoParams->getGenerateHtml() ) { |
| 150 |
|
| 151 |
$html = Html::element( |
| 152 |
'pre', |
| 153 |
[ 'class' => 'mw-code mw-js', 'dir' => 'ltr' ], |
| 154 |
"\n" . $content->getText() . "\n" |
| 155 |
) . "\n"; |
| 156 |
} else { |
| 157 |
$html = null; |
| 158 |
} |
| 159 |
|
| 160 |
$output->clearWrapperDivClass(); |
| 161 |
$output->setRawText( $html ); |
| 162 |
|
| 163 |
$output->setOutputFlag( ParserOutputFlags::NO_TOC ); |
| 164 |
$output->setSections( [] ); |
| 165 |
} |
| 166 |
} |
| 167 |
|
| 168 |
class_alias( JavaScriptContentHandler::class, 'JavaScriptContentHandler' ); |