38
38
private $mIsHtml, $mFormat, $mUnescapeAmps, $mHelp, $mCleared;
41
* Create a new instance of the formatter.
42
* If the format name ends with 'fm', wrap its output in the proper HTML.
42
* If $format ends with 'fm', pretty-print the output in HTML.
43
* @param $main ApiMain
44
* @param $format string Format name
44
46
public function __construct($main, $format) {
45
47
parent :: __construct($main, $format);
61
63
public abstract function getMimeType();
64
* If formatter outputs data results as is, the results must first be sanitized.
65
* An XML formatter on the other hand uses special tags, such as "_element" for special handling,
66
* and thus needs to override this function to return true.
66
* Whether this formatter needs raw data such as _element tags
68
69
public function getNeedsRawData() {
73
74
* Get the internal format name
75
77
public function getFormat() {
76
78
return $this->mFormat;
80
* Specify whether or not ampersands should be escaped to '&' when rendering. This
81
* should only be set to true for the help message when rendered in the default (xmlfm)
82
* format. This is a temporary special-case fix that should be removed once the help
83
* has been reworked to use a fully html interface.
82
* Specify whether or not sequences like " should be unescaped
83
* to " . This should only be set to true for the help message
84
* when rendered in the default (xmlfm) format. This is a temporary
85
* special-case fix that should be removed once the help has been
86
* reworked to use a fully HTML interface.
85
* @param boolean Whether or not ampersands should be escaped.
88
* @param $b bool Whether or not ampersands should be escaped.
87
90
public function setUnescapeAmps ( $b ) {
88
91
$this->mUnescapeAmps = $b;
92
* Returns true when an HTML filtering printer should be used.
95
* Returns true when the HTML pretty-printer should be used.
93
96
* The default implementation assumes that formats ending with 'fm'
94
97
* should be formatted in HTML.
96
100
public function getIsHtml() {
97
101
return $this->mIsHtml;
101
* Initialize the printer function and prepares the output headers, etc.
105
* Initialize the printer function and prepare the output headers, etc.
102
106
* This method must be the first outputing method during execution.
103
107
* A help screen's header is printed for the HTML-based output
108
* @param $isError bool Whether an error message is printed
105
110
function initPrinter($isError) {
106
111
$isHtml = $this->getIsHtml();
170
* The main format printing function. Call it to output the result string to the user.
171
* This function will automatically output HTML when format name ends in 'fm'.
175
* The main format printing function. Call it to output the result
176
* string to the user. This function will automatically output HTML
177
* when format name ends in 'fm'.
178
* @param $text string
173
180
public function printText($text) {
174
181
if ($this->getIsHtml())
191
* Says pretty-printer that it should use *bold* and $italics$ formatting
198
* Sets whether the pretty-printer should format *bold* and $italics$
193
201
public function setHelp( $help = true ) {
194
202
$this->mHelp = true;
198
* Prety-print various elements in HTML format, such as xml tags and URLs.
199
* This method also replaces any '<' with <
206
* Prety-print various elements in HTML format, such as xml tags and
207
* URLs. This method also escapes characters like <
208
* @param $text string
201
211
protected function formatHTML($text) {
202
212
global $wgUrlProtocols;
210
220
$protos = implode("|", $wgUrlProtocols);
211
221
# This regex hacks around bug 13218 (" included in the URL)
212
$text = preg_replace("#(($protos).*?)(")?([ \\'\"()<\n])#", '<a href="\\1">\\1</a>\\3\\4', $text);
222
$text = preg_replace("#(($protos).*?)(")?([ \\'\"<>\n]|<|>|")#", '<a href="\\1">\\1</a>\\3\\4', $text);
213
223
// identify requests to api.php
214
224
$text = preg_replace("#api\\.php\\?[^ \\()<\n\t]+#", '<a href="\\0">\\0</a>', $text);
215
225
if( $this->mHelp ) {
216
226
// make strings inside * bold
217
$text = ereg_replace("\\*[^<>\n]+\\*", '<b>\\0</b>', $text);
227
$text = preg_replace("#\\*[^<>\n]+\\*#", '<b>\\0</b>', $text);
218
228
// make strings inside $ italic
219
$text = ereg_replace("\\$[^<>\n]+\\$", '<b><i>\\0</i></b>', $text);
229
$text = preg_replace("#\\$[^<>\n]+\\$#", '<b><i>\\0</i></b>', $text);
222
232
/* Temporary fix for bad links in help messages. As a special case,
259
* Call this method to initialize output data. See self::execute()
266
* Call this method to initialize output data. See execute()
267
* @param $result ApiResult
268
* @param $feed object an instance of one of the $wgFeedClasses classes
269
* @param $feedItems array of FeedItem objects
261
271
public static function setResult($result, $feed, $feedItems) {
262
272
// Store output in the Result data.
263
273
// This way we can check during execution if any error has occured
264
$data = & $result->getData();
265
$data['_feed'] = $feed;
266
$data['_feeditems'] = $feedItems;
274
// Disable size checking for this because we can't continue
275
// cleanly; size checking would cause more problems than it'd
277
$result->disableSizeCheck();
278
$result->addValue(null, '_feed', $feed);
279
$result->addValue(null, '_feeditems', $feedItems);
280
$result->enableSizeCheck();
284
298
* This class expects the result data to be in a custom format set by self::setResult()
285
* $result['_feed'] - an instance of one of the $wgFeedClasses classes
286
* $result['_feeditems'] - an array of FeedItem instances
299
* $result['_feed'] - an instance of one of the $wgFeedClasses classes
300
* $result['_feeditems'] - an array of FeedItem instances
288
302
public function execute() {
289
303
$data = $this->getResultData();
304
318
public function getVersion() {
305
return __CLASS__ . ': $Id: ApiFormatBase.php 43470 2008-11-14 00:30:34Z tstarling $';
319
return __CLASS__ . ': $Id: ApiFormatBase.php 48521 2009-03-18 19:25:29Z ialex $';
b'\\ No newline at end of file'