[ Index ]

PHP Cross Reference of MediaWiki-1.24.0

title

Body

[close]

/includes/api/ -> ApiExpandTemplates.php (source)

   1  <?php
   2  /**
   3   *
   4   *
   5   * Created on Oct 05, 2007
   6   *
   7   * Copyright © 2007 Yuri Astrakhan "<Firstname><Lastname>@gmail.com"
   8   *
   9   * This program is free software; you can redistribute it and/or modify
  10   * it under the terms of the GNU General Public License as published by
  11   * the Free Software Foundation; either version 2 of the License, or
  12   * (at your option) any later version.
  13   *
  14   * This program is distributed in the hope that it will be useful,
  15   * but WITHOUT ANY WARRANTY; without even the implied warranty of
  16   * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  17   * GNU General Public License for more details.
  18   *
  19   * You should have received a copy of the GNU General Public License along
  20   * with this program; if not, write to the Free Software Foundation, Inc.,
  21   * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
  22   * http://www.gnu.org/copyleft/gpl.html
  23   *
  24   * @file
  25   */
  26  
  27  /**
  28   * API module that functions as a shortcut to the wikitext preprocessor. Expands
  29   * any templates in a provided string, and returns the result of this expansion
  30   * to the caller.
  31   *
  32   * @ingroup API
  33   */
  34  class ApiExpandTemplates extends ApiBase {
  35  
  36  	public function execute() {
  37          // Cache may vary on $wgUser because ParserOptions gets data from it
  38          $this->getMain()->setCacheMode( 'anon-public-user-private' );
  39  
  40          // Get parameters
  41          $params = $this->extractRequestParams();
  42          $this->requireMaxOneParameter( $params, 'prop', 'generatexml' );
  43  
  44          if ( $params['prop'] === null ) {
  45              $this->logFeatureUsage( 'action=expandtemplates&!prop' );
  46              $this->setWarning( 'Because no values have been specified for the prop parameter, a ' .
  47                  'legacy format has been used for the output. This format is deprecated, and in ' .
  48                  'the future, a default value will be set for the prop parameter, causing the new' .
  49                  'format to always be used.' );
  50              $prop = array();
  51          } else {
  52              $prop = array_flip( $params['prop'] );
  53          }
  54  
  55          // Create title for parser
  56          $title_obj = Title::newFromText( $params['title'] );
  57          if ( !$title_obj || $title_obj->isExternal() ) {
  58              $this->dieUsageMsg( array( 'invalidtitle', $params['title'] ) );
  59          }
  60  
  61          $result = $this->getResult();
  62  
  63          // Parse text
  64          global $wgParser;
  65          $options = ParserOptions::newFromContext( $this->getContext() );
  66  
  67          if ( $params['includecomments'] ) {
  68              $options->setRemoveComments( false );
  69          }
  70  
  71          $retval = array();
  72  
  73          if ( isset( $prop['parsetree'] ) || $params['generatexml'] ) {
  74              if ( !isset( $prop['parsetree'] ) ) {
  75                  $this->logFeatureUsage( 'action=expandtemplates&generatexml' );
  76              }
  77  
  78              $wgParser->startExternalParse( $title_obj, $options, OT_PREPROCESS );
  79              $dom = $wgParser->preprocessToDom( $params['text'] );
  80              if ( is_callable( array( $dom, 'saveXML' ) ) ) {
  81                  $xml = $dom->saveXML();
  82              } else {
  83                  $xml = $dom->__toString();
  84              }
  85              if ( isset( $prop['parsetree'] ) ) {
  86                  unset( $prop['parsetree'] );
  87                  $retval['parsetree'] = $xml;
  88              } else {
  89                  // the old way
  90                  $xml_result = array();
  91                  ApiResult::setContent( $xml_result, $xml );
  92                  $result->addValue( null, 'parsetree', $xml_result );
  93              }
  94          }
  95  
  96          // if they didn't want any output except (probably) the parse tree,
  97          // then don't bother actually fully expanding it
  98          if ( $prop || $params['prop'] === null ) {
  99              $wgParser->startExternalParse( $title_obj, $options, OT_PREPROCESS );
 100              $frame = $wgParser->getPreprocessor()->newFrame();
 101              $wikitext = $wgParser->preprocess( $params['text'], $title_obj, $options, null, $frame );
 102              if ( $params['prop'] === null ) {
 103                  // the old way
 104                  ApiResult::setContent( $retval, $wikitext );
 105              } else {
 106                  if ( isset( $prop['categories'] ) ) {
 107                      $categories = $wgParser->getOutput()->getCategories();
 108                      if ( !empty( $categories ) ) {
 109                          $categories_result = array();
 110                          foreach ( $categories as $category => $sortkey ) {
 111                              $entry = array();
 112                              $entry['sortkey'] = $sortkey;
 113                              ApiResult::setContent( $entry, $category );
 114                              $categories_result[] = $entry;
 115                          }
 116                          $result->setIndexedTagName( $categories_result, 'category' );
 117                          $retval['categories'] = $categories_result;
 118                      }
 119                  }
 120                  if ( isset( $prop['volatile'] ) && $frame->isVolatile() ) {
 121                      $retval['volatile'] = '';
 122                  }
 123                  if ( isset( $prop['ttl'] ) && $frame->getTTL() !== null ) {
 124                      $retval['ttl'] = $frame->getTTL();
 125                  }
 126                  if ( isset( $prop['wikitext'] ) ) {
 127                      $retval['wikitext'] = $wikitext;
 128                  }
 129              }
 130          }
 131          $result->setSubelements( $retval, array( 'wikitext', 'parsetree' ) );
 132          $result->addValue( null, $this->getModuleName(), $retval );
 133      }
 134  
 135  	public function getAllowedParams() {
 136          return array(
 137              'title' => array(
 138                  ApiBase::PARAM_DFLT => 'API',
 139              ),
 140              'text' => array(
 141                  ApiBase::PARAM_TYPE => 'string',
 142                  ApiBase::PARAM_REQUIRED => true,
 143              ),
 144              'prop' => array(
 145                  ApiBase::PARAM_TYPE => array(
 146                      'wikitext',
 147                      'categories',
 148                      'volatile',
 149                      'ttl',
 150                      'parsetree',
 151                  ),
 152                  ApiBase::PARAM_ISMULTI => true,
 153              ),
 154              'includecomments' => false,
 155              'generatexml' => array(
 156                  ApiBase::PARAM_TYPE => 'boolean',
 157                  ApiBase::PARAM_DEPRECATED => true,
 158              ),
 159          );
 160      }
 161  
 162  	public function getParamDescription() {
 163          return array(
 164              'text' => 'Wikitext to convert',
 165              'title' => 'Title of page',
 166              'prop' => array(
 167                  'Which pieces of information to get',
 168                  ' wikitext   - The expanded wikitext',
 169                  ' categories - Any categories present in the input that are not represented in ' .
 170                      'the wikitext output',
 171                  ' volatile   - Whether the output is volatile and should not be reused ' .
 172                      'elsewhere within the page',
 173                  ' ttl        - The maximum time after which caches of the result should be ' .
 174                      'invalidated',
 175                  ' parsetree  - The XML parse tree of the input',
 176                  'Note that if no values are selected, the result will contain the wikitext,',
 177                  'but the output will be in a deprecated format.',
 178              ),
 179              'includecomments' => 'Whether to include HTML comments in the output',
 180              'generatexml' => 'Generate XML parse tree (replaced by prop=parsetree)',
 181          );
 182      }
 183  
 184  	public function getDescription() {
 185          return 'Expands all templates in wikitext.';
 186      }
 187  
 188  	public function getExamples() {
 189          return array(
 190              'api.php?action=expandtemplates&text={{Project:Sandbox}}'
 191          );
 192      }
 193  
 194  	public function getHelpUrls() {
 195          return 'https://www.mediawiki.org/wiki/API:Parsing_wikitext#expandtemplates';
 196      }
 197  }


Generated: Fri Nov 28 14:03:12 2014 Cross-referenced by PHPXref 0.7.1