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
24 use MediaWiki\MediaWikiServices
;
27 * A special page that expands submitted templates, parser functions,
28 * and variables, allowing easier debugging of these.
30 * @ingroup SpecialPage
32 class SpecialExpandTemplates
extends SpecialPage
{
34 /** @var bool Whether or not to show the XML parse tree */
35 protected $generateXML;
37 /** @var bool Whether or not to show the raw HTML code */
38 protected $generateRawHtml;
40 /** @var bool Whether or not to remove comments in the expanded wikitext */
41 protected $removeComments;
43 /** @var bool Whether or not to remove <nowiki> tags in the expanded wikitext */
44 protected $removeNowiki;
46 /** @var int Maximum size in bytes to include. 50MB allows fixing those huge pages */
47 const MAX_INCLUDE_SIZE
= 50000000;
49 function __construct() {
50 parent
::__construct( 'ExpandTemplates' );
54 * Show the special page
55 * @param string|null $subpage
57 function execute( $subpage ) {
59 $this->addHelpLink( 'Help:ExpandTemplates' );
61 $request = $this->getRequest();
62 $titleStr = $request->getText( 'wpContextTitle' );
63 $title = Title
::newFromText( $titleStr );
66 $title = $this->getPageTitle();
68 $input = $request->getText( 'wpInput' );
69 $this->generateXML
= $request->getBool( 'wpGenerateXml' );
70 $this->generateRawHtml
= $request->getBool( 'wpGenerateRawHtml' );
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
);
80 $parser = MediaWikiServices
::getInstance()->getParser();
81 if ( $this->generateXML
) {
82 $parser->startExternalParse( $title, $options, Parser
::OT_PREPROCESS
);
83 $dom = $parser->preprocessToDom( $input );
85 if ( method_exists( $dom, 'saveXML' ) ) {
86 // @phan-suppress-next-line PhanUndeclaredMethod
87 $xml = $dom->saveXML();
89 // @phan-suppress-next-line PhanUndeclaredMethod
90 $xml = $dom->__toString();
94 $output = $parser->preprocess( $input, $title, $options );
96 $this->removeComments
= $request->getBool( 'wpRemoveComments', true );
97 $this->removeNowiki
= $request->getBool( 'wpRemoveNowiki', false );
101 $out = $this->getOutput();
103 $this->makeForm( $titleStr, $input );
105 if ( $output !== false ) {
106 if ( $this->generateXML
&& strlen( $output ) > 0 ) {
107 $out->addHTML( $this->makeOutput( $xml, 'expand_templates_xml_output' ) );
110 $tmp = $this->makeOutput( $output );
112 if ( $this->removeNowiki
) {
114 [ '_<nowiki>_', '_</nowiki>_', '_<nowiki */>_' ],
120 $config = $this->getConfig();
121 if ( MWTidy
::isEnabled() && $options->getTidy() ) {
122 $tmp = MWTidy
::tidy( $tmp );
124 wfDeprecated( 'disabling tidy', '1.33' );
127 $out->addHTML( $tmp );
129 $pout = $this->generateHtml( $title, $output );
130 $rawhtml = $pout->getText();
131 if ( $this->generateRawHtml
&& strlen( $rawhtml ) > 0 ) {
132 $out->addHTML( $this->makeOutput( $rawhtml, 'expand_templates_html_output' ) );
135 $this->showHtmlPreview( $title, $pout, $out );
140 * Callback for the HTMLForm used in self::makeForm.
141 * Checks, if the input was given, and if not, returns a fatal Status
142 * object with an error message.
144 * @param array $values The values submitted to the HTMLForm
147 public function onSubmitInput( array $values ) {
148 $status = Status
::newGood();
149 if ( !strlen( $values['input'] ) ) {
150 $status = Status
::newFatal( 'expand_templates_input_missing' );
156 * Generate a form allowing users to enter information
158 * @param string $title Value for context title field
159 * @param string $input Value for input textbox
161 private function makeForm( $title, $input ) {
165 'label' => $this->msg( 'expand_templates_title' )->plain(),
166 'name' => 'wpContextTitle',
167 'id' => 'contexttitle',
173 'type' => 'textarea',
175 'label' => $this->msg( 'expand_templates_input' )->text(),
179 'useeditfont' => true,
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 $form = HTMLForm
::factory( 'ooui', $fields, $this->getContext() );
213 ->setSubmitTextMsg( 'expand_templates_ok' )
214 ->setWrapperLegendMsg( 'expandtemplates' )
215 ->setHeaderText( $this->msg( 'expand_templates_intro' )->parse() )
216 ->setSubmitCallback( [ $this, 'onSubmitInput' ] )
221 * Generate a nice little box with a heading for output
223 * @param string $output Wiki text output
224 * @param string $heading
227 private function makeOutput( $output, $heading = 'expand_templates_output' ) {
228 $out = "<h2>" . $this->msg( $heading )->escaped() . "</h2>\n";
229 $out .= Xml
::textarea(
236 'readonly' => 'readonly',
237 'class' => 'mw-editfont-' . $this->getUser()->getOption( 'editfont' )
245 * Renders the supplied wikitext as html
247 * @param Title $title
248 * @param string $text
249 * @return ParserOutput
251 private function generateHtml( Title
$title, $text ) {
252 $popts = ParserOptions
::newFromContext( $this->getContext() );
253 $popts->setTargetLanguage( $title->getPageLanguage() );
254 return MediaWikiServices
::getInstance()->getParser()->parse( $text, $title, $popts );
258 * Wraps the provided html code in a div and outputs it to the page
260 * @param Title $title
261 * @param ParserOutput $pout
262 * @param OutputPage $out
264 private function showHtmlPreview( Title
$title, ParserOutput
$pout, OutputPage
$out ) {
265 $lang = $title->getPageViewLanguage();
266 $out->addHTML( "<h2>" . $this->msg( 'expand_templates_preview' )->escaped() . "</h2>\n" );
268 if ( $this->getConfig()->get( 'RawHtml' ) ) {
269 $request = $this->getRequest();
270 $user = $this->getUser();
272 // To prevent cross-site scripting attacks, don't show the preview if raw HTML is
273 // allowed and a valid edit token is not provided (T73111). However, MediaWiki
274 // does not currently provide logged-out users with CSRF protection; in that case,
275 // do not show the preview unless anonymous editing is allowed.
276 if ( $user->isAnon() && !MediaWikiServices
::getInstance()
277 ->getPermissionManager()
278 ->userHasRight( $user, 'edit' )
280 $error = [ 'expand_templates_preview_fail_html_anon' ];
281 } elseif ( !$user->matchEditToken( $request->getVal( 'wpEditToken' ), '', $request ) ) {
282 $error = [ 'expand_templates_preview_fail_html' ];
288 $out->wrapWikiMsg( "<div class='previewnote errorbox'>\n$1\n</div>", $error );
293 $out->addHTML( Html
::openElement( 'div', [
294 'class' => 'mw-content-' . $lang->getDir(),
295 'dir' => $lang->getDir(),
296 'lang' => $lang->getHtmlCode(),
298 $out->addParserOutputContent( $pout );
299 $out->addHTML( Html
::closeElement( 'div' ) );
300 $out->setCategoryLinks( $pout->getCategories() );
303 protected function getGroupName() {