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
00046 public function __construct($main, $format) {
00047 parent :: __construct($main, $format);
00048
00049 $this->mIsHtml = (substr($format, -2, 2) === 'fm');
00050 if ($this->mIsHtml)
00051 $this->mFormat = substr($format, 0, -2);
00052 else
00053 $this->mFormat = $format;
00054 $this->mFormat = strtoupper($this->mFormat);
00055 $this->mCleared = false;
00056 }
00057
00063 public abstract function getMimeType();
00064
00069 public function getNeedsRawData() {
00070 return false;
00071 }
00072
00077 public function getFormat() {
00078 return $this->mFormat;
00079 }
00080
00090 public function setUnescapeAmps ( $b ) {
00091 $this->mUnescapeAmps = $b;
00092 }
00093
00100 public function getIsHtml() {
00101 return $this->mIsHtml;
00102 }
00103
00110 function initPrinter($isError) {
00111 $isHtml = $this->getIsHtml();
00112 $mime = $isHtml ? 'text/html' : $this->getMimeType();
00113 $script = wfScript( 'api' );
00114
00115
00116
00117 if (is_null($mime))
00118 return;
00119
00120 header("Content-Type: $mime; charset=utf-8");
00121
00122 if ($isHtml) {
00123 ?>
00124 <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
00125 <html>
00126 <head>
00127 <?php if ($this->mUnescapeAmps) {
00128 ?> <title>MediaWiki API</title>
00129 <?php } else {
00130 ?> <title>MediaWiki API Result</title>
00131 <?php } ?>
00132 </head>
00133 <body>
00134 <?php
00135
00136
00137 if( !$isError ) {
00138 ?>
00139 <br/>
00140 <small>
00141 You are looking at the HTML representation of the <?php echo( $this->mFormat ); ?> format.<br/>
00142 HTML is good for debugging, but probably is not suitable for your application.<br/>
00143 See <a href='http://www.mediawiki.org/wiki/API'>complete documentation</a>, or
00144 <a href='<?php echo( $script ); ?>'>API help</a> for more information.
00145 </small>
00146 <?php
00147
00148
00149 }
00150 ?>
00151 <pre>
00152 <?php
00153
00154
00155 }
00156 }
00157
00161 public function closePrinter() {
00162 if ($this->getIsHtml()) {
00163 ?>
00164
00165 </pre>
00166 </body>
00167 </html>
00168 <?php
00169
00170
00171 }
00172 }
00173
00180 public function printText($text) {
00181 if ($this->getIsHtml())
00182 echo $this->formatHTML($text);
00183 else
00184 {
00185
00186
00187
00188 if(!$this->mCleared)
00189 {
00190 ob_clean();
00191 $this->mCleared = true;
00192 }
00193 echo $text;
00194 }
00195 }
00196
00201 public function setHelp( $help = true ) {
00202 $this->mHelp = true;
00203 }
00204
00211 protected function formatHTML($text) {
00212 global $wgUrlProtocols;
00213
00214
00215 $text = htmlspecialchars($text);
00216
00217
00218 $text = preg_replace('/\<(!--.*?--|.*?)\>/', '<span style="color:blue;"><\1></span>', $text);
00219
00220 $protos = implode("|", $wgUrlProtocols);
00221 # This regex hacks around bug 13218 (" included in the URL)
00222 $text = preg_replace("#(($protos).*?)(")?([ \\'\"<>\n]|<|>|")#", '<a href="\\1">\\1</a>\\3\\4', $text);
00223
00224 $text = preg_replace("#api\\.php\\?[^ \\()<\n\t]+#", '<a href="\\0">\\0</a>', $text);
00225 if( $this->mHelp ) {
00226
00227 $text = preg_replace("#\\*[^<>\n]+\\*#", '<b>\\0</b>', $text);
00228
00229 $text = preg_replace("#\\$[^<>\n]+\\$#", '<b><i>\\0</i></b>', $text);
00230 }
00231
00232
00233
00234
00235
00236 if ( $this->mUnescapeAmps )
00237 $text = preg_replace( '/&(amp|quot|lt|gt);/', '&\1;', $text );
00238
00239 return $text;
00240 }
00241
00242 protected function getExamples() {
00243 return 'api.php?action=query&meta=siteinfo&siprop=namespaces&format=' . $this->getModuleName();
00244 }
00245
00246 public function getDescription() {
00247 return $this->getIsHtml() ? ' (pretty-print in HTML)' : '';
00248 }
00249
00250 public static function getBaseVersion() {
00251 return __CLASS__ . ': $Id: ApiFormatBase.php 48521 2009-03-18 19:25:29Z ialex $';
00252 }
00253 }
00254
00259 class ApiFormatFeedWrapper extends ApiFormatBase {
00260
00261 public function __construct($main) {
00262 parent :: __construct($main, 'feed');
00263 }
00264
00271 public static function setResult($result, $feed, $feedItems) {
00272
00273
00274
00275
00276
00277 $result->disableSizeCheck();
00278 $result->addValue(null, '_feed', $feed);
00279 $result->addValue(null, '_feeditems', $feedItems);
00280 $result->enableSizeCheck();
00281 }
00282
00286 public function getMimeType() {
00287 return null;
00288 }
00289
00293 public function getNeedsRawData() {
00294 return true;
00295 }
00296
00302 public function execute() {
00303 $data = $this->getResultData();
00304 if (isset ($data['_feed']) && isset ($data['_feeditems'])) {
00305 $feed = $data['_feed'];
00306 $items = $data['_feeditems'];
00307
00308 $feed->outHeader();
00309 foreach ($items as & $item)
00310 $feed->outItem($item);
00311 $feed->outFooter();
00312 } else {
00313
00314 ApiBase::dieDebug( __METHOD__, 'Invalid feed class/item' );
00315 }
00316 }
00317
00318 public function getVersion() {
00319 return __CLASS__ . ': $Id: ApiFormatBase.php 48521 2009-03-18 19:25:29Z ialex $';
00320 }
00321 }