Merge "Improve docs for Title::getInternalURL/getCanonicalURL"
[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 ( MWTidy::isEnabled() && $options->getTidy() ) {
119 $tmp = MWTidy::tidy( $tmp );
120 } else {
121 wfDeprecated( 'disabling tidy', '1.33' );
122 }
123
124 $out->addHTML( $tmp );
125
126 $pout = $this->generateHtml( $title, $output );
127 $rawhtml = $pout->getText();
128 if ( $this->generateRawHtml && strlen( $rawhtml ) > 0 ) {
129 $out->addHTML( $this->makeOutput( $rawhtml, 'expand_templates_html_output' ) );
130 }
131
132 $this->showHtmlPreview( $title, $pout, $out );
133 }
134 }
135
136 /**
137 * Callback for the HTMLForm used in self::makeForm.
138 * Checks, if the input was given, and if not, returns a fatal Status
139 * object with an error message.
140 *
141 * @param array $values The values submitted to the HTMLForm
142 * @return Status
143 */
144 public function onSubmitInput( array $values ) {
145 $status = Status::newGood();
146 if ( !strlen( $values['input'] ) ) {
147 $status = Status::newFatal( 'expand_templates_input_missing' );
148 }
149 return $status;
150 }
151
152 /**
153 * Generate a form allowing users to enter information
154 *
155 * @param string $title Value for context title field
156 * @param string $input Value for input textbox
157 */
158 private function makeForm( $title, $input ) {
159 $fields = [
160 'contexttitle' => [
161 'type' => 'text',
162 'label' => $this->msg( 'expand_templates_title' )->plain(),
163 'name' => 'wpContextTitle',
164 'id' => 'contexttitle',
165 'size' => 60,
166 'default' => $title,
167 'autofocus' => true,
168 ],
169 'input' => [
170 'type' => 'textarea',
171 'name' => 'wpInput',
172 'label' => $this->msg( 'expand_templates_input' )->text(),
173 'rows' => 10,
174 'default' => $input,
175 'id' => 'input',
176 'useeditfont' => true,
177 ],
178 'removecomments' => [
179 'type' => 'check',
180 'label' => $this->msg( 'expand_templates_remove_comments' )->text(),
181 'name' => 'wpRemoveComments',
182 'id' => 'removecomments',
183 'default' => $this->removeComments,
184 ],
185 'removenowiki' => [
186 'type' => 'check',
187 'label' => $this->msg( 'expand_templates_remove_nowiki' )->text(),
188 'name' => 'wpRemoveNowiki',
189 'id' => 'removenowiki',
190 'default' => $this->removeNowiki,
191 ],
192 'generate_xml' => [
193 'type' => 'check',
194 'label' => $this->msg( 'expand_templates_generate_xml' )->text(),
195 'name' => 'wpGenerateXml',
196 'id' => 'generate_xml',
197 'default' => $this->generateXML,
198 ],
199 'generate_rawhtml' => [
200 'type' => 'check',
201 'label' => $this->msg( 'expand_templates_generate_rawhtml' )->text(),
202 'name' => 'wpGenerateRawHtml',
203 'id' => 'generate_rawhtml',
204 'default' => $this->generateRawHtml,
205 ],
206 ];
207
208 $form = HTMLForm::factory( 'ooui', $fields, $this->getContext() );
209 $form
210 ->setSubmitTextMsg( 'expand_templates_ok' )
211 ->setWrapperLegendMsg( 'expandtemplates' )
212 ->setHeaderText( $this->msg( 'expand_templates_intro' )->parse() )
213 ->setSubmitCallback( [ $this, 'onSubmitInput' ] )
214 ->showAlways();
215 }
216
217 /**
218 * Generate a nice little box with a heading for output
219 *
220 * @param string $output Wiki text output
221 * @param string $heading
222 * @return string
223 */
224 private function makeOutput( $output, $heading = 'expand_templates_output' ) {
225 $out = "<h2>" . $this->msg( $heading )->escaped() . "</h2>\n";
226 $out .= Xml::textarea(
227 'output',
228 $output,
229 10,
230 10,
231 [
232 'id' => 'output',
233 'readonly' => 'readonly',
234 'class' => 'mw-editfont-' . $this->getUser()->getOption( 'editfont' )
235 ]
236 );
237
238 return $out;
239 }
240
241 /**
242 * Renders the supplied wikitext as html
243 *
244 * @param Title $title
245 * @param string $text
246 * @return ParserOutput
247 */
248 private function generateHtml( Title $title, $text ) {
249 global $wgParser;
250
251 $popts = ParserOptions::newFromContext( $this->getContext() );
252 $popts->setTargetLanguage( $title->getPageLanguage() );
253 return $wgParser->parse( $text, $title, $popts );
254 }
255
256 /**
257 * Wraps the provided html code in a div and outputs it to the page
258 *
259 * @param Title $title
260 * @param ParserOutput $pout
261 * @param OutputPage $out
262 */
263 private function showHtmlPreview( Title $title, ParserOutput $pout, OutputPage $out ) {
264 $lang = $title->getPageViewLanguage();
265 $out->addHTML( "<h2>" . $this->msg( 'expand_templates_preview' )->escaped() . "</h2>\n" );
266
267 if ( $this->getConfig()->get( 'RawHtml' ) ) {
268 $request = $this->getRequest();
269 $user = $this->getUser();
270
271 // To prevent cross-site scripting attacks, don't show the preview if raw HTML is
272 // allowed and a valid edit token is not provided (T73111). However, MediaWiki
273 // does not currently provide logged-out users with CSRF protection; in that case,
274 // do not show the preview unless anonymous editing is allowed.
275 if ( $user->isAnon() && !$user->isAllowed( 'edit' ) ) {
276 $error = [ 'expand_templates_preview_fail_html_anon' ];
277 } elseif ( !$user->matchEditToken( $request->getVal( 'wpEditToken' ), '', $request ) ) {
278 $error = [ 'expand_templates_preview_fail_html' ];
279 } else {
280 $error = false;
281 }
282
283 if ( $error ) {
284 $out->wrapWikiMsg( "<div class='previewnote'>\n$1\n</div>", $error );
285 return;
286 }
287 }
288
289 $out->addHTML( Html::openElement( 'div', [
290 'class' => 'mw-content-' . $lang->getDir(),
291 'dir' => $lang->getDir(),
292 'lang' => $lang->getHtmlCode(),
293 ] ) );
294 $out->addParserOutputContent( $pout );
295 $out->addHTML( Html::closeElement( 'div' ) );
296 $out->setCategoryLinks( $pout->getCategories() );
297 }
298
299 protected function getGroupName() {
300 return 'wiki';
301 }
302 }