MediaWiki  master
JavaScriptContentHandler.php
Go to the documentation of this file.
1 <?php
24 
34 
38  public function __construct( $modelId = CONTENT_MODEL_JAVASCRIPT ) {
39  parent::__construct( $modelId, [ CONTENT_FORMAT_JAVASCRIPT ] );
40  }
41 
45  protected function getContentClass() {
46  return JavaScriptContent::class;
47  }
48 
49  public function supportsRedirects() {
50  return true;
51  }
52 
60  public function makeRedirectContent( Title $destination, $text = '' ) {
61  // The parameters are passed as a string so the / is not url-encoded by wfArrayToCgi
62  $url = $destination->getFullURL( 'action=raw&ctype=text/javascript', false, PROTO_RELATIVE );
63  $class = $this->getContentClass();
64  return new $class( '/* #REDIRECT */' . Xml::encodeJsCall( 'mw.loader.load', [ $url ] ) );
65  }
66 
67  public function preSaveTransform(
69  PreSaveTransformParams $pstParams
70  ): Content {
71  $shouldCallDeprecatedMethod = $this->shouldCallDeprecatedContentTransformMethod(
72  $content,
73  $pstParams
74  );
75 
76  if ( $shouldCallDeprecatedMethod ) {
77  return $this->callDeprecatedContentPST(
78  $content,
79  $pstParams
80  );
81  }
82 
83  '@phan-var JavascriptContent $content';
84 
85  $parserOptions = $pstParams->getParserOptions();
86  // @todo Make pre-save transformation optional for script pages (T34858)
87  $services = MediaWikiServices::getInstance();
88  if ( !$services->getUserOptionsLookup()->getBoolOption( $pstParams->getUser(), 'pst-cssjs' ) ) {
89  // Allow bot users to disable the pre-save transform for CSS/JS (T236828).
90  $parserOptions = clone $parserOptions;
91  $parserOptions->setPreSaveTransform( false );
92  }
93 
94  $text = $content->getText();
95  $pst = $services->getParser()->preSaveTransform(
96  $text,
97  $pstParams->getPage(),
98  $pstParams->getUser(),
99  $parserOptions
100  );
101 
102  $contentClass = $this->getContentClass();
103  return new $contentClass( $pst );
104  }
105 
122  protected function fillParserOutput(
124  ContentParseParams $cpoParams,
125  ParserOutput &$output
126  ) {
127  global $wgTextModelsToParse;
128  '@phan-var TextContent $content';
129  if ( in_array( $content->getModel(), $wgTextModelsToParse ) ) {
130  // parse just to get links etc into the database, HTML is replaced below.
131  $output = MediaWikiServices::getInstance()->getParser()
132  ->parse(
133  $content->getText(),
134  $cpoParams->getPage(),
135  $cpoParams->getParserOptions(),
136  true,
137  true,
138  $cpoParams->getRevId()
139  );
140  }
141 
142  if ( $cpoParams->getGenerateHtml() ) {
143  // Return JavaScript wrapped in a <pre> tag.
144  $html = Html::element(
145  'pre',
146  [ 'class' => 'mw-code mw-js', 'dir' => 'ltr' ],
147  "\n" . $content->getText() . "\n"
148  ) . "\n";
149  } else {
150  $html = '';
151  }
152 
153  $output->clearWrapperDivClass();
154  $output->setText( $html );
155  }
156 }
MediaWiki\Content\Renderer\ContentParseParams\getRevId
getRevId()
Definition: ContentParseParams.php:48
MediaWiki\Content\Renderer\ContentParseParams\getPage
getPage()
Definition: ContentParseParams.php:40
ParserOutput
Definition: ParserOutput.php:36
MediaWiki\Content\Renderer\ContentParseParams\getGenerateHtml
getGenerateHtml()
Definition: ContentParseParams.php:64
MediaWiki\MediaWikiServices
MediaWikiServices is the service locator for the application scope of MediaWiki.
Definition: MediaWikiServices.php:199
CodeContentHandler
Content handler for code content such as CSS, JavaScript, JSON, etc.
Definition: CodeContentHandler.php:33
JavaScriptContentHandler\preSaveTransform
preSaveTransform(Content $content, PreSaveTransformParams $pstParams)
Returns a $content object with pre-save transformations applied (or the same object if no transformat...
Definition: JavaScriptContentHandler.php:67
MediaWiki\Content\Transform\PreSaveTransformParams\getUser
getUser()
ContentHandler\shouldCallDeprecatedContentTransformMethod
shouldCallDeprecatedContentTransformMethod(Content $content, $params)
Check if we need to provide content overrides deprecated Content method.
Definition: ContentHandler.php:1734
MediaWiki\Content\Renderer\ContentParseParams
Definition: ContentParseParams.php:11
Xml\encodeJsCall
static encodeJsCall( $name, $args, $pretty=false)
Create a call to a JavaScript function.
Definition: Xml.php:691
MediaWiki\Content\Transform\PreSaveTransformParams
Definition: PreSaveTransformParams.php:12
JavaScriptContentHandler\supportsRedirects
supportsRedirects()
Returns true if this content model supports redirects.
Definition: JavaScriptContentHandler.php:49
PROTO_RELATIVE
const PROTO_RELATIVE
Definition: Defines.php:194
MediaWiki\Content\Renderer\ContentParseParams\getParserOptions
getParserOptions()
Definition: ContentParseParams.php:56
ParserOutput\setText
setText( $text)
Definition: ParserOutput.php:763
JavaScriptContentHandler\__construct
__construct( $modelId=CONTENT_MODEL_JAVASCRIPT)
Definition: JavaScriptContentHandler.php:38
ParserOutput\clearWrapperDivClass
clearWrapperDivClass()
Clears the CSS class to use for the wrapping div, effectively disabling the wrapper div until addWrap...
Definition: ParserOutput.php:502
$content
$content
Definition: router.php:76
JavaScriptContentHandler\fillParserOutput
fillParserOutput(Content $content, ContentParseParams $cpoParams, ParserOutput &$output)
Fills the provided ParserOutput object with information derived from the content.
Definition: JavaScriptContentHandler.php:122
JavaScriptContentHandler
Content handler for JavaScript pages.
Definition: JavaScriptContentHandler.php:33
Title\getFullURL
getFullURL( $query='', $query2=false, $proto=PROTO_RELATIVE)
Get a real URL referring to this title, with interwiki link and fragment.
Definition: Title.php:2185
JavaScriptContentHandler\getContentClass
getContentClass()
Definition: JavaScriptContentHandler.php:45
Content
Base interface for content objects.
Definition: Content.php:35
Title
Represents a title within MediaWiki.
Definition: Title.php:47
CONTENT_MODEL_JAVASCRIPT
const CONTENT_MODEL_JAVASCRIPT
Definition: Defines.php:209
JavaScriptContentHandler\makeRedirectContent
makeRedirectContent(Title $destination, $text='')
Create a redirect that is also valid JavaScript.
Definition: JavaScriptContentHandler.php:60
MediaWiki\Content\Transform\PreSaveTransformParams\getParserOptions
getParserOptions()
$wgTextModelsToParse
$wgTextModelsToParse
Determines which types of text are parsed as wikitext.
Definition: DefaultSettings.php:2477
ContentHandler\callDeprecatedContentPST
callDeprecatedContentPST(Content $content, PreSaveTransformParams $params)
Provided content overrides deprecated Content::preSaveTransform, call it and return.
Definition: ContentHandler.php:1757
Html\element
static element( $element, $attribs=[], $contents='')
Identical to rawElement(), but HTML-escapes $contents (like Xml::element()).
Definition: Html.php:232
CONTENT_FORMAT_JAVASCRIPT
const CONTENT_FORMAT_JAVASCRIPT
For JS pages.
Definition: Defines.php:226
MediaWiki\Content\Transform\PreSaveTransformParams\getPage
getPage()