tags in the expanded wikitext */ protected $removeNowiki; /** @var int Maximum size in bytes to include. 50MB allows fixing those huge pages */ private const MAX_INCLUDE_SIZE = 50000000; public function __construct() { parent::__construct( 'ExpandTemplates' ); } /** * Show the special page * @param string|null $subpage */ public function execute( $subpage ) { $this->setHeaders(); $this->addHelpLink( 'Help:ExpandTemplates' ); $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->setMaxIncludeSize( self::MAX_INCLUDE_SIZE ); $parser = MediaWikiServices::getInstance()->getParser(); if ( $this->generateXML ) { $parser->startExternalParse( $title, $options, Parser::OT_PREPROCESS ); $dom = $parser->preprocessToDom( $input ); if ( method_exists( $dom, 'saveXML' ) ) { // @phan-suppress-next-line PhanUndeclaredMethod $xml = $dom->saveXML(); } else { // @phan-suppress-next-line PhanUndeclaredMethod $xml = $dom->__toString(); } } $output = $parser->preprocess( $input, $title, $options ); } else { $this->removeComments = $request->getBool( 'wpRemoveComments', true ); $this->removeNowiki = $request->getBool( 'wpRemoveNowiki', false ); $output = false; } $out = $this->getOutput(); $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( [ '_<nowiki>_', '_</nowiki>_', '_<nowiki */>_' ], '', $tmp ); } $config = $this->getConfig(); $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 ); } } /** * Callback for the HTMLForm used in self::makeForm. * Checks, if the input was given, and if not, returns a fatal Status * object with an error message. * * @param array $values The values submitted to the HTMLForm * @return Status */ public function onSubmitInput( array $values ) { $status = Status::newGood(); if ( !strlen( $values['input'] ) ) { $status = Status::newFatal( 'expand_templates_input_missing' ); } return $status; } /** * Generate a form allowing users to enter information * * @param string $title Value for context title field * @param string $input Value for input textbox */ private function makeForm( $title, $input ) { $fields = [ 'contexttitle' => [ 'type' => 'text', 'label' => $this->msg( 'expand_templates_title' )->plain(), 'name' => 'wpContextTitle', 'id' => 'contexttitle', 'size' => 60, 'default' => $title, 'autofocus' => true, ], 'input' => [ 'type' => 'textarea', 'name' => 'wpInput', 'label' => $this->msg( 'expand_templates_input' )->text(), 'rows' => 10, 'default' => $input, 'id' => 'input', 'useeditfont' => true, ], 'removecomments' => [ 'type' => 'check', 'label' => $this->msg( 'expand_templates_remove_comments' )->text(), 'name' => 'wpRemoveComments', 'id' => 'removecomments', 'default' => $this->removeComments, ], 'removenowiki' => [ 'type' => 'check', 'label' => $this->msg( 'expand_templates_remove_nowiki' )->text(), 'name' => 'wpRemoveNowiki', 'id' => 'removenowiki', 'default' => $this->removeNowiki, ], 'generate_xml' => [ 'type' => 'check', 'label' => $this->msg( 'expand_templates_generate_xml' )->text(), 'name' => 'wpGenerateXml', 'id' => 'generate_xml', 'default' => $this->generateXML, ], 'generate_rawhtml' => [ 'type' => 'check', 'label' => $this->msg( 'expand_templates_generate_rawhtml' )->text(), 'name' => 'wpGenerateRawHtml', 'id' => 'generate_rawhtml', 'default' => $this->generateRawHtml, ], ]; $form = HTMLForm::factory( 'ooui', $fields, $this->getContext() ); $form ->setSubmitTextMsg( 'expand_templates_ok' ) ->setWrapperLegendMsg( 'expandtemplates' ) ->setHeaderText( $this->msg( 'expand_templates_intro' )->parse() ) ->setSubmitCallback( [ $this, 'onSubmitInput' ] ) ->showAlways(); } /** * 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 = "