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']);
50 global $wgParser, $wgUser;
52 $titleObj = Title
::newFromText($page);
54 $this->dieUsageMsg(array('missingtitle', $page));
56 // Try the parser cache first
57 $articleObj = new Article($titleObj);
58 $pcache =& ParserCache
::singleton();
59 $p_result = $pcache->get($articleObj, $wgUser);
61 $p_result = $wgParser->parse($articleObj->getContent(), $titleObj, new ParserOptions());
62 global $wgUseParserCache;
64 $pcache->save($p_result, $articleObj, $wgUser);
67 $titleObj = Title
::newFromText($title);
69 $titleObj = Title
::newFromText("API");
70 $p_result = $wgParser->parse($text, $titleObj, new ParserOptions());
74 $result = $this->getResult();
75 $result_array = array();
76 if(isset($prop['text'])) {
77 $result_array['text'] = array();
78 $result->setContent($result_array['text'], $p_result->getText());
80 if(isset($prop['langlinks']))
81 $result_array['langlinks'] = $this->formatLangLinks($p_result->getLanguageLinks());
82 if(isset($prop['categories']))
83 $result_array['categories'] = $this->formatCategoryLinks($p_result->getCategories());
84 if(isset($prop['links']))
85 $result_array['links'] = $this->formatLinks($p_result->getLinks());
86 if(isset($prop['templates']))
87 $result_array['templates'] = $this->formatLinks($p_result->getTemplates());
88 if(isset($prop['images']))
89 $result_array['images'] = array_keys($p_result->getImages());
90 if(isset($prop['externallinks']))
91 $result_array['externallinks'] = array_keys($p_result->getExternalLinks());
92 if(isset($prop['sections']))
93 $result_array['sections'] = $p_result->getSections();
95 $result_mapping = array(
101 'externallinks' => 'el',
104 $this->setIndexedTagNames( $result_array, $result_mapping );
105 $result->addValue( null, $this->getModuleName(), $result_array );
108 private function formatLangLinks( $links ) {
110 foreach( $links as $link ) {
112 $bits = split( ':', $link, 2 );
113 $entry['lang'] = $bits[0];
114 $this->getResult()->setContent( $entry, $bits[1] );
120 private function formatCategoryLinks( $links ) {
122 foreach( $links as $link => $sortkey ) {
124 $entry['sortkey'] = $sortkey;
125 $this->getResult()->setContent( $entry, $link );
131 private function formatLinks( $links ) {
133 foreach( $links as $ns => $nslinks ) {
134 foreach( $nslinks as $title => $id ) {
137 $this->getResult()->setContent( $entry, Title
::makeTitle( $ns, $title )->getFullText() );
139 $entry['exists'] = '';
146 private function setIndexedTagNames( &$array, $mapping ) {
147 foreach( $mapping as $key => $name ) {
148 if( isset( $array[$key] ) )
149 $this->getResult()->setIndexedTagName( $array[$key], $name );
153 public function getAllowedParams() {
156 ApiBase
:: PARAM_DFLT
=> 'API',
161 ApiBase
:: PARAM_DFLT
=> 'text|langlinks|categories|links|templates|images|externallinks|sections',
162 ApiBase
:: PARAM_ISMULTI
=> true,
163 ApiBase
:: PARAM_TYPE
=> array(
177 public function getParamDescription() {
179 'text' => 'Wikitext to parse',
180 'title' => 'Title of page the text belongs to',
181 'page' => 'Parse the content of this page. Cannot be used together with text and title',
182 'prop' => array('Which pieces of information to get.',
183 'NOTE: Section tree is only generated if there are more than 4 sections, or if the __TOC__ keyword is present'
188 public function getDescription() {
189 return 'This module parses wikitext and returns parser output';
192 protected function getExamples() {
194 'api.php?action=parse&text={{Project:Sandbox}}'
198 public function getVersion() {
199 return __CLASS__
. ': $Id$';