Debian Codegraph @

includes/specials/SpecialExpandTemplates.php

raw symbols SCIP
1 <?php
2 /**
3 * This program is free software; you can redistribute it and/or modify
4 * it under the terms of the GNU General Public License as published by
5 * the Free Software Foundation; either version 2 of the License, or
6 * (at your option) any later version.
7 *
8 * This program is distributed in the hope that it will be useful,
9 * but WITHOUT ANY WARRANTY; without even the implied warranty of
10 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
11 * GNU General Public License for more details.
12 *
13 * You should have received a copy of the GNU General Public License along
14 * with this program; if not, write to the Free Software Foundation, Inc.,
15 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
16 * http://www.gnu.org/copyleft/gpl.html
17 *
18 * @file
19 */
20
21 namespace MediaWiki\Specials;
22
23 use MediaWiki\Html\Html;
24 use MediaWiki\HTMLForm\HTMLForm;
25 use MediaWiki\MainConfigNames;
26 use MediaWiki\MediaWikiServices;
27 use MediaWiki\Output\OutputPage;
28 use MediaWiki\Parser\Parser;
29 use MediaWiki\Parser\ParserFactory;
30 use MediaWiki\Parser\ParserOptions;
31 use MediaWiki\Parser\ParserOutput;
32 use MediaWiki\SpecialPage\SpecialPage;
33 use MediaWiki\Status\Status;
34 use MediaWiki\Tidy\TidyDriverBase;
35 use MediaWiki\Title\Title;
36 use MediaWiki\User\Options\UserOptionsLookup;
37 use MediaWiki\Xml\Xml;
38
39 /**
40 * A special page to enter wikitext and expands its templates, parser functions,
41 * and variables, allowing easier debugging of these.
42 *
43 * @ingroup SpecialPage
44 * @ingroup Parser
45 */
46 class SpecialExpandTemplates extends SpecialPage {
47
48 /** @var int Maximum size in bytes to include. 50 MB allows fixing those huge pages */
49 private const MAX_INCLUDE_SIZE = 50_000_000;
50
51 private ParserFactory $parserFactory;
52 private UserOptionsLookup $userOptionsLookup;
53 private TidyDriverBase $tidy;
54
55 /**
56 * @param ParserFactory $parserFactory
57 * @param UserOptionsLookup $userOptionsLookup
58 * @param TidyDriverBase $tidy
59 */
60 public function __construct(
61 ParserFactory $parserFactory,
62 UserOptionsLookup $userOptionsLookup,
63 TidyDriverBase $tidy
64 ) {
65 parent::__construct( 'ExpandTemplates' );
66 $this->parserFactory = $parserFactory;
67 $this->userOptionsLookup = $userOptionsLookup;
68 $this->tidy = $tidy;
69 }
70
71 /**
72 * Show the special page
73 * @param string|null $subpage
74 */
75 public function execute( $subpage ) {
76 $this->setHeaders();
77 $this->addHelpLink( 'Help:ExpandTemplates' );
78
79 $request = $this->getRequest();
80 $input = $request->getText( 'wpInput' );
81
82 if ( strlen( $input ) ) {
83 $removeComments = $request->getBool( 'wpRemoveComments', false );
84 $removeNowiki = $request->getBool( 'wpRemoveNowiki', false );
85 $generateXML = $request->getBool( 'wpGenerateXml' );
86 $generateRawHtml = $request->getBool( 'wpGenerateRawHtml' );
87
88 $options = ParserOptions::newFromContext( $this->getContext() );
89 $options->setRemoveComments( $removeComments );
90 $options->setMaxIncludeSize( self::MAX_INCLUDE_SIZE );
91
92 $titleStr = $request->getText( 'wpContextTitle' );
93 $title = Title::newFromText( $titleStr );
94 if ( !$title ) {
95 $title = $this->getPageTitle();
96 $options->setTargetLanguage( $this->getContentLanguage() );
97 }
98
99 $parser = $this->parserFactory->getInstance();
100 if ( $generateXML ) {
101 $parser->startExternalParse( $title, $options, Parser::OT_PREPROCESS );
102 $dom = $parser->preprocessToDom( $input );
103
104 if ( method_exists( $dom, 'saveXML' ) ) {
105 // @phan-suppress-next-line PhanUndeclaredMethod
106 $xml = $dom->saveXML();
107 } else {
108 // @phan-suppress-next-line PhanUndeclaredMethod
109 $xml = $dom->__toString();
110 }
111 }
112
113 $output = $parser->preprocess( $input, $title, $options );
114 $this->makeForm();
115
116 $out = $this->getOutput();
117 if ( $generateXML && strlen( $output ) > 0 ) {
118 // @phan-suppress-next-line PhanPossiblyUndeclaredVariable xml is set when used
119 $out->addHTML( $this->makeOutput( $xml, 'expand_templates_xml_output' ) );
120 }
121
122 $tmp = $this->makeOutput( $output );
123
124 if ( $removeNowiki ) {
125 $tmp = preg_replace(
126 [ '_&lt;nowiki&gt;_', '_&lt;/nowiki&gt;_', '_&lt;nowiki */&gt;_' ],
127 '',
128 $tmp
129 );
130 }
131
132 $tmp = $this->tidy->tidy( $tmp );
133
134 $out->addHTML( $tmp );
135
136 $pout = $parser->parse( $output, $title, $options );
137 // TODO T371008 consider if using the Content framework makes sense instead of creating the pipeline
138 $rawhtml = MediaWikiServices::getInstance()->getDefaultOutputPipeline()
139 ->run( $pout, $options, [ 'enableSectionEditLinks' => false ] )->getContentHolderText();
140 if ( $generateRawHtml && strlen( $rawhtml ) > 0 ) {
141 $out->addHTML( $this->makeOutput( $rawhtml, 'expand_templates_html_output' ) );
142 }
143
144 $this->showHtmlPreview( $title, $pout, $out );
145 } else {
146 $this->makeForm();
147 }
148 }
149
150 /**
151 * Callback for the HTMLForm used in self::makeForm.
152 * Checks, if the input was given, and if not, returns a fatal Status
153 * object with an error message.
154 *
155 * @param array $values The values submitted to the HTMLForm
156 * @return Status
157 */
158 public function onSubmitInput( array $values ) {
159 $status = Status::newGood();
160 if ( !strlen( $values['Input'] ) ) {
161 $status = Status::newFatal( 'expand_templates_input_missing' );
162 }
163 return $status;
164 }
165
166 /**
167 * Generate a form allowing users to enter information
168 */
169 private function makeForm() {
170 $fields = [
171 'Input' => [
172 'type' => 'textarea',
173 'label' => $this->msg( 'expand_templates_input' )->text(),
174 'rows' => 10,
175 'id' => 'input',
176 'useeditfont' => true,
177 'required' => true,
178 'autofocus' => true,
179 ],
180 'ContextTitle' => [
181 'type' => 'text',
182 'label' => $this->msg( 'expand_templates_title' )->plain(),
183 'id' => 'contexttitle',
184 'size' => 60,
185 ],
186 'RemoveComments' => [
187 'type' => 'check',
188 'label' => $this->msg( 'expand_templates_remove_comments' )->text(),
189 'id' => 'removecomments',
190 'default' => true,
191 ],
192 'RemoveNowiki' => [
193 'type' => 'check',
194 'label' => $this->msg( 'expand_templates_remove_nowiki' )->text(),
195 'id' => 'removenowiki',
196 ],
197 'GenerateXml' => [
198 'type' => 'check',
199 'label' => $this->msg( 'expand_templates_generate_xml' )->text(),
200 'id' => 'generate_xml',
201 ],
202 'GenerateRawHtml' => [
203 'type' => 'check',
204 'label' => $this->msg( 'expand_templates_generate_rawhtml' )->text(),
205 'id' => 'generate_rawhtml',
206 ],
207 ];
208
209 $form = HTMLForm::factory( 'ooui', $fields, $this->getContext() );
210 $form
211 ->setSubmitTextMsg( 'expand_templates_ok' )
212 ->setWrapperLegendMsg( 'expandtemplates' )
213 ->setHeaderHtml( $this->msg( 'expand_templates_intro' )->parse() )
214 ->setSubmitCallback( [ $this, 'onSubmitInput' ] )
215 ->showAlways();
216 }
217
218 /**
219 * Generate a nice little box with a heading for output
220 *
221 * @param string $output Wiki text output
222 * @param string $heading
223 * @return string
224 */
225 private function makeOutput( $output, $heading = 'expand_templates_output' ) {
226 $out = "<h2>" . $this->msg( $heading )->escaped() . "</h2>\n";
227 $out .= Xml::textarea(
228 'output',
229 $output,
230 10,
231 10,
232 [
233 'id' => 'output',
234 'readonly' => 'readonly',
235 'class' => 'mw-editfont-' . $this->userOptionsLookup->getOption( $this->getUser(), 'editfont' )
236 ]
237 );
238
239 return $out;
240 }
241
242 /**
243 * Wraps the provided html code in a div and outputs it to the page
244 *
245 * @param Title $title
246 * @param ParserOutput $pout
247 * @param OutputPage $out
248 */
249 private function showHtmlPreview( Title $title, ParserOutput $pout, OutputPage $out ) {
250 $out->addHTML( "<h2>" . $this->msg( 'expand_templates_preview' )->escaped() . "</h2>\n" );
251
252 if ( $this->getConfig()->get( MainConfigNames::RawHtml ) ) {
253 $request = $this->getRequest();
254 $user = $this->getUser();
255
256 // To prevent cross-site scripting attacks, don't show the preview if raw HTML is
257 // allowed and a valid edit token is not provided (T73111). However, MediaWiki
258 // does not currently provide logged-out users with CSRF protection; in that case,
259 // do not show the preview unless anonymous editing is allowed.
260 if ( $user->isAnon() && !$this->getAuthority()->isAllowed( 'edit' ) ) {
261 $error = [ 'expand_templates_preview_fail_html_anon' ];
262 } elseif ( !$user->matchEditToken( $request->getVal( 'wpEditToken' ), '', $request ) ) {
263 $error = [ 'expand_templates_preview_fail_html' ];
264 } else {
265 $error = false;
266 }
267
268 if ( $error ) {
269 $out->addHTML(
270 Html::errorBox(
271 $out->msg( $error )->parse(),
272 '',
273 'previewnote'
274 )
275 );
276 return;
277 }
278 }
279
280 $out->addParserOutputContent( $pout, [ 'enableSectionEditLinks' => false ] );
281 $out->addCategoryLinks( $pout->getCategoryMap() );
282 }
283
284 protected function getGroupName() {
285 return 'wiki';
286 }
287 }
288
289 /** @deprecated class alias since 1.41 */
290 class_alias( SpecialExpandTemplates::class, 'SpecialExpandTemplates' );