Merge "Fix sessionfailure i18n message during authentication"
[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 /**
25 * A special page that expands submitted templates, parser functions,
26 * and variables, allowing easier debugging of these.
27 *
28 * @ingroup SpecialPage
29 */
30 class SpecialExpandTemplates extends SpecialPage {
31
32 /** @var bool Whether or not to show the XML parse tree */
33 protected $generateXML;
34
35 /** @var bool Whether or not to show the raw HTML code */
36 protected $generateRawHtml;
37
38 /** @var bool Whether or not to remove comments in the expanded wikitext */
39 protected $removeComments;
40
41 /** @var bool Whether or not to remove <nowiki> tags in the expanded wikitext */
42 protected $removeNowiki;
43
44 /** @var int Maximum size in bytes to include. 50MB allows fixing those huge pages */
45 const MAX_INCLUDE_SIZE = 50000000;
46
47 function __construct() {
48 parent::__construct( 'ExpandTemplates' );
49 }
50
51 /**
52 * Show the special page
53 * @param string|null $subpage
54 */
55 function execute( $subpage ) {
56 global $wgParser;
57
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 if ( $this->generateXML ) {
81 $wgParser->startExternalParse( $title, $options, Parser::OT_PREPROCESS );
82 $dom = $wgParser->preprocessToDom( $input );
83
84 if ( method_exists( $dom, 'saveXML' ) ) {
85 $xml = $dom->saveXML();
86 } else {
87 $xml = $dom->__toString();
88 }
89 }
90
91 $output = $wgParser->preprocess( $input, $title, $options );
92 } else {
93 $this->removeComments = $request->getBool( 'wpRemoveComments', true );
94 $this->removeNowiki = $request->getBool( 'wpRemoveNowiki', false );
95 $output = false;
96 }
97
98 $out = $this->getOutput();
99
100 $this->makeForm( $titleStr, $input );
101
102 if ( $output !== false ) {
103 if ( $this->generateXML && strlen( $output ) > 0 ) {
104 $out->addHTML( $this->makeOutput( $xml, 'expand_templates_xml_output' ) );
105 }
106
107 $tmp = $this->makeOutput( $output );
108
109 if ( $this->removeNowiki ) {
110 $tmp = preg_replace(
111 [ '_&lt;nowiki&gt;_', '_&lt;/nowiki&gt;_', '_&lt;nowiki */&gt;_' ],
112 '',
113 $tmp
114 );
115 }
116
117 $config = $this->getConfig();
118 if ( $config->get( 'UseTidy' ) && $options->getTidy() ) {
119 $tmp = MWTidy::tidy( $tmp );
120 }
121
122 $out->addHTML( $tmp );
123
124 $pout = $this->generateHtml( $title, $output );
125 $rawhtml = $pout->getText();
126 if ( $this->generateRawHtml && strlen( $rawhtml ) > 0 ) {
127 $out->addHTML( $this->makeOutput( $rawhtml, 'expand_templates_html_output' ) );
128 }
129
130 $this->showHtmlPreview( $title, $pout, $out );
131 }
132 }
133
134 /**
135 * Callback for the HTMLForm used in self::makeForm.
136 * Checks, if the input was given, and if not, returns a fatal Status
137 * object with an error message.
138 *
139 * @param array $values The values submitted to the HTMLForm
140 * @return Status
141 */
142 public function onSubmitInput( array $values ) {
143 $status = Status::newGood();
144 if ( !strlen( $values['input'] ) ) {
145 $status = Status::newFatal( 'expand_templates_input_missing' );
146 }
147 return $status;
148 }
149
150 /**
151 * Generate a form allowing users to enter information
152 *
153 * @param string $title Value for context title field
154 * @param string $input Value for input textbox
155 * @return string
156 */
157 private function makeForm( $title, $input ) {
158 $fields = [
159 'contexttitle' => [
160 'type' => 'text',
161 'label' => $this->msg( 'expand_templates_title' )->plain(),
162 'name' => 'wpContextTitle',
163 'id' => 'contexttitle',
164 'size' => 60,
165 'default' => $title,
166 'autofocus' => true,
167 ],
168 'input' => [
169 'type' => 'textarea',
170 'name' => 'wpInput',
171 'label' => $this->msg( 'expand_templates_input' )->text(),
172 'rows' => 10,
173 'default' => $input,
174 'id' => 'input',
175 'useeditfont' => true,
176 ],
177 'removecomments' => [
178 'type' => 'check',
179 'label' => $this->msg( 'expand_templates_remove_comments' )->text(),
180 'name' => 'wpRemoveComments',
181 'id' => 'removecomments',
182 'default' => $this->removeComments,
183 ],
184 'removenowiki' => [
185 'type' => 'check',
186 'label' => $this->msg( 'expand_templates_remove_nowiki' )->text(),
187 'name' => 'wpRemoveNowiki',
188 'id' => 'removenowiki',
189 'default' => $this->removeNowiki,
190 ],
191 'generate_xml' => [
192 'type' => 'check',
193 'label' => $this->msg( 'expand_templates_generate_xml' )->text(),
194 'name' => 'wpGenerateXml',
195 'id' => 'generate_xml',
196 'default' => $this->generateXML,
197 ],
198 'generate_rawhtml' => [
199 'type' => 'check',
200 'label' => $this->msg( 'expand_templates_generate_rawhtml' )->text(),
201 'name' => 'wpGenerateRawHtml',
202 'id' => 'generate_rawhtml',
203 'default' => $this->generateRawHtml,
204 ],
205 ];
206
207 $form = HTMLForm::factory( 'ooui', $fields, $this->getContext() );
208 $form
209 ->setSubmitTextMsg( 'expand_templates_ok' )
210 ->setWrapperLegendMsg( 'expandtemplates' )
211 ->setHeaderText( $this->msg( 'expand_templates_intro' )->parse() )
212 ->setSubmitCallback( [ $this, 'onSubmitInput' ] )
213 ->showAlways();
214 }
215
216 /**
217 * Generate a nice little box with a heading for output
218 *
219 * @param string $output Wiki text output
220 * @param string $heading
221 * @return string
222 */
223 private function makeOutput( $output, $heading = 'expand_templates_output' ) {
224 $out = "<h2>" . $this->msg( $heading )->escaped() . "</h2>\n";
225 $out .= Xml::textarea(
226 'output',
227 $output,
228 10,
229 10,
230 [
231 'id' => 'output',
232 'readonly' => 'readonly',
233 'class' => 'mw-editfont-' . $this->getUser()->getOption( 'editfont' )
234 ]
235 );
236
237 return $out;
238 }
239
240 /**
241 * Renders the supplied wikitext as html
242 *
243 * @param Title $title
244 * @param string $text
245 * @return ParserOutput
246 */
247 private function generateHtml( Title $title, $text ) {
248 global $wgParser;
249
250 $popts = ParserOptions::newFromContext( $this->getContext() );
251 $popts->setTargetLanguage( $title->getPageLanguage() );
252 return $wgParser->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 }