4 * Created on Sep 19, 2006
6 * API for MediaWiki 1.8+
8 * Copyright (C) 2006 Yuri Astrakhan <Firstname><Lastname>@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');
32 * This is the abstract base class for API formatters.
36 abstract class ApiFormatBase
extends ApiBase
{
38 private $mIsHtml, $mFormat;
41 * Create a new instance of the formatter.
42 * If the format name ends with 'fm', wrap its output in the proper HTML.
44 public function __construct($main, $format) {
45 parent
:: __construct($main, $format);
47 $this->mIsHtml
= (substr($format, -2, 2) === 'fm'); // ends with 'fm'
49 $this->mFormat
= substr($format, 0, -2); // remove ending 'fm'
51 $this->mFormat
= $format;
52 $this->mFormat
= strtoupper($this->mFormat
);
56 * Overriding class returns the mime type that should be sent to the client.
57 * This method is not called if getIsHtml() returns true.
60 public abstract function getMimeType();
63 * If formatter outputs data results as is, the results must first be sanitized.
64 * An XML formatter on the other hand uses special tags, such as "_element" for special handling,
65 * and thus needs to override this function to return true.
67 public function getNeedsRawData() {
72 * Returns true when an HTML filtering printer should be used.
73 * The default implementation assumes that formats ending with 'fm'
74 * should be formatted in HTML.
76 public function getIsHtml() {
77 return $this->mIsHtml
;
81 * Initialize the printer function and prepares the output headers, etc.
82 * This method must be the first outputing method during execution.
83 * A help screen's header is printed for the HTML-based output
85 function initPrinter($isError) {
86 $isHtml = $this->getIsHtml();
87 $mime = $isHtml ?
'text/html' : $this->getMimeType();
89 // Some printers (ex. Feed) do their own header settings,
90 // in which case $mime will be set to null
92 return; // skip any initialization
94 header("Content-Type: $mime; charset=utf-8");
98 <!DOCTYPE HTML
PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
101 <title
>MediaWiki API
</title
>
111 You are looking at the HTML representation of the
<?
=$this->mFormat?
> format
.<br
/>
112 HTML is good
for debugging
, but probably is not suitable
for your application
.<br
/>
113 Please see
"format" parameter documentation at the
<a href
='api.php'>API help
</a
>
114 for more information
.
129 * Finish printing. Closes HTML tags.
131 public function closePrinter() {
132 if ($this->getIsHtml()) {
145 * The main format printing function. Call it to output the result string to the user.
146 * This function will automatically output HTML when format name ends in 'fm'.
148 public function printText($text) {
149 if ($this->getIsHtml())
150 echo $this->formatHTML($text);
156 * Prety-print various elements in HTML format, such as xml tags and URLs.
157 * This method also replaces any '<' with <
159 protected function formatHTML($text) {
160 // encode all tags as safe blue strings
161 $text = ereg_replace('\<([^>]+)\>', '<span style="color:blue;"><\1></span>', $text);
163 $protos = "http|https|ftp|gopher";
164 $text = ereg_replace("($protos)://[^ \\'\"()<\n]+", '<a href="\\0">\\0</a>', $text);
165 // identify requests to api.php
166 $text = ereg_replace("api\\.php\\?[^ \\()<\n\t]+", '<a href="\\0">\\0</a>', $text);
167 // make strings inside * bold
168 $text = ereg_replace("\\*[^<>\n]+\\*", '<b>\\0</b>', $text);
169 // make strings inside $ italic
170 $text = ereg_replace("\\$[^<>\n]+\\$", '<b><i>\\0</i></b>', $text);
176 * Returns usage examples for this format.
178 protected function getExamples() {
179 return 'api.php?action=query&meta=siteinfo&siprop=namespaces&format=' . $this->getModuleName();
182 protected function getDescription() {
183 return $this->getIsHtml() ?
' (pretty-print in HTML)' : '';
186 public static function getBaseVersion() {
187 return __CLASS__
. ': $Id$';
192 * This printer is used to wrap an instance of the Feed class
195 class ApiFormatFeedWrapper
extends ApiFormatBase
{
197 public function __construct($main) {
198 parent
:: __construct($main, 'feed');
202 * Call this method to initialize output data. See self::execute()
204 public static function setResult($result, $feed, $feedItems) {
205 // Store output in the Result data.
206 // This way we can check during execution if any error has occured
207 $data = & $result->getData();
208 $data['_feed'] = $feed;
209 $data['_feeditems'] = $feedItems;
213 * Feed does its own headers
215 public function getMimeType() {
220 * Optimization - no need to sanitize data that will not be needed
222 public function getNeedsRawData() {
227 * This class expects the result data to be in a custom format set by self::setResult()
228 * $result['_feed'] - an instance of one of the $wgFeedClasses classes
229 * $result['_feeditems'] - an array of FeedItem instances
231 public function execute() {
232 $data = $this->getResultData();
233 if (isset ($data['_feed']) && isset ($data['_feeditems'])) {
234 $feed = $data['_feed'];
235 $items = $data['_feeditems'];
238 foreach ($items as & $item)
239 $feed->outItem($item);
242 // Error has occured, print something usefull
243 // TODO: make this error more informative using ApiBase :: dieDebug() or similar
244 wfHttpError(500, 'Internal Server Error', '');
248 public function getVersion() {
249 return __CLASS__
. ': $Id$';