3 * Implements Special:ExpandTemplates
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.
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.
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
21 * @ingroup SpecialPage
25 * A special page that expands submitted templates, parser functions,
26 * and variables, allowing easier debugging of these.
28 * @ingroup SpecialPage
30 class SpecialExpandTemplates
extends SpecialPage
{
32 /** @var bool Whether or not to show the XML parse tree */
33 protected $generateXML;
35 /** @var bool Whether or not to show the raw HTML code */
36 protected $generateRawHtml;
38 /** @var bool Whether or not to remove comments in the expanded wikitext */
39 protected $removeComments;
41 /** @var bool Whether or not to remove <nowiki> tags in the expanded wikitext */
42 protected $removeNowiki;
44 /** @var int Maximum size in bytes to include. 50MB allows fixing those huge pages */
45 const MAX_INCLUDE_SIZE
= 50000000;
47 function __construct() {
48 parent
::__construct( 'ExpandTemplates' );
52 * Show the special page
53 * @param string|null $subpage
55 function execute( $subpage ) {
60 $request = $this->getRequest();
61 $titleStr = $request->getText( 'wpContextTitle' );
62 $title = Title
::newFromText( $titleStr );
65 $title = $this->getPageTitle();
67 $input = $request->getText( 'wpInput' );
68 $this->generateXML
= $request->getBool( 'wpGenerateXml' );
69 $this->generateRawHtml
= $request->getBool( 'wpGenerateRawHtml' );
71 if ( strlen( $input ) ) {
72 $this->removeComments
= $request->getBool( 'wpRemoveComments', false );
73 $this->removeNowiki
= $request->getBool( 'wpRemoveNowiki', false );
74 $options = ParserOptions
::newFromContext( $this->getContext() );
75 $options->setRemoveComments( $this->removeComments
);
76 $options->setTidy( true );
77 $options->setMaxIncludeSize( self
::MAX_INCLUDE_SIZE
);
79 if ( $this->generateXML
) {
80 $wgParser->startExternalParse( $title, $options, Parser
::OT_PREPROCESS
);
81 $dom = $wgParser->preprocessToDom( $input );
83 if ( method_exists( $dom, 'saveXML' ) ) {
84 $xml = $dom->saveXML();
86 $xml = $dom->__toString();
90 $output = $wgParser->preprocess( $input, $title, $options );
92 $this->removeComments
= $request->getBool( 'wpRemoveComments', true );
93 $this->removeNowiki
= $request->getBool( 'wpRemoveNowiki', false );
97 $out = $this->getOutput();
99 $this->makeForm( $titleStr, $input );
101 if ( $output !== false ) {
102 if ( $this->generateXML
&& strlen( $output ) > 0 ) {
103 $out->addHTML( $this->makeOutput( $xml, 'expand_templates_xml_output' ) );
106 $tmp = $this->makeOutput( $output );
108 if ( $this->removeNowiki
) {
110 [ '_<nowiki>_', '_</nowiki>_', '_<nowiki */>_' ],
116 $config = $this->getConfig();
117 if ( $config->get( 'UseTidy' ) && $options->getTidy() ) {
118 $tmp = MWTidy
::tidy( $tmp );
121 $out->addHTML( $tmp );
123 $pout = $this->generateHtml( $title, $output );
124 $rawhtml = $pout->getText();
125 if ( $this->generateRawHtml
&& strlen( $rawhtml ) > 0 ) {
126 $out->addHTML( $this->makeOutput( $rawhtml, 'expand_templates_html_output' ) );
129 $this->showHtmlPreview( $title, $pout, $out );
134 * Callback for the HTMLForm used in self::makeForm.
135 * Checks, if the input was given, and if not, returns a fatal Status
136 * object with an error message.
138 * @param array $values The values submitted to the HTMLForm
141 public function onSubmitInput( array $values ) {
142 $status = Status
::newGood();
143 if ( !strlen( $values['input'] ) ) {
144 $status = Status
::newFatal( 'expand_templates_input_missing' );
150 * Generate a form allowing users to enter information
152 * @param string $title Value for context title field
153 * @param string $input Value for input textbox
156 private function makeForm( $title, $input ) {
160 'label' => $this->msg( 'expand_templates_title' )->plain(),
161 'name' => 'wpContextTitle',
162 'id' => 'contexttitle',
166 'cssclass' => 'mw-ui-input-inline',
169 'type' => 'textarea',
171 'label' => $this->msg( 'expand_templates_input' )->text(),
175 // The following classes can be used here:
176 // * mw-editfont-monospace
177 // * mw-editfont-sans-serif
178 // * mw-editfont-serif
179 'cssclass' => 'mw-editfont-' . $this->getUser()->getOption( 'editfont' ),
181 'removecomments' => [
183 'label' => $this->msg( 'expand_templates_remove_comments' )->text(),
184 'name' => 'wpRemoveComments',
185 'id' => 'removecomments',
186 'default' => $this->removeComments
,
190 'label' => $this->msg( 'expand_templates_remove_nowiki' )->text(),
191 'name' => 'wpRemoveNowiki',
192 'id' => 'removenowiki',
193 'default' => $this->removeNowiki
,
197 'label' => $this->msg( 'expand_templates_generate_xml' )->text(),
198 'name' => 'wpGenerateXml',
199 'id' => 'generate_xml',
200 'default' => $this->generateXML
,
202 'generate_rawhtml' => [
204 'label' => $this->msg( 'expand_templates_generate_rawhtml' )->text(),
205 'name' => 'wpGenerateRawHtml',
206 'id' => 'generate_rawhtml',
207 'default' => $this->generateRawHtml
,
211 $this->getOutput()->addModuleStyles( 'mediawiki.editfont.styles' );
213 $form = HTMLForm
::factory( 'ooui', $fields, $this->getContext() );
215 ->setSubmitTextMsg( 'expand_templates_ok' )
216 ->setWrapperLegendMsg( 'expandtemplates' )
217 ->setHeaderText( $this->msg( 'expand_templates_intro' )->parse() )
218 ->setSubmitCallback( [ $this, 'onSubmitInput' ] )
223 * Generate a nice little box with a heading for output
225 * @param string $output Wiki text output
226 * @param string $heading
229 private function makeOutput( $output, $heading = 'expand_templates_output' ) {
230 $out = "<h2>" . $this->msg( $heading )->escaped() . "</h2>\n";
231 $out .= Xml
::textarea(
238 'readonly' => 'readonly',
239 'class' => 'mw-editfont-' . $this->getUser()->getOption( 'editfont' )
247 * Renders the supplied wikitext as html
249 * @param Title $title
250 * @param string $text
251 * @return ParserOutput
253 private function generateHtml( Title
$title, $text ) {
256 $popts = ParserOptions
::newFromContext( $this->getContext() );
257 $popts->setTargetLanguage( $title->getPageLanguage() );
258 return $wgParser->parse( $text, $title, $popts );
262 * Wraps the provided html code in a div and outputs it to the page
264 * @param Title $title
265 * @param ParserOutput $pout
266 * @param OutputPage $out
268 private function showHtmlPreview( Title
$title, ParserOutput
$pout, OutputPage
$out ) {
269 $lang = $title->getPageViewLanguage();
270 $out->addHTML( "<h2>" . $this->msg( 'expand_templates_preview' )->escaped() . "</h2>\n" );
272 if ( $this->getConfig()->get( 'RawHtml' ) ) {
273 $request = $this->getRequest();
274 $user = $this->getUser();
276 // To prevent cross-site scripting attacks, don't show the preview if raw HTML is
277 // allowed and a valid edit token is not provided (T73111). However, MediaWiki
278 // does not currently provide logged-out users with CSRF protection; in that case,
279 // do not show the preview unless anonymous editing is allowed.
280 if ( $user->isAnon() && !$user->isAllowed( 'edit' ) ) {
281 $error = [ 'expand_templates_preview_fail_html_anon' ];
282 } elseif ( !$user->matchEditToken( $request->getVal( 'wpEditToken' ), '', $request ) ) {
283 $error = [ 'expand_templates_preview_fail_html' ];
289 $out->wrapWikiMsg( "<div class='previewnote'>\n$1\n</div>", $error );
294 $out->addHTML( Html
::openElement( 'div', [
295 'class' => 'mw-content-' . $lang->getDir(),
296 'dir' => $lang->getDir(),
297 'lang' => $lang->getHtmlCode(),
299 $out->addParserOutputContent( $pout );
300 $out->addHTML( Html
::closeElement( 'div' ) );
301 $out->setCategoryLinks( $pout->getCategories() );
304 protected function getGroupName() {