parserFactory = $parserFactory; $this->userOptionsLookup = $userOptionsLookup; $this->tidy = $tidy; } /** * Show the special page * @param string|null $subpage */ public function execute( $subpage ) { $this->setHeaders(); $this->addHelpLink( 'Help:ExpandTemplates' ); $request = $this->getRequest(); $input = $request->getText( 'wpInput' ); if ( strlen( $input ) ) { $removeComments = $request->getBool( 'wpRemoveComments', false ); $removeNowiki = $request->getBool( 'wpRemoveNowiki', false ); $generateXML = $request->getBool( 'wpGenerateXml' ); $generateRawHtml = $request->getBool( 'wpGenerateRawHtml' ); $options = ParserOptions::newFromContext( $this->getContext() ); $options->setRemoveComments( $removeComments ); $options->setMaxIncludeSize( self::MAX_INCLUDE_SIZE ); $titleStr = $request->getText( 'wpContextTitle' ); $title = Title::newFromText( $titleStr ); if ( !$title ) { $title = $this->getPageTitle(); $options->setTargetLanguage( $this->getContentLanguage() ); } $parser = $this->parserFactory->getInstance(); if ( $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 ); $this->makeForm(); $out = $this->getOutput(); if ( $generateXML && strlen( $output ) > 0 ) { // @phan-suppress-next-line PhanPossiblyUndeclaredVariable xml is set when used $out->addHTML( $this->makeOutput( $xml, 'expand_templates_xml_output' ) ); } $tmp = $this->makeOutput( $output ); if ( $removeNowiki ) { $tmp = preg_replace( [ '_<nowiki>_', '_</nowiki>_', '_<nowiki */>_' ], '', $tmp ); } $tmp = $this->tidy->tidy( $tmp ); $out->addHTML( $tmp ); $pout = $parser->parse( $output, $title, $options ); // TODO T371008 consider if using the Content framework makes sense instead of creating the pipeline $rawhtml = MediaWikiServices::getInstance()->getDefaultOutputPipeline() ->run( $pout, $options, [ 'enableSectionEditLinks' => false ] )->getContentHolderText(); if ( $generateRawHtml && strlen( $rawhtml ) > 0 ) { $out->addHTML( $this->makeOutput( $rawhtml, 'expand_templates_html_output' ) ); } $this->showHtmlPreview( $title, $pout, $out ); } else { $this->makeForm(); } } /** * 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 */ private function makeForm() { $fields = [ 'Input' => [ 'type' => 'textarea', 'label' => $this->msg( 'expand_templates_input' )->text(), 'rows' => 10, 'id' => 'input', 'useeditfont' => true, 'required' => true, 'autofocus' => true, ], 'ContextTitle' => [ 'type' => 'text', 'label' => $this->msg( 'expand_templates_title' )->plain(), 'id' => 'contexttitle', 'size' => 60, ], 'RemoveComments' => [ 'type' => 'check', 'label' => $this->msg( 'expand_templates_remove_comments' )->text(), 'id' => 'removecomments', 'default' => true, ], 'RemoveNowiki' => [ 'type' => 'check', 'label' => $this->msg( 'expand_templates_remove_nowiki' )->text(), 'id' => 'removenowiki', ], 'GenerateXml' => [ 'type' => 'check', 'label' => $this->msg( 'expand_templates_generate_xml' )->text(), 'id' => 'generate_xml', ], 'GenerateRawHtml' => [ 'type' => 'check', 'label' => $this->msg( 'expand_templates_generate_rawhtml' )->text(), 'id' => 'generate_rawhtml', ], ]; $form = HTMLForm::factory( 'ooui', $fields, $this->getContext() ); $form ->setSubmitTextMsg( 'expand_templates_ok' ) ->setWrapperLegendMsg( 'expandtemplates' ) ->setHeaderHtml( $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 = "