[ Index ] |
PHP Cross Reference of MediaWiki-1.24.0 |
[Summary view] [Print] [Text view]
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 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 32 /** @var bool Whether or not to show the XML parse tree */ 33 protected $generateXML; 34 35 /** @var bool Whether or not to show the raw HTML code */ 36 protected $generateRawHtml; 37 38 /** @var bool Whether or not to remove comments in the expanded wikitext */ 39 protected $removeComments; 40 41 /** @var bool Whether or not to remove <nowiki> tags in the expanded wikitext */ 42 protected $removeNowiki; 43 44 /** @var int Maximum size in bytes to include. 50MB allows fixing those huge pages */ 45 const MAX_INCLUDE_SIZE = 50000000; 46 47 function __construct() { 48 parent::__construct( 'ExpandTemplates' ); 49 } 50 51 /** 52 * Show the special page 53 * @param string|null $subpage 54 */ 55 function execute( $subpage ) { 56 global $wgParser; 57 58 $this->setHeaders(); 59 60 $request = $this->getRequest(); 61 $titleStr = $request->getText( 'wpContextTitle' ); 62 $title = Title::newFromText( $titleStr ); 63 64 if ( !$title ) { 65 $title = $this->getPageTitle(); 66 } 67 $input = $request->getText( 'wpInput' ); 68 $this->generateXML = $request->getBool( 'wpGenerateXml' ); 69 $this->generateRawHtml = $request->getBool( 'wpGenerateRawHtml' ); 70 71 if ( strlen( $input ) ) { 72 $this->removeComments = $request->getBool( 'wpRemoveComments', false ); 73 $this->removeNowiki = $request->getBool( 'wpRemoveNowiki', false ); 74 $options = ParserOptions::newFromContext( $this->getContext() ); 75 $options->setRemoveComments( $this->removeComments ); 76 $options->setTidy( true ); 77 $options->setMaxIncludeSize( self::MAX_INCLUDE_SIZE ); 78 79 if ( $this->generateXML ) { 80 $wgParser->startExternalParse( $title, $options, OT_PREPROCESS ); 81 $dom = $wgParser->preprocessToDom( $input ); 82 83 if ( method_exists( $dom, 'saveXML' ) ) { 84 $xml = $dom->saveXML(); 85 } else { 86 $xml = $dom->__toString(); 87 } 88 } 89 90 $output = $wgParser->preprocess( $input, $title, $options ); 91 } else { 92 $this->removeComments = $request->getBool( 'wpRemoveComments', true ); 93 $this->removeNowiki = $request->getBool( 'wpRemoveNowiki', false ); 94 $output = false; 95 } 96 97 $out = $this->getOutput(); 98 $out->addWikiMsg( 'expand_templates_intro' ); 99 $out->addHTML( $this->makeForm( $titleStr, $input ) ); 100 101 if ( $output !== false ) { 102 if ( $this->generateXML && strlen( $output ) > 0 ) { 103 $out->addHTML( $this->makeOutput( $xml, 'expand_templates_xml_output' ) ); 104 } 105 106 $tmp = $this->makeOutput( $output ); 107 108 if ( $this->removeNowiki ) { 109 $tmp = preg_replace( 110 array( '_<nowiki>_', '_</nowiki>_', '_<nowiki */>_' ), 111 '', 112 $tmp 113 ); 114 } 115 116 $config = $this->getConfig(); 117 if ( ( $config->get( 'UseTidy' ) && $options->getTidy() ) || $config->get( 'AlwaysUseTidy' ) ) { 118 $tmp = MWTidy::tidy( $tmp ); 119 } 120 121 $out->addHTML( $tmp ); 122 123 $pout = $this->generateHtml( $title, $output ); 124 $rawhtml = $pout->getText(); 125 if ( $this->generateRawHtml && strlen( $rawhtml ) > 0 ) { 126 $out->addHTML( $this->makeOutput( $rawhtml, 'expand_templates_html_output' ) ); 127 } 128 129 $this->showHtmlPreview( $title, $pout, $out ); 130 } 131 } 132 133 /** 134 * Generate a form allowing users to enter information 135 * 136 * @param string $title Value for context title field 137 * @param string $input Value for input textbox 138 * @return string 139 */ 140 private function makeForm( $title, $input ) { 141 $self = $this->getPageTitle(); 142 $request = $this->getRequest(); 143 $user = $this->getUser(); 144 145 $form = Xml::openElement( 146 'form', 147 array( 'method' => 'post', 'action' => $self->getLocalUrl() ) 148 ); 149 $form .= "<fieldset><legend>" . $this->msg( 'expandtemplates' )->escaped() . "</legend>\n"; 150 151 $form .= '<p>' . Xml::inputLabel( 152 $this->msg( 'expand_templates_title' )->plain(), 153 'wpContextTitle', 154 'contexttitle', 155 60, 156 $title, 157 array( 'autofocus' => true ) 158 ) . '</p>'; 159 $form .= '<p>' . Xml::label( 160 $this->msg( 'expand_templates_input' )->text(), 161 'input' 162 ) . '</p>'; 163 $form .= Xml::textarea( 164 'wpInput', 165 $input, 166 10, 167 10, 168 array( 'id' => 'input' ) 169 ); 170 171 $form .= '<p>' . Xml::checkLabel( 172 $this->msg( 'expand_templates_remove_comments' )->text(), 173 'wpRemoveComments', 174 'removecomments', 175 $this->removeComments 176 ) . '</p>'; 177 $form .= '<p>' . Xml::checkLabel( 178 $this->msg( 'expand_templates_remove_nowiki' )->text(), 179 'wpRemoveNowiki', 180 'removenowiki', 181 $this->removeNowiki 182 ) . '</p>'; 183 $form .= '<p>' . Xml::checkLabel( 184 $this->msg( 'expand_templates_generate_xml' )->text(), 185 'wpGenerateXml', 186 'generate_xml', 187 $this->generateXML 188 ) . '</p>'; 189 $form .= '<p>' . Xml::checkLabel( 190 $this->msg( 'expand_templates_generate_rawhtml' )->text(), 191 'wpGenerateRawHtml', 192 'generate_rawhtml', 193 $this->generateRawHtml 194 ) . '</p>'; 195 $form .= '<p>' . Xml::submitButton( 196 $this->msg( 'expand_templates_ok' )->text(), 197 array( 'accesskey' => 's' ) 198 ) . '</p>'; 199 $form .= "</fieldset>\n"; 200 $form .= Html::hidden( 'wpEditToken', $user->getEditToken( '', $request ) ); 201 $form .= Xml::closeElement( 'form' ); 202 203 return $form; 204 } 205 206 /** 207 * Generate a nice little box with a heading for output 208 * 209 * @param string $output Wiki text output 210 * @param string $heading 211 * @return string 212 */ 213 private function makeOutput( $output, $heading = 'expand_templates_output' ) { 214 $out = "<h2>" . $this->msg( $heading )->escaped() . "</h2>\n"; 215 $out .= Xml::textarea( 216 'output', 217 $output, 218 10, 219 10, 220 array( 'id' => 'output', 'readonly' => 'readonly' ) 221 ); 222 223 return $out; 224 } 225 226 /** 227 * Renders the supplied wikitext as html 228 * 229 * @param Title $title 230 * @param string $text 231 * @return ParserOutput 232 */ 233 private function generateHtml( Title $title, $text ) { 234 global $wgParser; 235 236 $popts = ParserOptions::newFromContext( $this->getContext() ); 237 $popts->setTargetLanguage( $title->getPageLanguage() ); 238 return $wgParser->parse( $text, $title, $popts ); 239 } 240 241 /** 242 * Wraps the provided html code in a div and outputs it to the page 243 * 244 * @param Title $title 245 * @param ParserOutput $pout 246 * @param OutputPage $out 247 */ 248 private function showHtmlPreview( Title $title, ParserOutput $pout, OutputPage $out ) { 249 $lang = $title->getPageViewLanguage(); 250 $out->addHTML( "<h2>" . $this->msg( 'expand_templates_preview' )->escaped() . "</h2>\n" ); 251 252 if ( $this->getConfig()->get( '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 (bug 71111). 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() && !$user->isAllowed( 'edit' ) ) { 261 $error = array( 'expand_templates_preview_fail_html_anon' ); 262 } elseif ( !$user->matchEditToken( $request->getVal( 'wpEditToken' ), '', $request ) ) { 263 $error = array( 'expand_templates_preview_fail_html' ); 264 } else { 265 $error = false; 266 } 267 268 if ( $error ) { 269 $out->wrapWikiMsg( "<div class='previewnote'>\n$1\n</div>", $error ); 270 return; 271 } 272 } 273 274 $out->addHTML( Html::openElement( 'div', array( 275 'class' => 'mw-content-' . $lang->getDir(), 276 'dir' => $lang->getDir(), 277 'lang' => $lang->getHtmlCode(), 278 ) ) ); 279 $out->addParserOutputContent( $pout ); 280 $out->addHTML( Html::closeElement( 'div' ) ); 281 } 282 283 protected function getGroupName() { 284 return 'wiki'; 285 } 286 }
title
Description
Body
title
Description
Body
title
Description
Body
title
Body
Generated: Fri Nov 28 14:03:12 2014 | Cross-referenced by PHPXref 0.7.1 |