Fix some places where globals where used without being declared as
[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
46 //Create title for parser
47 $title_obj = Title :: newFromText($params['title']);
48 if(!$title_obj)
49 $title_obj = Title :: newFromText("API"); // Default title is "API". For example, ExpandTemplates uses "ExpendTemplates" for it
50
51 // Parse text
52 global $wgParser;
53 $p_result = $wgParser->parse( $text, $title_obj, new ParserOptions() );
54
55 // Return result
56 $result = $this->getResult();
57 $result_array = array(
58 'text' => array(),
59 'langlinks' => $this->formatLangLinks( $p_result->getLanguageLinks() ),
60 'categories' => $this->formatCategoryLinks( $p_result->getCategories() ),
61 'links' => $this->formatLinks( $p_result->getLinks() ),
62 'templates' => $this->formatLinks( $p_result->getTemplates() ),
63 'images' => array_keys( $p_result->getImages() ),
64 'externallinks' => array_keys( $p_result->getExternalLinks() ),
65 'sections' => $p_result->getSections(),
66 );
67 $result_mapping = array(
68 'langlinks' => 'll',
69 'categories' => 'cl',
70 'links' => 'pl',
71 'templates' => 'tl',
72 'images' => 'img',
73 'externallinks' => 'el',
74 'sections' => 's',
75 );
76 $this->setIndexedTagNames( $result_array, $result_mapping );
77 $result->setContent( $result_array['text'], $p_result->getText() );
78 $result->addValue( null, $this->getModuleName(), $result_array );
79 }
80
81 private function formatLangLinks( $links ) {
82 $result = array();
83 foreach( $links as $link ) {
84 $entry = array();
85 $bits = split( ':', $link, 2 );
86 $entry['lang'] = $bits[0];
87 $this->getResult()->setContent( $entry, $bits[1] );
88 $result[] = $entry;
89 }
90 return $result;
91 }
92
93 private function formatCategoryLinks( $links ) {
94 $result = array();
95 foreach( $links as $link => $sortkey ) {
96 $entry = array();
97 $entry['sortkey'] = $sortkey;
98 $this->getResult()->setContent( $entry, $link );
99 $result[] = $entry;
100 }
101 return $result;
102 }
103
104 private function formatLinks( $links ) {
105 $result = array();
106 foreach( $links as $ns => $nslinks ) {
107 foreach( $nslinks as $title => $id ) {
108 $entry = array();
109 $entry['ns'] = $ns;
110 $this->getResult()->setContent( $entry, Title::makeTitle( $ns, $title )->getFullText() );
111 if( $id != 0 )
112 $entry['exists'] = '';
113 $result[] = $entry;
114 }
115 }
116 return $result;
117 }
118
119 private function setIndexedTagNames( &$array, $mapping ) {
120 foreach( $mapping as $key => $name ) {
121 if( isset( $array[$key] ) )
122 $this->getResult()->setIndexedTagName( $array[$key], $name );
123 }
124 }
125
126 protected function getAllowedParams() {
127 return array (
128 'title' => array(
129 ApiBase :: PARAM_DFLT => 'API',
130 ),
131 'text' => null
132 );
133 }
134
135 protected function getParamDescription() {
136 return array (
137 'text' => 'Wikitext to parse',
138 'title' => 'Title of page',
139 );
140 }
141
142 protected function getDescription() {
143 return 'This module parses wikitext and returns parser output';
144 }
145
146 protected function getExamples() {
147 return array (
148 'api.php?action=parse&text={{Project:Sandbox}}'
149 );
150 }
151
152 public function getVersion() {
153 return __CLASS__ . ': $Id$';
154 }
155 }
156