* API: help screen now shows default and allowed parameter values
[lhc/web/wiklou.git] / includes / api / ApiFormatBase.php
1 <?php
2
3
4 /*
5 * Created on Sep 19, 2006
6 *
7 * API for MediaWiki 1.8+
8 *
9 * Copyright (C) 2006 Yuri Astrakhan <FirstnameLastname@gmail.com>
10 *
11 * This program is free software; you can redistribute it and/or modify
12 * it under the terms of the GNU General Public License as published by
13 * the Free Software Foundation; either version 2 of the License, or
14 * (at your option) any later version.
15 *
16 * This program is distributed in the hope that it will be useful,
17 * but WITHOUT ANY WARRANTY; without even the implied warranty of
18 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
19 * GNU General Public License for more details.
20 *
21 * You should have received a copy of the GNU General Public License along
22 * with this program; if not, write to the Free Software Foundation, Inc.,
23 * 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
24 * http://www.gnu.org/copyleft/gpl.html
25 */
26
27 if (!defined('MEDIAWIKI')) {
28 // Eclipse helper - will be ignored in production
29 require_once ('ApiBase.php');
30 }
31
32 abstract class ApiFormatBase extends ApiBase {
33
34 private $mIsHtml, $mFormat;
35
36 /**
37 * Constructor
38 */
39 public function __construct($main, $format) {
40 parent :: __construct($main, $format);
41
42 $this->mIsHtml = (substr($format, -2, 2) === 'fm'); // ends with 'fm'
43 if ($this->mIsHtml)
44 $this->mFormat = substr($format, 0, -2); // remove ending 'fm'
45 else
46 $this->mFormat = $format;
47 $this->mFormat = strtoupper($this->mFormat);
48 }
49
50 /**
51 * Overriding class returns the mime type that should be sent to the client.
52 * This method is not called if getIsHtml() returns true.
53 * @return string
54 */
55 public abstract function getMimeType();
56
57 public function getNeedsRawData() {
58 return false;
59 }
60
61 /**
62 * Returns true when an HTML filtering printer should be used.
63 * The default implementation assumes that formats ending with 'fm'
64 * should be formatted in HTML.
65 */
66 public function getIsHtml() {
67 return $this->mIsHtml;
68 }
69
70 /**
71 * Initialize the printer function and prepares the output headers, etc.
72 * This method must be the first outputing method during execution.
73 * A help screen's header is printed for the HTML-based output
74 */
75 function initPrinter($isError) {
76 $isHtml = $this->getIsHtml();
77 $mime = $isHtml ? 'text/html' : $this->getMimeType();
78
79 // Some printers (ex. Feed) do their own header settings,
80 // in which case $mime will be set to null
81 if (is_null($mime))
82 return; // skip any initialization
83
84 header("Content-Type: $mime; charset=utf-8;");
85 header("Cache-Control: private, s-maxage=0, max-age=0");
86
87 if ($isHtml) {
88 ?>
89 <html>
90 <head>
91 <title>MediaWiki API</title>
92 </head>
93 <body>
94 <?php
95
96
97 if (!$isError) {
98 ?>
99 <br/>
100 <small>
101 This result is being shown in <?=$this->mFormat?> format,
102 which might not be suitable for your application.<br/>
103 See <a href='api.php'>API help</a> for more information.<br/>
104 </small>
105 <?php
106
107
108 }
109 ?>
110 <pre>
111 <?php
112
113
114 }
115 }
116
117 /**
118 * Finish printing. Closes HTML tags.
119 */
120 public function closePrinter() {
121 if ($this->getIsHtml()) {
122 ?>
123 </pre>
124 </body>
125 <?php
126
127
128 }
129 }
130
131 public function printText($text) {
132 if ($this->getIsHtml())
133 echo $this->formatHTML($text);
134 else
135 echo $text;
136 }
137
138 /**
139 * Prety-print various elements in HTML format, such as xml tags and URLs.
140 * This method also replaces any '<' with &lt;
141 */
142 protected function formatHTML($text) {
143 // encode all tags as safe blue strings
144 $text = ereg_replace('\<([^>]+)\>', '<font color=blue>&lt;\1&gt;</font>', $text);
145 // identify URLs
146 $text = ereg_replace("[a-zA-Z]+://[^ '()<\n]+", '<a href="\\0">\\0</a>', $text);
147 // identify requests to api.php
148 $text = ereg_replace("api\\.php\\?[^ ()<\n\t]+", '<a href="\\0">\\0</a>', $text);
149 // make strings inside * bold
150 $text = ereg_replace("\\*[^<>\n]+\\*", '<b>\\0</b>', $text);
151 // make strings inside $ italic
152 $text = ereg_replace("\\$[^<>\n]+\\$", '<b><i>\\0</i></b>', $text);
153
154 return $text;
155 }
156
157 /**
158 * Returns usage examples for this format.
159 */
160 protected function getExamples() {
161 return 'api.php?action=query&meta=siteinfo&si=namespaces&format=' . $this->getModuleName();
162 }
163
164 public static function getBaseVersion() {
165 return __CLASS__ . ': $Id$';
166 }
167 }
168
169 /**
170 * This printer is used to wrap an instance of the Feed class
171 */
172 class ApiFormatFeedWrapper extends ApiFormatBase {
173
174 public function __construct($main) {
175 parent :: __construct($main, 'feed');
176 }
177
178 /**
179 * Call this method to initialize output data
180 */
181 public static function setResult($result, $feed, $feedItems) {
182 // Store output in the Result data.
183 // This way we can check during execution if any error has occured
184 $data =& $result->getData();
185 $data['_feed'] = $feed;
186 $data['_feeditems'] = $feedItems;
187 }
188
189 /**
190 * Feed does its own headers
191 */
192 public function getMimeType() {
193 return null;
194 }
195
196 /**
197 * Optimization - no need to sanitize data that will not be needed
198 */
199 public function getNeedsRawData() {
200 return true;
201 }
202
203 public function execute() {
204 $data =& $this->getResultData();
205 if (isset($data['_feed']) && isset($data['_feeditems'])) {
206 $feed =& $data['_feed'];
207 $items =& $data['_feeditems'];
208
209 $feed->outHeader();
210 foreach($items as &$item)
211 $feed->outItem($item);
212 $feed->outFooter();
213 } else {
214 // Error has occured, print something usefull
215 // TODO: make this error more informative using $this->dieDebug() or similar
216 wfHttpError(500, 'Internal Server Error', '');
217 }
218 }
219
220 public function getVersion() {
221 return __CLASS__ . ': $Id:$';
222 }
223 }
224 ?>