(bug 13544) Added prop=revid to action=parse
[lhc/web/wiklou.git] / includes / api / ApiParse.php
1 <?php
2
3 /*
4 * Created on Dec 01, 2007
5 *
6 * API for MediaWiki 1.8+
7 *
8 * Copyright (C) 2007 Yuri Astrakhan <Firstname><Lastname>@gmail.com
9 *
10 * This program is free software; you can redistribute it and/or modify
11 * it under the terms of the GNU General Public License as published by
12 * the Free Software Foundation; either version 2 of the License, or
13 * (at your option) any later version.
14 *
15 * This program is distributed in the hope that it will be useful,
16 * but WITHOUT ANY WARRANTY; without even the implied warranty of
17 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
18 * GNU General Public License for more details.
19 *
20 * You should have received a copy of the GNU General Public License along
21 * with this program; if not, write to the Free Software Foundation, Inc.,
22 * 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
23 * http://www.gnu.org/copyleft/gpl.html
24 */
25
26 if (!defined('MEDIAWIKI')) {
27 // Eclipse helper - will be ignored in production
28 require_once ("ApiBase.php");
29 }
30
31 /**
32 * @addtogroup API
33 */
34 class ApiParse extends ApiBase {
35
36 public function __construct($main, $action) {
37 parent :: __construct($main, $action);
38 }
39
40 public function execute() {
41 // Get parameters
42 $params = $this->extractRequestParams();
43 $text = $params['text'];
44 $title = $params['title'];
45 $page = $params['page'];
46 if(!is_null($page) && (!is_null($text) || $title != "API"))
47 $this->dieUsage("The page parameter cannot be used together with the text and title parameters", 'params');
48 $prop = array_flip($params['prop']);
49 $revid = false;
50
51 global $wgParser, $wgUser;
52 if(!is_null($page)) {
53 $titleObj = Title::newFromText($page);
54 if(!$titleObj)
55 $this->dieUsage("The page you specified doesn't exist", 'missingtitle');
56
57 // Try the parser cache first
58 $articleObj = new Article($titleObj);
59 if(isset($prop['revid']))
60 $revid = $articleObj->getRevIdFetched();
61 $pcache =& ParserCache::singleton();
62 $p_result = $pcache->get($articleObj, $wgUser);
63 if(!$p_result) {
64 $p_result = $wgParser->parse($articleObj->getContent(), $titleObj, new ParserOptions());
65 global $wgUseParserCache;
66 if($wgUseParserCache)
67 $pcache->save($p_result, $articleObj, $wgUser);
68 }
69 } else {
70 $titleObj = Title::newFromText($title);
71 if(!$titleObj)
72 $titleObj = Title::newFromText("API");
73 $p_result = $wgParser->parse($text, $titleObj, new ParserOptions());
74 }
75
76 // Return result
77 $result = $this->getResult();
78 $result_array = array();
79 if(isset($prop['text'])) {
80 $result_array['text'] = array();
81 $result->setContent($result_array['text'], $p_result->getText());
82 }
83 if(isset($prop['langlinks']))
84 $result_array['langlinks'] = $this->formatLangLinks($p_result->getLanguageLinks());
85 if(isset($prop['categories']))
86 $result_array['categories'] = $this->formatCategoryLinks($p_result->getCategories());
87 if(isset($prop['links']))
88 $result_array['links'] = $this->formatLinks($p_result->getLinks());
89 if(isset($prop['templates']))
90 $result_array['templates'] = $this->formatLinks($p_result->getTemplates());
91 if(isset($prop['images']))
92 $result_array['images'] = array_keys($p_result->getImages());
93 if(isset($prop['externallinks']))
94 $result_array['externallinks'] = array_keys($p_result->getExternalLinks());
95 if(isset($prop['sections']))
96 $result_array['sections'] = $p_result->getSections();
97 if($revid !== false)
98 $result_array['revid'] = $revid;
99
100 $result_mapping = array(
101 'langlinks' => 'll',
102 'categories' => 'cl',
103 'links' => 'pl',
104 'templates' => 'tl',
105 'images' => 'img',
106 'externallinks' => 'el',
107 'sections' => 's',
108 );
109 $this->setIndexedTagNames( $result_array, $result_mapping );
110 $result->addValue( null, $this->getModuleName(), $result_array );
111 }
112
113 private function formatLangLinks( $links ) {
114 $result = array();
115 foreach( $links as $link ) {
116 $entry = array();
117 $bits = split( ':', $link, 2 );
118 $entry['lang'] = $bits[0];
119 $this->getResult()->setContent( $entry, $bits[1] );
120 $result[] = $entry;
121 }
122 return $result;
123 }
124
125 private function formatCategoryLinks( $links ) {
126 $result = array();
127 foreach( $links as $link => $sortkey ) {
128 $entry = array();
129 $entry['sortkey'] = $sortkey;
130 $this->getResult()->setContent( $entry, $link );
131 $result[] = $entry;
132 }
133 return $result;
134 }
135
136 private function formatLinks( $links ) {
137 $result = array();
138 foreach( $links as $ns => $nslinks ) {
139 foreach( $nslinks as $title => $id ) {
140 $entry = array();
141 $entry['ns'] = $ns;
142 $this->getResult()->setContent( $entry, Title::makeTitle( $ns, $title )->getFullText() );
143 if( $id != 0 )
144 $entry['exists'] = '';
145 $result[] = $entry;
146 }
147 }
148 return $result;
149 }
150
151 private function setIndexedTagNames( &$array, $mapping ) {
152 foreach( $mapping as $key => $name ) {
153 if( isset( $array[$key] ) )
154 $this->getResult()->setIndexedTagName( $array[$key], $name );
155 }
156 }
157
158 public function getAllowedParams() {
159 return array (
160 'title' => array(
161 ApiBase :: PARAM_DFLT => 'API',
162 ),
163 'text' => null,
164 'page' => null,
165 'prop' => array(
166 ApiBase :: PARAM_DFLT => 'text|langlinks|categories|links|templates|images|externallinks|sections|revid',
167 ApiBase :: PARAM_ISMULTI => true,
168 ApiBase :: PARAM_TYPE => array(
169 'text',
170 'langlinks',
171 'categories',
172 'links',
173 'templates',
174 'images',
175 'externallinks',
176 'sections',
177 'revid'
178 )
179 )
180 );
181 }
182
183 public function getParamDescription() {
184 return array (
185 'text' => 'Wikitext to parse',
186 'title' => 'Title of page the text belongs to',
187 'page' => 'Parse the content of this page. Cannot be used together with text and title',
188 'prop' => array('Which pieces of information to get.',
189 'NOTE: Section tree is only generated if there are more than 4 sections, or if the __TOC__ keyword is present'
190 ),
191 );
192 }
193
194 public function getDescription() {
195 return 'This module parses wikitext and returns parser output';
196 }
197
198 protected function getExamples() {
199 return array (
200 'api.php?action=parse&text={{Project:Sandbox}}'
201 );
202 }
203
204 public function getVersion() {
205 return __CLASS__ . ': $Id$';
206 }
207 }
208