Localisation updates from https://translatewiki.net.
[mediawiki.git] / includes / specials / SpecialExpandTemplates.php
blob89bacbdef5b0ff7ba0c15b46dc8cee9ccef9503c
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.
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.
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
18 * @file
21 namespace MediaWiki\Specials;
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;
39 /**
40 * A special page to enter wikitext and expands its templates, parser functions,
41 * and variables, allowing easier debugging of these.
43 * @ingroup SpecialPage
44 * @ingroup Parser
46 class SpecialExpandTemplates extends SpecialPage {
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;
51 private ParserFactory $parserFactory;
52 private UserOptionsLookup $userOptionsLookup;
53 private TidyDriverBase $tidy;
55 /**
56 * @param ParserFactory $parserFactory
57 * @param UserOptionsLookup $userOptionsLookup
58 * @param TidyDriverBase $tidy
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;
71 /**
72 * Show the special page
73 * @param string|null $subpage
75 public function execute( $subpage ) {
76 $this->setHeaders();
77 $this->addHelpLink( 'Help:ExpandTemplates' );
79 $request = $this->getRequest();
80 $input = $request->getText( 'wpInput' );
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' );
88 $options = ParserOptions::newFromContext( $this->getContext() );
89 $options->setRemoveComments( $removeComments );
90 $options->setMaxIncludeSize( self::MAX_INCLUDE_SIZE );
92 $titleStr = $request->getText( 'wpContextTitle' );
93 $title = Title::newFromText( $titleStr );
94 if ( !$title ) {
95 $title = $this->getPageTitle();
96 $options->setTargetLanguage( $this->getContentLanguage() );
99 $parser = $this->parserFactory->getInstance();
100 if ( $generateXML ) {
101 $parser->startExternalParse( $title, $options, Parser::OT_PREPROCESS );
102 $dom = $parser->preprocessToDom( $input );
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();
113 $output = $parser->preprocess( $input, $title, $options );
114 $this->makeForm();
116 $out = $this->getOutput();
117 if ( $generateXML ) {
118 // @phan-suppress-next-line PhanPossiblyUndeclaredVariable xml is set when used
119 $out->addHTML( $this->makeOutput( $xml, 'expand_templates_xml_output' ) );
122 $tmp = $this->makeOutput( $output );
124 if ( $removeNowiki ) {
125 $tmp = preg_replace(
126 [ '_&lt;nowiki&gt;_', '_&lt;/nowiki&gt;_', '_&lt;nowiki */&gt;_' ],
128 $tmp
132 $tmp = $this->tidy->tidy( $tmp );
134 $out->addHTML( $tmp );
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 // @phan-suppress-next-line SecurityCheck-DoubleEscaped Wanted here to display the html
142 $out->addHTML( $this->makeOutput( $rawhtml, 'expand_templates_html_output' ) );
145 $this->showHtmlPreview( $title, $pout, $out );
146 } else {
147 $this->makeForm();
152 * Callback for the HTMLForm used in self::makeForm.
153 * Checks, if the input was given, and if not, returns a fatal Status
154 * object with an error message.
156 * @param array $values The values submitted to the HTMLForm
157 * @return Status
159 public function onSubmitInput( array $values ) {
160 $status = Status::newGood();
161 if ( !strlen( $values['Input'] ) ) {
162 $status = Status::newFatal( 'expand_templates_input_missing' );
164 return $status;
168 * Generate a form allowing users to enter information
170 private function makeForm() {
171 $fields = [
172 'Input' => [
173 'type' => 'textarea',
174 'label' => $this->msg( 'expand_templates_input' )->text(),
175 'rows' => 10,
176 'id' => 'input',
177 'useeditfont' => true,
178 'required' => true,
179 'autofocus' => true,
181 'ContextTitle' => [
182 'type' => 'text',
183 'label' => $this->msg( 'expand_templates_title' )->plain(),
184 'id' => 'contexttitle',
185 'size' => 60,
187 'RemoveComments' => [
188 'type' => 'check',
189 'label' => $this->msg( 'expand_templates_remove_comments' )->text(),
190 'id' => 'removecomments',
191 'default' => true,
193 'RemoveNowiki' => [
194 'type' => 'check',
195 'label' => $this->msg( 'expand_templates_remove_nowiki' )->text(),
196 'id' => 'removenowiki',
198 'GenerateXml' => [
199 'type' => 'check',
200 'label' => $this->msg( 'expand_templates_generate_xml' )->text(),
201 'id' => 'generate_xml',
203 'GenerateRawHtml' => [
204 'type' => 'check',
205 'label' => $this->msg( 'expand_templates_generate_rawhtml' )->text(),
206 'id' => 'generate_rawhtml',
210 $form = HTMLForm::factory( 'ooui', $fields, $this->getContext() );
211 $form
212 ->setSubmitTextMsg( 'expand_templates_ok' )
213 ->setWrapperLegendMsg( 'expandtemplates' )
214 ->setHeaderHtml( $this->msg( 'expand_templates_intro' )->parse() )
215 ->setSubmitCallback( [ $this, 'onSubmitInput' ] )
216 ->showAlways();
220 * Generate a nice little box with a heading for output
222 * @param string $output Wiki text output
223 * @param string $heading
224 * @return string
226 private function makeOutput( $output, $heading = 'expand_templates_output' ) {
227 $out = "<h2>" . $this->msg( $heading )->escaped() . "</h2>\n";
228 $out .= Xml::textarea(
229 'output',
230 $output,
234 'id' => 'output',
235 'readonly' => 'readonly',
236 'class' => 'mw-editfont-' . $this->userOptionsLookup->getOption( $this->getUser(), 'editfont' )
240 return $out;
244 * Wraps the provided html code in a div and outputs it to the page
246 * @param Title $title
247 * @param ParserOutput $pout
248 * @param OutputPage $out
250 private function showHtmlPreview( Title $title, ParserOutput $pout, OutputPage $out ) {
251 $out->addHTML( "<h2>" . $this->msg( 'expand_templates_preview' )->escaped() . "</h2>\n" );
253 if ( $this->getConfig()->get( MainConfigNames::RawHtml ) ) {
254 $request = $this->getRequest();
255 $user = $this->getUser();
257 // To prevent cross-site scripting attacks, don't show the preview if raw HTML is
258 // allowed and a valid edit token is not provided (T73111). However, MediaWiki
259 // does not currently provide logged-out users with CSRF protection; in that case,
260 // do not show the preview unless anonymous editing is allowed.
261 if ( $user->isAnon() && !$this->getAuthority()->isAllowed( 'edit' ) ) {
262 $error = [ 'expand_templates_preview_fail_html_anon' ];
263 } elseif ( !$user->matchEditToken( $request->getVal( 'wpEditToken' ), '', $request ) ) {
264 $error = [ 'expand_templates_preview_fail_html' ];
265 } else {
266 $error = false;
269 if ( $error ) {
270 $out->addHTML(
271 Html::errorBox(
272 $out->msg( $error )->parse(),
274 'previewnote'
277 return;
281 $out->addParserOutputContent( $pout, [ 'enableSectionEditLinks' => false ] );
282 $out->addCategoryLinks( $pout->getCategoryMap() );
285 protected function getGroupName() {
286 return 'wiki';
290 /** @deprecated class alias since 1.41 */
291 class_alias( SpecialExpandTemplates::class, 'SpecialExpandTemplates' );