Merge "Use /usr/bin/ as default folder for DjVu tools in unit tests"
[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 boolean whether or not to show the XML parse tree */
33 protected $generateXML;
34
35 /** @var boolean whether or not to show the raw HTML code */
36 protected $generateRawHtml;
37
38 /** @var boolean whether or not to remove comments in the expanded wikitext */
39 protected $removeComments;
40
41 /** @var boolean whether or not to remove <nowiki> tags in the expanded wikitext */
42 protected $removeNowiki;
43
44 /** @var 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, $wgUseTidy, $wgAlwaysUseTidy;
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 if ( ( $wgUseTidy && $options->getTidy() ) || $wgAlwaysUseTidy ) {
116 $tmp = MWTidy::tidy( $tmp );
117 }
118
119 $out->addHTML( $tmp );
120
121 $rawhtml = $this->generateHtml( $title, $output );
122
123 if ( $this->generateRawHtml && strlen( $rawhtml ) > 0 ) {
124 $out->addHTML( $this->makeOutput( $rawhtml, 'expand_templates_html_output' ) );
125 }
126
127 $this->showHtmlPreview( $title, $rawhtml, $out );
128 }
129 }
130
131 /**
132 * Generate a form allowing users to enter information
133 *
134 * @param string $title Value for context title field
135 * @param string $input Value for input textbox
136 * @return string
137 */
138 private function makeForm( $title, $input ) {
139 $self = $this->getPageTitle();
140 $form = Xml::openElement(
141 'form',
142 array( 'method' => 'post', 'action' => $self->getLocalUrl() )
143 );
144 $form .= "<fieldset><legend>" . $this->msg( 'expandtemplates' )->escaped() . "</legend>\n";
145
146 $form .= '<p>' . Xml::inputLabel(
147 $this->msg( 'expand_templates_title' )->plain(),
148 'wpContextTitle',
149 'contexttitle',
150 60,
151 $title,
152 array( 'autofocus' => true )
153 ) . '</p>';
154 $form .= '<p>' . Xml::label(
155 $this->msg( 'expand_templates_input' )->text(),
156 'input'
157 ) . '</p>';
158 $form .= Xml::textarea(
159 'wpInput',
160 $input,
161 10,
162 10,
163 array( 'id' => 'input' )
164 );
165
166 $form .= '<p>' . Xml::checkLabel(
167 $this->msg( 'expand_templates_remove_comments' )->text(),
168 'wpRemoveComments',
169 'removecomments',
170 $this->removeComments
171 ) . '</p>';
172 $form .= '<p>' . Xml::checkLabel(
173 $this->msg( 'expand_templates_remove_nowiki' )->text(),
174 'wpRemoveNowiki',
175 'removenowiki',
176 $this->removeNowiki
177 ) . '</p>';
178 $form .= '<p>' . Xml::checkLabel(
179 $this->msg( 'expand_templates_generate_xml' )->text(),
180 'wpGenerateXml',
181 'generate_xml',
182 $this->generateXML
183 ) . '</p>';
184 $form .= '<p>' . Xml::checkLabel(
185 $this->msg( 'expand_templates_generate_rawhtml' )->text(),
186 'wpGenerateRawHtml',
187 'generate_rawhtml',
188 $this->generateRawHtml
189 ) . '</p>';
190 $form .= '<p>' . Xml::submitButton(
191 $this->msg( 'expand_templates_ok' )->text(),
192 array( 'accesskey' => 's' )
193 ) . '</p>';
194 $form .= "</fieldset>\n";
195 $form .= Xml::closeElement( 'form' );
196
197 return $form;
198 }
199
200 /**
201 * Generate a nice little box with a heading for output
202 *
203 * @param string $output Wiki text output
204 * @param string $heading
205 * @return string
206 */
207 private function makeOutput( $output, $heading = 'expand_templates_output' ) {
208 $out = "<h2>" . $this->msg( $heading )->escaped() . "</h2>\n";
209 $out .= Xml::textarea(
210 'output',
211 $output,
212 10,
213 10,
214 array( 'id' => 'output', 'readonly' => 'readonly' )
215 );
216
217 return $out;
218 }
219
220 /**
221 * Renders the supplied wikitext as html
222 *
223 * @param Title $title
224 * @param string $text
225 * @return string
226 */
227 private function generateHtml( Title $title, $text ) {
228 global $wgParser;
229
230 $popts = ParserOptions::newFromContext( $this->getContext() );
231 $popts->setTargetLanguage( $title->getPageLanguage() );
232 $pout = $wgParser->parse( $text, $title, $popts );
233
234 return $pout->getText();
235 }
236
237 /**
238 * Wraps the provided html code in a div and outputs it to the page
239 *
240 * @param Title $title
241 * @param string $html
242 * @param OutputPage $out
243 */
244 private function showHtmlPreview( Title $title, $html, OutputPage $out ) {
245 $lang = $title->getPageViewLanguage();
246 $out->addHTML( "<h2>" . $this->msg( 'expand_templates_preview' )->escaped() . "</h2>\n" );
247 $out->addHTML( Html::openElement( 'div', array(
248 'class' => 'mw-content-' . $lang->getDir(),
249 'dir' => $lang->getDir(),
250 'lang' => $lang->getHtmlCode(),
251 ) ) );
252 $out->addHTML( $html );
253 $out->addHTML( Html::closeElement( 'div' ) );
254 }
255
256 protected function getGroupName() {
257 return 'wiki';
258 }
259 }