Merge "Update type hints in TraditionalImageGallery"
[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 remove comments in the expanded wikitext */
36 protected $removeComments;
37
38 /** @var boolean whether or not to remove <nowiki> tags in the expanded wikitext */
39 protected $removeNowiki;
40
41 /** @var maximum size in bytes to include. 50MB allows fixing those huge pages */
42 const MAX_INCLUDE_SIZE = 50000000;
43
44 function __construct() {
45 parent::__construct( 'ExpandTemplates' );
46 }
47
48 /**
49 * Show the special page
50 */
51 function execute( $subpage ) {
52 global $wgParser, $wgUseTidy, $wgAlwaysUseTidy;
53
54 $this->setHeaders();
55
56 $request = $this->getRequest();
57 $titleStr = $request->getText( 'wpContextTitle' );
58 $title = Title::newFromText( $titleStr );
59
60 if ( !$title ) {
61 $title = $this->getTitle();
62 }
63 $input = $request->getText( 'wpInput' );
64 $this->generateXML = $request->getBool( 'wpGenerateXml' );
65
66 if ( strlen( $input ) ) {
67 $this->removeComments = $request->getBool( 'wpRemoveComments', false );
68 $this->removeNowiki = $request->getBool( 'wpRemoveNowiki', false );
69 $options = ParserOptions::newFromContext( $this->getContext() );
70 $options->setRemoveComments( $this->removeComments );
71 $options->setTidy( true );
72 $options->setMaxIncludeSize( self::MAX_INCLUDE_SIZE );
73
74 if ( $this->generateXML ) {
75 $wgParser->startExternalParse( $title, $options, OT_PREPROCESS );
76 $dom = $wgParser->preprocessToDom( $input );
77
78 if ( method_exists( $dom, 'saveXML' ) ) {
79 $xml = $dom->saveXML();
80 } else {
81 $xml = $dom->__toString();
82 }
83 }
84
85 $output = $wgParser->preprocess( $input, $title, $options );
86 } else {
87 $this->removeComments = $request->getBool( 'wpRemoveComments', true );
88 $this->removeNowiki = $request->getBool( 'wpRemoveNowiki', false );
89 $output = false;
90 }
91
92 $out = $this->getOutput();
93 $out->addWikiMsg( 'expand_templates_intro' );
94 $out->addHTML( $this->makeForm( $titleStr, $input ) );
95
96 if ( $output !== false ) {
97 if ( $this->generateXML && strlen( $output ) > 0 ) {
98 $out->addHTML( $this->makeOutput( $xml, 'expand_templates_xml_output' ) );
99 }
100
101 $tmp = $this->makeOutput( $output );
102
103 if ( $this->removeNowiki ) {
104 $tmp = preg_replace(
105 array( '_&lt;nowiki&gt;_', '_&lt;/nowiki&gt;_', '_&lt;nowiki */&gt;_' ),
106 '',
107 $tmp
108 );
109 }
110
111 if ( ( $wgUseTidy && $options->getTidy() ) || $wgAlwaysUseTidy ) {
112 $tmp = MWTidy::tidy( $tmp );
113 }
114
115 $out->addHTML( $tmp );
116 $this->showHtmlPreview( $title, $output, $out );
117 }
118
119 }
120
121 /**
122 * Generate a form allowing users to enter information
123 *
124 * @param string $title Value for context title field
125 * @param string $input Value for input textbox
126 * @return string
127 */
128 private function makeForm( $title, $input ) {
129 $self = $this->getTitle();
130 $form = Xml::openElement(
131 'form',
132 array( 'method' => 'post', 'action' => $self->getLocalUrl() )
133 );
134 $form .= "<fieldset><legend>" . $this->msg( 'expandtemplates' )->escaped() . "</legend>\n";
135
136 $form .= '<p>' . Xml::inputLabel(
137 $this->msg( 'expand_templates_title' )->plain(),
138 'wpContextTitle',
139 'contexttitle',
140 60,
141 $title,
142 array( 'autofocus' => true )
143 ) . '</p>';
144 $form .= '<p>' . Xml::label(
145 $this->msg( 'expand_templates_input' )->text(),
146 'input'
147 ) . '</p>';
148 $form .= Xml::textarea(
149 'wpInput',
150 $input,
151 10,
152 10,
153 array( 'id' => 'input' )
154 );
155
156 $form .= '<p>' . Xml::checkLabel(
157 $this->msg( 'expand_templates_remove_comments' )->text(),
158 'wpRemoveComments',
159 'removecomments',
160 $this->removeComments
161 ) . '</p>';
162 $form .= '<p>' . Xml::checkLabel(
163 $this->msg( 'expand_templates_remove_nowiki' )->text(),
164 'wpRemoveNowiki',
165 'removenowiki',
166 $this->removeNowiki
167 ) . '</p>';
168 $form .= '<p>' . Xml::checkLabel(
169 $this->msg( 'expand_templates_generate_xml' )->text(),
170 'wpGenerateXml',
171 'generate_xml',
172 $this->generateXML
173 ) . '</p>';
174 $form .= '<p>' . Xml::submitButton(
175 $this->msg( 'expand_templates_ok' )->text(),
176 array( 'accesskey' => 's' )
177 ) . '</p>';
178 $form .= "</fieldset>\n";
179 $form .= Xml::closeElement( 'form' );
180
181 return $form;
182 }
183
184 /**
185 * Generate a nice little box with a heading for output
186 *
187 * @param string $output Wiki text output
188 * @param string $heading
189 * @return string
190 */
191 private function makeOutput( $output, $heading = 'expand_templates_output' ) {
192 $out = "<h2>" . $this->msg( $heading )->escaped() . "</h2>\n";
193 $out .= Xml::textarea(
194 'output',
195 $output,
196 10,
197 10,
198 array( 'id' => 'output', 'readonly' => 'readonly' )
199 );
200
201 return $out;
202 }
203
204 /**
205 * Render the supplied wiki text and append to the page as a preview
206 *
207 * @param Title $title
208 * @param string $text
209 * @param OutputPage $out
210 */
211 private function showHtmlPreview( Title $title, $text, OutputPage $out ) {
212 global $wgParser;
213
214 $popts = ParserOptions::newFromContext( $this->getContext() );
215 $popts->setTargetLanguage( $title->getPageLanguage() );
216 $pout = $wgParser->parse( $text, $title, $popts );
217 $lang = $title->getPageViewLanguage();
218
219 $out->addHTML( "<h2>" . $this->msg( 'expand_templates_preview' )->escaped() . "</h2>\n" );
220 $out->addHTML( Html::openElement( 'div', array(
221 'class' => 'mw-content-' . $lang->getDir(),
222 'dir' => $lang->getDir(),
223 'lang' => $lang->getHtmlCode(),
224 ) ) );
225
226 $out->addHTML( $pout->getText() );
227 $out->addHTML( Html::closeElement( 'div' ) );
228 }
229 }