ApiParse.php 8.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283
  1. <?php
  2. /*
  3. * Created on Dec 01, 2007
  4. *
  5. * API for MediaWiki 1.8+
  6. *
  7. * Copyright (C) 2007 Yuri Astrakhan <Firstname><Lastname>@gmail.com
  8. *
  9. * This program is free software; you can redistribute it and/or modify
  10. * it under the terms of the GNU General Public License as published by
  11. * the Free Software Foundation; either version 2 of the License, or
  12. * (at your option) any later version.
  13. *
  14. * This program is distributed in the hope that it will be useful,
  15. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  16. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  17. * GNU General Public License for more details.
  18. *
  19. * You should have received a copy of the GNU General Public License along
  20. * with this program; if not, write to the Free Software Foundation, Inc.,
  21. * 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
  22. * http://www.gnu.org/copyleft/gpl.html
  23. */
  24. if (!defined('MEDIAWIKI')) {
  25. // Eclipse helper - will be ignored in production
  26. require_once ("ApiBase.php");
  27. }
  28. /**
  29. * @ingroup API
  30. */
  31. class ApiParse extends ApiBase {
  32. public function __construct($main, $action) {
  33. parent :: __construct($main, $action);
  34. }
  35. public function execute() {
  36. // Get parameters
  37. $params = $this->extractRequestParams();
  38. $text = $params['text'];
  39. $title = $params['title'];
  40. $page = $params['page'];
  41. $oldid = $params['oldid'];
  42. if(!is_null($page) && (!is_null($text) || $title != "API"))
  43. $this->dieUsage("The page parameter cannot be used together with the text and title parameters", 'params');
  44. $prop = array_flip($params['prop']);
  45. $revid = false;
  46. // The parser needs $wgTitle to be set, apparently the
  47. // $title parameter in Parser::parse isn't enough *sigh*
  48. global $wgParser, $wgUser, $wgTitle;
  49. $popts = new ParserOptions();
  50. $popts->setTidy(true);
  51. $popts->enableLimitReport();
  52. $redirValues = null;
  53. if(!is_null($oldid) || !is_null($page))
  54. {
  55. if(!is_null($oldid))
  56. {
  57. # Don't use the parser cache
  58. $rev = Revision::newFromID($oldid);
  59. if(!$rev)
  60. $this->dieUsage("There is no revision ID $oldid", 'missingrev');
  61. if(!$rev->userCan(Revision::DELETED_TEXT))
  62. $this->dieUsage("You don't have permission to view deleted revisions", 'permissiondenied');
  63. $text = $rev->getText( Revision::FOR_THIS_USER );
  64. $titleObj = $rev->getTitle();
  65. $wgTitle = $titleObj;
  66. $p_result = $wgParser->parse($text, $titleObj, $popts);
  67. }
  68. else
  69. {
  70. if($params['redirects'])
  71. {
  72. $req = new FauxRequest(array(
  73. 'action' => 'query',
  74. 'redirects' => '',
  75. 'titles' => $page
  76. ));
  77. $main = new ApiMain($req);
  78. $main->execute();
  79. $data = $main->getResultData();
  80. $redirValues = @$data['query']['redirects'];
  81. $to = $page;
  82. foreach((array)$redirValues as $r)
  83. $to = $r['to'];
  84. }
  85. else
  86. $to = $page;
  87. $titleObj = Title::newFromText($to);
  88. if(!$titleObj)
  89. $this->dieUsage("The page you specified doesn't exist", 'missingtitle');
  90. $articleObj = new Article($titleObj);
  91. if(isset($prop['revid']))
  92. $oldid = $articleObj->getRevIdFetched();
  93. // Try the parser cache first
  94. $pcache = ParserCache::singleton();
  95. $p_result = $pcache->get($articleObj, $wgUser);
  96. if(!$p_result)
  97. {
  98. $p_result = $wgParser->parse($articleObj->getContent(), $titleObj, $popts);
  99. global $wgUseParserCache;
  100. if($wgUseParserCache)
  101. $pcache->save($p_result, $articleObj, $popts);
  102. }
  103. }
  104. }
  105. else
  106. {
  107. $titleObj = Title::newFromText($title);
  108. if(!$titleObj)
  109. $titleObj = Title::newFromText("API");
  110. $wgTitle = $titleObj;
  111. if($params['pst'] || $params['onlypst'])
  112. $text = $wgParser->preSaveTransform($text, $titleObj, $wgUser, $popts);
  113. if($params['onlypst'])
  114. {
  115. // Build a result and bail out
  116. $result_array['text'] = array();
  117. $this->getResult()->setContent($result_array['text'], $text);
  118. $this->getResult()->addValue(null, $this->getModuleName(), $result_array);
  119. return;
  120. }
  121. $p_result = $wgParser->parse($text, $titleObj, $popts);
  122. }
  123. // Return result
  124. $result = $this->getResult();
  125. $result_array = array();
  126. if($params['redirects'] && !is_null($redirValues))
  127. $result_array['redirects'] = $redirValues;
  128. if(isset($prop['text'])) {
  129. $result_array['text'] = array();
  130. $result->setContent($result_array['text'], $p_result->getText());
  131. }
  132. if(isset($prop['langlinks']))
  133. $result_array['langlinks'] = $this->formatLangLinks($p_result->getLanguageLinks());
  134. if(isset($prop['categories']))
  135. $result_array['categories'] = $this->formatCategoryLinks($p_result->getCategories());
  136. if(isset($prop['links']))
  137. $result_array['links'] = $this->formatLinks($p_result->getLinks());
  138. if(isset($prop['templates']))
  139. $result_array['templates'] = $this->formatLinks($p_result->getTemplates());
  140. if(isset($prop['images']))
  141. $result_array['images'] = array_keys($p_result->getImages());
  142. if(isset($prop['externallinks']))
  143. $result_array['externallinks'] = array_keys($p_result->getExternalLinks());
  144. if(isset($prop['sections']))
  145. $result_array['sections'] = $p_result->getSections();
  146. if(isset($prop['displaytitle']))
  147. $result_array['displaytitle'] = $p_result->getDisplayTitle() ?
  148. $p_result->getDisplayTitle() :
  149. $titleObj->getPrefixedText();
  150. if(!is_null($oldid))
  151. $result_array['revid'] = intval($oldid);
  152. $result_mapping = array(
  153. 'redirects' => 'r',
  154. 'langlinks' => 'll',
  155. 'categories' => 'cl',
  156. 'links' => 'pl',
  157. 'templates' => 'tl',
  158. 'images' => 'img',
  159. 'externallinks' => 'el',
  160. 'sections' => 's',
  161. );
  162. $this->setIndexedTagNames( $result_array, $result_mapping );
  163. $result->addValue( null, $this->getModuleName(), $result_array );
  164. }
  165. private function formatLangLinks( $links ) {
  166. $result = array();
  167. foreach( $links as $link ) {
  168. $entry = array();
  169. $bits = split( ':', $link, 2 );
  170. $entry['lang'] = $bits[0];
  171. $this->getResult()->setContent( $entry, $bits[1] );
  172. $result[] = $entry;
  173. }
  174. return $result;
  175. }
  176. private function formatCategoryLinks( $links ) {
  177. $result = array();
  178. foreach( $links as $link => $sortkey ) {
  179. $entry = array();
  180. $entry['sortkey'] = $sortkey;
  181. $this->getResult()->setContent( $entry, $link );
  182. $result[] = $entry;
  183. }
  184. return $result;
  185. }
  186. private function formatLinks( $links ) {
  187. $result = array();
  188. foreach( $links as $ns => $nslinks ) {
  189. foreach( $nslinks as $title => $id ) {
  190. $entry = array();
  191. $entry['ns'] = $ns;
  192. $this->getResult()->setContent( $entry, Title::makeTitle( $ns, $title )->getFullText() );
  193. if( $id != 0 )
  194. $entry['exists'] = '';
  195. $result[] = $entry;
  196. }
  197. }
  198. return $result;
  199. }
  200. private function setIndexedTagNames( &$array, $mapping ) {
  201. foreach( $mapping as $key => $name ) {
  202. if( isset( $array[$key] ) )
  203. $this->getResult()->setIndexedTagName( $array[$key], $name );
  204. }
  205. }
  206. public function getAllowedParams() {
  207. return array (
  208. 'title' => array(
  209. ApiBase :: PARAM_DFLT => 'API',
  210. ),
  211. 'text' => null,
  212. 'page' => null,
  213. 'redirects' => false,
  214. 'oldid' => null,
  215. 'prop' => array(
  216. ApiBase :: PARAM_DFLT => 'text|langlinks|categories|links|templates|images|externallinks|sections|revid|displaytitle',
  217. ApiBase :: PARAM_ISMULTI => true,
  218. ApiBase :: PARAM_TYPE => array(
  219. 'text',
  220. 'langlinks',
  221. 'categories',
  222. 'links',
  223. 'templates',
  224. 'images',
  225. 'externallinks',
  226. 'sections',
  227. 'revid',
  228. 'displaytitle',
  229. )
  230. ),
  231. 'pst' => false,
  232. 'onlypst' => false,
  233. );
  234. }
  235. public function getParamDescription() {
  236. return array (
  237. 'text' => 'Wikitext to parse',
  238. 'redirects' => 'If the page parameter is set to a redirect, resolve it',
  239. 'title' => 'Title of page the text belongs to',
  240. 'page' => 'Parse the content of this page. Cannot be used together with text and title',
  241. 'oldid' => 'Parse the content of this revision. Overrides page',
  242. 'prop' => array('Which pieces of information to get.',
  243. 'NOTE: Section tree is only generated if there are more than 4 sections, or if the __TOC__ keyword is present'
  244. ),
  245. 'pst' => array( 'Do a pre-save transform on the input before parsing it.',
  246. 'Ignored if page or oldid is used.'
  247. ),
  248. 'onlypst' => array('Do a PST on the input, but don\'t parse it.',
  249. 'Returns PSTed wikitext. Ignored if page or oldid is used.'
  250. ),
  251. );
  252. }
  253. public function getDescription() {
  254. return 'This module parses wikitext and returns parser output';
  255. }
  256. protected function getExamples() {
  257. return array (
  258. 'api.php?action=parse&text={{Project:Sandbox}}'
  259. );
  260. }
  261. public function getVersion() {
  262. return __CLASS__ . ': $Id: ApiParse.php 48544 2009-03-18 23:27:48Z aboostani $';
  263. }
  264. }