2f742267071b9bb0aaa0022cbef131999e5ad3e5
4 * Created on Dec 01, 2007
6 * API for MediaWiki 1.8+
8 * Copyright (C) 2007 Yuri Astrakhan <Firstname><Lastname>@gmail.com
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.
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.
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
26 if (!defined('MEDIAWIKI')) {
27 // Eclipse helper - will be ignored in production
28 require_once ("ApiBase.php");
34 class ApiParse
extends ApiBase
{
36 public function __construct($main, $action) {
37 parent
:: __construct($main, $action);
40 public function execute() {
42 $params = $this->extractRequestParams();
43 $text = $params['text'];
44 $title = $params['title'];
45 $page = $params['page'];
46 $oldid = $params['oldid'];
47 if(!is_null($page) && (!is_null($text) ||
$title != "API"))
48 $this->dieUsage("The page parameter cannot be used together with the text and title parameters", 'params');
49 $prop = array_flip($params['prop']);
52 global $wgParser, $wgUser;
53 $popts = new ParserOptions();
54 $popts->setTidy(true);
55 $popts->enableLimitReport();
57 if(!is_null($oldid) ||
!is_null($page))
61 # Don't use the parser cache
62 $rev = Revision
::newFromID($oldid);
64 $this->dieUsage("There is no revision ID $oldid", 'missingrev');
65 if(!$rev->userCan(Revision
::DELETED_TEXT
))
66 $this->dieUsage("You don't have permission to view deleted revisions", 'permissiondenied');
67 $text = $rev->getText( Revision
::FOR_THIS_USER
);
68 $titleObj = $rev->getTitle();
69 $p_result = $wgParser->parse($text, $titleObj, $popts);
73 if($params['redirects'])
75 $req = new FauxRequest(array(
80 $main = new ApiMain($req);
82 $data = $main->getResultData();
83 $redirValues = @$data['query']['redirects'];
85 foreach((array)$redirValues as $r)
90 $titleObj = Title
::newFromText($to);
92 $this->dieUsage("The page you specified doesn't exist", 'missingtitle');
94 $articleObj = new Article($titleObj);
95 if(isset($prop['revid']))
96 $oldid = $articleObj->getRevIdFetched();
97 // Try the parser cache first
98 $pcache = ParserCache
::singleton();
99 $p_result = $pcache->get($articleObj, $wgUser);
102 $p_result = $wgParser->parse($articleObj->getContent(), $titleObj, $popts);
103 global $wgUseParserCache;
104 if($wgUseParserCache)
105 $pcache->save($p_result, $articleObj, $wgUser);
111 $titleObj = Title
::newFromText($title);
113 $titleObj = Title
::newFromText("API");
114 if($params['pst'] ||
$params['onlypst'])
115 $text = $wgParser->preSaveTransform($text, $titleObj, $wgUser, $popts);
116 if($params['onlypst'])
118 // Build a result and bail out
119 $result_array['text'] = array();
120 $this->getResult()->setContent($result_array['text'], $text);
121 $this->getResult()->addValue(null, $this->getModuleName(), $result_array);
124 $p_result = $wgParser->parse($text, $titleObj, $popts);
128 $result = $this->getResult();
129 $result_array = array();
130 if($params['redirects'] && !is_null($redirValues))
131 $result_array['redirects'] = $redirValues;
132 if(isset($prop['text'])) {
133 $result_array['text'] = array();
134 $result->setContent($result_array['text'], $p_result->getText());
136 if(isset($prop['langlinks']))
137 $result_array['langlinks'] = $this->formatLangLinks($p_result->getLanguageLinks());
138 if(isset($prop['categories']))
139 $result_array['categories'] = $this->formatCategoryLinks($p_result->getCategories());
140 if(isset($prop['links']))
141 $result_array['links'] = $this->formatLinks($p_result->getLinks());
142 if(isset($prop['templates']))
143 $result_array['templates'] = $this->formatLinks($p_result->getTemplates());
144 if(isset($prop['images']))
145 $result_array['images'] = array_keys($p_result->getImages());
146 if(isset($prop['externallinks']))
147 $result_array['externallinks'] = array_keys($p_result->getExternalLinks());
148 if(isset($prop['sections']))
149 $result_array['sections'] = $p_result->getSections();
151 $result_array['revid'] = $oldid;
153 $result_mapping = array(
156 'categories' => 'cl',
160 'externallinks' => 'el',
163 $this->setIndexedTagNames( $result_array, $result_mapping );
164 $result->addValue( null, $this->getModuleName(), $result_array );
167 private function formatLangLinks( $links ) {
169 foreach( $links as $link ) {
171 $bits = split( ':', $link, 2 );
172 $entry['lang'] = $bits[0];
173 $this->getResult()->setContent( $entry, $bits[1] );
179 private function formatCategoryLinks( $links ) {
181 foreach( $links as $link => $sortkey ) {
183 $entry['sortkey'] = $sortkey;
184 $this->getResult()->setContent( $entry, $link );
190 private function formatLinks( $links ) {
192 foreach( $links as $ns => $nslinks ) {
193 foreach( $nslinks as $title => $id ) {
196 $this->getResult()->setContent( $entry, Title
::makeTitle( $ns, $title )->getFullText() );
198 $entry['exists'] = '';
205 private function setIndexedTagNames( &$array, $mapping ) {
206 foreach( $mapping as $key => $name ) {
207 if( isset( $array[$key] ) )
208 $this->getResult()->setIndexedTagName( $array[$key], $name );
212 public function getAllowedParams() {
215 ApiBase
:: PARAM_DFLT
=> 'API',
219 'redirects' => false,
222 ApiBase
:: PARAM_DFLT
=> 'text|langlinks|categories|links|templates|images|externallinks|sections|revid',
223 ApiBase
:: PARAM_ISMULTI
=> true,
224 ApiBase
:: PARAM_TYPE
=> array(
241 public function getParamDescription() {
243 'text' => 'Wikitext to parse',
244 'redirects' => 'If the page parameter is set to a redirect, resolve it',
245 'title' => 'Title of page the text belongs to',
246 'page' => 'Parse the content of this page. Cannot be used together with text and title',
247 'oldid' => 'Parse the content of this revision. Overrides page',
248 'prop' => array('Which pieces of information to get.',
249 'NOTE: Section tree is only generated if there are more than 4 sections, or if the __TOC__ keyword is present'
251 'pst' => array( 'Do a pre-save transform on the input before parsing it.',
252 'Ignored if page or oldid is used.'
254 'onlypst' => array('Do a PST on the input, but don\'t parse it.',
255 'Returns PSTed wikitext. Ignored if page or oldid is used.'
260 public function getDescription() {
261 return 'This module parses wikitext and returns parser output';
264 protected function getExamples() {
266 'api.php?action=parse&text={{Project:Sandbox}}'
270 public function getVersion() {
271 return __CLASS__
. ': $Id$';