MediaWiki  REL1_20
MediaTransformOutput.php
Go to the documentation of this file.
00001 <?php
00029 abstract class MediaTransformOutput {
00033         var $file;
00034 
00035         var $width, $height, $url, $page, $path;
00036         protected $storagePath = false;
00037 
00041         public function getWidth() {
00042                 return $this->width;
00043         }
00044 
00048         public function getHeight() {
00049                 return $this->height;
00050         }
00051 
00057         public function getExtension() {
00058                 return $this->path ? FileBackend::extensionFromPath( $this->path ) : false;
00059         }
00060 
00064         public function getUrl() {
00065                 return $this->url;
00066         }
00067 
00071         public function getStoragePath() {
00072                 return $this->storagePath;
00073         }
00074 
00079         public function setStoragePath( $storagePath ) {
00080                 $this->storagePath = $storagePath;
00081         }
00082 
00103         abstract public function toHtml( $options = array() );
00104 
00109         public function isError() {
00110                 return false;
00111         }
00112 
00121         public function hasFile() {
00122                 // If TRANSFORM_LATER, $this->path will be false.
00123                 // Note: a null path means "use the source file".
00124                 return ( !$this->isError() && ( $this->path || $this->path === null ) );
00125         }
00126 
00133         public function fileIsSource() {
00134                 return ( !$this->isError() && $this->path === null );
00135         }
00136 
00143         public function getLocalCopyPath() {
00144                 if ( $this->isError() ) {
00145                         return false;
00146                 } elseif ( $this->path === null ) {
00147                         return $this->file->getLocalRefPath();
00148                 } else {
00149                         return $this->path; // may return false
00150                 }
00151         }
00152 
00159         public function streamFile( $headers = array() ) {
00160                 if ( !$this->path ) {
00161                         return false;
00162                 } elseif ( FileBackend::isStoragePath( $this->path ) ) {
00163                         $be = $this->file->getRepo()->getBackend();
00164                         return $be->streamFile( array( 'src' => $this->path, 'headers' => $headers ) )->isOK();
00165                 } else { // FS-file
00166                         return StreamFile::stream( $this->getLocalCopyPath(), $headers );
00167                 }
00168         }
00169 
00178         protected function linkWrap( $linkAttribs, $contents ) {
00179                 if ( $linkAttribs ) {
00180                         return Xml::tags( 'a', $linkAttribs, $contents );
00181                 } else {
00182                         return $contents;
00183                 }
00184         }
00185 
00191         public function getDescLinkAttribs( $title = null, $params = '' ) {
00192                 $query = $this->page ? ( 'page=' . urlencode( $this->page ) ) : '';
00193                 if( $params ) {
00194                         $query .= $query ? '&'.$params : $params;
00195                 }
00196                 $attribs = array(
00197                         'href' => $this->file->getTitle()->getLocalURL( $query ),
00198                         'class' => 'image',
00199                 );
00200                 if ( $title ) {
00201                         $attribs['title'] = $title;
00202                 }
00203                 return $attribs;
00204         }
00205 }
00206 
00212 class ThumbnailImage extends MediaTransformOutput {
00226         function __construct( $file, $url, $path = false, $parameters = array() ) {
00227                 # Previous parameters:
00228                 #   $file, $url, $width, $height, $path = false, $page = false
00229 
00230                 if( is_array( $parameters ) ){
00231                         $defaults = array(
00232                                 'page' => false
00233                         );
00234                         $actualParams = $parameters + $defaults;
00235                 } else {
00236                         # Using old format, should convert. Later a warning could be added here.
00237                         $numArgs = func_num_args();
00238                         $actualParams = array(
00239                                 'width' => $path,
00240                                 'height' => $parameters,
00241                                 'page' => ( $numArgs > 5 ) ? func_get_arg( 5 ) : false
00242                         );
00243                         $path = ( $numArgs > 4 ) ? func_get_arg( 4 ) : false;
00244                 }
00245 
00246                 $this->file = $file;
00247                 $this->url = $url;
00248                 $this->path = $path;
00249 
00250                 # These should be integers when they get here.
00251                 # If not, there's a bug somewhere.  But let's at
00252                 # least produce valid HTML code regardless.
00253                 $this->width = round( $actualParams['width'] );
00254                 $this->height = round( $actualParams['height'] );
00255 
00256                 $this->page = $actualParams['page'];
00257         }
00258 
00286         function toHtml( $options = array() ) {
00287                 if ( count( func_get_args() ) == 2 ) {
00288                         throw new MWException( __METHOD__ .' called in the old style' );
00289                 }
00290 
00291                 $alt = empty( $options['alt'] ) ? '' : $options['alt'];
00292 
00293                 $query = empty( $options['desc-query'] )  ? '' : $options['desc-query'];
00294 
00295                 if ( !empty( $options['custom-url-link'] ) ) {
00296                         $linkAttribs = array( 'href' => $options['custom-url-link'] );
00297                         if ( !empty( $options['title'] ) ) {
00298                                 $linkAttribs['title'] = $options['title'];
00299                         }
00300                         if ( !empty( $options['custom-target-link'] ) ) {
00301                                 $linkAttribs['target'] = $options['custom-target-link'];
00302                         } elseif ( !empty( $options['parser-extlink-target'] ) ) {
00303                                 $linkAttribs['target'] = $options['parser-extlink-target'];
00304                         }
00305                         if ( !empty( $options['parser-extlink-rel'] ) ) {
00306                                 $linkAttribs['rel'] = $options['parser-extlink-rel'];
00307                         }
00308                 } elseif ( !empty( $options['custom-title-link'] ) ) {
00309                         $title = $options['custom-title-link'];
00310                         $linkAttribs = array(
00311                                 'href' => $title->getLinkURL(),
00312                                 'title' => empty( $options['title'] ) ? $title->getFullText() : $options['title']
00313                         );
00314                 } elseif ( !empty( $options['desc-link'] ) ) {
00315                         $linkAttribs = $this->getDescLinkAttribs( empty( $options['title'] ) ? null : $options['title'], $query );
00316                 } elseif ( !empty( $options['file-link'] ) ) {
00317                         $linkAttribs = array( 'href' => $this->file->getURL() );
00318                 } else {
00319                         $linkAttribs = false;
00320                 }
00321 
00322                 $attribs = array(
00323                         'alt' => $alt,
00324                         'src' => $this->url,
00325                         'width' => $this->width,
00326                         'height' => $this->height,
00327                 );
00328                 if ( !empty( $options['valign'] ) ) {
00329                         $attribs['style'] = "vertical-align: {$options['valign']}";
00330                 }
00331                 if ( !empty( $options['img-class'] ) ) {
00332                         $attribs['class'] = $options['img-class'];
00333                 }
00334                 return $this->linkWrap( $linkAttribs, Xml::element( 'img', $attribs ) );
00335         }
00336 
00337 }
00338 
00344 class MediaTransformError extends MediaTransformOutput {
00345         var $htmlMsg, $textMsg, $width, $height, $url, $path;
00346 
00347         function __construct( $msg, $width, $height /*, ... */ ) {
00348                 $args = array_slice( func_get_args(), 3 );
00349                 $htmlArgs = array_map( 'htmlspecialchars', $args );
00350                 $htmlArgs = array_map( 'nl2br', $htmlArgs );
00351 
00352                 $this->htmlMsg = wfMessage( $msg )->rawParams( $htmlArgs )->escaped();
00353                 $this->textMsg = wfMessage( $msg )->rawParams( $htmlArgs )->text();
00354                 $this->width = intval( $width );
00355                 $this->height = intval( $height );
00356                 $this->url = false;
00357                 $this->path = false;
00358         }
00359 
00360         function toHtml( $options = array() ) {
00361                 return "<div class=\"MediaTransformError\" style=\"" .
00362                         "width: {$this->width}px; height: {$this->height}px; display:inline-block;\">" .
00363                         $this->htmlMsg .
00364                         "</div>";
00365         }
00366 
00367         function toText() {
00368                 return $this->textMsg;
00369         }
00370 
00371         function getHtmlMsg() {
00372                 return $this->htmlMsg;
00373         }
00374 
00375         function isError() {
00376                 return true;
00377         }
00378 }
00379 
00385 class TransformParameterError extends MediaTransformError {
00386         function __construct( $params ) {
00387                 parent::__construct( 'thumbnail_error',
00388                         max( isset( $params['width']  ) ? $params['width']  : 0, 120 ),
00389                         max( isset( $params['height'] ) ? $params['height'] : 0, 120 ),
00390                         wfMessage( 'thumbnail_invalid_params' )->text() );
00391         }
00392 }