MediaWiki  master
JavaScriptContentHandler.php
Go to the documentation of this file.
1 <?php
27 
37 
41  public function __construct( $modelId = CONTENT_MODEL_JAVASCRIPT ) {
42  parent::__construct( $modelId, [ CONTENT_FORMAT_JAVASCRIPT ] );
43  }
44 
48  protected function getContentClass() {
49  return JavaScriptContent::class;
50  }
51 
52  public function supportsRedirects() {
53  return true;
54  }
55 
63  public function makeRedirectContent( Title $destination, $text = '' ) {
64  // The parameters are passed as a string so the / is not url-encoded by wfArrayToCgi
65  $url = $destination->getFullURL( 'action=raw&ctype=text/javascript', false, PROTO_RELATIVE );
66  $class = $this->getContentClass();
67  return new $class( '/* #REDIRECT */' . Html::encodeJsCall( 'mw.loader.load', [ $url ] ) );
68  }
69 
70  public function preSaveTransform(
72  PreSaveTransformParams $pstParams
73  ): Content {
74  $shouldCallDeprecatedMethod = $this->shouldCallDeprecatedContentTransformMethod(
75  $content,
76  $pstParams
77  );
78 
79  if ( $shouldCallDeprecatedMethod ) {
80  return $this->callDeprecatedContentPST(
81  $content,
82  $pstParams
83  );
84  }
85 
86  '@phan-var JavascriptContent $content';
87 
88  $parserOptions = $pstParams->getParserOptions();
89  // @todo Make pre-save transformation optional for script pages (T34858)
90  $services = MediaWikiServices::getInstance();
91  if ( !$services->getUserOptionsLookup()->getBoolOption( $pstParams->getUser(), 'pst-cssjs' ) ) {
92  // Allow bot users to disable the pre-save transform for CSS/JS (T236828).
93  $parserOptions = clone $parserOptions;
94  $parserOptions->setPreSaveTransform( false );
95  }
96 
97  $text = $content->getText();
98  $pst = $services->getParserFactory()->getInstance()->preSaveTransform(
99  $text,
100  $pstParams->getPage(),
101  $pstParams->getUser(),
102  $parserOptions
103  );
104 
105  $contentClass = $this->getContentClass();
106  return new $contentClass( $pst );
107  }
108 
125  protected function fillParserOutput(
127  ContentParseParams $cpoParams,
128  ParserOutput &$output
129  ) {
130  $textModelsToParse = MediaWikiServices::getInstance()->getMainConfig()->get(
131  MainConfigNames::TextModelsToParse );
132  '@phan-var JavaScriptContent $content';
133  if ( in_array( $content->getModel(), $textModelsToParse ) ) {
134  // parse just to get links etc into the database, HTML is replaced below.
135  $output = MediaWikiServices::getInstance()->getParserFactory()->getInstance()
136  ->parse(
137  $content->getText(),
138  $cpoParams->getPage(),
140  $cpoParams->getPage(),
141  $content->getModel(),
142  'canonical'
143  ),
144  true,
145  true,
146  $cpoParams->getRevId()
147  );
148  }
149 
150  if ( $cpoParams->getGenerateHtml() ) {
151  // Return JavaScript wrapped in a <pre> tag.
152  $html = Html::element(
153  'pre',
154  [ 'class' => 'mw-code mw-js', 'dir' => 'ltr' ],
155  "\n" . $content->getText() . "\n"
156  ) . "\n";
157  } else {
158  $html = null;
159  }
160 
161  $output->clearWrapperDivClass();
162  $output->setText( $html );
163  }
164 }
const CONTENT_FORMAT_JAVASCRIPT
For JS pages.
Definition: Defines.php:227
const PROTO_RELATIVE
Definition: Defines.php:193
const CONTENT_MODEL_JAVASCRIPT
Definition: Defines.php:210
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.
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.
This class is a collection of static functions that serve two purposes:
Definition: Html.php:57
A class containing constants representing the names of configuration variables.
Service locator for MediaWiki core services.
Represents a title within MediaWiki.
Definition: Title.php:76
getFullURL( $query='', $query2=false, $proto=PROTO_RELATIVE)
Get a real URL referring to this title, with interwiki link and fragment.
Definition: Title.php:2135
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.
static makeParserOptionsFromTitleAndModel(PageReference $pageRef, string $contentModel, $context)
Create canonical parser options for a given title and content model.
Definition: WikiPage.php:1911
Base interface for representing page content.
Definition: Content.php:39
$content
Definition: router.php:76