05feb3043ca9944b7c4be17049f7780587013c19
3 * Base class for the output of file transformation methods.
5 * This program is free software; you can redistribute it and/or modify
6 * it under the terms of the GNU General Public License as published by
7 * the Free Software Foundation; either version 2 of the License, or
8 * (at your option) any later version.
10 * This program is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 * GNU General Public License for more details.
15 * You should have received a copy of the GNU General Public License along
16 * with this program; if not, write to the Free Software Foundation, Inc.,
17 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
18 * http://www.gnu.org/copyleft/gpl.html
25 * Base class for the output of MediaHandler::doTransform() and File::transform().
29 abstract class MediaTransformOutput
{
30 /** @var array Associative array mapping optional supplementary image files
31 * from pixel density (eg 1.5 or 2) to additional URLs.
33 public $responsiveUrls = array();
35 /** @var File object */
38 /** @var int Image width */
41 /** @var int Image height */
44 /** @var string URL path to the thumb */
47 /** @var bool|string */
50 /** @var bool|string Filesystem path to the thumb */
53 /** @var bool|string Language code, false if not set */
56 /** @var bool|string Permanent storage path */
57 protected $storagePath = false;
60 * @return int Width of the output box
62 public function getWidth() {
67 * @return int Height of the output box
69 public function getHeight() {
76 public function getFile() {
81 * Get the final extension of the thumbnail.
82 * Returns false for scripted transformations.
83 * @return string|false
85 public function getExtension() {
86 return $this->path ? FileBackend
::extensionFromPath( $this->path
) : false;
90 * @return string|false The thumbnail URL
92 public function getUrl() {
97 * @return string|bool The permanent thumbnail storage path
99 public function getStoragePath() {
100 return $this->storagePath
;
104 * @param string $storagePath The permanent storage path
107 public function setStoragePath( $storagePath ) {
108 $this->storagePath
= $storagePath;
112 * Fetch HTML for this transform output
114 * @param array $options Associative array of options. Boolean options
115 * should be indicated with a value of true for true, and false or
118 * alt Alternate text or caption
119 * desc-link Boolean, show a description link
120 * file-link Boolean, show a file download link
121 * custom-url-link Custom URL to link to
122 * custom-title-link Custom Title object to link to
123 * valign vertical-align property, if the output is an inline element
124 * img-class Class applied to the "<img>" tag, if there is such a tag
126 * For images, desc-link and file-link are implemented as a click-through. For
127 * sounds and videos, they may be displayed in other ways.
131 abstract public function toHtml( $options = array() );
134 * This will be overridden to return true in error classes
137 public function isError() {
142 * Check if an output thumbnail file actually exists.
143 * This will return false if there was an error, the
144 * thumbnail is to be handled client-side only, or if
145 * transformation was deferred via TRANSFORM_LATER.
149 public function hasFile() {
150 // If TRANSFORM_LATER, $this->path will be false.
151 // Note: a null path means "use the source file".
152 return ( !$this->isError() && ( $this->path ||
$this->path
=== null ) );
156 * Check if the output thumbnail is the same as the source.
157 * This can occur if the requested width was bigger than the source.
161 public function fileIsSource() {
162 return ( !$this->isError() && $this->path
=== null );
166 * Get the path of a file system copy of the thumbnail.
167 * Callers should never write to this path.
169 * @return string|bool Returns false if there isn't one
171 public function getLocalCopyPath() {
172 if ( $this->isError() ) {
174 } elseif ( $this->path
=== null ) {
175 return $this->file
->getLocalRefPath(); // assume thumb was not scaled
176 } elseif ( FileBackend
::isStoragePath( $this->path
) ) {
177 $be = $this->file
->getRepo()->getBackend();
178 // The temp file will be process cached by FileBackend
179 $fsFile = $be->getLocalReference( array( 'src' => $this->path
) );
181 return $fsFile ?
$fsFile->getPath() : false;
183 return $this->path
; // may return false
188 * Stream the file if there were no errors
190 * @param array $headers Additional HTTP headers to send on success
191 * @return bool Success
193 public function streamFile( $headers = array() ) {
194 if ( !$this->path
) {
196 } elseif ( FileBackend
::isStoragePath( $this->path
) ) {
197 $be = $this->file
->getRepo()->getBackend();
199 return $be->streamFile( array( 'src' => $this->path
, 'headers' => $headers ) )->isOK();
201 return StreamFile
::stream( $this->getLocalCopyPath(), $headers );
206 * Wrap some XHTML text in an anchor tag with the given attributes
208 * @param array $linkAttribs
209 * @param string $contents
212 protected function linkWrap( $linkAttribs, $contents ) {
213 if ( $linkAttribs ) {
214 return Xml
::tags( 'a', $linkAttribs, $contents );
221 * @param $title string
222 * @param string|array $params Query parameters to add
225 public function getDescLinkAttribs( $title = null, $params = array() ) {
226 if ( is_array( $params ) ) {
231 if ( $this->page
&& $this->page
!== 1 ) {
232 $query['page'] = $this->page
;
235 $query['lang'] = $this->lang
;
238 if ( is_string( $params ) && $params !== '' ) {
239 $query = $params . '&' . wfArrayToCgi( $query );
243 'href' => $this->file
->getTitle()->getLocalURL( $query ),
247 $attribs['title'] = $title;
255 * Media transform output for images
259 class ThumbnailImage
extends MediaTransformOutput
{
261 * Get a thumbnail object from a file and parameters.
262 * If $path is set to null, the output file is treated as a source copy.
263 * If $path is set to false, no output file will be created.
264 * $parameters should include, as a minimum, (file) 'width' and 'height'.
265 * It may also include a 'page' parameter for multipage files.
268 * @param string $url URL path to the thumb
269 * @param string|bool $path Filesystem path to the thumb
270 * @param array $parameters Associative array of parameters
272 function __construct( $file, $url, $path = false, $parameters = array() ) {
273 # Previous parameters:
274 # $file, $url, $width, $height, $path = false, $page = false
281 if ( is_array( $parameters ) ) {
282 $actualParams = $parameters +
$defaults;
284 # Using old format, should convert. Later a warning could be added here.
285 $numArgs = func_num_args();
286 $actualParams = array(
288 'height' => $parameters,
289 'page' => ( $numArgs > 5 ) ?
func_get_arg( 5 ) : false
291 $path = ( $numArgs > 4 ) ?
func_get_arg( 4 ) : false;
298 # These should be integers when they get here.
299 # If not, there's a bug somewhere. But let's at
300 # least produce valid HTML code regardless.
301 $this->width
= round( $actualParams['width'] );
302 $this->height
= round( $actualParams['height'] );
304 $this->page
= $actualParams['page'];
305 $this->lang
= $actualParams['lang'];
309 * Return HTML <img ... /> tag for the thumbnail, will include
310 * width and height attributes and a blank alt text (as required).
312 * @param array $options Associative array of options. Boolean options
313 * should be indicated with a value of true for true, and false or
316 * alt HTML alt attribute
317 * title HTML title attribute
318 * desc-link Boolean, show a description link
319 * file-link Boolean, show a file download link
320 * valign vertical-align property, if the output is an inline element
321 * img-class Class applied to the \<img\> tag, if there is such a tag
322 * desc-query String, description link query params
323 * override-width Override width attribute. Should generally not set
324 * override-height Override height attribute. Should generally not set
325 * no-dimensions Boolean, skip width and height attributes (useful if
327 * custom-url-link Custom URL to link to
328 * custom-title-link Custom Title object to link to
329 * custom target-link Value of the target attribute, for custom-target-link
330 * parser-extlink-* Attributes added by parser for external links:
331 * parser-extlink-rel: add rel="nofollow"
332 * parser-extlink-target: link target, but overridden by custom-target-link
334 * For images, desc-link and file-link are implemented as a click-through. For
335 * sounds and videos, they may be displayed in other ways.
337 * @throws MWException
340 function toHtml( $options = array() ) {
341 if ( count( func_get_args() ) == 2 ) {
342 throw new MWException( __METHOD__
. ' called in the old style' );
345 $alt = empty( $options['alt'] ) ?
'' : $options['alt'];
347 $query = empty( $options['desc-query'] ) ?
'' : $options['desc-query'];
349 if ( !empty( $options['custom-url-link'] ) ) {
350 $linkAttribs = array( 'href' => $options['custom-url-link'] );
351 if ( !empty( $options['title'] ) ) {
352 $linkAttribs['title'] = $options['title'];
354 if ( !empty( $options['custom-target-link'] ) ) {
355 $linkAttribs['target'] = $options['custom-target-link'];
356 } elseif ( !empty( $options['parser-extlink-target'] ) ) {
357 $linkAttribs['target'] = $options['parser-extlink-target'];
359 if ( !empty( $options['parser-extlink-rel'] ) ) {
360 $linkAttribs['rel'] = $options['parser-extlink-rel'];
362 } elseif ( !empty( $options['custom-title-link'] ) ) {
363 /** @var Title $title */
364 $title = $options['custom-title-link'];
365 $linkAttribs = array(
366 'href' => $title->getLinkURL(),
367 'title' => empty( $options['title'] ) ?
$title->getFullText() : $options['title']
369 } elseif ( !empty( $options['desc-link'] ) ) {
370 $linkAttribs = $this->getDescLinkAttribs(
371 empty( $options['title'] ) ?
null : $options['title'],
374 } elseif ( !empty( $options['file-link'] ) ) {
375 $linkAttribs = array( 'href' => $this->file
->getURL() );
377 $linkAttribs = false;
385 if ( empty( $options['no-dimensions'] ) ) {
386 $attribs['width'] = $this->width
;
387 $attribs['height'] = $this->height
;
389 if ( !empty( $options['valign'] ) ) {
390 $attribs['style'] = "vertical-align: {$options['valign']}";
392 if ( !empty( $options['img-class'] ) ) {
393 $attribs['class'] = $options['img-class'];
395 if ( isset( $options['override-height'] ) ) {
396 $attribs['height'] = $options['override-height'];
398 if ( isset( $options['override-width'] ) ) {
399 $attribs['width'] = $options['override-width'];
402 // Additional densities for responsive images, if specified.
403 if ( !empty( $this->responsiveUrls
) ) {
404 $attribs['srcset'] = Html
::srcSet( $this->responsiveUrls
);
407 wfRunHooks( 'ThumbnailBeforeProduceHTML', array( $this, &$attribs, &$linkAttribs ) );
409 return $this->linkWrap( $linkAttribs, Xml
::element( 'img', $attribs ) );
414 * Basic media transform error class
418 class MediaTransformError
extends MediaTransformOutput
{
419 /** @var string HTML formatted version of the error */
422 /** @var string Plain text formatted version of the error */
425 function __construct( $msg, $width, $height /*, ... */ ) {
426 $args = array_slice( func_get_args(), 3 );
427 $htmlArgs = array_map( 'htmlspecialchars', $args );
428 $htmlArgs = array_map( 'nl2br', $htmlArgs );
430 $this->htmlMsg
= wfMessage( $msg )->rawParams( $htmlArgs )->escaped();
431 $this->textMsg
= wfMessage( $msg )->rawParams( $htmlArgs )->text();
432 $this->width
= intval( $width );
433 $this->height
= intval( $height );
438 function toHtml( $options = array() ) {
439 return "<div class=\"MediaTransformError\" style=\"" .
440 "width: {$this->width}px; height: {$this->height}px; display:inline-block;\">" .
446 return $this->textMsg
;
449 function getHtmlMsg() {
450 return $this->htmlMsg
;
459 * Shortcut class for parameter validation errors
463 class TransformParameterError
extends MediaTransformError
{
464 function __construct( $params ) {
465 parent
::__construct( 'thumbnail_error',
466 max( isset( $params['width'] ) ?
$params['width'] : 0, 120 ),
467 max( isset( $params['height'] ) ?
$params['height'] : 0, 120 ),
468 wfMessage( 'thumbnail_invalid_params' )->text() );