Update.
[mediawiki.git] / includes / api / ApiFormatBase.php
blobd3a7a79c3ac9cfdb47eab60fa7467a8b5ecbc703
1 <?php
4 /*
5 * Created on Sep 19, 2006
7 * API for MediaWiki 1.8+
9 * Copyright (C) 2006 Yuri Astrakhan <FirstnameLastname@gmail.com>
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.
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.
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
27 if (!defined('MEDIAWIKI')) {
28 // Eclipse helper - will be ignored in production
29 require_once ('ApiBase.php');
32 abstract class ApiFormatBase extends ApiBase {
34 private $mIsHtml, $mFormat;
36 /**
37 * Constructor
39 public function __construct($main, $format) {
40 parent :: __construct($main, $format);
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);
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
55 public abstract function getMimeType();
57 public function getNeedsRawData() {
58 return false;
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.
66 public function getIsHtml() {
67 return $this->mIsHtml;
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
75 function initPrinter($isError) {
76 $isHtml = $this->getIsHtml();
77 $mime = $isHtml ? 'text/html' : $this->getMimeType();
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
84 header("Content-Type: $mime; charset=utf-8;");
86 if ($isHtml) {
88 <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
89 <html>
90 <head>
91 <title>MediaWiki API</title>
92 </head>
93 <body>
94 <?php
97 if (!$isError) {
99 <br/>
100 <small>
101 You are looking at the HTML representation of the <?=$this->mFormat?> format.<br/>
102 HTML is good for debugging, but probably is not suitable for your application.<br/>
103 Please see "format" parameter documentation at the <a href='api.php'>API help</a>
104 for more information.
105 </small>
106 <?php
111 <pre>
112 <?php
119 * Finish printing. Closes HTML tags.
121 public function closePrinter() {
122 if ($this->getIsHtml()) {
125 </pre>
126 </body>
127 </html>
128 <?php
134 public function printText($text) {
135 if ($this->getIsHtml())
136 echo $this->formatHTML($text);
137 else
138 echo $text;
142 * Prety-print various elements in HTML format, such as xml tags and URLs.
143 * This method also replaces any '<' with &lt;
145 protected function formatHTML($text) {
146 // encode all tags as safe blue strings
147 $text = ereg_replace('\<([^>]+)\>', '<span style="color:blue;">&lt;\1&gt;</span>', $text);
148 // identify URLs
149 $protos = "http|https|ftp|gopher";
150 $text = ereg_replace("($protos)://[^ '\"()<\n]+", '<a href="\\0">\\0</a>', $text);
151 // identify requests to api.php
152 $text = ereg_replace("api\\.php\\?[^ ()<\n\t]+", '<a href="\\0">\\0</a>', $text);
153 // make strings inside * bold
154 $text = ereg_replace("\\*[^<>\n]+\\*", '<b>\\0</b>', $text);
155 // make strings inside $ italic
156 $text = ereg_replace("\\$[^<>\n]+\\$", '<b><i>\\0</i></b>', $text);
158 return $text;
162 * Returns usage examples for this format.
164 protected function getExamples() {
165 return 'api.php?action=query&meta=siteinfo&siprop=namespaces&format=' . $this->getModuleName();
168 protected function getDescription() {
169 return $this->getIsHtml() ? ' (pretty-print in HTML)' : '';
172 public static function getBaseVersion() {
173 return __CLASS__ . ': $Id$';
178 * This printer is used to wrap an instance of the Feed class
180 class ApiFormatFeedWrapper extends ApiFormatBase {
182 public function __construct($main) {
183 parent :: __construct($main, 'feed');
187 * Call this method to initialize output data
189 public static function setResult($result, $feed, $feedItems) {
190 // Store output in the Result data.
191 // This way we can check during execution if any error has occured
192 $data = & $result->getData();
193 $data['_feed'] = $feed;
194 $data['_feeditems'] = $feedItems;
198 * Feed does its own headers
200 public function getMimeType() {
201 return null;
205 * Optimization - no need to sanitize data that will not be needed
207 public function getNeedsRawData() {
208 return true;
211 public function execute() {
212 $data = $this->getResultData();
213 if (isset ($data['_feed']) && isset ($data['_feeditems'])) {
214 $feed = $data['_feed'];
215 $items = $data['_feeditems'];
217 $feed->outHeader();
218 foreach ($items as & $item)
219 $feed->outItem($item);
220 $feed->outFooter();
221 } else {
222 // Error has occured, print something usefull
223 // TODO: make this error more informative using ApiBase :: dieDebug() or similar
224 wfHttpError(500, 'Internal Server Error', '');
228 public function getVersion() {
229 return __CLASS__ . ': $Id$';