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 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']);
51 global $wgParser, $wgUser;
53 $titleObj = Title
::newFromText($page);
55 $this->dieUsage("The page you specified doesn't exist", 'missingtitle');
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);
64 $p_result = $wgParser->parse($articleObj->getContent(), $titleObj, new ParserOptions());
65 global $wgUseParserCache;
67 $pcache->save($p_result, $articleObj, $wgUser);
70 $titleObj = Title
::newFromText($title);
72 $titleObj = Title
::newFromText("API");
73 $p_result = $wgParser->parse($text, $titleObj, new ParserOptions());
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());
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();
98 $result_array['revid'] = $revid;
100 $result_mapping = array(
102 'categories' => 'cl',
106 'externallinks' => 'el',
109 $this->setIndexedTagNames( $result_array, $result_mapping );
110 $result->addValue( null, $this->getModuleName(), $result_array );
113 private function formatLangLinks( $links ) {
115 foreach( $links as $link ) {
117 $bits = split( ':', $link, 2 );
118 $entry['lang'] = $bits[0];
119 $this->getResult()->setContent( $entry, $bits[1] );
125 private function formatCategoryLinks( $links ) {
127 foreach( $links as $link => $sortkey ) {
129 $entry['sortkey'] = $sortkey;
130 $this->getResult()->setContent( $entry, $link );
136 private function formatLinks( $links ) {
138 foreach( $links as $ns => $nslinks ) {
139 foreach( $nslinks as $title => $id ) {
142 $this->getResult()->setContent( $entry, Title
::makeTitle( $ns, $title )->getFullText() );
144 $entry['exists'] = '';
151 private function setIndexedTagNames( &$array, $mapping ) {
152 foreach( $mapping as $key => $name ) {
153 if( isset( $array[$key] ) )
154 $this->getResult()->setIndexedTagName( $array[$key], $name );
158 public function getAllowedParams() {
161 ApiBase
:: PARAM_DFLT
=> 'API',
166 ApiBase
:: PARAM_DFLT
=> 'text|langlinks|categories|links|templates|images|externallinks|sections|revid',
167 ApiBase
:: PARAM_ISMULTI
=> true,
168 ApiBase
:: PARAM_TYPE
=> array(
183 public function getParamDescription() {
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'
194 public function getDescription() {
195 return 'This module parses wikitext and returns parser output';
198 protected function getExamples() {
200 'api.php?action=parse&text={{Project:Sandbox}}'
204 public function getVersion() {
205 return __CLASS__
. ': $Id$';