f87cd8162058a1411a1252a8fe5be7f5bfc965e8
[lhc/web/wiklou.git] / includes / api / ApiFormatBase.php
1 <?php
2
3
4 /*
5 * Created on Sep 19, 2006
6 *
7 * API for MediaWiki 1.8+
8 *
9 * Copyright (C) 2006 Yuri Astrakhan <FirstnameLastname@gmail.com>
10 *
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.
15 *
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.
20 *
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
25 */
26
27 if (!defined('MEDIAWIKI')) {
28 // Eclipse helper - will be ignored in production
29 require_once ('ApiBase.php');
30 }
31
32 abstract class ApiFormatBase extends ApiBase {
33
34 private $mIsHtml, $mFormat;
35
36 /**
37 * Constructor
38 */
39 public function __construct($main, $format) {
40 parent :: __construct($main, $format);
41
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);
48 }
49
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
54 */
55 public abstract function getMimeType();
56
57 public function execute() {
58 ApiBase :: dieDebug(__METHOD__, 'This is not an executable module');
59 }
60
61 /**
62 * Format modules must override this method to implement actual printing
63 */
64 public abstract function executePrinter();
65
66 public function getNeedsRawData() {
67 return false;
68 }
69
70 /**
71 * Returns true when an HTML filtering printer should be used.
72 * The default implementation assumes that formats ending with 'fm'
73 * should be formatted in HTML.
74 */
75 public function getIsHtml() {
76 return $this->mIsHtml;
77 }
78
79 /**
80 * Initialize the printer function and prepares the output headers, etc.
81 * This method must be the first outputing method during execution.
82 * A help screen's header is printed for the HTML-based output
83 */
84 function initPrinter($isError) {
85 $isHtml = $this->getIsHtml();
86 $mime = $isHtml ? 'text/html' : $this->getMimeType();
87 header("Content-Type: $mime; charset=utf-8;");
88 header("Cache-Control: private, s-maxage=0, max-age=0");
89
90 if ($isHtml) {
91 ?>
92 <html>
93 <head>
94 <title>MediaWiki API</title>
95 </head>
96 <body>
97 <?php
98
99
100 if (!$isError) {
101 ?>
102 <br/>
103 <small>
104 This result is being shown in <?=$this->mFormat?> format,
105 which might not be suitable for your application.<br/>
106 See <a href='api.php'>API help</a> for more information.<br/>
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 </pre>
127 </body>
128 <?php
129
130
131 }
132 }
133
134 public function printText($text) {
135 if ($this->getIsHtml())
136 echo $this->formatHTML($text);
137 else
138 echo $text;
139 }
140
141 /**
142 * Prety-print various elements in HTML format, such as xml tags and URLs.
143 * This method also replaces any '<' with &lt;
144 */
145 protected function formatHTML($text) {
146 // encode all tags as safe blue strings
147 $text = ereg_replace('\<([^>]+)\>', '<font color=blue>&lt;\1&gt;</font>', $text);
148 // identify URLs
149 $text = ereg_replace("[a-zA-Z]+://[^ '()<\n]+", '<a href="\\0">\\0</a>', $text);
150 // identify requests to api.php
151 $text = ereg_replace("api\\.php\\?[^ ()<\n\t]+", '<a href="\\0">\\0</a>', $text);
152 // make strings inside * bold
153 $text = ereg_replace("\\*[^<>\n]+\\*", '<b>\\0</b>', $text);
154 // make strings inside $ italic
155 $text = ereg_replace("\\$[^<>\n]+\\$", '<b><i>\\0</i></b>', $text);
156
157 return $text;
158 }
159
160 /**
161 * Returns usage examples for this format.
162 */
163 protected function getExamples() {
164 return 'api.php?action=query&meta=siteinfo&si=namespaces&format=' . $this->getModuleName();
165 }
166
167 public static function getBaseVersion() {
168 return __CLASS__ . ': $Id$';
169 }
170 }
171 ?>