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 boolean whether or not to show the XML parse tree */
33 protected $generateXML;
35 /** @var boolean whether or not to show the raw HTML code */
36 protected $generateRawHtml;
38 /** @var boolean whether or not to remove comments in the expanded wikitext */
39 protected $removeComments;
41 /** @var boolean whether or not to remove <nowiki> tags in the expanded wikitext */
42 protected $removeNowiki;
44 /** @var 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
54 function execute( $subpage ) {
55 global $wgParser, $wgUseTidy, $wgAlwaysUseTidy;
59 $request = $this->getRequest();
60 $titleStr = $request->getText( 'wpContextTitle' );
61 $title = Title
::newFromText( $titleStr );
64 $title = $this->getTitle();
66 $input = $request->getText( 'wpInput' );
67 $this->generateXML
= $request->getBool( 'wpGenerateXml' );
68 $this->generateRawHtml
= $request->getBool( 'wpGenerateRawHtml' );
70 if ( strlen( $input ) ) {
71 $this->removeComments
= $request->getBool( 'wpRemoveComments', false );
72 $this->removeNowiki
= $request->getBool( 'wpRemoveNowiki', false );
73 $options = ParserOptions
::newFromContext( $this->getContext() );
74 $options->setRemoveComments( $this->removeComments
);
75 $options->setTidy( true );
76 $options->setMaxIncludeSize( self
::MAX_INCLUDE_SIZE
);
78 if ( $this->generateXML
) {
79 $wgParser->startExternalParse( $title, $options, OT_PREPROCESS
);
80 $dom = $wgParser->preprocessToDom( $input );
82 if ( method_exists( $dom, 'saveXML' ) ) {
83 $xml = $dom->saveXML();
85 $xml = $dom->__toString();
89 $output = $wgParser->preprocess( $input, $title, $options );
91 $this->removeComments
= $request->getBool( 'wpRemoveComments', true );
92 $this->removeNowiki
= $request->getBool( 'wpRemoveNowiki', false );
96 $out = $this->getOutput();
97 $out->addWikiMsg( 'expand_templates_intro' );
98 $out->addHTML( $this->makeForm( $titleStr, $input ) );
100 if ( $output !== false ) {
101 if ( $this->generateXML
&& strlen( $output ) > 0 ) {
102 $out->addHTML( $this->makeOutput( $xml, 'expand_templates_xml_output' ) );
105 $tmp = $this->makeOutput( $output );
107 if ( $this->removeNowiki
) {
109 array( '_<nowiki>_', '_</nowiki>_', '_<nowiki */>_' ),
115 if ( ( $wgUseTidy && $options->getTidy() ) ||
$wgAlwaysUseTidy ) {
116 $tmp = MWTidy
::tidy( $tmp );
119 $out->addHTML( $tmp );
121 $rawhtml = $this->generateHtml( $title, $output );
123 if ( $this->generateRawHtml
&& strlen( $rawhtml ) > 0 ) {
124 $out->addHTML( $this->makeOutput( $rawhtml, 'expand_templates_html_output' ) );
127 $this->showHtmlPreview( $title, $rawhtml, $out );
134 * Generate a form allowing users to enter information
136 * @param string $title Value for context title field
137 * @param string $input Value for input textbox
140 private function makeForm( $title, $input ) {
141 $self = $this->getTitle();
142 $form = Xml
::openElement(
144 array( 'method' => 'post', 'action' => $self->getLocalUrl() )
146 $form .= "<fieldset><legend>" . $this->msg( 'expandtemplates' )->escaped() . "</legend>\n";
148 $form .= '<p>' . Xml
::inputLabel(
149 $this->msg( 'expand_templates_title' )->plain(),
154 array( 'autofocus' => true )
156 $form .= '<p>' . Xml
::label(
157 $this->msg( 'expand_templates_input' )->text(),
160 $form .= Xml
::textarea(
165 array( 'id' => 'input' )
168 $form .= '<p>' . Xml
::checkLabel(
169 $this->msg( 'expand_templates_remove_comments' )->text(),
172 $this->removeComments
174 $form .= '<p>' . Xml
::checkLabel(
175 $this->msg( 'expand_templates_remove_nowiki' )->text(),
180 $form .= '<p>' . Xml
::checkLabel(
181 $this->msg( 'expand_templates_generate_xml' )->text(),
186 $form .= '<p>' . Xml
::checkLabel(
187 $this->msg( 'expand_templates_generate_rawhtml' )->text(),
190 $this->generateRawHtml
192 $form .= '<p>' . Xml
::submitButton(
193 $this->msg( 'expand_templates_ok' )->text(),
194 array( 'accesskey' => 's' )
196 $form .= "</fieldset>\n";
197 $form .= Xml
::closeElement( 'form' );
203 * Generate a nice little box with a heading for output
205 * @param string $output Wiki text output
206 * @param string $heading
209 private function makeOutput( $output, $heading = 'expand_templates_output' ) {
210 $out = "<h2>" . $this->msg( $heading )->escaped() . "</h2>\n";
211 $out .= Xml
::textarea(
216 array( 'id' => 'output', 'readonly' => 'readonly' )
223 * Renders the supplied wikitext as html
225 * @param Title $title
226 * @param string $text
229 private function generateHtml( Title
$title, $text ) {
232 $popts = ParserOptions
::newFromContext( $this->getContext() );
233 $popts->setTargetLanguage( $title->getPageLanguage() );
234 $pout = $wgParser->parse( $text, $title, $popts );
236 return $pout->getText();
240 * Wraps the provided html code in a div and outputs it to the page
242 * @param Title $title
243 * @param string $html
244 * @param OutputPage $out
246 private function showHtmlPreview( Title
$title, $html, OutputPage
$out ) {
247 $lang = $title->getPageViewLanguage();
248 $out->addHTML( "<h2>" . $this->msg( 'expand_templates_preview' )->escaped() . "</h2>\n" );
249 $out->addHTML( Html
::openElement( 'div', array(
250 'class' => 'mw-content-' . $lang->getDir(),
251 'dir' => $lang->getDir(),
252 'lang' => $lang->getHtmlCode(),
254 $out->addHTML( $html );
255 $out->addHTML( Html
::closeElement( 'div' ) );