Add rate limiter to Special:ConfirmEmail
[lhc/web/wiklou.git] / includes / specials / SpecialExpandTemplates.php
1 <?php
2 /**
3 * Implements Special:ExpandTemplates
4 *
5 * This program is free software; you can redistribute it and/or modify
6 * it under the terms of the GNU General Public License as published by
7 * the Free Software Foundation; either version 2 of the License, or
8 * (at your option) any later version.
9 *
10 * This program is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 * GNU General Public License for more details.
14 *
15 * You should have received a copy of the GNU General Public License along
16 * with this program; if not, write to the Free Software Foundation, Inc.,
17 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
18 * http://www.gnu.org/copyleft/gpl.html
19 *
20 * @file
21 * @ingroup SpecialPage
22 */
23
24 use MediaWiki\MediaWikiServices;
25
26 /**
27 * A special page that expands submitted templates, parser functions,
28 * and variables, allowing easier debugging of these.
29 *
30 * @ingroup SpecialPage
31 */
32 class SpecialExpandTemplates extends SpecialPage {
33
34 /** @var bool Whether or not to show the XML parse tree */
35 protected $generateXML;
36
37 /** @var bool Whether or not to show the raw HTML code */
38 protected $generateRawHtml;
39
40 /** @var bool Whether or not to remove comments in the expanded wikitext */
41 protected $removeComments;
42
43 /** @var bool Whether or not to remove <nowiki> tags in the expanded wikitext */
44 protected $removeNowiki;
45
46 /** @var int Maximum size in bytes to include. 50MB allows fixing those huge pages */
47 const MAX_INCLUDE_SIZE = 50000000;
48
49 function __construct() {
50 parent::__construct( 'ExpandTemplates' );
51 }
52
53 /**
54 * Show the special page
55 * @param string|null $subpage
56 */
57 function execute( $subpage ) {
58 $this->setHeaders();
59 $this->addHelpLink( 'Help:ExpandTemplates' );
60
61 $request = $this->getRequest();
62 $titleStr = $request->getText( 'wpContextTitle' );
63 $title = Title::newFromText( $titleStr );
64
65 if ( !$title ) {
66 $title = $this->getPageTitle();
67 }
68 $input = $request->getText( 'wpInput' );
69 $this->generateXML = $request->getBool( 'wpGenerateXml' );
70 $this->generateRawHtml = $request->getBool( 'wpGenerateRawHtml' );
71
72 if ( strlen( $input ) ) {
73 $this->removeComments = $request->getBool( 'wpRemoveComments', false );
74 $this->removeNowiki = $request->getBool( 'wpRemoveNowiki', false );
75 $options = ParserOptions::newFromContext( $this->getContext() );
76 $options->setRemoveComments( $this->removeComments );
77 $options->setTidy( true );
78 $options->setMaxIncludeSize( self::MAX_INCLUDE_SIZE );
79
80 $parser = MediaWikiServices::getInstance()->getParser();
81 if ( $this->generateXML ) {
82 $parser->startExternalParse( $title, $options, Parser::OT_PREPROCESS );
83 $dom = $parser->preprocessToDom( $input );
84
85 if ( method_exists( $dom, 'saveXML' ) ) {
86 $xml = $dom->saveXML();
87 } else {
88 $xml = $dom->__toString();
89 }
90 }
91
92 $output = $parser->preprocess( $input, $title, $options );
93 } else {
94 $this->removeComments = $request->getBool( 'wpRemoveComments', true );
95 $this->removeNowiki = $request->getBool( 'wpRemoveNowiki', false );
96 $output = false;
97 }
98
99 $out = $this->getOutput();
100
101 $this->makeForm( $titleStr, $input );
102
103 if ( $output !== false ) {
104 if ( $this->generateXML && strlen( $output ) > 0 ) {
105 $out->addHTML( $this->makeOutput( $xml, 'expand_templates_xml_output' ) );
106 }
107
108 $tmp = $this->makeOutput( $output );
109
110 if ( $this->removeNowiki ) {
111 $tmp = preg_replace(
112 [ '_&lt;nowiki&gt;_', '_&lt;/nowiki&gt;_', '_&lt;nowiki */&gt;_' ],
113 '',
114 $tmp
115 );
116 }
117
118 $config = $this->getConfig();
119 if ( MWTidy::isEnabled() && $options->getTidy() ) {
120 $tmp = MWTidy::tidy( $tmp );
121 } else {
122 wfDeprecated( 'disabling tidy', '1.33' );
123 }
124
125 $out->addHTML( $tmp );
126
127 $pout = $this->generateHtml( $title, $output );
128 $rawhtml = $pout->getText();
129 if ( $this->generateRawHtml && strlen( $rawhtml ) > 0 ) {
130 $out->addHTML( $this->makeOutput( $rawhtml, 'expand_templates_html_output' ) );
131 }
132
133 $this->showHtmlPreview( $title, $pout, $out );
134 }
135 }
136
137 /**
138 * Callback for the HTMLForm used in self::makeForm.
139 * Checks, if the input was given, and if not, returns a fatal Status
140 * object with an error message.
141 *
142 * @param array $values The values submitted to the HTMLForm
143 * @return Status
144 */
145 public function onSubmitInput( array $values ) {
146 $status = Status::newGood();
147 if ( !strlen( $values['input'] ) ) {
148 $status = Status::newFatal( 'expand_templates_input_missing' );
149 }
150 return $status;
151 }
152
153 /**
154 * Generate a form allowing users to enter information
155 *
156 * @param string $title Value for context title field
157 * @param string $input Value for input textbox
158 */
159 private function makeForm( $title, $input ) {
160 $fields = [
161 'contexttitle' => [
162 'type' => 'text',
163 'label' => $this->msg( 'expand_templates_title' )->plain(),
164 'name' => 'wpContextTitle',
165 'id' => 'contexttitle',
166 'size' => 60,
167 'default' => $title,
168 'autofocus' => true,
169 ],
170 'input' => [
171 'type' => 'textarea',
172 'name' => 'wpInput',
173 'label' => $this->msg( 'expand_templates_input' )->text(),
174 'rows' => 10,
175 'default' => $input,
176 'id' => 'input',
177 'useeditfont' => true,
178 ],
179 'removecomments' => [
180 'type' => 'check',
181 'label' => $this->msg( 'expand_templates_remove_comments' )->text(),
182 'name' => 'wpRemoveComments',
183 'id' => 'removecomments',
184 'default' => $this->removeComments,
185 ],
186 'removenowiki' => [
187 'type' => 'check',
188 'label' => $this->msg( 'expand_templates_remove_nowiki' )->text(),
189 'name' => 'wpRemoveNowiki',
190 'id' => 'removenowiki',
191 'default' => $this->removeNowiki,
192 ],
193 'generate_xml' => [
194 'type' => 'check',
195 'label' => $this->msg( 'expand_templates_generate_xml' )->text(),
196 'name' => 'wpGenerateXml',
197 'id' => 'generate_xml',
198 'default' => $this->generateXML,
199 ],
200 'generate_rawhtml' => [
201 'type' => 'check',
202 'label' => $this->msg( 'expand_templates_generate_rawhtml' )->text(),
203 'name' => 'wpGenerateRawHtml',
204 'id' => 'generate_rawhtml',
205 'default' => $this->generateRawHtml,
206 ],
207 ];
208
209 $form = HTMLForm::factory( 'ooui', $fields, $this->getContext() );
210 $form
211 ->setSubmitTextMsg( 'expand_templates_ok' )
212 ->setWrapperLegendMsg( 'expandtemplates' )
213 ->setHeaderText( $this->msg( 'expand_templates_intro' )->parse() )
214 ->setSubmitCallback( [ $this, 'onSubmitInput' ] )
215 ->showAlways();
216 }
217
218 /**
219 * Generate a nice little box with a heading for output
220 *
221 * @param string $output Wiki text output
222 * @param string $heading
223 * @return string
224 */
225 private function makeOutput( $output, $heading = 'expand_templates_output' ) {
226 $out = "<h2>" . $this->msg( $heading )->escaped() . "</h2>\n";
227 $out .= Xml::textarea(
228 'output',
229 $output,
230 10,
231 10,
232 [
233 'id' => 'output',
234 'readonly' => 'readonly',
235 'class' => 'mw-editfont-' . $this->getUser()->getOption( 'editfont' )
236 ]
237 );
238
239 return $out;
240 }
241
242 /**
243 * Renders the supplied wikitext as html
244 *
245 * @param Title $title
246 * @param string $text
247 * @return ParserOutput
248 */
249 private function generateHtml( Title $title, $text ) {
250 $popts = ParserOptions::newFromContext( $this->getContext() );
251 $popts->setTargetLanguage( $title->getPageLanguage() );
252 return MediaWikiServices::getInstance()->getParser()->parse( $text, $title, $popts );
253 }
254
255 /**
256 * Wraps the provided html code in a div and outputs it to the page
257 *
258 * @param Title $title
259 * @param ParserOutput $pout
260 * @param OutputPage $out
261 */
262 private function showHtmlPreview( Title $title, ParserOutput $pout, OutputPage $out ) {
263 $lang = $title->getPageViewLanguage();
264 $out->addHTML( "<h2>" . $this->msg( 'expand_templates_preview' )->escaped() . "</h2>\n" );
265
266 if ( $this->getConfig()->get( 'RawHtml' ) ) {
267 $request = $this->getRequest();
268 $user = $this->getUser();
269
270 // To prevent cross-site scripting attacks, don't show the preview if raw HTML is
271 // allowed and a valid edit token is not provided (T73111). However, MediaWiki
272 // does not currently provide logged-out users with CSRF protection; in that case,
273 // do not show the preview unless anonymous editing is allowed.
274 if ( $user->isAnon() && !$user->isAllowed( 'edit' ) ) {
275 $error = [ 'expand_templates_preview_fail_html_anon' ];
276 } elseif ( !$user->matchEditToken( $request->getVal( 'wpEditToken' ), '', $request ) ) {
277 $error = [ 'expand_templates_preview_fail_html' ];
278 } else {
279 $error = false;
280 }
281
282 if ( $error ) {
283 $out->wrapWikiMsg( "<div class='previewnote'>\n$1\n</div>", $error );
284 return;
285 }
286 }
287
288 $out->addHTML( Html::openElement( 'div', [
289 'class' => 'mw-content-' . $lang->getDir(),
290 'dir' => $lang->getDir(),
291 'lang' => $lang->getHtmlCode(),
292 ] ) );
293 $out->addParserOutputContent( $pout );
294 $out->addHTML( Html::closeElement( 'div' ) );
295 $out->setCategoryLinks( $pout->getCategories() );
296 }
297
298 protected function getGroupName() {
299 return 'wiki';
300 }
301 }