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