UltrasonicNXT has uploaded a new change for review.

  https://gerrit.wikimedia.org/r/96810


Change subject: Move ExpandTemplates special into core
......................................................................

Move ExpandTemplates special into core

Very little modification to extension, add some docs, and GNU license as
per the other core extensions. I have not moved across any of the
messages; according to the bug report, translatewiki will sort this out.

Bug: 28264
Change-Id: I7ef63488dc3ad3885bcf99ff52852e1c6981942b
---
M includes/AutoLoader.php
M includes/SpecialPageFactory.php
A includes/specials/SpecialExpandTemplates.php
3 files changed, 233 insertions(+), 0 deletions(-)


  git pull ssh://gerrit.wikimedia.org:29418/mediawiki/core 
refs/changes/10/96810/1

diff --git a/includes/AutoLoader.php b/includes/AutoLoader.php
index f73f24b..632f6e1 100644
--- a/includes/AutoLoader.php
+++ b/includes/AutoLoader.php
@@ -960,6 +960,7 @@
        'SpecialContributions' => 'includes/specials/SpecialContributions.php',
        'SpecialEditWatchlist' => 'includes/specials/SpecialEditWatchlist.php',
        'SpecialEmailUser' => 'includes/specials/SpecialEmailuser.php',
+       'SpecialExpandTemplates' => 
'includes/specials/SpecialExpandTemplates.php',
        'SpecialExport' => 'includes/specials/SpecialExport.php',
        'SpecialFilepath' => 'includes/specials/SpecialFilepath.php',
        'SpecialImport' => 'includes/specials/SpecialImport.php',
diff --git a/includes/SpecialPageFactory.php b/includes/SpecialPageFactory.php
index 11edc8a..037b859 100644
--- a/includes/SpecialPageFactory.php
+++ b/includes/SpecialPageFactory.php
@@ -149,6 +149,7 @@
                'Undelete'                  => 'SpecialUndelete',
                'Whatlinkshere'             => 'SpecialWhatlinkshere',
                'MergeHistory'              => 'SpecialMergeHistory',
+               'ExpandTemplates'           => 'SpecialExpandTemplates',
 
                // Other
                'Booksources'               => 'SpecialBookSources',
