MediaWiki  REL1_23
SpecialExpandTemplates.php
Go to the documentation of this file.
00001 <?php
00030 class SpecialExpandTemplates extends SpecialPage {
00031 
00033     protected $generateXML;
00034 
00036     protected $generateRawHtml;
00037 
00039     protected $removeComments;
00040 
00042     protected $removeNowiki;
00043 
00045     const MAX_INCLUDE_SIZE = 50000000;
00046 
00047     function __construct() {
00048         parent::__construct( 'ExpandTemplates' );
00049     }
00050 
00054     function execute( $subpage ) {
00055         global $wgParser, $wgUseTidy, $wgAlwaysUseTidy;
00056 
00057         $this->setHeaders();
00058 
00059         $request = $this->getRequest();
00060         $titleStr = $request->getText( 'wpContextTitle' );
00061         $title = Title::newFromText( $titleStr );
00062 
00063         if ( !$title ) {
00064             $title = $this->getPageTitle();
00065         }
00066         $input = $request->getText( 'wpInput' );
00067         $this->generateXML = $request->getBool( 'wpGenerateXml' );
00068         $this->generateRawHtml = $request->getBool( 'wpGenerateRawHtml' );
00069 
00070         if ( strlen( $input ) ) {
00071             $this->removeComments = $request->getBool( 'wpRemoveComments', false );
00072             $this->removeNowiki = $request->getBool( 'wpRemoveNowiki', false );
00073             $options = ParserOptions::newFromContext( $this->getContext() );
00074             $options->setRemoveComments( $this->removeComments );
00075             $options->setTidy( true );
00076             $options->setMaxIncludeSize( self::MAX_INCLUDE_SIZE );
00077 
00078             if ( $this->generateXML ) {
00079                 $wgParser->startExternalParse( $title, $options, OT_PREPROCESS );
00080                 $dom = $wgParser->preprocessToDom( $input );
00081 
00082                 if ( method_exists( $dom, 'saveXML' ) ) {
00083                     $xml = $dom->saveXML();
00084                 } else {
00085                     $xml = $dom->__toString();
00086                 }
00087             }
00088 
00089             $output = $wgParser->preprocess( $input, $title, $options );
00090         } else {
00091             $this->removeComments = $request->getBool( 'wpRemoveComments', true );
00092             $this->removeNowiki = $request->getBool( 'wpRemoveNowiki', false );
00093             $output = false;
00094         }
00095 
00096         $out = $this->getOutput();
00097         $out->addWikiMsg( 'expand_templates_intro' );
00098         $out->addHTML( $this->makeForm( $titleStr, $input ) );
00099 
00100         if ( $output !== false ) {
00101             if ( $this->generateXML && strlen( $output ) > 0 ) {
00102                 $out->addHTML( $this->makeOutput( $xml, 'expand_templates_xml_output' ) );
00103             }
00104 
00105             $tmp = $this->makeOutput( $output );
00106 
00107             if ( $this->removeNowiki ) {
00108                 $tmp = preg_replace(
00109                     array( '_&lt;nowiki&gt;_', '_&lt;/nowiki&gt;_', '_&lt;nowiki */&gt;_' ),
00110                     '',
00111                     $tmp
00112                 );
00113             }
00114 
00115             if ( ( $wgUseTidy && $options->getTidy() ) || $wgAlwaysUseTidy ) {
00116                 $tmp = MWTidy::tidy( $tmp );
00117             }
00118 
00119             $out->addHTML( $tmp );
00120 
00121             $rawhtml = $this->generateHtml( $title, $output );
00122 
00123             if ( $this->generateRawHtml && strlen( $rawhtml ) > 0 ) {
00124                 $out->addHTML( $this->makeOutput( $rawhtml, 'expand_templates_html_output' ) );
00125             }
00126 
00127             $this->showHtmlPreview( $title, $rawhtml, $out );
00128         }
00129     }
00130 
00138     private function makeForm( $title, $input ) {
00139         $self = $this->getPageTitle();
00140         $form = Xml::openElement(
00141             'form',
00142             array( 'method' => 'post', 'action' => $self->getLocalUrl() )
00143         );
00144         $form .= "<fieldset><legend>" . $this->msg( 'expandtemplates' )->escaped() . "</legend>\n";
00145 
00146         $form .= '<p>' . Xml::inputLabel(
00147             $this->msg( 'expand_templates_title' )->plain(),
00148             'wpContextTitle',
00149             'contexttitle',
00150             60,
00151             $title,
00152             array( 'autofocus' => true )
00153         ) . '</p>';
00154         $form .= '<p>' . Xml::label(
00155             $this->msg( 'expand_templates_input' )->text(),
00156             'input'
00157         ) . '</p>';
00158         $form .= Xml::textarea(
00159             'wpInput',
00160             $input,
00161             10,
00162             10,
00163             array( 'id' => 'input' )
00164         );
00165 
00166         $form .= '<p>' . Xml::checkLabel(
00167             $this->msg( 'expand_templates_remove_comments' )->text(),
00168             'wpRemoveComments',
00169             'removecomments',
00170             $this->removeComments
00171         ) . '</p>';
00172         $form .= '<p>' . Xml::checkLabel(
00173             $this->msg( 'expand_templates_remove_nowiki' )->text(),
00174             'wpRemoveNowiki',
00175             'removenowiki',
00176             $this->removeNowiki
00177         ) . '</p>';
00178         $form .= '<p>' . Xml::checkLabel(
00179             $this->msg( 'expand_templates_generate_xml' )->text(),
00180             'wpGenerateXml',
00181             'generate_xml',
00182             $this->generateXML
00183         ) . '</p>';
00184         $form .= '<p>' . Xml::checkLabel(
00185             $this->msg( 'expand_templates_generate_rawhtml' )->text(),
00186             'wpGenerateRawHtml',
00187             'generate_rawhtml',
00188             $this->generateRawHtml
00189         ) . '</p>';
00190         $form .= '<p>' . Xml::submitButton(
00191             $this->msg( 'expand_templates_ok' )->text(),
00192             array( 'accesskey' => 's' )
00193         ) . '</p>';
00194         $form .= "</fieldset>\n";
00195         $form .= Xml::closeElement( 'form' );
00196 
00197         return $form;
00198     }
00199 
00207     private function makeOutput( $output, $heading = 'expand_templates_output' ) {
00208         $out = "<h2>" . $this->msg( $heading )->escaped() . "</h2>\n";
00209         $out .= Xml::textarea(
00210             'output',
00211             $output,
00212             10,
00213             10,
00214             array( 'id' => 'output', 'readonly' => 'readonly' )
00215         );
00216 
00217         return $out;
00218     }
00219 
00227     private function generateHtml( Title $title, $text ) {
00228         global $wgParser;
00229 
00230         $popts = ParserOptions::newFromContext( $this->getContext() );
00231         $popts->setTargetLanguage( $title->getPageLanguage() );
00232         $pout = $wgParser->parse( $text, $title, $popts );
00233 
00234         return $pout->getText();
00235     }
00236 
00244     private function showHtmlPreview( Title $title, $html, OutputPage $out ) {
00245         $lang = $title->getPageViewLanguage();
00246         $out->addHTML( "<h2>" . $this->msg( 'expand_templates_preview' )->escaped() . "</h2>\n" );
00247         $out->addHTML( Html::openElement( 'div', array(
00248             'class' => 'mw-content-' . $lang->getDir(),
00249             'dir' => $lang->getDir(),
00250             'lang' => $lang->getHtmlCode(),
00251         ) ) );
00252         $out->addHTML( $html );
00253         $out->addHTML( Html::closeElement( 'div' ) );
00254     }
00255 
00256     protected function getGroupName() {
00257         return 'wiki';
00258     }
00259 }