MediaWiki  REL1_22
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, $lang;
00036 
00041     public $responsiveUrls = array();
00042 
00043     protected $storagePath = false;
00044 
00048     public function getWidth() {
00049         return $this->width;
00050     }
00051 
00055     public function getHeight() {
00056         return $this->height;
00057     }
00058 
00064     public function getExtension() {
00065         return $this->path ? FileBackend::extensionFromPath( $this->path ) : false;
00066     }
00067 
00071     public function getUrl() {
00072         return $this->url;
00073     }
00074 
00078     public function getStoragePath() {
00079         return $this->storagePath;
00080     }
00081 
00086     public function setStoragePath( $storagePath ) {
00087         $this->storagePath = $storagePath;
00088     }
00089 
00110     abstract public function toHtml( $options = array() );
00111 
00116     public function isError() {
00117         return false;
00118     }
00119 
00128     public function hasFile() {
00129         // If TRANSFORM_LATER, $this->path will be false.
00130         // Note: a null path means "use the source file".
00131         return ( !$this->isError() && ( $this->path || $this->path === null ) );
00132     }
00133 
00140     public function fileIsSource() {
00141         return ( !$this->isError() && $this->path === null );
00142     }
00143 
00150     public function getLocalCopyPath() {
00151         if ( $this->isError() ) {
00152             return false;
00153         } elseif ( $this->path === null ) {
00154             return $this->file->getLocalRefPath(); // assume thumb was not scaled
00155         } elseif ( FileBackend::isStoragePath( $this->path ) ) {
00156             $be = $this->file->getRepo()->getBackend();
00157             // The temp file will be process cached by FileBackend
00158             $fsFile = $be->getLocalReference( array( 'src' => $this->path ) );
00159             return $fsFile ? $fsFile->getPath() : false;
00160         } else {
00161             return $this->path; // may return false
00162         }
00163     }
00164 
00171     public function streamFile( $headers = array() ) {
00172         if ( !$this->path ) {
00173             return false;
00174         } elseif ( FileBackend::isStoragePath( $this->path ) ) {
00175             $be = $this->file->getRepo()->getBackend();
00176             return $be->streamFile( array( 'src' => $this->path, 'headers' => $headers ) )->isOK();
00177         } else { // FS-file
00178             return StreamFile::stream( $this->getLocalCopyPath(), $headers );
00179         }
00180     }
00181 
00190     protected function linkWrap( $linkAttribs, $contents ) {
00191         if ( $linkAttribs ) {
00192             return Xml::tags( 'a', $linkAttribs, $contents );
00193         } else {
00194             return $contents;
00195         }
00196     }
00197 
00203     public function getDescLinkAttribs( $title = null, $params = array() ) {
00204         if ( is_array( $params ) ) {
00205             $query = $params;
00206         } else {
00207             $query = array();
00208         }
00209         if ( $this->page && $this->page !== 1 ) {
00210             $query['page'] = $this->page;
00211         }
00212         if ( $this->lang ) {
00213             $query['lang'] = $this->lang;
00214         }
00215 
00216         if ( is_string( $params ) && $params !== '' ) {
00217             $query = $params . '&' . wfArrayToCgi( $query );
00218         }
00219 
00220         $attribs = array(
00221             'href' => $this->file->getTitle()->getLocalURL( $query ),
00222             'class' => 'image',
00223         );
00224         if ( $title ) {
00225             $attribs['title'] = $title;
00226         }
00227         return $attribs;
00228     }
00229 }
00230 
00236 class ThumbnailImage extends MediaTransformOutput {
00250     function __construct( $file, $url, $path = false, $parameters = array() ) {
00251         # Previous parameters:
00252         #   $file, $url, $width, $height, $path = false, $page = false
00253 
00254         $defaults = array(
00255             'page' => false,
00256             'lang' => false
00257         );
00258 
00259         if ( is_array( $parameters ) ) {
00260             $actualParams = $parameters + $defaults;
00261         } else {
00262             # Using old format, should convert. Later a warning could be added here.
00263             $numArgs = func_num_args();
00264             $actualParams = array(
00265                 'width' => $path,
00266                 'height' => $parameters,
00267                 'page' => ( $numArgs > 5 ) ? func_get_arg( 5 ) : false
00268             ) + $defaults;
00269             $path = ( $numArgs > 4 ) ? func_get_arg( 4 ) : false;
00270         }
00271 
00272         $this->file = $file;
00273         $this->url = $url;
00274         $this->path = $path;
00275 
00276         # These should be integers when they get here.
00277         # If not, there's a bug somewhere.  But let's at
00278         # least produce valid HTML code regardless.
00279         $this->width = round( $actualParams['width'] );
00280         $this->height = round( $actualParams['height'] );
00281 
00282         $this->page = $actualParams['page'];
00283         $this->lang = $actualParams['lang'];
00284     }
00285 
00316     function toHtml( $options = array() ) {
00317         if ( count( func_get_args() ) == 2 ) {
00318             throw new MWException( __METHOD__ . ' called in the old style' );
00319         }
00320 
00321         $alt = empty( $options['alt'] ) ? '' : $options['alt'];
00322 
00323         $query = empty( $options['desc-query'] ) ? '' : $options['desc-query'];
00324 
00325         if ( !empty( $options['custom-url-link'] ) ) {
00326             $linkAttribs = array( 'href' => $options['custom-url-link'] );
00327             if ( !empty( $options['title'] ) ) {
00328                 $linkAttribs['title'] = $options['title'];
00329             }
00330             if ( !empty( $options['custom-target-link'] ) ) {
00331                 $linkAttribs['target'] = $options['custom-target-link'];
00332             } elseif ( !empty( $options['parser-extlink-target'] ) ) {
00333                 $linkAttribs['target'] = $options['parser-extlink-target'];
00334             }
00335             if ( !empty( $options['parser-extlink-rel'] ) ) {
00336                 $linkAttribs['rel'] = $options['parser-extlink-rel'];
00337             }
00338         } elseif ( !empty( $options['custom-title-link'] ) ) {
00339             $title = $options['custom-title-link'];
00340             $linkAttribs = array(
00341                 'href' => $title->getLinkURL(),
00342                 'title' => empty( $options['title'] ) ? $title->getFullText() : $options['title']
00343             );
00344         } elseif ( !empty( $options['desc-link'] ) ) {
00345             $linkAttribs = $this->getDescLinkAttribs( empty( $options['title'] ) ? null : $options['title'], $query );
00346         } elseif ( !empty( $options['file-link'] ) ) {
00347             $linkAttribs = array( 'href' => $this->file->getURL() );
00348         } else {
00349             $linkAttribs = false;
00350         }
00351 
00352         $attribs = array(
00353             'alt' => $alt,
00354             'src' => $this->url,
00355             'width' => $this->width,
00356             'height' => $this->height
00357         );
00358         if ( !empty( $options['valign'] ) ) {
00359             $attribs['style'] = "vertical-align: {$options['valign']}";
00360         }
00361         if ( !empty( $options['img-class'] ) ) {
00362             $attribs['class'] = $options['img-class'];
00363         }
00364         if ( isset( $options['override-height'] ) ) {
00365             $attribs['height'] = $options['override-height'];
00366         }
00367         if ( isset( $options['override-width'] ) ) {
00368             $attribs['width'] = $options['override-width'];
00369         }
00370 
00371         // Additional densities for responsive images, if specified.
00372         if ( !empty( $this->responsiveUrls ) ) {
00373             $attribs['srcset'] = Html::srcSet( $this->responsiveUrls );
00374         }
00375 
00376         wfRunHooks( 'ThumbnailBeforeProduceHTML', array( $this, &$attribs, &$linkAttribs ) );
00377 
00378         return $this->linkWrap( $linkAttribs, Xml::element( 'img', $attribs ) );
00379     }
00380 
00381 }
00382 
00388 class MediaTransformError extends MediaTransformOutput {
00389     var $htmlMsg, $textMsg, $width, $height, $url, $path;
00390 
00391     function __construct( $msg, $width, $height /*, ... */ ) {
00392         $args = array_slice( func_get_args(), 3 );
00393         $htmlArgs = array_map( 'htmlspecialchars', $args );
00394         $htmlArgs = array_map( 'nl2br', $htmlArgs );
00395 
00396         $this->htmlMsg = wfMessage( $msg )->rawParams( $htmlArgs )->escaped();
00397         $this->textMsg = wfMessage( $msg )->rawParams( $htmlArgs )->text();
00398         $this->width = intval( $width );
00399         $this->height = intval( $height );
00400         $this->url = false;
00401         $this->path = false;
00402     }
00403 
00404     function toHtml( $options = array() ) {
00405         return "<div class=\"MediaTransformError\" style=\"" .
00406             "width: {$this->width}px; height: {$this->height}px; display:inline-block;\">" .
00407             $this->htmlMsg .
00408             "</div>";
00409     }
00410 
00411     function toText() {
00412         return $this->textMsg;
00413     }
00414 
00415     function getHtmlMsg() {
00416         return $this->htmlMsg;
00417     }
00418 
00419     function isError() {
00420         return true;
00421     }
00422 }
00423 
00429 class TransformParameterError extends MediaTransformError {
00430     function __construct( $params ) {
00431         parent::__construct( 'thumbnail_error',
00432             max( isset( $params['width'] ) ? $params['width'] : 0, 120 ),
00433             max( isset( $params['height'] ) ? $params['height'] : 0, 120 ),
00434             wfMessage( 'thumbnail_invalid_params' )->text() );
00435     }
00436 }