MediaWiki
REL1_21
|
00001 <?php 00029 abstract class MediaTransformOutput { 00033 var $file; 00034 00035 var $width, $height, $url, $page, $path; 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(); 00155 } else { 00156 return $this->path; // may return false 00157 } 00158 } 00159 00166 public function streamFile( $headers = array() ) { 00167 if ( !$this->path ) { 00168 return false; 00169 } elseif ( FileBackend::isStoragePath( $this->path ) ) { 00170 $be = $this->file->getRepo()->getBackend(); 00171 return $be->streamFile( array( 'src' => $this->path, 'headers' => $headers ) )->isOK(); 00172 } else { // FS-file 00173 return StreamFile::stream( $this->getLocalCopyPath(), $headers ); 00174 } 00175 } 00176 00185 protected function linkWrap( $linkAttribs, $contents ) { 00186 if ( $linkAttribs ) { 00187 return Xml::tags( 'a', $linkAttribs, $contents ); 00188 } else { 00189 return $contents; 00190 } 00191 } 00192 00198 public function getDescLinkAttribs( $title = null, $params = '' ) { 00199 $query = ''; 00200 if ( $this->page && $this->page !== 1 ) { 00201 $query = 'page=' . urlencode( $this->page ); 00202 } 00203 if( $params ) { 00204 $query .= $query ? '&' . $params : $params; 00205 } 00206 $attribs = array( 00207 'href' => $this->file->getTitle()->getLocalURL( $query ), 00208 'class' => 'image', 00209 ); 00210 if ( $title ) { 00211 $attribs['title'] = $title; 00212 } 00213 return $attribs; 00214 } 00215 } 00216 00222 class ThumbnailImage extends MediaTransformOutput { 00236 function __construct( $file, $url, $path = false, $parameters = array() ) { 00237 # Previous parameters: 00238 # $file, $url, $width, $height, $path = false, $page = false 00239 00240 if( is_array( $parameters ) ) { 00241 $defaults = array( 00242 'page' => false 00243 ); 00244 $actualParams = $parameters + $defaults; 00245 } else { 00246 # Using old format, should convert. Later a warning could be added here. 00247 $numArgs = func_num_args(); 00248 $actualParams = array( 00249 'width' => $path, 00250 'height' => $parameters, 00251 'page' => ( $numArgs > 5 ) ? func_get_arg( 5 ) : false 00252 ); 00253 $path = ( $numArgs > 4 ) ? func_get_arg( 4 ) : false; 00254 } 00255 00256 $this->file = $file; 00257 $this->url = $url; 00258 $this->path = $path; 00259 00260 # These should be integers when they get here. 00261 # If not, there's a bug somewhere. But let's at 00262 # least produce valid HTML code regardless. 00263 $this->width = round( $actualParams['width'] ); 00264 $this->height = round( $actualParams['height'] ); 00265 00266 $this->page = $actualParams['page']; 00267 } 00268 00297 function toHtml( $options = array() ) { 00298 if ( count( func_get_args() ) == 2 ) { 00299 throw new MWException( __METHOD__ .' called in the old style' ); 00300 } 00301 00302 $alt = empty( $options['alt'] ) ? '' : $options['alt']; 00303 00304 $query = empty( $options['desc-query'] ) ? '' : $options['desc-query']; 00305 00306 if ( !empty( $options['custom-url-link'] ) ) { 00307 $linkAttribs = array( 'href' => $options['custom-url-link'] ); 00308 if ( !empty( $options['title'] ) ) { 00309 $linkAttribs['title'] = $options['title']; 00310 } 00311 if ( !empty( $options['custom-target-link'] ) ) { 00312 $linkAttribs['target'] = $options['custom-target-link']; 00313 } elseif ( !empty( $options['parser-extlink-target'] ) ) { 00314 $linkAttribs['target'] = $options['parser-extlink-target']; 00315 } 00316 if ( !empty( $options['parser-extlink-rel'] ) ) { 00317 $linkAttribs['rel'] = $options['parser-extlink-rel']; 00318 } 00319 } elseif ( !empty( $options['custom-title-link'] ) ) { 00320 $title = $options['custom-title-link']; 00321 $linkAttribs = array( 00322 'href' => $title->getLinkURL(), 00323 'title' => empty( $options['title'] ) ? $title->getFullText() : $options['title'] 00324 ); 00325 } elseif ( !empty( $options['desc-link'] ) ) { 00326 $linkAttribs = $this->getDescLinkAttribs( empty( $options['title'] ) ? null : $options['title'], $query ); 00327 } elseif ( !empty( $options['file-link'] ) ) { 00328 $linkAttribs = array( 'href' => $this->file->getURL() ); 00329 } else { 00330 $linkAttribs = false; 00331 } 00332 00333 $attribs = array( 00334 'alt' => $alt, 00335 'src' => $this->url, 00336 'width' => $this->width, 00337 'height' => $this->height 00338 ); 00339 if ( !empty( $options['valign'] ) ) { 00340 $attribs['style'] = "vertical-align: {$options['valign']}"; 00341 } 00342 if ( !empty( $options['img-class'] ) ) { 00343 $attribs['class'] = $options['img-class']; 00344 } 00345 00346 // Additional densities for responsive images, if specified. 00347 if ( !empty( $this->responsiveUrls ) ) { 00348 $attribs['srcset'] = Html::srcSet( $this->responsiveUrls ); 00349 } 00350 00351 wfRunHooks( 'ThumbnailBeforeProduceHTML', array( $this, &$attribs, &$linkAttribs ) ); 00352 00353 return $this->linkWrap( $linkAttribs, Xml::element( 'img', $attribs ) ); 00354 } 00355 00356 } 00357 00363 class MediaTransformError extends MediaTransformOutput { 00364 var $htmlMsg, $textMsg, $width, $height, $url, $path; 00365 00366 function __construct( $msg, $width, $height /*, ... */ ) { 00367 $args = array_slice( func_get_args(), 3 ); 00368 $htmlArgs = array_map( 'htmlspecialchars', $args ); 00369 $htmlArgs = array_map( 'nl2br', $htmlArgs ); 00370 00371 $this->htmlMsg = wfMessage( $msg )->rawParams( $htmlArgs )->escaped(); 00372 $this->textMsg = wfMessage( $msg )->rawParams( $htmlArgs )->text(); 00373 $this->width = intval( $width ); 00374 $this->height = intval( $height ); 00375 $this->url = false; 00376 $this->path = false; 00377 } 00378 00379 function toHtml( $options = array() ) { 00380 return "<div class=\"MediaTransformError\" style=\"" . 00381 "width: {$this->width}px; height: {$this->height}px; display:inline-block;\">" . 00382 $this->htmlMsg . 00383 "</div>"; 00384 } 00385 00386 function toText() { 00387 return $this->textMsg; 00388 } 00389 00390 function getHtmlMsg() { 00391 return $this->htmlMsg; 00392 } 00393 00394 function isError() { 00395 return true; 00396 } 00397 } 00398 00404 class TransformParameterError extends MediaTransformError { 00405 function __construct( $params ) { 00406 parent::__construct( 'thumbnail_error', 00407 max( isset( $params['width'] ) ? $params['width'] : 0, 120 ), 00408 max( isset( $params['height'] ) ? $params['height'] : 0, 120 ), 00409 wfMessage( 'thumbnail_invalid_params' )->text() ); 00410 } 00411 }