* Add a nice fieldset around the input form
[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
50 global $wgParser, $wgUser;
51 if(!is_null($page)) {
52 $titleObj = Title::newFromText($page);
53 if(!$titleObj)
54 $this->dieUsageMsg(array('missingtitle', $page));
55
56 // Try the parser cache first
57 $articleObj = new Article($titleObj);
58 $pcache =& ParserCache::singleton();
59 $p_result = $pcache->get($articleObj, $wgUser);
60 if(!$p_result) {
61 $p_result = $wgParser->parse($articleObj->getContent(), $titleObj, new ParserOptions());
62 global $wgUseParserCache;
63 if($wgUseParserCache)
64 $pcache->save($p_result, $articleObj, $wgUser);
65 }
66 } else {
67 $titleObj = Title::newFromText($title);
68 if(!$titleObj)
69 $titleObj = Title::newFromText("API");
70 $p_result = $wgParser->parse($text, $titleObj, new ParserOptions());
71 }
72
73 // Return result
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());
79 }
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();
94
95 $result_mapping = array(
96 'langlinks' => 'll',
97 'categories' => 'cl',
98 'links' => 'pl',
99 'templates' => 'tl',
100 'images' => 'img',
101 'externallinks' => 'el',
102 'sections' => 's',
103 );
104 $this->setIndexedTagNames( $result_array, $result_mapping );
105 $result->addValue( null, $this->getModuleName(), $result_array );
106 }
107
108 private function formatLangLinks( $links ) {
109 $result = array();
110 foreach( $links as $link ) {
111 $entry = array();
112 $bits = split( ':', $link, 2 );
113 $entry['lang'] = $bits[0];
114 $this->getResult()->setContent( $entry, $bits[1] );
115 $result[] = $entry;
116 }
117 return $result;
118 }
119
120 private function formatCategoryLinks( $links ) {
121 $result = array();
122 foreach( $links as $link => $sortkey ) {
123 $entry = array();
124 $entry['sortkey'] = $sortkey;
125 $this->getResult()->setContent( $entry, $link );
126 $result[] = $entry;
127 }
128 return $result;
129 }
130
131 private function formatLinks( $links ) {
132 $result = array();
133 foreach( $links as $ns => $nslinks ) {
134 foreach( $nslinks as $title => $id ) {
135 $entry = array();
136 $entry['ns'] = $ns;
137 $this->getResult()->setContent( $entry, Title::makeTitle( $ns, $title )->getFullText() );
138 if( $id != 0 )
139 $entry['exists'] = '';
140 $result[] = $entry;
141 }
142 }
143 return $result;
144 }
145
146 private function setIndexedTagNames( &$array, $mapping ) {
147 foreach( $mapping as $key => $name ) {
148 if( isset( $array[$key] ) )
149 $this->getResult()->setIndexedTagName( $array[$key], $name );
150 }
151 }
152
153 public function getAllowedParams() {
154 return array (
155 'title' => array(
156 ApiBase :: PARAM_DFLT => 'API',
157 ),
158 'text' => null,
159 'page' => null,
160 'prop' => array(
161 ApiBase :: PARAM_DFLT => 'text|langlinks|categories|links|templates|images|externallinks|sections',
162 ApiBase :: PARAM_ISMULTI => true,
163 ApiBase :: PARAM_TYPE => array(
164 'text',
165 'langlinks',
166 'categories',
167 'links',
168 'templates',
169 'images',
170 'externallinks',
171 'sections'
172 )
173 )
174 );
175 }
176
177 public function getParamDescription() {
178 return array (
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'
184 ),
185 );
186 }
187
188 public function getDescription() {
189 return 'This module parses wikitext and returns parser output';
190 }
191
192 protected function getExamples() {
193 return array (
194 'api.php?action=parse&text={{Project:Sandbox}}'
195 );
196 }
197
198 public function getVersion() {
199 return __CLASS__ . ': $Id$';
200 }
201 }
202