Add category output to ApiExpandTemplates
[lhc/web/wiklou.git] / includes / api / ApiExpandTemplates.php
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
43 // Create title for parser
44 $title_obj = Title::newFromText( $params['title'] );
45 if ( !$title_obj || $title_obj->isExternal() ) {
46 $this->dieUsageMsg( array( 'invalidtitle', $params['title'] ) );
47 }
48
49 $result = $this->getResult();
50
51 // Parse text
52 global $wgParser;
53 $options = ParserOptions::newFromContext( $this->getContext() );
54
55 if ( $params['includecomments'] ) {
56 $options->setRemoveComments( false );
57 }
58
59 if ( $params['generatexml'] ) {
60 $wgParser->startExternalParse( $title_obj, $options, OT_PREPROCESS );
61 $dom = $wgParser->preprocessToDom( $params['text'] );
62 if ( is_callable( array( $dom, 'saveXML' ) ) ) {
63 $xml = $dom->saveXML();
64 } else {
65 $xml = $dom->__toString();
66 }
67 $xml_result = array();
68 ApiResult::setContent( $xml_result, $xml );
69 $result->addValue( null, 'parsetree', $xml_result );
70 }
71 $frame = $wgParser->getPreprocessor()->newFrame();
72 $retval = $wgParser->preprocess( $params['text'], $title_obj, $options, null, $frame );
73 $categories = $wgParser->getOutput()->getCategories();
74 if ( !empty( $categories ) ) {
75 $categories_result = array();
76 foreach ( $categories as $category => $sortkey ) {
77 $entry = array();
78 $entry['sortkey'] = $sortkey;
79 ApiResult::setContent( $entry, $category );
80 $categories_result[] = $entry;
81 }
82 $result->setIndexedTagName( $categories_result, 'category' );
83 $result->addValue( null, 'categories', $categories_result );
84 }
85
86 // Return result
87 $retval_array = array();
88 ApiResult::setContent( $retval_array, $retval );
89 if ( $frame->isVolatile() ) {
90 $retval_array['volatile'] = '';
91 }
92 $result->addValue( null, $this->getModuleName(), $retval_array );
93 }
94
95 public function getAllowedParams() {
96 return array(
97 'title' => array(
98 ApiBase::PARAM_DFLT => 'API',
99 ),
100 'text' => array(
101 ApiBase::PARAM_TYPE => 'string',
102 ApiBase::PARAM_REQUIRED => true,
103 ),
104 'generatexml' => false,
105 'includecomments' => false,
106 );
107 }
108
109 public function getParamDescription() {
110 return array(
111 'text' => 'Wikitext to convert',
112 'title' => 'Title of page',
113 'generatexml' => 'Generate XML parse tree',
114 'includecomments' => 'Whether to include HTML comments in the output',
115 );
116 }
117
118 public function getResultProperties() {
119 return array(
120 '' => array(
121 '*' => 'string'
122 )
123 );
124 }
125
126 public function getDescription() {
127 return 'Expands all templates in wikitext.';
128 }
129
130 public function getPossibleErrors() {
131 return array_merge( parent::getPossibleErrors(), array(
132 array( 'invalidtitle', 'title' ),
133 ) );
134 }
135
136 public function getExamples() {
137 return array(
138 'api.php?action=expandtemplates&text={{Project:Sandbox}}'
139 );
140 }
141
142 public function getHelpUrls() {
143 return 'https://www.mediawiki.org/wiki/API:Parsing_wikitext#expandtemplates';
144 }
145 }