MediaWiki REL1_37
JavaScriptContentHandler.php
Go to the documentation of this file.
1<?php
23
33
37 public function __construct( $modelId = CONTENT_MODEL_JAVASCRIPT ) {
38 parent::__construct( $modelId, [ CONTENT_FORMAT_JAVASCRIPT ] );
39 }
40
44 protected function getContentClass() {
45 return JavaScriptContent::class;
46 }
47
48 public function supportsRedirects() {
49 return true;
50 }
51
59 public function makeRedirectContent( Title $destination, $text = '' ) {
60 // The parameters are passed as a string so the / is not url-encoded by wfArrayToCgi
61 $url = $destination->getFullURL( 'action=raw&ctype=text/javascript', false, PROTO_RELATIVE );
62 $class = $this->getContentClass();
63 return new $class( '/* #REDIRECT */' . Xml::encodeJsCall( 'mw.loader.load', [ $url ] ) );
64 }
65
66 public function preSaveTransform(
68 PreSaveTransformParams $pstParams
69 ): Content {
70 $shouldCallDeprecatedMethod = $this->shouldCallDeprecatedContentTransformMethod(
72 $pstParams
73 );
74
75 if ( $shouldCallDeprecatedMethod ) {
76 return $this->callDeprecatedContentPST(
77 $content,
78 $pstParams
79 );
80 }
81
82 '@phan-var JavascriptContent $content';
83
84 $parserOptions = $pstParams->getParserOptions();
85 // @todo Make pre-save transformation optional for script pages (T34858)
86 $services = MediaWikiServices::getInstance();
87 if ( !$services->getUserOptionsLookup()->getBoolOption( $pstParams->getUser(), 'pst-cssjs' ) ) {
88 // Allow bot users to disable the pre-save transform for CSS/JS (T236828).
89 $parserOptions = clone $parserOptions;
90 $parserOptions->setPreSaveTransform( false );
91 }
92
93 $text = $content->getText();
94 $pst = $services->getParser()->preSaveTransform(
95 $text,
96 $pstParams->getPage(),
97 $pstParams->getUser(),
98 $parserOptions
99 );
100
101 $contentClass = $this->getContentClass();
102 return new $contentClass( $pst );
103 }
104}
const CONTENT_FORMAT_JAVASCRIPT
For JS pages.
Definition Defines.php:226
const PROTO_RELATIVE
Definition Defines.php:194
const CONTENT_MODEL_JAVASCRIPT
Definition Defines.php:209
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)
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.
MediaWikiServices is the service locator for the application scope of MediaWiki.
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:2211
Base interface for content objects.
Definition Content.php:35
$content
Definition router.php:76