MediaWiki  1.23.6
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();
141  'form',
142  array( 'method' => 'post', 'action' => $self->getLocalUrl() )
143  );
144  $form .= "<fieldset><legend>" . $this->msg( 'expandtemplates' )->escaped() . "</legend>\n";
145 
146  $form .= '<p>' . Xml::inputLabel(
147  $this->msg( 'expand_templates_title' )->plain(),
148  'wpContextTitle',
149  'contexttitle',
150  60,
151  $title,
152  array( 'autofocus' => true )
153  ) . '</p>';
154  $form .= '<p>' . Xml::label(
155  $this->msg( 'expand_templates_input' )->text(),
156  'input'
157  ) . '</p>';
158  $form .= Xml::textarea(
159  'wpInput',
160  $input,
161  10,
162  10,
163  array( 'id' => 'input' )
164  );
165 
166  $form .= '<p>' . Xml::checkLabel(
167  $this->msg( 'expand_templates_remove_comments' )->text(),
168  'wpRemoveComments',
169  'removecomments',
170  $this->removeComments
171  ) . '</p>';
172  $form .= '<p>' . Xml::checkLabel(
173  $this->msg( 'expand_templates_remove_nowiki' )->text(),
174  'wpRemoveNowiki',
175  'removenowiki',
176  $this->removeNowiki
177  ) . '</p>';
178  $form .= '<p>' . Xml::checkLabel(
179  $this->msg( 'expand_templates_generate_xml' )->text(),
180  'wpGenerateXml',
181  'generate_xml',
182  $this->generateXML
183  ) . '</p>';
184  $form .= '<p>' . Xml::checkLabel(
185  $this->msg( 'expand_templates_generate_rawhtml' )->text(),
186  'wpGenerateRawHtml',
187  'generate_rawhtml',
188  $this->generateRawHtml
189  ) . '</p>';
190  $form .= '<p>' . Xml::submitButton(
191  $this->msg( 'expand_templates_ok' )->text(),
192  array( 'accesskey' => 's' )
193  ) . '</p>';
194  $form .= "</fieldset>\n";
195  $form .= Xml::closeElement( 'form' );
196 
197  return $form;
198  }
199 
207  private function makeOutput( $output, $heading = 'expand_templates_output' ) {
208  $out = "<h2>" . $this->msg( $heading )->escaped() . "</h2>\n";
209  $out .= Xml::textarea(
210  'output',
211  $output,
212  10,
213  10,
214  array( 'id' => 'output', 'readonly' => 'readonly' )
215  );
216 
217  return $out;
218  }
219 
227  private function generateHtml( Title $title, $text ) {
229 
230  $popts = ParserOptions::newFromContext( $this->getContext() );
231  $popts->setTargetLanguage( $title->getPageLanguage() );
232  $pout = $wgParser->parse( $text, $title, $popts );
233 
234  return $pout->getText();
235  }
236 
244  private function showHtmlPreview( Title $title, $html, OutputPage $out ) {
245  $lang = $title->getPageViewLanguage();
246  $out->addHTML( "<h2>" . $this->msg( 'expand_templates_preview' )->escaped() . "</h2>\n" );
247  $out->addHTML( Html::openElement( 'div', array(
248  'class' => 'mw-content-' . $lang->getDir(),
249  'dir' => $lang->getDir(),
250  'lang' => $lang->getHtmlCode(),
251  ) ) );
252  $out->addHTML( $html );
253  $out->addHTML( Html::closeElement( 'div' ) );
254  }
255 
256  protected function getGroupName() {
257  return 'wiki';
258  }
259 }
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:2573
SpecialExpandTemplates\execute
execute( $subpage)
Show the special page.
Definition: SpecialExpandTemplates.php:50
OT_PREPROCESS
const OT_PREPROCESS
Definition: Defines.php:232
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:203
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:240
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
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:252
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
SpecialExpandTemplates\generateHtml
generateHtml(Title $title, $text)
Renders the supplied wikitext as html.
Definition: SpecialExpandTemplates.php:223
$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:567
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
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