1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
|
<?php
/**
* Implements Special:ExpandTemplates
*
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation; either version 2 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License along
* with this program; if not, write to the Free Software Foundation, Inc.,
* 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
* http://www.gnu.org/copyleft/gpl.html
*
* @file
* @ingroup SpecialPage
*/
/**
* A special page that expands submitted templates, parser functions,
* and variables, allowing easier debugging of these.
*
* @ingroup SpecialPage
*/
class SpecialExpandTemplates extends SpecialPage {
/** @var bool Whether or not to show the XML parse tree */
protected $generateXML;
/** @var bool Whether or not to show the raw HTML code */
protected $generateRawHtml;
/** @var bool Whether or not to remove comments in the expanded wikitext */
protected $removeComments;
/** @var bool Whether or not to remove <nowiki> tags in the expanded wikitext */
protected $removeNowiki;
/** @var int Maximum size in bytes to include. 50MB allows fixing those huge pages */
const MAX_INCLUDE_SIZE = 50000000;
function __construct() {
parent::__construct( 'ExpandTemplates' );
}
/**
* Show the special page
* @param string|null $subpage
*/
function execute( $subpage ) {
global $wgParser;
$this->setHeaders();
$request = $this->getRequest();
$titleStr = $request->getText( 'wpContextTitle' );
$title = Title::newFromText( $titleStr );
if ( !$title ) {
$title = $this->getPageTitle();
}
$input = $request->getText( 'wpInput' );
$this->generateXML = $request->getBool( 'wpGenerateXml' );
$this->generateRawHtml = $request->getBool( 'wpGenerateRawHtml' );
if ( strlen( $input ) ) {
$this->removeComments = $request->getBool( 'wpRemoveComments', false );
$this->removeNowiki = $request->getBool( 'wpRemoveNowiki', false );
$options = ParserOptions::newFromContext( $this->getContext() );
$options->setRemoveComments( $this->removeComments );
$options->setTidy( true );
$options->setMaxIncludeSize( self::MAX_INCLUDE_SIZE );
if ( $this->generateXML ) {
$wgParser->startExternalParse( $title, $options, Parser::OT_PREPROCESS );
$dom = $wgParser->preprocessToDom( $input );
if ( method_exists( $dom, 'saveXML' ) ) {
$xml = $dom->saveXML();
} else {
$xml = $dom->__toString();
}
}
$output = $wgParser->preprocess( $input, $title, $options );
} else {
$this->removeComments = $request->getBool( 'wpRemoveComments', true );
$this->removeNowiki = $request->getBool( 'wpRemoveNowiki', false );
$output = false;
}
$out = $this->getOutput();
$out->addWikiMsg( 'expand_templates_intro' );
$out->addHTML( $this->makeForm( $titleStr, $input ) );
if ( $output !== false ) {
if ( $this->generateXML && strlen( $output ) > 0 ) {
$out->addHTML( $this->makeOutput( $xml, 'expand_templates_xml_output' ) );
}
$tmp = $this->makeOutput( $output );
if ( $this->removeNowiki ) {
$tmp = preg_replace(
array( '_<nowiki>_', '_</nowiki>_', '_<nowiki */>_' ),
'',
$tmp
);
}
$config = $this->getConfig();
if ( ( $config->get( 'UseTidy' ) && $options->getTidy() ) || $config->get( 'AlwaysUseTidy' ) ) {
$tmp = MWTidy::tidy( $tmp );
}
$out->addHTML( $tmp );
$pout = $this->generateHtml( $title, $output );
$rawhtml = $pout->getText();
if ( $this->generateRawHtml && strlen( $rawhtml ) > 0 ) {
$out->addHTML( $this->makeOutput( $rawhtml, 'expand_templates_html_output' ) );
}
$this->showHtmlPreview( $title, $pout, $out );
}
}
/**
* Generate a form allowing users to enter information
*
* @param string $title Value for context title field
* @param string $input Value for input textbox
* @return string
*/
private function makeForm( $title, $input ) {
$self = $this->getPageTitle();
$request = $this->getRequest();
$user = $this->getUser();
$form = Xml::openElement(
'form',
array( 'method' => 'post', 'action' => $self->getLocalUrl() )
);
$form .= "<fieldset><legend>" . $this->msg( 'expandtemplates' )->escaped() . "</legend>\n";
$form .= '<p>' . Xml::inputLabel(
$this->msg( 'expand_templates_title' )->plain(),
'wpContextTitle',
'contexttitle',
60,
$title,
array( 'autofocus' => '', 'class' => 'mw-ui-input-inline' )
) . '</p>';
$form .= '<p>' . Xml::label(
$this->msg( 'expand_templates_input' )->text(),
'input'
) . '</p>';
$form .= Xml::textarea(
'wpInput',
$input,
10,
10,
array( 'id' => 'input' )
);
$form .= '<p>' . Xml::checkLabel(
$this->msg( 'expand_templates_remove_comments' )->text(),
'wpRemoveComments',
'removecomments',
$this->removeComments
) . '</p>';
$form .= '<p>' . Xml::checkLabel(
$this->msg( 'expand_templates_remove_nowiki' )->text(),
'wpRemoveNowiki',
'removenowiki',
$this->removeNowiki
) . '</p>';
$form .= '<p>' . Xml::checkLabel(
$this->msg( 'expand_templates_generate_xml' )->text(),
'wpGenerateXml',
'generate_xml',
$this->generateXML
) . '</p>';
$form .= '<p>' . Xml::checkLabel(
$this->msg( 'expand_templates_generate_rawhtml' )->text(),
'wpGenerateRawHtml',
'generate_rawhtml',
$this->generateRawHtml
) . '</p>';
$form .= '<p>' . Xml::submitButton(
$this->msg( 'expand_templates_ok' )->text(),
array( 'accesskey' => 's' )
) . '</p>';
$form .= "</fieldset>\n";
$form .= Html::hidden( 'wpEditToken', $user->getEditToken( '', $request ) );
$form .= Xml::closeElement( 'form' );
return $form;
}
/**
* Generate a nice little box with a heading for output
*
* @param string $output Wiki text output
* @param string $heading
* @return string
*/
private function makeOutput( $output, $heading = 'expand_templates_output' ) {
$out = "<h2>" . $this->msg( $heading )->escaped() . "</h2>\n";
$out .= Xml::textarea(
'output',
$output,
10,
10,
array( 'id' => 'output', 'readonly' => 'readonly' )
);
return $out;
}
/**
* Renders the supplied wikitext as html
*
* @param Title $title
* @param string $text
* @return ParserOutput
*/
private function generateHtml( Title $title, $text ) {
global $wgParser;
$popts = ParserOptions::newFromContext( $this->getContext() );
$popts->setTargetLanguage( $title->getPageLanguage() );
return $wgParser->parse( $text, $title, $popts );
}
/**
* Wraps the provided html code in a div and outputs it to the page
*
* @param Title $title
* @param ParserOutput $pout
* @param OutputPage $out
*/
private function showHtmlPreview( Title $title, ParserOutput $pout, OutputPage $out ) {
$lang = $title->getPageViewLanguage();
$out->addHTML( "<h2>" . $this->msg( 'expand_templates_preview' )->escaped() . "</h2>\n" );
if ( $this->getConfig()->get( 'RawHtml' ) ) {
$request = $this->getRequest();
$user = $this->getUser();
// To prevent cross-site scripting attacks, don't show the preview if raw HTML is
// allowed and a valid edit token is not provided (bug 71111). However, MediaWiki
// does not currently provide logged-out users with CSRF protection; in that case,
// do not show the preview unless anonymous editing is allowed.
if ( $user->isAnon() && !$user->isAllowed( 'edit' ) ) {
$error = array( 'expand_templates_preview_fail_html_anon' );
} elseif ( !$user->matchEditToken( $request->getVal( 'wpEditToken' ), '', $request ) ) {
$error = array( 'expand_templates_preview_fail_html' );
} else {
$error = false;
}
if ( $error ) {
$out->wrapWikiMsg( "<div class='previewnote'>\n$1\n</div>", $error );
return;
}
}
$out->addHTML( Html::openElement( 'div', array(
'class' => 'mw-content-' . $lang->getDir(),
'dir' => $lang->getDir(),
'lang' => $lang->getHtmlCode(),
) ) );
$out->addParserOutputContent( $pout );
$out->addHTML( Html::closeElement( 'div' ) );
$out->setCategoryLinks( $pout->getCategories() );
}
protected function getGroupName() {
return 'wiki';
}
}
|