MediaWiki
REL1_19
|
00001 <?php 00014 abstract class MediaTransformOutput { 00018 var $file; 00019 00020 var $width, $height, $url, $page, $path; 00021 protected $storagePath = false; 00022 00026 public function getWidth() { 00027 return $this->width; 00028 } 00029 00033 public function getHeight() { 00034 return $this->height; 00035 } 00036 00040 public function getUrl() { 00041 return $this->url; 00042 } 00043 00047 public function getStoragePath() { 00048 return $this->storagePath; 00049 } 00050 00055 public function setStoragePath( $storagePath ) { 00056 $this->storagePath = $storagePath; 00057 } 00058 00079 abstract public function toHtml( $options = array() ); 00080 00084 public function isError() { 00085 return false; 00086 } 00087 00096 public function hasFile() { 00097 // If TRANSFORM_LATER, $this->path will be false. 00098 // Note: a null path means "use the source file". 00099 return ( !$this->isError() && ( $this->path || $this->path === null ) ); 00100 } 00101 00108 public function fileIsSource() { 00109 return ( !$this->isError() && $this->path === null ); 00110 } 00111 00118 public function getLocalCopyPath() { 00119 if ( $this->isError() ) { 00120 return false; 00121 } elseif ( $this->path === null ) { 00122 return $this->file->getLocalRefPath(); 00123 } else { 00124 return $this->path; // may return false 00125 } 00126 } 00127 00134 public function streamFile( $headers = array() ) { 00135 return $this->path && StreamFile::stream( $this->getLocalCopyPath(), $headers ); 00136 } 00137 00146 protected function linkWrap( $linkAttribs, $contents ) { 00147 if ( $linkAttribs ) { 00148 return Xml::tags( 'a', $linkAttribs, $contents ); 00149 } else { 00150 return $contents; 00151 } 00152 } 00153 00159 public function getDescLinkAttribs( $title = null, $params = '' ) { 00160 $query = $this->page ? ( 'page=' . urlencode( $this->page ) ) : ''; 00161 if( $params ) { 00162 $query .= $query ? '&'.$params : $params; 00163 } 00164 $attribs = array( 00165 'href' => $this->file->getTitle()->getLocalURL( $query ), 00166 'class' => 'image', 00167 ); 00168 if ( $title ) { 00169 $attribs['title'] = $title; 00170 } 00171 return $attribs; 00172 } 00173 } 00174 00180 class ThumbnailImage extends MediaTransformOutput { 00194 function __construct( $file, $url, $width, $height, $path = false, $page = false ) { 00195 $this->file = $file; 00196 $this->url = $url; 00197 # These should be integers when they get here. 00198 # If not, there's a bug somewhere. But let's at 00199 # least produce valid HTML code regardless. 00200 $this->width = round( $width ); 00201 $this->height = round( $height ); 00202 $this->path = $path; 00203 $this->page = $page; 00204 } 00205 00230 function toHtml( $options = array() ) { 00231 if ( count( func_get_args() ) == 2 ) { 00232 throw new MWException( __METHOD__ .' called in the old style' ); 00233 } 00234 00235 $alt = empty( $options['alt'] ) ? '' : $options['alt']; 00236 00237 $query = empty( $options['desc-query'] ) ? '' : $options['desc-query']; 00238 00239 if ( !empty( $options['custom-url-link'] ) ) { 00240 $linkAttribs = array( 'href' => $options['custom-url-link'] ); 00241 if ( !empty( $options['title'] ) ) { 00242 $linkAttribs['title'] = $options['title']; 00243 } 00244 if ( !empty( $options['custom-target-link'] ) ) { 00245 $linkAttribs['target'] = $options['custom-target-link']; 00246 } 00247 } elseif ( !empty( $options['custom-title-link'] ) ) { 00248 $title = $options['custom-title-link']; 00249 $linkAttribs = array( 00250 'href' => $title->getLinkURL(), 00251 'title' => empty( $options['title'] ) ? $title->getFullText() : $options['title'] 00252 ); 00253 } elseif ( !empty( $options['desc-link'] ) ) { 00254 $linkAttribs = $this->getDescLinkAttribs( empty( $options['title'] ) ? null : $options['title'], $query ); 00255 } elseif ( !empty( $options['file-link'] ) ) { 00256 $linkAttribs = array( 'href' => $this->file->getURL() ); 00257 } else { 00258 $linkAttribs = false; 00259 } 00260 00261 $attribs = array( 00262 'alt' => $alt, 00263 'src' => $this->url, 00264 'width' => $this->width, 00265 'height' => $this->height, 00266 ); 00267 if ( !empty( $options['valign'] ) ) { 00268 $attribs['style'] = "vertical-align: {$options['valign']}"; 00269 } 00270 if ( !empty( $options['img-class'] ) ) { 00271 $attribs['class'] = $options['img-class']; 00272 } 00273 return $this->linkWrap( $linkAttribs, Xml::element( 'img', $attribs ) ); 00274 } 00275 00276 } 00277 00283 class MediaTransformError extends MediaTransformOutput { 00284 var $htmlMsg, $textMsg, $width, $height, $url, $path; 00285 00286 function __construct( $msg, $width, $height /*, ... */ ) { 00287 $args = array_slice( func_get_args(), 3 ); 00288 $htmlArgs = array_map( 'htmlspecialchars', $args ); 00289 $htmlArgs = array_map( 'nl2br', $htmlArgs ); 00290 00291 $this->htmlMsg = wfMessage( $msg )->rawParams( $htmlArgs )->escaped(); 00292 $this->textMsg = wfMessage( $msg )->rawParams( $htmlArgs )->text(); 00293 $this->width = intval( $width ); 00294 $this->height = intval( $height ); 00295 $this->url = false; 00296 $this->path = false; 00297 } 00298 00299 function toHtml( $options = array() ) { 00300 return "<div class=\"MediaTransformError\" style=\"" . 00301 "width: {$this->width}px; height: {$this->height}px; display:inline-block;\">" . 00302 $this->htmlMsg . 00303 "</div>"; 00304 } 00305 00306 function toText() { 00307 return $this->textMsg; 00308 } 00309 00310 function getHtmlMsg() { 00311 return $this->htmlMsg; 00312 } 00313 00314 function isError() { 00315 return true; 00316 } 00317 } 00318 00324 class TransformParameterError extends MediaTransformError { 00325 function __construct( $params ) { 00326 parent::__construct( 'thumbnail_error', 00327 max( isset( $params['width'] ) ? $params['width'] : 0, 120 ), 00328 max( isset( $params['height'] ) ? $params['height'] : 0, 120 ), 00329 wfMsg( 'thumbnail_invalid_params' ) ); 00330 } 00331 }