API: disabled login module until it is more secure. added performance comment in...
[lhc/web/wiklou.git] / includes / api / ApiFormatBase.php
1 <?php
2
3 /*
4 * Created on Sep 19, 2006
5 *
6 * API for MediaWiki 1.8+
7 *
8 * Copyright (C) 2006 Yuri Astrakhan <FirstnameLastname@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 abstract class ApiFormatBase extends ApiBase {
35
36 private $mIsHtml, $mFormat;
37
38 /**
39 * Constructor
40 */
41 public function __construct($main, $format) {
42 parent :: __construct($main, $format);
43
44 $this->mIsHtml = (substr($format, -2, 2) === 'fm'); // ends with 'fm'
45 if ($this->mIsHtml)
46 $this->mFormat = substr($format, 0, -2); // remove ending 'fm'
47 else
48 $this->mFormat = $format;
49 $this->mFormat = strtoupper($this->mFormat);
50 }
51
52 /**
53 * Overriding class returns the mime type that should be sent to the client.
54 * This method is not called if getIsHtml() returns true.
55 * @return string
56 */
57 public abstract function getMimeType();
58
59 public function getNeedsRawData() {
60 return false;
61 }
62
63 /**
64 * Returns true when an HTML filtering printer should be used.
65 * The default implementation assumes that formats ending with 'fm'
66 * should be formatted in HTML.
67 */
68 public function getIsHtml() {
69 return $this->mIsHtml;
70 }
71
72 /**
73 * Initialize the printer function and prepares the output headers, etc.
74 * This method must be the first outputing method during execution.
75 * A help screen's header is printed for the HTML-based output
76 */
77 function initPrinter($isError) {
78 $isHtml = $this->getIsHtml();
79 $mime = $isHtml ? 'text/html' : $this->getMimeType();
80
81 // Some printers (ex. Feed) do their own header settings,
82 // in which case $mime will be set to null
83 if (is_null($mime))
84 return; // skip any initialization
85
86 header("Content-Type: $mime; charset=utf-8");
87
88 if ($isHtml) {
89 ?>
90 <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
91 <html>
92 <head>
93 <title>MediaWiki API</title>
94 </head>
95 <body>
96 <?php
97
98
99 if (!$isError) {
100 ?>
101 <br/>
102 <small>
103 You are looking at the HTML representation of the <?=$this->mFormat?> format.<br/>
104 HTML is good for debugging, but probably is not suitable for your application.<br/>
105 Please see "format" parameter documentation at the <a href='api.php'>API help</a>
106 for more information.
107 </small>
108 <?php
109
110
111 }
112 ?>
113 <pre>
114 <?php
115
116
117 }
118 }
119
120 /**
121 * Finish printing. Closes HTML tags.
122 */
123 public function closePrinter() {
124 if ($this->getIsHtml()) {
125 ?>
126
127 </pre>
128 </body>
129 </html>
130 <?php
131
132
133 }
134 }
135
136 public function printText($text) {
137 if ($this->getIsHtml())
138 echo $this->formatHTML($text);
139 else
140 echo $text;
141 }
142
143 /**
144 * Prety-print various elements in HTML format, such as xml tags and URLs.
145 * This method also replaces any '<' with &lt;
146 */
147 protected function formatHTML($text) {
148 // encode all tags as safe blue strings
149 $text = ereg_replace('\<([^>]+)\>', '<span style="color:blue;">&lt;\1&gt;</span>', $text);
150 // identify URLs
151 $protos = "http|https|ftp|gopher";
152 $text = ereg_replace("($protos)://[^ '\"()<\n]+", '<a href="\\0">\\0</a>', $text);
153 // identify requests to api.php
154 $text = ereg_replace("api\\.php\\?[^ ()<\n\t]+", '<a href="\\0">\\0</a>', $text);
155 // make strings inside * bold
156 $text = ereg_replace("\\*[^<>\n]+\\*", '<b>\\0</b>', $text);
157 // make strings inside $ italic
158 $text = ereg_replace("\\$[^<>\n]+\\$", '<b><i>\\0</i></b>', $text);
159
160 return $text;
161 }
162
163 /**
164 * Returns usage examples for this format.
165 */
166 protected function getExamples() {
167 return 'api.php?action=query&meta=siteinfo&siprop=namespaces&format=' . $this->getModuleName();
168 }
169
170 protected function getDescription() {
171 return $this->getIsHtml() ? ' (pretty-print in HTML)' : '';
172 }
173
174 public static function getBaseVersion() {
175 return __CLASS__ . ': $Id$';
176 }
177 }
178
179 /**
180 * This printer is used to wrap an instance of the Feed class
181 * @addtogroup API
182 */
183 class ApiFormatFeedWrapper extends ApiFormatBase {
184
185 public function __construct($main) {
186 parent :: __construct($main, 'feed');
187 }
188
189 /**
190 * Call this method to initialize output data
191 */
192 public static function setResult($result, $feed, $feedItems) {
193 // Store output in the Result data.
194 // This way we can check during execution if any error has occured
195 $data = & $result->getData();
196 $data['_feed'] = $feed;
197 $data['_feeditems'] = $feedItems;
198 }
199
200 /**
201 * Feed does its own headers
202 */
203 public function getMimeType() {
204 return null;
205 }
206
207 /**
208 * Optimization - no need to sanitize data that will not be needed
209 */
210 public function getNeedsRawData() {
211 return true;
212 }
213
214 public function execute() {
215 $data = $this->getResultData();
216 if (isset ($data['_feed']) && isset ($data['_feeditems'])) {
217 $feed = $data['_feed'];
218 $items = $data['_feeditems'];
219
220 $feed->outHeader();
221 foreach ($items as & $item)
222 $feed->outItem($item);
223 $feed->outFooter();
224 } else {
225 // Error has occured, print something usefull
226 // TODO: make this error more informative using ApiBase :: dieDebug() or similar
227 wfHttpError(500, 'Internal Server Error', '');
228 }
229 }
230
231 public function getVersion() {
232 return __CLASS__ . ': $Id$';
233 }
234 }
235 ?>