MediaWiki 1.40.4
SpecialExpandTemplates.php
Go to the documentation of this file.
1<?php
29
37
39 private const MAX_INCLUDE_SIZE = 50000000;
40
42 private $parser;
43
45 private $userOptionsLookup;
46
48 private $tidy;
49
55 public function __construct(
56 Parser $parser,
57 UserOptionsLookup $userOptionsLookup,
58 TidyDriverBase $tidy
59 ) {
60 parent::__construct( 'ExpandTemplates' );
61 $this->parser = $parser;
62 $this->userOptionsLookup = $userOptionsLookup;
63 $this->tidy = $tidy;
64 }
65
70 public function execute( $subpage ) {
71 $this->setHeaders();
72 $this->addHelpLink( 'Help:ExpandTemplates' );
73
74 $request = $this->getRequest();
75 $input = $request->getText( 'wpInput' );
76
77 if ( strlen( $input ) ) {
78 $removeComments = $request->getBool( 'wpRemoveComments', false );
79 $removeNowiki = $request->getBool( 'wpRemoveNowiki', false );
80 $generateXML = $request->getBool( 'wpGenerateXml' );
81 $generateRawHtml = $request->getBool( 'wpGenerateRawHtml' );
82
83 $options = ParserOptions::newFromContext( $this->getContext() );
84 $options->setRemoveComments( $removeComments );
85 $options->setMaxIncludeSize( self::MAX_INCLUDE_SIZE );
86
87 $titleStr = $request->getText( 'wpContextTitle' );
88 $title = Title::newFromText( $titleStr );
89 if ( !$title ) {
90 $title = $this->getPageTitle();
91 $options->setTargetLanguage( $this->getContentLanguage() );
92 }
93
94 if ( $generateXML ) {
95 $this->parser->startExternalParse( $title, $options, Parser::OT_PREPROCESS );
96 $dom = $this->parser->preprocessToDom( $input );
97
98 if ( method_exists( $dom, 'saveXML' ) ) {
99 // @phan-suppress-next-line PhanUndeclaredMethod
100 $xml = $dom->saveXML();
101 } else {
102 // @phan-suppress-next-line PhanUndeclaredMethod
103 $xml = $dom->__toString();
104 }
105 }
106
107 $output = $this->parser->preprocess( $input, $title, $options );
108 $this->makeForm();
109
110 $out = $this->getOutput();
111 if ( $generateXML && strlen( $output ) > 0 ) {
112 // @phan-suppress-next-line PhanPossiblyUndeclaredVariable xml is set when used
113 $out->addHTML( $this->makeOutput( $xml, 'expand_templates_xml_output' ) );
114 }
115
116 $tmp = $this->makeOutput( $output );
117
118 if ( $removeNowiki ) {
119 $tmp = preg_replace(
120 [ '_&lt;nowiki&gt;_', '_&lt;/nowiki&gt;_', '_&lt;nowiki */&gt;_' ],
121 '',
122 $tmp
123 );
124 }
125
126 $tmp = $this->tidy->tidy( $tmp );
127
128 $out->addHTML( $tmp );
129
130 $pout = $this->parser->parse( $output, $title, $options );
131 $rawhtml = $pout->getText( [ 'enableSectionEditLinks' => false ] );
132 if ( $generateRawHtml && strlen( $rawhtml ) > 0 ) {
133 // @phan-suppress-next-line SecurityCheck-DoubleEscaped Wanted here to display the html
134 $out->addHTML( $this->makeOutput( $rawhtml, 'expand_templates_html_output' ) );
135 }
136
137 $this->showHtmlPreview( $title, $pout, $out );
138 } else {
139 $this->makeForm();
140 }
141 }
142
151 public function onSubmitInput( array $values ) {
152 $status = Status::newGood();
153 if ( !strlen( $values['Input'] ) ) {
154 $status = Status::newFatal( 'expand_templates_input_missing' );
155 }
156 return $status;
157 }
158
162 private function makeForm() {
163 $fields = [
164 'ContextTitle' => [
165 'type' => 'text',
166 'label' => $this->msg( 'expand_templates_title' )->plain(),
167 'id' => 'contexttitle',
168 'size' => 60,
169 'autofocus' => true,
170 ],
171 'Input' => [
172 'type' => 'textarea',
173 'label' => $this->msg( 'expand_templates_input' )->text(),
174 'rows' => 10,
175 'id' => 'input',
176 'useeditfont' => true,
177 ],
178 'RemoveComments' => [
179 'type' => 'check',
180 'label' => $this->msg( 'expand_templates_remove_comments' )->text(),
181 'id' => 'removecomments',
182 'default' => true,
183 ],
184 'RemoveNowiki' => [
185 'type' => 'check',
186 'label' => $this->msg( 'expand_templates_remove_nowiki' )->text(),
187 'id' => 'removenowiki',
188 ],
189 'GenerateXml' => [
190 'type' => 'check',
191 'label' => $this->msg( 'expand_templates_generate_xml' )->text(),
192 'id' => 'generate_xml',
193 ],
194 'GenerateRawHtml' => [
195 'type' => 'check',
196 'label' => $this->msg( 'expand_templates_generate_rawhtml' )->text(),
197 'id' => 'generate_rawhtml',
198 ],
199 ];
200
201 $form = HTMLForm::factory( 'ooui', $fields, $this->getContext() );
202 $form
203 ->setSubmitTextMsg( 'expand_templates_ok' )
204 ->setWrapperLegendMsg( 'expandtemplates' )
205 ->setHeaderHtml( $this->msg( 'expand_templates_intro' )->parse() )
206 ->setSubmitCallback( [ $this, 'onSubmitInput' ] )
207 ->showAlways();
208 }
209
217 private function makeOutput( $output, $heading = 'expand_templates_output' ) {
218 $out = "<h2>" . $this->msg( $heading )->escaped() . "</h2>\n";
219 $out .= Xml::textarea(
220 'output',
221 $output,
222 10,
223 10,
224 [
225 'id' => 'output',
226 'readonly' => 'readonly',
227 'class' => 'mw-editfont-' . $this->userOptionsLookup->getOption( $this->getUser(), 'editfont' )
228 ]
229 );
230
231 return $out;
232 }
233
241 private function showHtmlPreview( Title $title, ParserOutput $pout, OutputPage $out ) {
242 $lang = $title->getPageViewLanguage();
243 $out->addHTML( "<h2>" . $this->msg( 'expand_templates_preview' )->escaped() . "</h2>\n" );
244
245 if ( $this->getConfig()->get( MainConfigNames::RawHtml ) ) {
246 $request = $this->getRequest();
247 $user = $this->getUser();
248
249 // To prevent cross-site scripting attacks, don't show the preview if raw HTML is
250 // allowed and a valid edit token is not provided (T73111). However, MediaWiki
251 // does not currently provide logged-out users with CSRF protection; in that case,
252 // do not show the preview unless anonymous editing is allowed.
253 if ( $user->isAnon() && !$this->getAuthority()->isAllowed( 'edit' ) ) {
254 $error = [ 'expand_templates_preview_fail_html_anon' ];
255 } elseif ( !$user->matchEditToken( $request->getVal( 'wpEditToken' ), '', $request ) ) {
256 $error = [ 'expand_templates_preview_fail_html' ];
257 } else {
258 $error = false;
259 }
260
261 if ( $error ) {
262 $out->addHTML(
263 Html::errorBox(
264 $out->msg( $error )->parse(),
265 '',
266 'previewnote'
267 )
268 );
269 return;
270 }
271 }
272
273 $out->addHTML( Html::openElement( 'div', [
274 'class' => 'mw-content-' . $lang->getDir(),
275 'dir' => $lang->getDir(),
276 'lang' => $lang->getHtmlCode(),
277 ] ) );
278 $out->addParserOutputContent( $pout, [ 'enableSectionEditLinks' => false ] );
279 $out->addHTML( Html::closeElement( 'div' ) );
280 $out->setCategoryLinks( $pout->getCategories() );
281 }
282
283 protected function getGroupName() {
284 return 'wiki';
285 }
286}
msg( $key,... $params)
Get a Message object with context set Parameters are the same as wfMessage()
This class is a collection of static functions that serve two purposes:
Definition Html.php:55
A class containing constants representing the names of configuration variables.
Base class for HTML cleanup utilities.
Represents a title within MediaWiki.
Definition Title.php:82
Provides access to user options.
This is one of the Core classes and should be read at least once by any new developers.
setCategoryLinks(array $categories)
Reset the category links (but not the category list) and add $categories.
addParserOutputContent(ParserOutput $parserOutput, $poOptions=[])
Add the HTML and enhancements for it (like ResourceLoader modules) associated with a ParserOutput obj...
addHTML( $text)
Append $text to the body HTML.
getText( $options=[])
Get the output HTML.
PHP Parser - Processes wiki markup (which uses a more user-friendly syntax, such as "[[link]]" for ma...
Definition Parser.php:107
const OT_PREPROCESS
Definition Parser.php:142
A special page that expands submitted templates, parser functions, and variables, allowing easier deb...
__construct(Parser $parser, UserOptionsLookup $userOptionsLookup, TidyDriverBase $tidy)
onSubmitInput(array $values)
Callback for the HTMLForm used in self::makeForm.
getGroupName()
Under which header this special page is listed in Special:SpecialPages See messages 'specialpages-gro...
execute( $subpage)
Show the special page.
Parent class for all special pages.
setHeaders()
Sets headers - this should be called from the execute() method of all derived classes!
getOutput()
Get the OutputPage being used for this instance.
getUser()
Shortcut to get the User executing this instance.
getContext()
Gets the context this SpecialPage is executed in.
msg( $key,... $params)
Wrapper around wfMessage that sets the current context.
getConfig()
Shortcut to get main config object.
getRequest()
Get the WebRequest being used for this instance.
getPageTitle( $subpage=false)
Get a self-referential title object.
addHelpLink( $to, $overrideBaseUrl=false)
Adds help link with an icon via page indicators.
getContentLanguage()
Shortcut to get content language.
static textarea( $name, $content, $cols=40, $rows=5, $attribs=[])
Shortcut for creating textareas.
Definition Xml.php:652
if(!isset( $args[0])) $lang