60eec392149857702304de397e35627814a0ecf8
[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 */
54 function execute( $subpage ) {
55 global $wgParser;
56
57 $this->setHeaders();
58
59 $request = $this->getRequest();
60 $titleStr = $request->getText( 'wpContextTitle' );
61 $title = Title::newFromText( $titleStr );
62
63 if ( !$title ) {
64 $title = $this->getPageTitle();
65 }
66 $input = $request->getText( 'wpInput' );
67 $this->generateXML = $request->getBool( 'wpGenerateXml' );
68 $this->generateRawHtml = $request->getBool( 'wpGenerateRawHtml' );
69
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 );
77
78 if ( $this->generateXML ) {
79 $wgParser->startExternalParse( $title, $options, OT_PREPROCESS );
80 $dom = $wgParser->preprocessToDom( $input );
81
82 if ( method_exists( $dom, 'saveXML' ) ) {
83 $xml = $dom->saveXML();
84 } else {
85 $xml = $dom->__toString();
86 }
87 }
88
89 $output = $wgParser->preprocess( $input, $title, $options );
90 } else {
91 $this->removeComments = $request->getBool( 'wpRemoveComments', true );
92 $this->removeNowiki = $request->getBool( 'wpRemoveNowiki', false );
93 $output = false;
94 }
95
96 $out = $this->getOutput();
97 $out->addWikiMsg( 'expand_templates_intro' );
98 $out->addHTML( $this->makeForm( $titleStr, $input ) );
99
100 if ( $output !== false ) {
101 if ( $this->generateXML && strlen( $output ) > 0 ) {
102 $out->addHTML( $this->makeOutput( $xml, 'expand_templates_xml_output' ) );
103 }
104
105 $tmp = $this->makeOutput( $output );
106
107 if ( $this->removeNowiki ) {
108 $tmp = preg_replace(
109 array( '_&lt;nowiki&gt;_', '_&lt;/nowiki&gt;_', '_&lt;nowiki */&gt;_' ),
110 '',
111 $tmp
112 );
113 }
114
115 $config = $this->getConfig();
116 if ( ( $config->get( 'UseTidy' ) && $options->getTidy() ) || $config->get( 'AlwaysUseTidy' ) ) {
117 $tmp = MWTidy::tidy( $tmp );
118 }
119
120 $out->addHTML( $tmp );
121
122 $pout = $this->generateHtml( $title, $output );
123 $rawhtml = $pout->getText();
124 if ( $this->generateRawHtml && strlen( $rawhtml ) > 0 ) {
125 $out->addHTML( $this->makeOutput( $rawhtml, 'expand_templates_html_output' ) );
126 }
127
128 $this->showHtmlPreview( $title, $pout, $out );
129 }
130 }
131
132 /**
133 * Generate a form allowing users to enter information
134 *
135 * @param string $title Value for context title field
136 * @param string $input Value for input textbox
137 * @return string
138 */
139 private function makeForm( $title, $input ) {
140 $self = $this->getPageTitle();
141 $form = Xml::openElement(
142 'form',
143 array( 'method' => 'post', 'action' => $self->getLocalUrl() )
144 );
145 $form .= "<fieldset><legend>" . $this->msg( 'expandtemplates' )->escaped() . "</legend>\n";
146
147 $form .= '<p>' . Xml::inputLabel(
148 $this->msg( 'expand_templates_title' )->plain(),
149 'wpContextTitle',
150 'contexttitle',
151 60,
152 $title,
153 array( 'autofocus' => true )
154 ) . '</p>';
155 $form .= '<p>' . Xml::label(
156 $this->msg( 'expand_templates_input' )->text(),
157 'input'
158 ) . '</p>';
159 $form .= Xml::textarea(
160 'wpInput',
161 $input,
162 10,
163 10,
164 array( 'id' => 'input' )
165 );
166
167 $form .= '<p>' . Xml::checkLabel(
168 $this->msg( 'expand_templates_remove_comments' )->text(),
169 'wpRemoveComments',
170 'removecomments',
171 $this->removeComments
172 ) . '</p>';
173 $form .= '<p>' . Xml::checkLabel(
174 $this->msg( 'expand_templates_remove_nowiki' )->text(),
175 'wpRemoveNowiki',
176 'removenowiki',
177 $this->removeNowiki
178 ) . '</p>';
179 $form .= '<p>' . Xml::checkLabel(
180 $this->msg( 'expand_templates_generate_xml' )->text(),
181 'wpGenerateXml',
182 'generate_xml',
183 $this->generateXML
184 ) . '</p>';
185 $form .= '<p>' . Xml::checkLabel(
186 $this->msg( 'expand_templates_generate_rawhtml' )->text(),
187 'wpGenerateRawHtml',
188 'generate_rawhtml',
189 $this->generateRawHtml
190 ) . '</p>';
191 $form .= '<p>' . Xml::submitButton(
192 $this->msg( 'expand_templates_ok' )->text(),
193 array( 'accesskey' => 's' )
194 ) . '</p>';
195 $form .= "</fieldset>\n";
196 $form .= Xml::closeElement( 'form' );
197
198 return $form;
199 }
200
201 /**
202 * Generate a nice little box with a heading for output
203 *
204 * @param string $output Wiki text output
205 * @param string $heading
206 * @return string
207 */
208 private function makeOutput( $output, $heading = 'expand_templates_output' ) {
209 $out = "<h2>" . $this->msg( $heading )->escaped() . "</h2>\n";
210 $out .= Xml::textarea(
211 'output',
212 $output,
213 10,
214 10,
215 array( 'id' => 'output', 'readonly' => 'readonly' )
216 );
217
218 return $out;
219 }
220
221 /**
222 * Renders the supplied wikitext as html
223 *
224 * @param Title $title
225 * @param string $text
226 * @return ParserOutput
227 */
228 private function generateHtml( Title $title, $text ) {
229 global $wgParser;
230
231 $popts = ParserOptions::newFromContext( $this->getContext() );
232 $popts->setTargetLanguage( $title->getPageLanguage() );
233 return $wgParser->parse( $text, $title, $popts );
234 }
235
236 /**
237 * Wraps the provided html code in a div and outputs it to the page
238 *
239 * @param Title $title
240 * @param ParserOutput $pout
241 * @param OutputPage $out
242 */
243 private function showHtmlPreview( Title $title, ParserOutput $pout, OutputPage $out ) {
244 $lang = $title->getPageViewLanguage();
245 $out->addHTML( "<h2>" . $this->msg( 'expand_templates_preview' )->escaped() . "</h2>\n" );
246 $out->addHTML( Html::openElement( 'div', array(
247 'class' => 'mw-content-' . $lang->getDir(),
248 'dir' => $lang->getDir(),
249 'lang' => $lang->getHtmlCode(),
250 ) ) );
251 $out->addParserOutputContent( $pout );
252 $out->addHTML( Html::closeElement( 'div' ) );
253 }
254
255 protected function getGroupName() {
256 return 'wiki';
257 }
258 }