4 * Created on Sep 19, 2006
6 * API for MediaWiki 1.8+
8 * Copyright (C) 2006 Yuri Astrakhan <FirstnameLastname@gmail.com>
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.
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.
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
26 if (!defined('MEDIAWIKI')) {
27 // Eclipse helper - will be ignored in production
28 require_once ('ApiBase.php');
34 abstract class ApiFormatBase
extends ApiBase
{
36 private $mIsHtml, $mFormat;
41 public function __construct($main, $format) {
42 parent
:: __construct($main, $format);
44 $this->mIsHtml
= (substr($format, -2, 2) === 'fm'); // ends with 'fm'
46 $this->mFormat
= substr($format, 0, -2); // remove ending 'fm'
48 $this->mFormat
= $format;
49 $this->mFormat
= strtoupper($this->mFormat
);
53 * Overriding class returns the mime type that should be sent to the client.
54 * This method is not called if getIsHtml() returns true.
57 public abstract function getMimeType();
59 public function getNeedsRawData() {
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.
68 public function getIsHtml() {
69 return $this->mIsHtml
;
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
77 function initPrinter($isError) {
78 $isHtml = $this->getIsHtml();
79 $mime = $isHtml ?
'text/html' : $this->getMimeType();
81 // Some printers (ex. Feed) do their own header settings,
82 // in which case $mime will be set to null
84 return; // skip any initialization
86 header("Content-Type: $mime; charset=utf-8");
90 <!DOCTYPE HTML
PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
93 <title
>MediaWiki API
</title
>
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
.
121 * Finish printing. Closes HTML tags.
123 public function closePrinter() {
124 if ($this->getIsHtml()) {
136 public function printText($text) {
137 if ($this->getIsHtml())
138 echo $this->formatHTML($text);
144 * Prety-print various elements in HTML format, such as xml tags and URLs.
145 * This method also replaces any '<' with <
147 protected function formatHTML($text) {
148 // encode all tags as safe blue strings
149 $text = ereg_replace('\<([^>]+)\>', '<span style="color:blue;"><\1></span>', $text);
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);
164 * Returns usage examples for this format.
166 protected function getExamples() {
167 return 'api.php?action=query&meta=siteinfo&siprop=namespaces&format=' . $this->getModuleName();
170 protected function getDescription() {
171 return $this->getIsHtml() ?
' (pretty-print in HTML)' : '';
174 public static function getBaseVersion() {
175 return __CLASS__
. ': $Id$';
180 * This printer is used to wrap an instance of the Feed class
183 class ApiFormatFeedWrapper
extends ApiFormatBase
{
185 public function __construct($main) {
186 parent
:: __construct($main, 'feed');
190 * Call this method to initialize output data
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;
201 * Feed does its own headers
203 public function getMimeType() {
208 * Optimization - no need to sanitize data that will not be needed
210 public function getNeedsRawData() {
214 public function execute() {
215 $data = $this->getResultData();
216 if (isset ($data['_feed']) && isset ($data['_feeditems'])) {
217 $feed = $data['_feed'];
218 $items = $data['_feeditems'];
221 foreach ($items as & $item)
222 $feed->outItem($item);
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', '');
231 public function getVersion() {
232 return __CLASS__
. ': $Id$';