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
{
35 var $width, $height, $url, $page, $path;
38 * @var array Associative array mapping optional supplementary image files
39 * from pixel density (eg 1.5 or 2) to additional URLs.
41 public $responsiveUrls = array();
43 protected $storagePath = false;
46 * @return integer Width of the output box
48 public function getWidth() {
53 * @return integer Height of the output box
55 public function getHeight() {
60 * Get the final extension of the thumbnail.
61 * Returns false for scripted transformations.
62 * @return string|false
64 public function getExtension() {
65 return $this->path ? FileBackend
::extensionFromPath( $this->path
) : false;
69 * @return string|false The thumbnail URL
71 public function getUrl() {
76 * @return string|bool The permanent thumbnail storage path
78 public function getStoragePath() {
79 return $this->storagePath
;
83 * @param $storagePath string The permanent storage path
86 public function setStoragePath( $storagePath ) {
87 $this->storagePath
= $storagePath;
91 * Fetch HTML for this transform output
93 * @param $options array Associative array of options. Boolean options
94 * should be indicated with a value of true for true, and false or
97 * alt Alternate text or caption
98 * desc-link Boolean, show a description link
99 * file-link Boolean, show a file download link
100 * custom-url-link Custom URL to link to
101 * custom-title-link Custom Title object to link to
102 * valign vertical-align property, if the output is an inline element
103 * img-class Class applied to the "<img>" tag, if there is such a tag
105 * For images, desc-link and file-link are implemented as a click-through. For
106 * sounds and videos, they may be displayed in other ways.
110 abstract public function toHtml( $options = array() );
113 * This will be overridden to return true in error classes
116 public function isError() {
121 * Check if an output thumbnail file actually exists.
122 * This will return false if there was an error, the
123 * thumbnail is to be handled client-side only, or if
124 * transformation was deferred via TRANSFORM_LATER.
128 public function hasFile() {
129 // If TRANSFORM_LATER, $this->path will be false.
130 // Note: a null path means "use the source file".
131 return ( !$this->isError() && ( $this->path ||
$this->path
=== null ) );
135 * Check if the output thumbnail is the same as the source.
136 * This can occur if the requested width was bigger than the source.
140 public function fileIsSource() {
141 return ( !$this->isError() && $this->path
=== null );
145 * Get the path of a file system copy of the thumbnail.
146 * Callers should never write to this path.
148 * @return string|bool Returns false if there isn't one
150 public function getLocalCopyPath() {
151 if ( $this->isError() ) {
153 } elseif ( $this->path
=== null ) {
154 return $this->file
->getLocalRefPath();
156 return $this->path
; // may return false
161 * Stream the file if there were no errors
163 * @param $headers Array Additional HTTP headers to send on success
164 * @return Bool success
166 public function streamFile( $headers = array() ) {
167 if ( !$this->path
) {
169 } elseif ( FileBackend
::isStoragePath( $this->path
) ) {
170 $be = $this->file
->getRepo()->getBackend();
171 return $be->streamFile( array( 'src' => $this->path
, 'headers' => $headers ) )->isOK();
173 return StreamFile
::stream( $this->getLocalCopyPath(), $headers );
178 * Wrap some XHTML text in an anchor tag with the given attributes
180 * @param $linkAttribs array
181 * @param $contents string
185 protected function linkWrap( $linkAttribs, $contents ) {
186 if ( $linkAttribs ) {
187 return Xml
::tags( 'a', $linkAttribs, $contents );
194 * @param $title string
195 * @param $params array
198 public function getDescLinkAttribs( $title = null, $params = '' ) {
199 $query = $this->page ?
( 'page=' . urlencode( $this->page
) ) : '';
201 $query .= $query ?
'&'.$params : $params;
204 'href' => $this->file
->getTitle()->getLocalURL( $query ),
208 $attribs['title'] = $title;
215 * Media transform output for images
219 class ThumbnailImage
extends MediaTransformOutput
{
221 * Get a thumbnail object from a file and parameters.
222 * If $path is set to null, the output file is treated as a source copy.
223 * If $path is set to false, no output file will be created.
224 * $parameters should include, as a minimum, (file) 'width' and 'height'.
225 * It may also include a 'page' parameter for multipage files.
227 * @param $file File object
228 * @param $url String: URL path to the thumb
229 * @param $path String|bool|null: filesystem path to the thumb
230 * @param $parameters Array: Associative array of parameters
233 function __construct( $file, $url, $path = false, $parameters = array() ) {
234 # Previous parameters:
235 # $file, $url, $width, $height, $path = false, $page = false
237 if( is_array( $parameters ) ){
241 $actualParams = $parameters +
$defaults;
243 # Using old format, should convert. Later a warning could be added here.
244 $numArgs = func_num_args();
245 $actualParams = array(
247 'height' => $parameters,
248 'page' => ( $numArgs > 5 ) ?
func_get_arg( 5 ) : false
250 $path = ( $numArgs > 4 ) ?
func_get_arg( 4 ) : false;
257 # These should be integers when they get here.
258 # If not, there's a bug somewhere. But let's at
259 # least produce valid HTML code regardless.
260 $this->width
= round( $actualParams['width'] );
261 $this->height
= round( $actualParams['height'] );
263 $this->page
= $actualParams['page'];
267 * Return HTML <img ... /> tag for the thumbnail, will include
268 * width and height attributes and a blank alt text (as required).
270 * @param $options array Associative array of options. Boolean options
271 * should be indicated with a value of true for true, and false or
274 * alt HTML alt attribute
275 * title HTML title attribute
276 * desc-link Boolean, show a description link
277 * file-link Boolean, show a file download link
278 * valign vertical-align property, if the output is an inline element
279 * img-class Class applied to the \<img\> tag, if there is such a tag
280 * desc-query String, description link query params
281 * custom-url-link Custom URL to link to
282 * custom-title-link Custom Title object to link to
283 * custom target-link Value of the target attribute, for custom-target-link
284 * parser-extlink-* Attributes added by parser for external links:
285 * parser-extlink-rel: add rel="nofollow"
286 * parser-extlink-target: link target, but overridden by custom-target-link
288 * For images, desc-link and file-link are implemented as a click-through. For
289 * sounds and videos, they may be displayed in other ways.
291 * @throws MWException
294 function toHtml( $options = array() ) {
295 if ( count( func_get_args() ) == 2 ) {
296 throw new MWException( __METHOD__
.' called in the old style' );
299 $alt = empty( $options['alt'] ) ?
'' : $options['alt'];
301 $query = empty( $options['desc-query'] ) ?
'' : $options['desc-query'];
303 if ( !empty( $options['custom-url-link'] ) ) {
304 $linkAttribs = array( 'href' => $options['custom-url-link'] );
305 if ( !empty( $options['title'] ) ) {
306 $linkAttribs['title'] = $options['title'];
308 if ( !empty( $options['custom-target-link'] ) ) {
309 $linkAttribs['target'] = $options['custom-target-link'];
310 } elseif ( !empty( $options['parser-extlink-target'] ) ) {
311 $linkAttribs['target'] = $options['parser-extlink-target'];
313 if ( !empty( $options['parser-extlink-rel'] ) ) {
314 $linkAttribs['rel'] = $options['parser-extlink-rel'];
316 } elseif ( !empty( $options['custom-title-link'] ) ) {
317 $title = $options['custom-title-link'];
318 $linkAttribs = array(
319 'href' => $title->getLinkURL(),
320 'title' => empty( $options['title'] ) ?
$title->getFullText() : $options['title']
322 } elseif ( !empty( $options['desc-link'] ) ) {
323 $linkAttribs = $this->getDescLinkAttribs( empty( $options['title'] ) ?
null : $options['title'], $query );
324 } elseif ( !empty( $options['file-link'] ) ) {
325 $linkAttribs = array( 'href' => $this->file
->getURL() );
327 $linkAttribs = false;
333 'width' => $this->width
,
334 'height' => $this->height
336 if ( !empty( $options['valign'] ) ) {
337 $attribs['style'] = "vertical-align: {$options['valign']}";
339 if ( !empty( $options['img-class'] ) ) {
340 $attribs['class'] = $options['img-class'];
343 // Additional densities for responsive images, if specified.
344 if ( !empty( $this->responsiveUrls
) ) {
345 $attribs['srcset'] = Html
::srcSet( $this->responsiveUrls
);
347 return $this->linkWrap( $linkAttribs, Xml
::element( 'img', $attribs ) );
353 * Basic media transform error class
357 class MediaTransformError
extends MediaTransformOutput
{
358 var $htmlMsg, $textMsg, $width, $height, $url, $path;
360 function __construct( $msg, $width, $height /*, ... */ ) {
361 $args = array_slice( func_get_args(), 3 );
362 $htmlArgs = array_map( 'htmlspecialchars', $args );
363 $htmlArgs = array_map( 'nl2br', $htmlArgs );
365 $this->htmlMsg
= wfMessage( $msg )->rawParams( $htmlArgs )->escaped();
366 $this->textMsg
= wfMessage( $msg )->rawParams( $htmlArgs )->text();
367 $this->width
= intval( $width );
368 $this->height
= intval( $height );
373 function toHtml( $options = array() ) {
374 return "<div class=\"MediaTransformError\" style=\"" .
375 "width: {$this->width}px; height: {$this->height}px; display:inline-block;\">" .
381 return $this->textMsg
;
384 function getHtmlMsg() {
385 return $this->htmlMsg
;
394 * Shortcut class for parameter validation errors
398 class TransformParameterError
extends MediaTransformError
{
399 function __construct( $params ) {
400 parent
::__construct( 'thumbnail_error',
401 max( isset( $params['width'] ) ?
$params['width'] : 0, 120 ),
402 max( isset( $params['height'] ) ?
$params['height'] : 0, 120 ),
403 wfMessage( 'thumbnail_invalid_params' )->text() );