MediaWiki  1.23.13
SpecialExpandTemplates.php
Go to the documentation of this file.
1 <?php
31 
33  protected $generateXML;
34 
36  protected $generateRawHtml;
37 
39  protected $removeComments;
40 
42  protected $removeNowiki;
43 
45  const MAX_INCLUDE_SIZE = 50000000;
46 
47  function __construct() {
48  parent::__construct( 'ExpandTemplates' );
49  }
50 
54  function execute( $subpage ) {
55  global $wgParser, $wgUseTidy, $wgAlwaysUseTidy;
56 
57  $this->setHeaders();
58 
59  $request = $this->getRequest();
60  $titleStr = $request->getText( 'wpContextTitle' );
61  $title = Title::newFromText( $titleStr );
62 
63  if ( !$title ) {
64  $title = $this->getPageTitle();
65  }
66  $input = $request->getText( 'wpInput' );
67  $this->generateXML = $request->getBool( 'wpGenerateXml' );
68  $this->generateRawHtml = $request->getBool( 'wpGenerateRawHtml' );
69 
70  if ( strlen( $input ) ) {
71  $this->removeComments = $request->getBool( 'wpRemoveComments', false );
72  $this->removeNowiki = $request->getBool( 'wpRemoveNowiki', false );
74  $options->setRemoveComments( $this->removeComments );
75  $options->setTidy( true );
76  $options->setMaxIncludeSize( self::MAX_INCLUDE_SIZE );
77 
78  if ( $this->generateXML ) {
79  $wgParser->startExternalParse( $title, $options, OT_PREPROCESS );
80  $dom = $wgParser->preprocessToDom( $input );
81 
82  if ( method_exists( $dom, 'saveXML' ) ) {
83  $xml = $dom->saveXML();
84  } else {
85  $xml = $dom->__toString();
86  }
87  }
88 
89  $output = $wgParser->preprocess( $input, $title, $options );
90  } else {
91  $this->removeComments = $request->getBool( 'wpRemoveComments', true );
92  $this->removeNowiki = $request->getBool( 'wpRemoveNowiki', false );
93  $output = false;
94  }
95 
96  $out = $this->getOutput();
97  $out->addWikiMsg( 'expand_templates_intro' );
98  $out->addHTML( $this->makeForm( $titleStr, $input ) );
99 
100  if ( $output !== false ) {
101  if ( $this->generateXML && strlen( $output ) > 0 ) {
102  $out->addHTML( $this->makeOutput( $xml, 'expand_templates_xml_output' ) );
103  }
104 
105  $tmp = $this->makeOutput( $output );
106 
107  if ( $this->removeNowiki ) {
108  $tmp = preg_replace(
109  array( '_&lt;nowiki&gt;_', '_&lt;/nowiki&gt;_', '_&lt;nowiki */&gt;_' ),
110  '',
111  $tmp
112  );
113  }
114 
115  if ( ( $wgUseTidy && $options->getTidy() ) || $wgAlwaysUseTidy ) {
116  $tmp = MWTidy::tidy( $tmp );
117  }
118 
119  $out->addHTML( $tmp );
120 
121  $rawhtml = $this->generateHtml( $title, $output );
122 
123  if ( $this->generateRawHtml && strlen( $rawhtml ) > 0 ) {
124  $out->addHTML( $this->makeOutput( $rawhtml, 'expand_templates_html_output' ) );
125  }
126 
127  $this->showHtmlPreview( $title, $rawhtml, $out );
128  }
129  }
130 
138  private function makeForm( $title, $input ) {
139  $self = $this->getPageTitle();
140  $request = $this->getRequest();
141  $user = $this->getUser();
142 
144  'form',
145  array( 'method' => 'post', 'action' => $self->getLocalUrl() )
146  );
147  $form .= "<fieldset><legend>" . $this->msg( 'expandtemplates' )->escaped() . "</legend>\n";
148 
149  $form .= '<p>' . Xml::inputLabel(
150  $this->msg( 'expand_templates_title' )->plain(),
151  'wpContextTitle',
152  'contexttitle',
153  60,
154  $title,
155  array( 'autofocus' => true )
156  ) . '</p>';
157  $form .= '<p>' . Xml::label(
158  $this->msg( 'expand_templates_input' )->text(),
159  'input'
160  ) . '</p>';
161  $form .= Xml::textarea(
162  'wpInput',
163  $input,
164  10,
165  10,
166  array( 'id' => 'input' )
167  );
168 
169  $form .= '<p>' . Xml::checkLabel(
170  $this->msg( 'expand_templates_remove_comments' )->text(),
171  'wpRemoveComments',
172  'removecomments',
173  $this->removeComments
174  ) . '</p>';
175  $form .= '<p>' . Xml::checkLabel(
176  $this->msg( 'expand_templates_remove_nowiki' )->text(),
177  'wpRemoveNowiki',
178  'removenowiki',
179  $this->removeNowiki
180  ) . '</p>';
181  $form .= '<p>' . Xml::checkLabel(
182  $this->msg( 'expand_templates_generate_xml' )->text(),
183  'wpGenerateXml',
184  'generate_xml',
185  $this->generateXML
186  ) . '</p>';
187  $form .= '<p>' . Xml::checkLabel(
188  $this->msg( 'expand_templates_generate_rawhtml' )->text(),
189  'wpGenerateRawHtml',
190  'generate_rawhtml',
191  $this->generateRawHtml
192  ) . '</p>';
193  $form .= '<p>' . Xml::submitButton(
194  $this->msg( 'expand_templates_ok' )->text(),
195  array( 'accesskey' => 's' )
196  ) . '</p>';
197  $form .= "</fieldset>\n";
198  $form .= Html::hidden( 'wpEditToken', $user->getEditToken( '', $request ) );
199  $form .= Xml::closeElement( 'form' );
200 
201  return $form;
202  }
203 
211  private function makeOutput( $output, $heading = 'expand_templates_output' ) {
212  $out = "<h2>" . $this->msg( $heading )->escaped() . "</h2>\n";
213  $out .= Xml::textarea(
214  'output',
215  $output,
216  10,
217  10,
218  array( 'id' => 'output', 'readonly' => 'readonly' )
219  );
220 
221  return $out;
222  }
223 
231  private function generateHtml( Title $title, $text ) {
233 
234  $popts = ParserOptions::newFromContext( $this->getContext() );
235  $popts->setTargetLanguage( $title->getPageLanguage() );
236  $pout = $wgParser->parse( $text, $title, $popts );
237 
238  return $pout->getText();
239  }
240 
248  private function showHtmlPreview( Title $title, $html, OutputPage $out ) {
249  $lang = $title->getPageViewLanguage();
250  $out->addHTML( "<h2>" . $this->msg( 'expand_templates_preview' )->escaped() . "</h2>\n" );
251 
252  global $wgRawHtml;
253  if ( $wgRawHtml ) {
254  $request = $this->getRequest();
255  $user = $this->getUser();
256 
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 (bug 71111). 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() && !$user->isAllowed( 'edit' ) ) {
262  $error = array( 'expand_templates_preview_fail_html_anon' );
263  } elseif ( !$user->matchEditToken( $request->getVal( 'wpEditToken' ), '', $request ) ) {
264  $error = array( 'expand_templates_preview_fail_html' );
265  } else {
266  $error = false;
267  }
268 
269  if ( $error ) {
270  $out->wrapWikiMsg( "<div class='previewnote'>\n$1\n</div>", $error );
271  return;
272  }
273  }
274 
275  $out->addHTML( Html::openElement( 'div', array(
276  'class' => 'mw-content-' . $lang->getDir(),
277  'dir' => $lang->getDir(),
278  'lang' => $lang->getHtmlCode(),
279  ) ) );
280  $out->addHTML( $html );
281  $out->addHTML( Html::closeElement( 'div' ) );
282  }
283 
284  protected function getGroupName() {
285  return 'wiki';
286  }
287 }
Xml\checkLabel
static checkLabel( $label, $name, $id, $checked=false, $attribs=array())
Convenience function to build an HTML checkbox with a label.
Definition: Xml.php:433
SpecialPage\getPageTitle
getPageTitle( $subpage=false)
Get a self-referential title object.
Definition: SpecialPage.php:488
Title\newFromText
static newFromText( $text, $defaultNamespace=NS_MAIN)
Create a new Title from text, such as what one would find in a link.
Definition: Title.php:189
php
skin txt MediaWiki includes four core it has been set as the default in MediaWiki since the replacing Monobook it had been been the default skin since before being replaced by Vector largely rewritten in while keeping its appearance Several legacy skins were removed in the as the burden of supporting them became too heavy to bear Those in etc for skin dependent CSS etc for skin dependent JavaScript These can also be customised on a per user by etc This feature has led to a wide variety of user styles becoming that gallery is a good place to ending in php
Definition: skin.txt:62
$html
null means default in associative array with keys and values unescaped Should be merged with default with a value of false meaning to suppress the attribute in associative array with keys and values unescaped noclasses just before the function returns a value If you return an< a > element with HTML attributes $attribs and contents $html will be returned If you return $ret will be returned and may include noclasses & $html
Definition: hooks.txt:1530
SpecialPage\getOutput
getOutput()
Get the OutputPage being used for this instance.
Definition: SpecialPage.php:535
text
design txt This is a brief overview of the new design More thorough and up to date information is available on the documentation wiki at etc Handles the details of getting and saving to the user table of the and dealing with sessions and cookies OutputPage Encapsulates the entire HTML page that will be sent in response to any server request It is used by calling its functions to add text
Definition: design.txt:12
$form
usually copyright or history_copyright This message must be in HTML not wikitext $subpages will be ignored and the rest of subPageSubtitle() will run. 'SkinTemplateBuildNavUrlsNav_urlsAfterPermalink' whether MediaWiki currently thinks this is a CSS JS page Hooks may change this value to override the return value of Title::isCssOrJsPage(). 'TitleIsAlwaysKnown' whether MediaWiki currently thinks this page is known isMovable() always returns false. $title whether MediaWiki currently thinks this page is movable Hooks may change this value to override the return value of Title::isMovable(). 'TitleIsWikitextPage' whether MediaWiki currently thinks this is a wikitext page Hooks may change this value to override the return value of Title::isWikitextPage() 'TitleMove' use UploadVerification and UploadVerifyFile instead $form
Definition: hooks.txt:2578
SpecialExpandTemplates\execute
execute( $subpage)
Show the special page.
Definition: SpecialExpandTemplates.php:50
OT_PREPROCESS
const OT_PREPROCESS
Definition: Defines.php:232
Html\hidden
static hidden( $name, $value, $attribs=array())
Convenience function to produce an input element with type=hidden.
Definition: Html.php:665
SpecialExpandTemplates\$generateRawHtml
boolean $generateRawHtml
whether or not to show the raw HTML code *
Definition: SpecialExpandTemplates.php:34
Xml\openElement
static openElement( $element, $attribs=null)
This opens an XML element.
Definition: Xml.php:109
SpecialExpandTemplates\makeOutput
makeOutput( $output, $heading='expand_templates_output')
Generate a nice little box with a heading for output.
Definition: SpecialExpandTemplates.php:207
Html\closeElement
static closeElement( $element)
Returns "</$element>", except if $wgWellFormedXml is off, in which case it returns the empty string w...
Definition: Html.php:235
Html\openElement
static openElement( $element, $attribs=array())
Identical to rawElement(), but has no third parameter and omits the end tag (and the self-closing '/'...
Definition: Html.php:166
$out
$out
Definition: UtfNormalGenerate.php:167
SpecialExpandTemplates\showHtmlPreview
showHtmlPreview(Title $title, $html, OutputPage $out)
Wraps the provided html code in a div and outputs it to the page.
Definition: SpecialExpandTemplates.php:244
SpecialExpandTemplates\$removeNowiki
boolean $removeNowiki
whether or not to remove <nowiki> tags in the expanded wikitext *
Definition: SpecialExpandTemplates.php:38
array
the array() calling protocol came about after MediaWiki 1.4rc1.
List of Api Query prop modules.
SpecialPage\setHeaders
setHeaders()
Sets headers - this should be called from the execute() method of all derived classes!
Definition: SpecialPage.php:352
SpecialPage\getUser
getUser()
Shortcut to get the User executing this instance.
Definition: SpecialPage.php:545
Xml\inputLabel
static inputLabel( $label, $name, $id, $size=false, $value=false, $attribs=array())
Convenience function to build an HTML text input field with a label.
Definition: Xml.php:398
global
when a variable name is used in a it is silently declared as a new masking the global
Definition: design.txt:93
SpecialExpandTemplates\getGroupName
getGroupName()
Under which header this special page is listed in Special:SpecialPages See messages 'specialpages-gro...
Definition: SpecialExpandTemplates.php:280
OutputPage
This class should be covered by a general architecture document which does not exist as of January 20...
Definition: OutputPage.php:38
SpecialPage\getContext
getContext()
Gets the context this SpecialPage is executed in.
Definition: SpecialPage.php:508
SpecialExpandTemplates
A special page that expands submitted templates, parser functions, and variables, allowing easier deb...
Definition: SpecialExpandTemplates.php:30
$options
null means default in associative array with keys and values unescaped Should be merged with default with a value of false meaning to suppress the attribute in associative array with keys and values unescaped & $options
Definition: hooks.txt:1530
$title
presenting them properly to the user as errors is done by the caller $title
Definition: hooks.txt:1324
SpecialPage\msg
msg()
Wrapper around wfMessage that sets the current context.
Definition: SpecialPage.php:609
ParserOptions\newFromContext
static newFromContext(IContextSource $context)
Get a ParserOptions object from a IContextSource object.
Definition: ParserOptions.php:396
SpecialPage
Parent class for all special pages.
Definition: SpecialPage.php:33
SpecialPage\getRequest
getRequest()
Get the WebRequest being used for this instance.
Definition: SpecialPage.php:525
SpecialExpandTemplates\makeForm
makeForm( $title, $input)
Generate a form allowing users to enter information.
Definition: SpecialExpandTemplates.php:134
Xml\textarea
static textarea( $name, $content, $cols=40, $rows=5, $attribs=array())
Shortcut for creating textareas.
Definition: Xml.php:589
$user
please add to it if you re going to add events to the MediaWiki code where normally authentication against an external auth plugin would be creating a account $user
Definition: hooks.txt:237
SpecialExpandTemplates\generateHtml
generateHtml(Title $title, $text)
Renders the supplied wikitext as html.
Definition: SpecialExpandTemplates.php:227
$self
$self
Definition: doMaintenance.php:54
Title
Represents a title within MediaWiki.
Definition: Title.php:35
Xml\closeElement
static closeElement( $element)
Shortcut to close an XML element.
Definition: Xml.php:118
$wgParser
$wgParser
Definition: Setup.php:587
SpecialExpandTemplates\$generateXML
boolean $generateXML
whether or not to show the XML parse tree *
Definition: SpecialExpandTemplates.php:32
$output
& $output
Definition: hooks.txt:375
SpecialExpandTemplates\MAX_INCLUDE_SIZE
const MAX_INCLUDE_SIZE
Definition: SpecialExpandTemplates.php:41
Xml\submitButton
static submitButton( $value, $attribs=array())
Convenience function to build an HTML submit button.
Definition: Xml.php:463
$error
usually copyright or history_copyright This message must be in HTML not wikitext $subpages will be ignored and the rest of subPageSubtitle() will run. 'SkinTemplateBuildNavUrlsNav_urlsAfterPermalink' whether MediaWiki currently thinks this is a CSS JS page Hooks may change this value to override the return value of Title::isCssOrJsPage(). 'TitleIsAlwaysKnown' whether MediaWiki currently thinks this page is known isMovable() always returns false. $title whether MediaWiki currently thinks this page is movable Hooks may change this value to override the return value of Title::isMovable(). 'TitleIsWikitextPage' whether MediaWiki currently thinks this is a wikitext page Hooks may change this value to override the return value of Title::isWikitextPage() 'TitleMove' use UploadVerification and UploadVerifyFile instead where the first element is the message key and the remaining elements are used as parameters to the message based on mime etc Preferred in most cases over UploadVerification object with all info about the upload string as detected by MediaWiki Handlers will typically only apply for specific mime types object & $error
Definition: hooks.txt:2578
Xml\label
static label( $label, $id, $attribs=array())
Convenience function to build an HTML form label.
Definition: Xml.php:374
SpecialExpandTemplates\__construct
__construct()
Definition: SpecialExpandTemplates.php:43
MWTidy\tidy
static tidy( $text)
Interface with html tidy, used if $wgUseTidy = true.
Definition: Tidy.php:126
SpecialExpandTemplates\$removeComments
boolean $removeComments
whether or not to remove comments in the expanded wikitext *
Definition: SpecialExpandTemplates.php:36