MediaWiki  REL1_24
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 
00055     function execute( $subpage ) {
00056         global $wgParser;
00057 
00058         $this->setHeaders();
00059 
00060         $request = $this->getRequest();
00061         $titleStr = $request->getText( 'wpContextTitle' );
00062         $title = Title::newFromText( $titleStr );
00063 
00064         if ( !$title ) {
00065             $title = $this->getPageTitle();
00066         }
00067         $input = $request->getText( 'wpInput' );
00068         $this->generateXML = $request->getBool( 'wpGenerateXml' );
00069         $this->generateRawHtml = $request->getBool( 'wpGenerateRawHtml' );
00070 
00071         if ( strlen( $input ) ) {
00072             $this->removeComments = $request->getBool( 'wpRemoveComments', false );
00073             $this->removeNowiki = $request->getBool( 'wpRemoveNowiki', false );
00074             $options = ParserOptions::newFromContext( $this->getContext() );
00075             $options->setRemoveComments( $this->removeComments );
00076             $options->setTidy( true );
00077             $options->setMaxIncludeSize( self::MAX_INCLUDE_SIZE );
00078 
00079             if ( $this->generateXML ) {
00080                 $wgParser->startExternalParse( $title, $options, OT_PREPROCESS );
00081                 $dom = $wgParser->preprocessToDom( $input );
00082 
00083                 if ( method_exists( $dom, 'saveXML' ) ) {
00084                     $xml = $dom->saveXML();
00085                 } else {
00086                     $xml = $dom->__toString();
00087                 }
00088             }
00089 
00090             $output = $wgParser->preprocess( $input, $title, $options );
00091         } else {
00092             $this->removeComments = $request->getBool( 'wpRemoveComments', true );
00093             $this->removeNowiki = $request->getBool( 'wpRemoveNowiki', false );
00094             $output = false;
00095         }
00096 
00097         $out = $this->getOutput();
00098         $out->addWikiMsg( 'expand_templates_intro' );
00099         $out->addHTML( $this->makeForm( $titleStr, $input ) );
00100 
00101         if ( $output !== false ) {
00102             if ( $this->generateXML && strlen( $output ) > 0 ) {
00103                 $out->addHTML( $this->makeOutput( $xml, 'expand_templates_xml_output' ) );
00104             }
00105 
00106             $tmp = $this->makeOutput( $output );
00107 
00108             if ( $this->removeNowiki ) {
00109                 $tmp = preg_replace(
00110                     array( '_&lt;nowiki&gt;_', '_&lt;/nowiki&gt;_', '_&lt;nowiki */&gt;_' ),
00111                     '',
00112                     $tmp
00113                 );
00114             }
00115 
00116             $config = $this->getConfig();
00117             if ( ( $config->get( 'UseTidy' ) && $options->getTidy() ) || $config->get( 'AlwaysUseTidy' ) ) {
00118                 $tmp = MWTidy::tidy( $tmp );
00119             }
00120 
00121             $out->addHTML( $tmp );
00122 
00123             $pout = $this->generateHtml( $title, $output );
00124             $rawhtml = $pout->getText();
00125             if ( $this->generateRawHtml && strlen( $rawhtml ) > 0 ) {
00126                 $out->addHTML( $this->makeOutput( $rawhtml, 'expand_templates_html_output' ) );
00127             }
00128 
00129             $this->showHtmlPreview( $title, $pout, $out );
00130         }
00131     }
00132 
00140     private function makeForm( $title, $input ) {
00141         $self = $this->getPageTitle();
00142         $form = Xml::openElement(
00143             'form',
00144             array( 'method' => 'post', 'action' => $self->getLocalUrl() )
00145         );
00146         $form .= "<fieldset><legend>" . $this->msg( 'expandtemplates' )->escaped() . "</legend>\n";
00147 
00148         $form .= '<p>' . Xml::inputLabel(
00149             $this->msg( 'expand_templates_title' )->plain(),
00150             'wpContextTitle',
00151             'contexttitle',
00152             60,
00153             $title,
00154             array( 'autofocus' => true )
00155         ) . '</p>';
00156         $form .= '<p>' . Xml::label(
00157             $this->msg( 'expand_templates_input' )->text(),
00158             'input'
00159         ) . '</p>';
00160         $form .= Xml::textarea(
00161             'wpInput',
00162             $input,
00163             10,
00164             10,
00165             array( 'id' => 'input' )
00166         );
00167 
00168         $form .= '<p>' . Xml::checkLabel(
00169             $this->msg( 'expand_templates_remove_comments' )->text(),
00170             'wpRemoveComments',
00171             'removecomments',
00172             $this->removeComments
00173         ) . '</p>';
00174         $form .= '<p>' . Xml::checkLabel(
00175             $this->msg( 'expand_templates_remove_nowiki' )->text(),
00176             'wpRemoveNowiki',
00177             'removenowiki',
00178             $this->removeNowiki
00179         ) . '</p>';
00180         $form .= '<p>' . Xml::checkLabel(
00181             $this->msg( 'expand_templates_generate_xml' )->text(),
00182             'wpGenerateXml',
00183             'generate_xml',
00184             $this->generateXML
00185         ) . '</p>';
00186         $form .= '<p>' . Xml::checkLabel(
00187             $this->msg( 'expand_templates_generate_rawhtml' )->text(),
00188             'wpGenerateRawHtml',
00189             'generate_rawhtml',
00190             $this->generateRawHtml
00191         ) . '</p>';
00192         $form .= '<p>' . Xml::submitButton(
00193             $this->msg( 'expand_templates_ok' )->text(),
00194             array( 'accesskey' => 's' )
00195         ) . '</p>';
00196         $form .= "</fieldset>\n";
00197         $form .= Xml::closeElement( 'form' );
00198 
00199         return $form;
00200     }
00201 
00209     private function makeOutput( $output, $heading = 'expand_templates_output' ) {
00210         $out = "<h2>" . $this->msg( $heading )->escaped() . "</h2>\n";
00211         $out .= Xml::textarea(
00212             'output',
00213             $output,
00214             10,
00215             10,
00216             array( 'id' => 'output', 'readonly' => 'readonly' )
00217         );
00218 
00219         return $out;
00220     }
00221 
00229     private function generateHtml( Title $title, $text ) {
00230         global $wgParser;
00231 
00232         $popts = ParserOptions::newFromContext( $this->getContext() );
00233         $popts->setTargetLanguage( $title->getPageLanguage() );
00234         return $wgParser->parse( $text, $title, $popts );
00235     }
00236 
00244     private function showHtmlPreview( Title $title, ParserOutput $pout, 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->addParserOutputContent( $pout );
00253         $out->addHTML( Html::closeElement( 'div' ) );
00254     }
00255 
00256     protected function getGroupName() {
00257         return 'wiki';
00258     }
00259 }