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, $mOriginalFormat;
39 public function __construct($main, $format) {
40 parent
:: __construct($main);
42 $this->mOriginalFormat
= $format;
43 $this->mIsHtml
= (substr($format, -2, 2) === 'fm'); // ends with 'fm'
45 $this->mFormat
= substr($format, 0, -2); // remove ending 'fm'
47 $this->mFormat
= $format;
48 $this->mFormat
= strtoupper($this->mFormat
);
52 * Overriding class returns the mime type that should be sent to the client.
53 * This method is not called if getIsHtml() returns true.
56 public abstract function getMimeType();
58 public function getNeedsRawData() {
63 * Returns true when an HTML filtering printer should be used.
64 * The default implementation assumes that formats ending with 'fm'
65 * should be formatted in HTML.
67 public function getIsHtml() {
68 return $this->mIsHtml
;
72 * Initialize the printer function and prepares the output headers, etc.
73 * This method must be the first outputing method during execution.
74 * A help screen's header is printed for the HTML-based output
76 function initPrinter($isError) {
77 $isHtml = $this->getIsHtml();
78 $mime = $isHtml ?
'text/html' : $this->getMimeType();
79 header("Content-Type: $mime; charset=utf-8;");
85 <title
>MediaWiki API
</title
>
95 This result is being shown in
<?
=$this->mFormat?
> format
,
96 which might not be suitable
for your application
.<br
/>
97 See
<a href
='api.php'>API help
</a
> for more information
.<br
/>
112 * Finish printing. Closes HTML tags.
114 public function closePrinter() {
115 if ($this->getIsHtml()) {
125 public function printText($text) {
126 if ($this->getIsHtml())
127 echo $this->formatHTML($text);
133 * Prety-print various elements in HTML format, such as xml tags and URLs.
134 * This method also replaces any '<' with <
136 protected function formatHTML($text) {
137 // encode all tags as safe blue strings
138 $text = ereg_replace('\<([^>]+)\>', '<font color=blue><\1></font>', $text);
140 $text = ereg_replace("[a-zA-Z]+://[^ '()<\n]+", '<a href="\\0">\\0</a>', $text);
141 // identify requests to api.php
142 $text = ereg_replace("api\\.php\\?[^ ()<\n\t]+", '<a href="\\0">\\0</a>', $text);
143 // make strings inside * bold
144 $text = ereg_replace("\\*[^<>\n]+\\*", '<b>\\0</b>', $text);
150 * Returns usage examples for this format.
152 protected function getExamples() {
153 return 'api.php?action=query&meta=siteinfo&si=namespaces&format=' . $this->mOriginalFormat
;