diff --git a/includes/specials/SpecialExpandTemplates.php 
b/includes/specials/SpecialExpandTemplates.php
new file mode 100644
index 0000000..014a7da
--- /dev/null
+++ b/includes/specials/SpecialExpandTemplates.php
@@ -0,0 +1,231 @@
+<?php
+/**
+ * Implements Special:ExpandTemplates
+ *
+ * This program is free software; you can redistribute it and/or modify
+ * it under the terms of the GNU General Public License as published by
+ * the Free Software Foundation; either version 2 of the License, or
+ * (at your option) any later version.
+ *
+ * This program is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
+ * GNU General Public License for more details.
+ *
+ * You should have received a copy of the GNU General Public License along
+ * with this program; if not, write to the Free Software Foundation, Inc.,
+ * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
+ * http://www.gnu.org/copyleft/gpl.html
+ *
+ * @file
+ * @ingroup SpecialPage
+ */
+
+/**
+ * A special page that expands submitted templates, parser functions,
+ * and variables, allowing easier debugging of these.
+ * 
+ * @ingroup SpecialPage
+ */
+class SpecialExpandTemplates extends SpecialPage {
+       
+       /** @var boolean whether or not to show the XML parse tree */
+       var $generateXML;
+       
+       /** @var boolean whether or not to remove comments in the expanded 
wikitext */
+       var $removeComments;
+       
+       /** @var boolean whether or not to remove <nowiki> tags in the expanded 
wikitext */
+       var $removeNowiki;
+       
+       /** @var boolean */
+       var $isNewParser;
+
+       /** @var maximum size in bytes to include. 50MB allows fixing those 
huge pages */
+       const MAX_INCLUDE_SIZE = 50000000;
+
+       function __construct() {
+               parent::__construct( 'ExpandTemplates' );
+       }
+
+       /**
+        * Show the special page
+        */
+       function execute( $subpage ) {
+               global $wgParser;
+
+               $this->setHeaders();
+
+               $this->isNewParser = is_callable( array( $wgParser, 
'preprocessToDom' ) );
+
+               $request = $this->getRequest();
+               $titleStr = $request->getText( 'contexttitle' );
+               $title = Title::newFromText( $titleStr );
+
+               if ( !$title ) {
+                       $title = $this->getTitle();
+               }
+               $input = $request->getText( 'input' );
+               $this->generateXML = $this->isNewParser ? $request->getBool( 
'generate_xml' ) : false;
+
+               if ( strlen( $input ) ) {
+                       $this->removeComments = $request->getBool( 
'removecomments', false );
+                       $this->removeNowiki = $request->getBool( 
'removenowiki', false );
+                       $options = ParserOptions::newFromContext( 
$this->getContext() );
+                       $options->setRemoveComments( $this->removeComments );
+                       $options->setTidy( true );
+                       $options->setMaxIncludeSize( self::MAX_INCLUDE_SIZE );
+
+                       if ( $this->generateXML ) {
+                               $wgParser->startExternalParse( $title, 
$options, OT_PREPROCESS );
+                               $dom = $wgParser->preprocessToDom( $input );
+
+                               if ( is_callable( array( $dom, 'saveXML' ) ) ) {
+                                       $xml = $dom->saveXML();
+                               } else {
+                                       $xml = $dom->__toString();
+                               }
+                       }
+
+                       $output = $wgParser->preprocess( $input, $title, 
$options );
+               } else {
+                       $this->removeComments = $request->getBool( 
'removecomments', true );
+                       $this->removeNowiki = $request->getBool( 
'removenowiki', false );
+                       $output = false;
+               }
+
+               $out = $this->getOutput();
+               $out->addWikiMsg( 'expand_templates_intro' );
+               $out->addHTML( $this->makeForm( $titleStr, $input ) );
+
+               if( $output !== false ) {
+                       global $wgUseTidy, $wgAlwaysUseTidy;
+
+                       if ( $this->generateXML ) {
+                               $out->addHTML( $this->makeOutput( $xml, 
'expand_templates_xml_output' ) );
+                       }
+
+                       $tmp = $this->makeOutput( $output );
+
+                       if ( $this->removeNowiki ) {
+                               $tmp = preg_replace(
+                                       array( '_&lt;nowiki&gt;_', 
'_&lt;/nowiki&gt;_', '_&lt;nowiki */&gt;_' ),
+                                       '',
+                                       $tmp
+                               );
+                       }
+
+                       if( ( $wgUseTidy && $options->getTidy() ) || 
$wgAlwaysUseTidy ) {
+                               $tmp = MWTidy::tidy( $tmp );
+                       }
+
+                       $out->addHTML( $tmp );
+                       $this->showHtmlPreview( $title, $output, $out );
+               }
+
+       }
+
+       /**
+        * Generate a form allowing users to enter information
+        *
+        * @param $title string Value for context title field
+        * @param $input string Value for input textbox
+        * @return string
+        */
+       private function makeForm( $title, $input ) {
+               $self = $this->getTitle();
+               $form  = Xml::openElement(
+                       'form',
+                       array( 'method' => 'post', 'action' => 
$self->getLocalUrl() )
+               );
+               $form .= "<fieldset><legend>" . $this->msg( 'expandtemplates' 
)->escaped() . "</legend>\n";
+
+               $form .= '<p>' . Xml::inputLabel(
+                       $this->msg( 'expand_templates_title' )->plain(),
+                       'contexttitle',
+                       'contexttitle',
+                       60,
+                       $title,
+                       array( 'autofocus' => true )
+               ) . '</p>';
+               $form .= '<p>' . Xml::label(
+                       $this->msg( 'expand_templates_input' )->text(),
+                       'input'
+               ) . '</p>';
+               $form .= Xml::openElement(
+                       'textarea',
+                       array( 'name' => 'input', 'id' => 'input', 'rows' => 
10, 'cols' => 10 )
+               );
+
+               $form .= htmlspecialchars( $input );
+               $form .= Xml::closeElement( 'textarea' );
+               $form .= '<p>' . Xml::checkLabel(
+                       $this->msg( 'expand_templates_remove_comments' 
)->text(),
+                       'removecomments',
+                       'removecomments',
+                       $this->removeComments
+               ) . '</p>';
+               $form .= '<p>' . Xml::checkLabel(
+                       $this->msg( 'expand_templates_remove_nowiki' )->text(),
+                       'removenowiki',
+                       'removenowiki',
+                       $this->removeNowiki
+               ) . '</p>';
+               if ( $this->isNewParser ) {
+                       $form .= '<p>' . Xml::checkLabel(
+                               $this->msg( 'expand_templates_generate_xml' 
)->text(),
+                               'generate_xml',
+                               'generate_xml',
+                               $this->generateXML
+                       ) . '</p>';
+               }
+               $form .= '<p>' . Xml::submitButton(
+                       $this->msg( 'expand_templates_ok' )->text(),
+                       array( 'accesskey' => 's' )
+               ) . '</p>';
+               $form .= "</fieldset>\n";
+               $form .= Xml::closeElement( 'form' );
+               return $form;
+       }
+
+       /**
+        * Generate a nice little box with a heading for output
+        *
+        * @param $output string Wiki text output
+        * @param $heading string
+        * @return string
+        */
+       private function makeOutput( $output, $heading = 
'expand_templates_output' ) {
+               $out  = "<h2>" . $this->msg( $heading )->escaped() . "</h2>\n";
+               $out .= Xml::openElement(
+                       'textarea',
+                       array( 'id' => 'output', 'rows' => 10, 'cols' => 10, 
'readonly' => 'readonly' )
+               );
+               $out .= htmlspecialchars( $output );
+               $out .= Xml::closeElement( 'textarea' );
+               return $out;
+       }
+
+       /**
+        * Render the supplied wiki text and append to the page as a preview
+        *
+        * @param Title $title
+        * @param string $text
+        * @param OutputPage $out
+        */
+       private function showHtmlPreview( $title, $text, $out ) {
+               global $wgParser;
+               $popts = ParserOptions::newFromContext( $this->getContext() );
+               $popts->setTargetLanguage( $title->getPageLanguage() );
+               $pout = $wgParser->parse( $text, $title, $popts );
+               $lang = $title->getPageViewLanguage();
+               $out->addHTML( "<h2>" . $this->msg( 'expand_templates_preview' 
)->escaped() . "</h2>\n" );
+               $out->addHTML( Html::openElement( 'div', array(
+                       'class' => 'mw-content-' . $lang->getDir(),
+                       'dir' => $lang->getDir(),
+                       'lang' => $lang->getHtmlCode(),
+               ) ) );
+               $out->addHTML( $pout->getText() );
+               $out->addHTML( Html::closeElement( 'div' ) );
+       }
+}

-- 
To view, visit https://gerrit.wikimedia.org/r/96810
To unsubscribe, visit https://gerrit.wikimedia.org/r/settings

Gerrit-MessageType: newchange
Gerrit-Change-Id: I7ef63488dc3ad3885bcf99ff52852e1c6981942b
Gerrit-PatchSet: 1
Gerrit-Project: mediawiki/core
Gerrit-Branch: master
Gerrit-Owner: UltrasonicNXT <[email protected]>

_______________________________________________
MediaWiki-commits mailing list
[email protected]
https://lists.wikimedia.org/mailman/listinfo/mediawiki-commits

Reply via email to