1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331
|
<?php
/**
* Base class for the output of file transformation methods.
*
* @file
* @ingroup Media
*/
/**
* Base class for the output of MediaHandler::doTransform() and File::transform().
*
* @ingroup Media
*/
abstract class MediaTransformOutput {
/**
* @var File
*/
var $file;
var $width, $height, $url, $page, $path;
protected $storagePath = false;
/**
* Get the width of the output box
*/
public function getWidth() {
return $this->width;
}
/**
* Get the height of the output box
*/
public function getHeight() {
return $this->height;
}
/**
* @return string The thumbnail URL
*/
public function getUrl() {
return $this->url;
}
/**
* @return string|false The permanent thumbnail storage path
*/
public function getStoragePath() {
return $this->storagePath;
}
/**
* @param $storagePath string The permanent storage path
* @return void
*/
public function setStoragePath( $storagePath ) {
$this->storagePath = $storagePath;
}
/**
* Fetch HTML for this transform output
*
* @param $options array Associative array of options. Boolean options
* should be indicated with a value of true for true, and false or
* absent for false.
*
* alt Alternate text or caption
* desc-link Boolean, show a description link
* file-link Boolean, show a file download link
* custom-url-link Custom URL to link to
* custom-title-link Custom Title object to link to
* valign vertical-align property, if the output is an inline element
* img-class Class applied to the <img> tag, if there is such a tag
*
* For images, desc-link and file-link are implemented as a click-through. For
* sounds and videos, they may be displayed in other ways.
*
* @return string
*/
abstract public function toHtml( $options = array() );
/**
* This will be overridden to return true in error classes
*/
public function isError() {
return false;
}
/**
* Check if an output thumbnail file actually exists.
* This will return false if there was an error, the
* thumbnail is to be handled client-side only, or if
* transformation was deferred via TRANSFORM_LATER.
*
* @return Bool
*/
public function hasFile() {
// If TRANSFORM_LATER, $this->path will be false.
// Note: a null path means "use the source file".
return ( !$this->isError() && ( $this->path || $this->path === null ) );
}
/**
* Check if the output thumbnail is the same as the source.
* This can occur if the requested width was bigger than the source.
*
* @return Bool
*/
public function fileIsSource() {
return ( !$this->isError() && $this->path === null );
}
/**
* Get the path of a file system copy of the thumbnail.
* Callers should never write to this path.
*
* @return string|false Returns false if there isn't one
*/
public function getLocalCopyPath() {
if ( $this->isError() ) {
return false;
} elseif ( $this->path === null ) {
return $this->file->getLocalRefPath();
} else {
return $this->path; // may return false
}
}
/**
* Stream the file if there were no errors
*
* @param $headers Array Additional HTTP headers to send on success
* @return Bool success
*/
public function streamFile( $headers = array() ) {
return $this->path && StreamFile::stream( $this->getLocalCopyPath(), $headers );
}
/**
* Wrap some XHTML text in an anchor tag with the given attributes
*
* @param $linkAttribs array
* @param $contents string
*
* @return string
*/
protected function linkWrap( $linkAttribs, $contents ) {
if ( $linkAttribs ) {
return Xml::tags( 'a', $linkAttribs, $contents );
} else {
return $contents;
}
}
/**
* @param $title string
* @param $params array
* @return array
*/
public function getDescLinkAttribs( $title = null, $params = '' ) {
$query = $this->page ? ( 'page=' . urlencode( $this->page ) ) : '';
if( $params ) {
$query .= $query ? '&'.$params : $params;
}
$attribs = array(
'href' => $this->file->getTitle()->getLocalURL( $query ),
'class' => 'image',
);
if ( $title ) {
$attribs['title'] = $title;
}
return $attribs;
}
}
/**
* Media transform output for images
*
* @ingroup Media
*/
class ThumbnailImage extends MediaTransformOutput {
/**
* Get a thumbnail object from a file and parameters.
* If $path is set to null, the output file is treated as a source copy.
* If $path is set to false, no output file will be created.
*
* @param $file File object
* @param $url String: URL path to the thumb
* @param $width Integer: file's width
* @param $height Integer: file's height
* @param $path String|false|null: filesystem path to the thumb
* @param $page Integer: page number, for multipage files
* @private
*/
function __construct( $file, $url, $width, $height, $path = false, $page = false ) {
$this->file = $file;
$this->url = $url;
# These should be integers when they get here.
# If not, there's a bug somewhere. But let's at
# least produce valid HTML code regardless.
$this->width = round( $width );
$this->height = round( $height );
$this->path = $path;
$this->page = $page;
}
/**
* Return HTML <img ... /> tag for the thumbnail, will include
* width and height attributes and a blank alt text (as required).
*
* @param $options array Associative array of options. Boolean options
* should be indicated with a value of true for true, and false or
* absent for false.
*
* alt HTML alt attribute
* title HTML title attribute
* desc-link Boolean, show a description link
* file-link Boolean, show a file download link
* valign vertical-align property, if the output is an inline element
* img-class Class applied to the \<img\> tag, if there is such a tag
* desc-query String, description link query params
* custom-url-link Custom URL to link to
* custom-title-link Custom Title object to link to
* custom target-link Value of the target attribute, for custom-target-link
*
* For images, desc-link and file-link are implemented as a click-through. For
* sounds and videos, they may be displayed in other ways.
*
* @return string
*/
function toHtml( $options = array() ) {
if ( count( func_get_args() ) == 2 ) {
throw new MWException( __METHOD__ .' called in the old style' );
}
$alt = empty( $options['alt'] ) ? '' : $options['alt'];
$query = empty( $options['desc-query'] ) ? '' : $options['desc-query'];
if ( !empty( $options['custom-url-link'] ) ) {
$linkAttribs = array( 'href' => $options['custom-url-link'] );
if ( !empty( $options['title'] ) ) {
$linkAttribs['title'] = $options['title'];
}
if ( !empty( $options['custom-target-link'] ) ) {
$linkAttribs['target'] = $options['custom-target-link'];
}
} elseif ( !empty( $options['custom-title-link'] ) ) {
$title = $options['custom-title-link'];
$linkAttribs = array(
'href' => $title->getLinkURL(),
'title' => empty( $options['title'] ) ? $title->getFullText() : $options['title']
);
} elseif ( !empty( $options['desc-link'] ) ) {
$linkAttribs = $this->getDescLinkAttribs( empty( $options['title'] ) ? null : $options['title'], $query );
} elseif ( !empty( $options['file-link'] ) ) {
$linkAttribs = array( 'href' => $this->file->getURL() );
} else {
$linkAttribs = false;
}
$attribs = array(
'alt' => $alt ? $alt : '(thumbnail)',
'src' => $this->url,
'width' => $this->width,
'height' => $this->height,
);
if ( !empty( $options['valign'] ) ) {
$attribs['style'] = "vertical-align: {$options['valign']}";
}
if ( !empty( $options['img-class'] ) ) {
$attribs['class'] = $options['img-class'];
}
return $this->linkWrap( $linkAttribs, Xml::element( 'img', $attribs ) );
}
}
/**
* Basic media transform error class
*
* @ingroup Media
*/
class MediaTransformError extends MediaTransformOutput {
var $htmlMsg, $textMsg, $width, $height, $url, $path;
function __construct( $msg, $width, $height /*, ... */ ) {
$args = array_slice( func_get_args(), 3 );
$htmlArgs = array_map( 'htmlspecialchars', $args );
$htmlArgs = array_map( 'nl2br', $htmlArgs );
$this->htmlMsg = wfMessage( $msg )->rawParams( $htmlArgs )->escaped();
$this->textMsg = wfMessage( $msg )->rawParams( $htmlArgs )->text();
$this->width = intval( $width );
$this->height = intval( $height );
$this->url = false;
$this->path = false;
}
function toHtml( $options = array() ) {
return "<div class=\"MediaTransformError\" style=\"" .
"width: {$this->width}px; height: {$this->height}px; display:inline-block;\">" .
$this->htmlMsg .
"</div>";
}
function toText() {
return $this->textMsg;
}
function getHtmlMsg() {
return $this->htmlMsg;
}
function isError() {
return true;
}
}
/**
* Shortcut class for parameter validation errors
*
* @ingroup Media
*/
class TransformParameterError extends MediaTransformError {
function __construct( $params ) {
parent::__construct( 'thumbnail_error',
max( isset( $params['width'] ) ? $params['width'] : 0, 120 ),
max( isset( $params['height'] ) ? $params['height'] : 0, 120 ),
wfMsg( 'thumbnail_invalid_params' ) );
}
}
|