MediaWiki  REL1_24
ApiExpandTemplates.php
Go to the documentation of this file.
00001 <?php
00034 class ApiExpandTemplates extends ApiBase {
00035 
00036     public function execute() {
00037         // Cache may vary on $wgUser because ParserOptions gets data from it
00038         $this->getMain()->setCacheMode( 'anon-public-user-private' );
00039 
00040         // Get parameters
00041         $params = $this->extractRequestParams();
00042         $this->requireMaxOneParameter( $params, 'prop', 'generatexml' );
00043 
00044         if ( $params['prop'] === null ) {
00045             $this->logFeatureUsage( 'action=expandtemplates&!prop' );
00046             $this->setWarning( 'Because no values have been specified for the prop parameter, a ' .
00047                 'legacy format has been used for the output. This format is deprecated, and in ' .
00048                 'the future, a default value will be set for the prop parameter, causing the new' .
00049                 'format to always be used.' );
00050             $prop = array();
00051         } else {
00052             $prop = array_flip( $params['prop'] );
00053         }
00054 
00055         // Create title for parser
00056         $title_obj = Title::newFromText( $params['title'] );
00057         if ( !$title_obj || $title_obj->isExternal() ) {
00058             $this->dieUsageMsg( array( 'invalidtitle', $params['title'] ) );
00059         }
00060 
00061         $result = $this->getResult();
00062 
00063         // Parse text
00064         global $wgParser;
00065         $options = ParserOptions::newFromContext( $this->getContext() );
00066 
00067         if ( $params['includecomments'] ) {
00068             $options->setRemoveComments( false );
00069         }
00070 
00071         $retval = array();
00072 
00073         if ( isset( $prop['parsetree'] ) || $params['generatexml'] ) {
00074             if ( !isset( $prop['parsetree'] ) ) {
00075                 $this->logFeatureUsage( 'action=expandtemplates&generatexml' );
00076             }
00077 
00078             $wgParser->startExternalParse( $title_obj, $options, OT_PREPROCESS );
00079             $dom = $wgParser->preprocessToDom( $params['text'] );
00080             if ( is_callable( array( $dom, 'saveXML' ) ) ) {
00081                 $xml = $dom->saveXML();
00082             } else {
00083                 $xml = $dom->__toString();
00084             }
00085             if ( isset( $prop['parsetree'] ) ) {
00086                 unset( $prop['parsetree'] );
00087                 $retval['parsetree'] = $xml;
00088             } else {
00089                 // the old way
00090                 $xml_result = array();
00091                 ApiResult::setContent( $xml_result, $xml );
00092                 $result->addValue( null, 'parsetree', $xml_result );
00093             }
00094         }
00095 
00096         // if they didn't want any output except (probably) the parse tree,
00097         // then don't bother actually fully expanding it
00098         if ( $prop || $params['prop'] === null ) {
00099             $wgParser->startExternalParse( $title_obj, $options, OT_PREPROCESS );
00100             $frame = $wgParser->getPreprocessor()->newFrame();
00101             $wikitext = $wgParser->preprocess( $params['text'], $title_obj, $options, null, $frame );
00102             if ( $params['prop'] === null ) {
00103                 // the old way
00104                 ApiResult::setContent( $retval, $wikitext );
00105             } else {
00106                 if ( isset( $prop['categories'] ) ) {
00107                     $categories = $wgParser->getOutput()->getCategories();
00108                     if ( !empty( $categories ) ) {
00109                         $categories_result = array();
00110                         foreach ( $categories as $category => $sortkey ) {
00111                             $entry = array();
00112                             $entry['sortkey'] = $sortkey;
00113                             ApiResult::setContent( $entry, $category );
00114                             $categories_result[] = $entry;
00115                         }
00116                         $result->setIndexedTagName( $categories_result, 'category' );
00117                         $retval['categories'] = $categories_result;
00118                     }
00119                 }
00120                 if ( isset( $prop['volatile'] ) && $frame->isVolatile() ) {
00121                     $retval['volatile'] = '';
00122                 }
00123                 if ( isset( $prop['ttl'] ) && $frame->getTTL() !== null ) {
00124                     $retval['ttl'] = $frame->getTTL();
00125                 }
00126                 if ( isset( $prop['wikitext'] ) ) {
00127                     $retval['wikitext'] = $wikitext;
00128                 }
00129             }
00130         }
00131         $result->setSubelements( $retval, array( 'wikitext', 'parsetree' ) );
00132         $result->addValue( null, $this->getModuleName(), $retval );
00133     }
00134 
00135     public function getAllowedParams() {
00136         return array(
00137             'title' => array(
00138                 ApiBase::PARAM_DFLT => 'API',
00139             ),
00140             'text' => array(
00141                 ApiBase::PARAM_TYPE => 'string',
00142                 ApiBase::PARAM_REQUIRED => true,
00143             ),
00144             'prop' => array(
00145                 ApiBase::PARAM_TYPE => array(
00146                     'wikitext',
00147                     'categories',
00148                     'volatile',
00149                     'ttl',
00150                     'parsetree',
00151                 ),
00152                 ApiBase::PARAM_ISMULTI => true,
00153             ),
00154             'includecomments' => false,
00155             'generatexml' => array(
00156                 ApiBase::PARAM_TYPE => 'boolean',
00157                 ApiBase::PARAM_DEPRECATED => true,
00158             ),
00159         );
00160     }
00161 
00162     public function getParamDescription() {
00163         return array(
00164             'text' => 'Wikitext to convert',
00165             'title' => 'Title of page',
00166             'prop' => array(
00167                 'Which pieces of information to get',
00168                 ' wikitext   - The expanded wikitext',
00169                 ' categories - Any categories present in the input that are not represented in ' .
00170                     'the wikitext output',
00171                 ' volatile   - Whether the output is volatile and should not be reused ' .
00172                     'elsewhere within the page',
00173                 ' ttl        - The maximum time after which caches of the result should be ' .
00174                     'invalidated',
00175                 ' parsetree  - The XML parse tree of the input',
00176                 'Note that if no values are selected, the result will contain the wikitext,',
00177                 'but the output will be in a deprecated format.',
00178             ),
00179             'includecomments' => 'Whether to include HTML comments in the output',
00180             'generatexml' => 'Generate XML parse tree (replaced by prop=parsetree)',
00181         );
00182     }
00183 
00184     public function getDescription() {
00185         return 'Expands all templates in wikitext.';
00186     }
00187 
00188     public function getExamples() {
00189         return array(
00190             'api.php?action=expandtemplates&text={{Project:Sandbox}}'
00191         );
00192     }
00193 
00194     public function getHelpUrls() {
00195         return 'https://www.mediawiki.org/wiki/API:Parsing_wikitext#expandtemplates';
00196     }
00197 }