5 * Created on Sep 19, 2006
7 * Copyright © 2006 Yuri Astrakhan <Firstname><Lastname>@gmail.com
9 * This program is free software; you can redistribute it and/or modify
10 * it under the terms of the GNU General Public License as published by
11 * the Free Software Foundation; either version 2 of the License, or
12 * (at your option) any later version.
14 * This program is distributed in the hope that it will be useful,
15 * but WITHOUT ANY WARRANTY; without even the implied warranty of
16 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17 * GNU General Public License for more details.
19 * You should have received a copy of the GNU General Public License along
20 * with this program; if not, write to the Free Software Foundation, Inc.,
21 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
22 * http://www.gnu.org/copyleft/gpl.html
27 if ( !defined( 'MEDIAWIKI' ) ) {
28 // Eclipse helper - will be ignored in production
29 require_once( 'ApiBase.php' );
33 * This is the abstract base class for API formatters.
37 abstract class ApiFormatBase
extends ApiBase
{
39 private $mIsHtml, $mFormat, $mUnescapeAmps, $mHelp, $mCleared;
40 private $mBufferResult = false, $mBuffer, $mDisabled = false;
44 * If $format ends with 'fm', pretty-print the output in HTML.
45 * @param $main ApiMain
46 * @param $format string Format name
48 public function __construct( $main, $format ) {
49 parent
::__construct( $main, $format );
51 $this->mIsHtml
= ( substr( $format, - 2, 2 ) === 'fm' ); // ends with 'fm'
52 if ( $this->mIsHtml
) {
53 $this->mFormat
= substr( $format, 0, - 2 ); // remove ending 'fm'
55 $this->mFormat
= $format;
57 $this->mFormat
= strtoupper( $this->mFormat
);
58 $this->mCleared
= false;
62 * Overriding class returns the mime type that should be sent to the client.
63 * This method is not called if getIsHtml() returns true.
66 public abstract function getMimeType();
69 * Whether this formatter needs raw data such as _element tags
72 public function getNeedsRawData() {
77 * Get the internal format name
80 public function getFormat() {
81 return $this->mFormat
;
85 * Specify whether or not sequences like &quot; should be unescaped
86 * to " . This should only be set to true for the help message
87 * when rendered in the default (xmlfm) format. This is a temporary
88 * special-case fix that should be removed once the help has been
89 * reworked to use a fully HTML interface.
91 * @param $b bool Whether or not ampersands should be escaped.
93 public function setUnescapeAmps ( $b ) {
94 $this->mUnescapeAmps
= $b;
98 * Returns true when the HTML pretty-printer should be used.
99 * The default implementation assumes that formats ending with 'fm'
100 * should be formatted in HTML.
103 public function getIsHtml() {
104 return $this->mIsHtml
;
108 * Whether this formatter can format the help message in a nice way.
109 * By default, this returns the same as getIsHtml().
110 * When action=help is set explicitly, the help will always be shown
113 public function getWantsHelp() {
114 return $this->getIsHtml();
118 * Disable the formatter completely. This causes calls to initPrinter(),
119 * printText() and closePrinter() to be ignored.
121 public function disable() {
122 $this->mDisabled
= true;
125 public function isDisabled() {
126 return $this->mDisabled
;
130 * Initialize the printer function and prepare the output headers, etc.
131 * This method must be the first outputing method during execution.
132 * A help screen's header is printed for the HTML-based output
133 * @param $isError bool Whether an error message is printed
135 function initPrinter( $isError ) {
136 if ( $this->mDisabled
) {
139 $isHtml = $this->getIsHtml();
140 $mime = $isHtml ?
'text/html' : $this->getMimeType();
141 $script = wfScript( 'api' );
143 // Some printers (ex. Feed) do their own header settings,
144 // in which case $mime will be set to null
145 if ( is_null( $mime ) ) {
146 return; // skip any initialization
149 if( !$this->getMain()->isInternalMode() ) {
150 header( "Content-Type: $mime; charset=utf-8" );
158 <?php
if ( $this->mUnescapeAmps
) {
159 ?
> <title
>MediaWiki API
</title
>
161 ?
> <title
>MediaWiki API Result
</title
>
172 You are looking at the HTML representation of the
<?php
echo( $this->mFormat
); ?
> format
.<br
/>
173 HTML is good
for debugging
, but probably is not suitable
for your application
.<br
/>
174 See
<a href
='http://www.mediawiki.org/wiki/API'>complete documentation
</a
>, or
175 <a href
='<?php echo( $script ); ?>'>API help
</a
> for more information
.
190 * Finish printing. Closes HTML tags.
192 public function closePrinter() {
193 if ( $this->mDisabled
) {
196 if ( $this->getIsHtml() ) {
209 * The main format printing function. Call it to output the result
210 * string to the user. This function will automatically output HTML
211 * when format name ends in 'fm'.
212 * @param $text string
214 public function printText( $text ) {
215 if ( $this->mDisabled
) {
218 if ( $this->mBufferResult
) {
219 $this->mBuffer
= $text;
220 } elseif ( $this->getIsHtml() ) {
221 echo $this->formatHTML( $text );
223 // For non-HTML output, clear all errors that might have been
224 // displayed if display_errors=On
225 // Do this only once, of course
226 if ( !$this->mCleared
) {
228 $this->mCleared
= true;
235 * Get the contents of the buffer.
237 public function getBuffer() {
238 return $this->mBuffer
;
241 * Set the flag to buffer the result instead of printing it.
243 public function setBufferResult( $value ) {
244 $this->mBufferResult
= $value;
248 * Sets whether the pretty-printer should format *bold* and $italics$
251 public function setHelp( $help = true ) {
252 $this->mHelp
= $help;
256 * Pretty-print various elements in HTML format, such as xml tags and
257 * URLs. This method also escapes characters like <
258 * @param $text string
261 protected function formatHTML( $text ) {
262 global $wgUrlProtocols;
264 // Escape everything first for full coverage
265 $text = htmlspecialchars( $text );
267 // encode all comments or tags as safe blue strings
268 $text = preg_replace( '/\<(!--.*?--|.*?)\>/', '<span style="color:blue;"><\1></span>', $text );
270 $protos = implode( "|", $wgUrlProtocols );
271 // This regex hacks around bug 13218 (" included in the URL)
272 $text = preg_replace( "#(($protos).*?)(")?([ \\'\"<>\n]|<|>|")#", '<a href="\\1">\\1</a>\\3\\4', $text );
273 // identify requests to api.php
274 $text = preg_replace( "#api\\.php\\?[^ \\()<\n\t]+#", '<a href="\\0">\\0</a>', $text );
275 if ( $this->mHelp
) {
276 // make strings inside * bold
277 $text = preg_replace( "#\\*[^<>\n]+\\*#", '<b>\\0</b>', $text );
278 // make strings inside $ italic
279 $text = preg_replace( "#\\$[^<>\n]+\\$#", '<b><i>\\0</i></b>', $text );
283 * Temporary fix for bad links in help messages. As a special case,
284 * XML-escaped metachars are de-escaped one level in the help message
285 * for legibility. Should be removed once we have completed a fully-HTML
286 * version of the help message.
288 if ( $this->mUnescapeAmps
) {
289 $text = preg_replace( '/&(amp|quot|lt|gt);/', '&\1;', $text );
295 protected function getExamples() {
296 return 'api.php?action=query&meta=siteinfo&siprop=namespaces&format=' . $this->getModuleName();
299 public function getDescription() {
300 return $this->getIsHtml() ?
' (pretty-print in HTML)' : '';
303 public static function getBaseVersion() {
304 return __CLASS__
. ': $Id$';
309 * This printer is used to wrap an instance of the Feed class
312 class ApiFormatFeedWrapper
extends ApiFormatBase
{
314 public function __construct( $main ) {
315 parent
::__construct( $main, 'feed' );
319 * Call this method to initialize output data. See execute()
320 * @param $result ApiResult
321 * @param $feed object an instance of one of the $wgFeedClasses classes
322 * @param $feedItems array of FeedItem objects
324 public static function setResult( $result, $feed, $feedItems ) {
325 // Store output in the Result data.
326 // This way we can check during execution if any error has occured
327 // Disable size checking for this because we can't continue
328 // cleanly; size checking would cause more problems than it'd
330 $result->disableSizeCheck();
331 $result->addValue( null, '_feed', $feed );
332 $result->addValue( null, '_feeditems', $feedItems );
333 $result->enableSizeCheck();
337 * Feed does its own headers
339 public function getMimeType() {
344 * Optimization - no need to sanitize data that will not be needed
346 public function getNeedsRawData() {
351 * This class expects the result data to be in a custom format set by self::setResult()
352 * $result['_feed'] - an instance of one of the $wgFeedClasses classes
353 * $result['_feeditems'] - an array of FeedItem instances
355 public function execute() {
356 $data = $this->getResultData();
357 if ( isset( $data['_feed'] ) && isset( $data['_feeditems'] ) ) {
358 $feed = $data['_feed'];
359 $items = $data['_feeditems'];
362 foreach ( $items as & $item ) {
363 $feed->outItem( $item );
367 // Error has occured, print something useful
368 ApiBase
::dieDebug( __METHOD__
, 'Invalid feed class/item' );
372 public function getVersion() {
373 return __CLASS__
. ': $Id$';