SpecialExpandTemplates.php 9.1 KB

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