MediaWiki  REL1_20
ApiFormatBase.php
Go to the documentation of this file.
00001 <?php
00032 abstract class ApiFormatBase extends ApiBase {
00033 
00034         private $mIsHtml, $mFormat, $mUnescapeAmps, $mHelp, $mCleared;
00035         private $mBufferResult = false, $mBuffer, $mDisabled = false;
00036 
00043         public function __construct( $main, $format ) {
00044                 parent::__construct( $main, $format );
00045 
00046                 $this->mIsHtml = ( substr( $format, - 2, 2 ) === 'fm' ); // ends with 'fm'
00047                 if ( $this->mIsHtml ) {
00048                         $this->mFormat = substr( $format, 0, - 2 ); // remove ending 'fm'
00049                 } else {
00050                         $this->mFormat = $format;
00051                 }
00052                 $this->mFormat = strtoupper( $this->mFormat );
00053                 $this->mCleared = false;
00054         }
00055 
00061         public abstract function getMimeType();
00062 
00067         public function getNeedsRawData() {
00068                 return false;
00069         }
00070 
00075         public function getFormat() {
00076                 return $this->mFormat;
00077         }
00078 
00088         public function setUnescapeAmps ( $b ) {
00089                 $this->mUnescapeAmps = $b;
00090         }
00091 
00098         public function getIsHtml() {
00099                 return $this->mIsHtml;
00100         }
00101 
00108         public function getWantsHelp() {
00109                 return $this->getIsHtml();
00110         }
00111 
00116         public function disable() {
00117                 $this->mDisabled = true;
00118         }
00119 
00120         public function isDisabled() {
00121                 return $this->mDisabled;
00122         }
00123 
00130         function initPrinter( $isError ) {
00131                 if ( $this->mDisabled ) {
00132                         return;
00133                 }
00134                 $isHtml = $this->getIsHtml();
00135                 $mime = $isHtml ? 'text/html' : $this->getMimeType();
00136                 $script = wfScript( 'api' );
00137 
00138                 // Some printers (ex. Feed) do their own header settings,
00139                 // in which case $mime will be set to null
00140                 if ( is_null( $mime ) ) {
00141                         return; // skip any initialization
00142                 }
00143 
00144                 $this->getMain()->getRequest()->response()->header( "Content-Type: $mime; charset=utf-8" );
00145 
00146                 //Set X-Frame-Options API results (bug 39180)
00147                 global $wgApiFrameOptions;
00148                 if ( $wgApiFrameOptions ) {
00149                         $this->getMain()->getRequest()->response()->header( "X-Frame-Options: $wgApiFrameOptions" );
00150                 }
00151 
00152                 if ( $isHtml ) {
00153 ?>
00154 <!DOCTYPE HTML>
00155 <html>
00156 <head>
00157 <?php if ( $this->mUnescapeAmps ) {
00158 ?>      <title>MediaWiki API</title>
00159 <?php } else {
00160 ?>      <title>MediaWiki API Result</title>
00161 <?php } ?>
00162 </head>
00163 <body>
00164 <?php
00165 
00166 
00167                         if ( !$isError ) {
00168 ?>
00169 <br />
00170 <small>
00171 You are looking at the HTML representation of the <?php echo( $this->mFormat ); ?> format.<br />
00172 HTML is good for debugging, but is unsuitable for application use.<br />
00173 Specify the format parameter to change the output format.<br />
00174 To see the non HTML representation of the <?php echo( $this->mFormat ); ?> format, set format=<?php echo( strtolower( $this->mFormat ) ); ?>.<br />
00175 See the <a href='https://www.mediawiki.org/wiki/API'>complete documentation</a>, or
00176 <a href='<?php echo( $script ); ?>'>API help</a> for more information.
00177 </small>
00178 <?php
00179 
00180 
00181                         }
00182 ?>
00183 <pre>
00184 <?php
00185 
00186 
00187                 }
00188         }
00189 
00193         public function closePrinter() {
00194                 if ( $this->mDisabled ) {
00195                         return;
00196                 }
00197                 if ( $this->getIsHtml() ) {
00198 ?>
00199 
00200 </pre>
00201 </body>
00202 </html>
00203 <?php
00204 
00205 
00206                 }
00207         }
00208 
00215         public function printText( $text ) {
00216                 if ( $this->mDisabled ) {
00217                         return;
00218                 }
00219                 if ( $this->mBufferResult ) {
00220                         $this->mBuffer = $text;
00221                 } elseif ( $this->getIsHtml() ) {
00222                         echo $this->formatHTML( $text );
00223                 } else {
00224                         // For non-HTML output, clear all errors that might have been
00225                         // displayed if display_errors=On
00226                         // Do this only once, of course
00227                         if ( !$this->mCleared ) {
00228                                 ob_clean();
00229                                 $this->mCleared = true;
00230                         }
00231                         echo $text;
00232                 }
00233         }
00234 
00238         public function getBuffer() {
00239                 return $this->mBuffer;
00240         }
00241 
00246         public function setBufferResult( $value ) {
00247                 $this->mBufferResult = $value;
00248         }
00249 
00254         public function setHelp( $help = true ) {
00255                 $this->mHelp = $help;
00256         }
00257 
00264         protected function formatHTML( $text ) {
00265                 // Escape everything first for full coverage
00266                 $text = htmlspecialchars( $text );
00267 
00268                 // encode all comments or tags as safe blue strings
00269                 $text = str_replace( '&lt;', '<span style="color:blue;">&lt;', $text );
00270                 $text = str_replace( '&gt;', '&gt;</span>', $text );
00271                 // identify URLs
00272                 $protos = wfUrlProtocolsWithoutProtRel();
00273                 // This regex hacks around bug 13218 (&quot; included in the URL)
00274                 $text = preg_replace( "#(((?i)$protos).*?)(&quot;)?([ \\'\"<>\n]|&lt;|&gt;|&quot;)#", '<a href="\\1">\\1</a>\\3\\4', $text );
00275                 // identify requests to api.php
00276                 $text = preg_replace( "#api\\.php\\?[^ <\n\t]+#", '<a href="\\0">\\0</a>', $text );
00277                 if ( $this->mHelp ) {
00278                         // make strings inside * bold
00279                         $text = preg_replace( "#\\*[^<>\n]+\\*#", '<b>\\0</b>', $text );
00280                         // make strings inside $ italic
00281                         $text = preg_replace( "#\\$[^<>\n]+\\$#", '<b><i>\\0</i></b>', $text );
00282                 }
00283 
00290                 if ( $this->mUnescapeAmps ) {
00291                         $text = preg_replace( '/&amp;(amp|quot|lt|gt);/', '&\1;', $text );
00292                 }
00293 
00294                 return $text;
00295         }
00296 
00297         public function getExamples() {
00298                 return array(
00299                         'api.php?action=query&meta=siteinfo&siprop=namespaces&format=' . $this->getModuleName()
00300                                 => "Format the query result in the {$this->getModuleName()} format",
00301                 );
00302         }
00303 
00304         public function getHelpUrls() {
00305                 return 'https://www.mediawiki.org/wiki/API:Data_formats';
00306         }
00307 
00308         public function getDescription() {
00309                 return $this->getIsHtml() ? ' (pretty-print in HTML)' : '';
00310         }
00311 
00312         public static function getBaseVersion() {
00313                 return __CLASS__ . ': $Id$';
00314         }
00315 }
00316 
00321 class ApiFormatFeedWrapper extends ApiFormatBase {
00322 
00323         public function __construct( $main ) {
00324                 parent::__construct( $main, 'feed' );
00325         }
00326 
00333         public static function setResult( $result, $feed, $feedItems ) {
00334                 // Store output in the Result data.
00335                 // This way we can check during execution if any error has occurred
00336                 // Disable size checking for this because we can't continue
00337                 // cleanly; size checking would cause more problems than it'd
00338                 // solve
00339                 $result->disableSizeCheck();
00340                 $result->addValue( null, '_feed', $feed );
00341                 $result->addValue( null, '_feeditems', $feedItems );
00342                 $result->enableSizeCheck();
00343         }
00344 
00350         public function getMimeType() {
00351                 return null;
00352         }
00353 
00359         public function getNeedsRawData() {
00360                 return true;
00361         }
00362 
00368         public function execute() {
00369                 $data = $this->getResultData();
00370                 if ( isset( $data['_feed'] ) && isset( $data['_feeditems'] ) ) {
00371                         $feed = $data['_feed'];
00372                         $items = $data['_feeditems'];
00373 
00374                         $feed->outHeader();
00375                         foreach ( $items as & $item ) {
00376                                 $feed->outItem( $item );
00377                         }
00378                         $feed->outFooter();
00379                 } else {
00380                         // Error has occurred, print something useful
00381                         ApiBase::dieDebug( __METHOD__, 'Invalid feed class/item' );
00382                 }
00383         }
00384 
00385         public function getVersion() {
00386                 return __CLASS__ . ': $Id$';
00387         }
00388 }