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();
56 if(!is_null($oldid) ||
!is_null($page))
60 # Don't use the parser cache
61 $rev = Revision
::newFromID($oldid);
63 $this->dieUsage("There is no revision ID $oldid", 'missingrev');
64 if(!$rev->userCan(Revision
::DELETED_TEXT
))
65 $this->dieUsage("You don't have permission to view deleted revisions", 'permissiondenied');
66 $text = $rev->getRawText();
67 $titleObj = $rev->getTitle();
68 $p_result = $wgParser->parse($text, $titleObj, $popts);
72 $titleObj = Title
::newFromText($page);
74 $this->dieUsage("The page you specified doesn't exist", 'missingtitle');
76 // Try the parser cache first
77 $articleObj = new Article($titleObj);
78 if(isset($prop['revid']))
79 $oldid = $articleObj->getRevIdFetched();
80 $pcache = ParserCache
::singleton();
81 $p_result = $pcache->get($articleObj, $wgUser);
83 $p_result = $wgParser->parse($articleObj->getContent(), $titleObj, $popts);
84 global $wgUseParserCache;
86 $pcache->save($p_result, $articleObj, $wgUser);
92 $titleObj = Title
::newFromText($title);
94 $titleObj = Title
::newFromText("API");
95 $p_result = $wgParser->parse($text, $titleObj, $popts);
99 $result = $this->getResult();
100 $result_array = array();
101 if(isset($prop['text'])) {
102 $result_array['text'] = array();
103 $result->setContent($result_array['text'], $p_result->getText());
105 if(isset($prop['langlinks']))
106 $result_array['langlinks'] = $this->formatLangLinks($p_result->getLanguageLinks());
107 if(isset($prop['categories']))
108 $result_array['categories'] = $this->formatCategoryLinks($p_result->getCategories());
109 if(isset($prop['links']))
110 $result_array['links'] = $this->formatLinks($p_result->getLinks());
111 if(isset($prop['templates']))
112 $result_array['templates'] = $this->formatLinks($p_result->getTemplates());
113 if(isset($prop['images']))
114 $result_array['images'] = array_keys($p_result->getImages());
115 if(isset($prop['externallinks']))
116 $result_array['externallinks'] = array_keys($p_result->getExternalLinks());
117 if(isset($prop['sections']))
118 $result_array['sections'] = $p_result->getSections();
120 $result_array['revid'] = $oldid;
122 $result_mapping = array(
124 'categories' => 'cl',
128 'externallinks' => 'el',
131 $this->setIndexedTagNames( $result_array, $result_mapping );
132 $result->addValue( null, $this->getModuleName(), $result_array );
135 private function formatLangLinks( $links ) {
137 foreach( $links as $link ) {
139 $bits = split( ':', $link, 2 );
140 $entry['lang'] = $bits[0];
141 $this->getResult()->setContent( $entry, $bits[1] );
147 private function formatCategoryLinks( $links ) {
149 foreach( $links as $link => $sortkey ) {
151 $entry['sortkey'] = $sortkey;
152 $this->getResult()->setContent( $entry, $link );
158 private function formatLinks( $links ) {
160 foreach( $links as $ns => $nslinks ) {
161 foreach( $nslinks as $title => $id ) {
164 $this->getResult()->setContent( $entry, Title
::makeTitle( $ns, $title )->getFullText() );
166 $entry['exists'] = '';
173 private function setIndexedTagNames( &$array, $mapping ) {
174 foreach( $mapping as $key => $name ) {
175 if( isset( $array[$key] ) )
176 $this->getResult()->setIndexedTagName( $array[$key], $name );
180 public function getAllowedParams() {
183 ApiBase
:: PARAM_DFLT
=> 'API',
189 ApiBase
:: PARAM_DFLT
=> 'text|langlinks|categories|links|templates|images|externallinks|sections|revid',
190 ApiBase
:: PARAM_ISMULTI
=> true,
191 ApiBase
:: PARAM_TYPE
=> array(
206 public function getParamDescription() {
208 'text' => 'Wikitext to parse',
209 'title' => 'Title of page the text belongs to',
210 'page' => 'Parse the content of this page. Cannot be used together with text and title',
211 'oldid' => 'Parse the content of this revision. Overrides page',
212 'prop' => array('Which pieces of information to get.',
213 'NOTE: Section tree is only generated if there are more than 4 sections, or if the __TOC__ keyword is present'
218 public function getDescription() {
219 return 'This module parses wikitext and returns parser output';
222 protected function getExamples() {
224 'api.php?action=parse&text={{Project:Sandbox}}'
228 public function getVersion() {
229 return __CLASS__
. ': $Id$';