00001 <?php
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023
00024
00025
00026 if ( !defined( 'MEDIAWIKI' ) ) {
00027
00028 require_once ( 'ApiBase.php' );
00029 }
00030
00036 abstract class ApiFormatBase extends ApiBase {
00037
00038 private $mIsHtml, $mFormat, $mUnescapeAmps, $mHelp, $mCleared;
00039 private $mBufferResult = false, $mBuffer;
00040
00047 public function __construct( $main, $format ) {
00048 parent :: __construct( $main, $format );
00049
00050 $this->mIsHtml = ( substr( $format, - 2, 2 ) === 'fm' );
00051 if ( $this->mIsHtml )
00052 $this->mFormat = substr( $format, 0, - 2 );
00053 else
00054 $this->mFormat = $format;
00055 $this->mFormat = strtoupper( $this->mFormat );
00056 $this->mCleared = false;
00057 }
00058
00064 public abstract function getMimeType();
00065
00070 public function getNeedsRawData() {
00071 return false;
00072 }
00073
00078 public function getFormat() {
00079 return $this->mFormat;
00080 }
00081
00091 public function setUnescapeAmps ( $b ) {
00092 $this->mUnescapeAmps = $b;
00093 }
00094
00101 public function getIsHtml() {
00102 return $this->mIsHtml;
00103 }
00104
00111 public function getWantsHelp() {
00112 return $this->getIsHtml();
00113 }
00114
00121 function initPrinter( $isError ) {
00122 $isHtml = $this->getIsHtml();
00123 $mime = $isHtml ? 'text/html' : $this->getMimeType();
00124 $script = wfScript( 'api' );
00125
00126
00127
00128 if ( is_null( $mime ) )
00129 return;
00130
00131 header( "Content-Type: $mime; charset=utf-8" );
00132
00133 if ( $isHtml ) {
00134 ?>
00135 <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
00136 <html>
00137 <head>
00138 <?php if ( $this->mUnescapeAmps ) {
00139 ?> <title>MediaWiki API</title>
00140 <?php } else {
00141 ?> <title>MediaWiki API Result</title>
00142 <?php } ?>
00143 </head>
00144 <body>
00145 <?php
00146
00147
00148 if ( !$isError ) {
00149 ?>
00150 <br />
00151 <small>
00152 You are looking at the HTML representation of the <?php echo( $this->mFormat ); ?> format.<br />
00153 HTML is good for debugging, but probably is not suitable for your application.<br />
00154 See <a href='http://www.mediawiki.org/wiki/API'>complete documentation</a>, or
00155 <a href='<?php echo( $script ); ?>'>API help</a> for more information.
00156 </small>
00157 <?php
00158
00159
00160 }
00161 ?>
00162 <pre>
00163 <?php
00164
00165
00166 }
00167 }
00168
00172 public function closePrinter() {
00173 if ( $this->getIsHtml() ) {
00174 ?>
00175
00176 </pre>
00177 </body>
00178 </html>
00179 <?php
00180
00181
00182 }
00183 }
00184
00191 public function printText( $text ) {
00192 if ( $this->mBufferResult ) {
00193 $this->mBuffer = $text;
00194 } elseif ( $this->getIsHtml() ) {
00195 echo $this->formatHTML( $text );
00196 } else {
00197
00198
00199
00200 if ( !$this->mCleared )
00201 {
00202 ob_clean();
00203 $this->mCleared = true;
00204 }
00205 echo $text;
00206 }
00207 }
00208
00212 public function getBuffer() {
00213 return $this->mBuffer;
00214 }
00218 public function setBufferResult( $value ) {
00219 $this->mBufferResult = $value;
00220 }
00221
00226 public function setHelp( $help = true ) {
00227 $this->mHelp = true;
00228 }
00229
00236 protected function formatHTML( $text ) {
00237 global $wgUrlProtocols;
00238
00239
00240 $text = htmlspecialchars( $text );
00241
00242
00243 $text = preg_replace( '/\<(!--.*?--|.*?)\>/', '<span style="color:blue;"><\1></span>', $text );
00244
00245 $protos = implode( "|", $wgUrlProtocols );
00246
00247 $text = preg_replace( "#(($protos).*?)(")?([ \\'\"<>\n]|<|>|")#", '<a href="\\1">\\1</a>\\3\\4', $text );
00248
00249 $text = preg_replace( "#api\\.php\\?[^ \\()<\n\t]+#", '<a href="\\0">\\0</a>', $text );
00250 if ( $this->mHelp ) {
00251
00252 $text = preg_replace( "#\\*[^<>\n]+\\*#", '<b>\\0</b>', $text );
00253
00254 $text = preg_replace( "#\\$[^<>\n]+\\$#", '<b><i>\\0</i></b>', $text );
00255 }
00256
00257
00258
00259
00260
00261 if ( $this->mUnescapeAmps )
00262 $text = preg_replace( '/&(amp|quot|lt|gt);/', '&\1;', $text );
00263
00264 return $text;
00265 }
00266
00267 protected function getExamples() {
00268 return 'api.php?action=query&meta=siteinfo&siprop=namespaces&format=' . $this->getModuleName();
00269 }
00270
00271 public function getDescription() {
00272 return $this->getIsHtml() ? ' (pretty-print in HTML)' : '';
00273 }
00274
00275 public static function getBaseVersion() {
00276 return __CLASS__ . ': $Id: ApiFormatBase.php 62367 2010-02-12 14:09:42Z siebrand $';
00277 }
00278 }
00279
00284 class ApiFormatFeedWrapper extends ApiFormatBase {
00285
00286 public function __construct( $main ) {
00287 parent :: __construct( $main, 'feed' );
00288 }
00289
00296 public static function setResult( $result, $feed, $feedItems ) {
00297
00298
00299
00300
00301
00302 $result->disableSizeCheck();
00303 $result->addValue( null, '_feed', $feed );
00304 $result->addValue( null, '_feeditems', $feedItems );
00305 $result->enableSizeCheck();
00306 }
00307
00311 public function getMimeType() {
00312 return null;
00313 }
00314
00318 public function getNeedsRawData() {
00319 return true;
00320 }
00321
00327 public function execute() {
00328 $data = $this->getResultData();
00329 if ( isset ( $data['_feed'] ) && isset ( $data['_feeditems'] ) ) {
00330 $feed = $data['_feed'];
00331 $items = $data['_feeditems'];
00332
00333 $feed->outHeader();
00334 foreach ( $items as & $item )
00335 $feed->outItem( $item );
00336 $feed->outFooter();
00337 } else {
00338
00339 ApiBase::dieDebug( __METHOD__, 'Invalid feed class/item' );
00340 }
00341 }
00342
00343 public function getVersion() {
00344 return __CLASS__ . ': $Id: ApiFormatBase.php 62367 2010-02-12 14:09:42Z siebrand $';
00345 }
00346 }