3 * Base class for the output of file transformation methods.
10 * Base class for the output of MediaHandler::doTransform() and File::transform().
14 abstract class MediaTransformOutput
{
20 var $width, $height, $url, $page, $path;
21 protected $storagePath = false;
24 * Get the width of the output box
26 public function getWidth() {
31 * Get the height of the output box
33 public function getHeight() {
38 * @return string The thumbnail URL
40 public function getUrl() {
45 * @return string|false The permanent thumbnail storage path
47 public function getStoragePath() {
48 return $this->storagePath
;
52 * @param $storagePath string The permanent storage path
55 public function setStoragePath( $storagePath ) {
56 $this->storagePath
= $storagePath;
60 * Fetch HTML for this transform output
62 * @param $options array Associative array of options. Boolean options
63 * should be indicated with a value of true for true, and false or
66 * alt Alternate text or caption
67 * desc-link Boolean, show a description link
68 * file-link Boolean, show a file download link
69 * custom-url-link Custom URL to link to
70 * custom-title-link Custom Title object to link to
71 * valign vertical-align property, if the output is an inline element
72 * img-class Class applied to the <img> tag, if there is such a tag
74 * For images, desc-link and file-link are implemented as a click-through. For
75 * sounds and videos, they may be displayed in other ways.
79 abstract public function toHtml( $options = array() );
82 * This will be overridden to return true in error classes
84 public function isError() {
89 * Check if an output thumbnail file was actually made.
90 * This will return false if there was an error, the
91 * thumnail is to be handled client-side only, or if
92 * transformation was deferred via TRANSFORM_LATER.
96 public function hasFile() {
97 // If TRANSFORM_LATER, $this->path will be false
98 return ( !$this->isError() && $this->path
);
102 * Check if the output thumbnail file is the same as the source.
103 * This can occur if the requested width was bigger than the source.
107 public function fileIsSource() {
108 return ( !$this->isError() && $this->path
=== $this->file
->getLocalRefPath() );
112 * Get the path of a file system copy of the thumbnail
114 * @return string|false Returns false if there isn't one
116 public function getLocalCopyPath() {
121 * Stream the file if there were no errors
123 * @param $headers Array Additional HTTP headers to send on success
124 * @return Bool success
126 public function streamFile( $headers = array() ) {
127 return $this->path
&& StreamFile
::stream( $this->path
, $headers );
131 * Wrap some XHTML text in an anchor tag with the given attributes
133 * @param $linkAttribs array
134 * @param $contents string
138 protected function linkWrap( $linkAttribs, $contents ) {
139 if ( $linkAttribs ) {
140 return Xml
::tags( 'a', $linkAttribs, $contents );
147 * @param $title string
148 * @param $params array
151 public function getDescLinkAttribs( $title = null, $params = '' ) {
152 $query = $this->page ?
( 'page=' . urlencode( $this->page
) ) : '';
154 $query .= $query ?
'&'.$params : $params;
157 'href' => $this->file
->getTitle()->getLocalURL( $query ),
161 $attribs['title'] = $title;
168 * Media transform output for images
172 class ThumbnailImage
extends MediaTransformOutput
{
175 * @param $file File object
176 * @param $url String: URL path to the thumb
177 * @param $width Integer: file's width
178 * @param $height Integer: file's height
179 * @param $path String: filesystem path to the thumb
180 * @param $page Integer: page number, for multipage files
183 function __construct( $file, $url, $width, $height, $path = false, $page = false ) {
186 # These should be integers when they get here.
187 # If not, there's a bug somewhere. But let's at
188 # least produce valid HTML code regardless.
189 $this->width
= round( $width );
190 $this->height
= round( $height );
196 * Return HTML <img ... /> tag for the thumbnail, will include
197 * width and height attributes and a blank alt text (as required).
199 * @param $options array Associative array of options. Boolean options
200 * should be indicated with a value of true for true, and false or
203 * alt HTML alt attribute
204 * title HTML title attribute
205 * desc-link Boolean, show a description link
206 * file-link Boolean, show a file download link
207 * valign vertical-align property, if the output is an inline element
208 * img-class Class applied to the \<img\> tag, if there is such a tag
209 * desc-query String, description link query params
210 * custom-url-link Custom URL to link to
211 * custom-title-link Custom Title object to link to
212 * custom target-link Value of the target attribute, for custom-target-link
214 * For images, desc-link and file-link are implemented as a click-through. For
215 * sounds and videos, they may be displayed in other ways.
219 function toHtml( $options = array() ) {
220 if ( count( func_get_args() ) == 2 ) {
221 throw new MWException( __METHOD__
.' called in the old style' );
224 $alt = empty( $options['alt'] ) ?
'' : $options['alt'];
226 $query = empty( $options['desc-query'] ) ?
'' : $options['desc-query'];
228 if ( !empty( $options['custom-url-link'] ) ) {
229 $linkAttribs = array( 'href' => $options['custom-url-link'] );
230 if ( !empty( $options['title'] ) ) {
231 $linkAttribs['title'] = $options['title'];
233 if ( !empty( $options['custom-target-link'] ) ) {
234 $linkAttribs['target'] = $options['custom-target-link'];
236 } elseif ( !empty( $options['custom-title-link'] ) ) {
237 $title = $options['custom-title-link'];
238 $linkAttribs = array(
239 'href' => $title->getLinkURL(),
240 'title' => empty( $options['title'] ) ?
$title->getFullText() : $options['title']
242 } elseif ( !empty( $options['desc-link'] ) ) {
243 $linkAttribs = $this->getDescLinkAttribs( empty( $options['title'] ) ?
null : $options['title'], $query );
244 } elseif ( !empty( $options['file-link'] ) ) {
245 $linkAttribs = array( 'href' => $this->file
->getURL() );
247 $linkAttribs = false;
253 'width' => $this->width
,
254 'height' => $this->height
,
256 if ( !empty( $options['valign'] ) ) {
257 $attribs['style'] = "vertical-align: {$options['valign']}";
259 if ( !empty( $options['img-class'] ) ) {
260 $attribs['class'] = $options['img-class'];
262 return $this->linkWrap( $linkAttribs, Xml
::element( 'img', $attribs ) );
268 * Basic media transform error class
272 class MediaTransformError
extends MediaTransformOutput
{
273 var $htmlMsg, $textMsg, $width, $height, $url, $path;
275 function __construct( $msg, $width, $height /*, ... */ ) {
276 $args = array_slice( func_get_args(), 3 );
277 $htmlArgs = array_map( 'htmlspecialchars', $args );
278 $htmlArgs = array_map( 'nl2br', $htmlArgs );
280 $this->htmlMsg
= wfMessage( $msg )->rawParams( $htmlArgs )->escaped();
281 $this->textMsg
= wfMessage( $msg )->rawParams( $htmlArgs )->text();
282 $this->width
= intval( $width );
283 $this->height
= intval( $height );
288 function toHtml( $options = array() ) {
289 return "<div class=\"MediaTransformError\" style=\"" .
290 "width: {$this->width}px; height: {$this->height}px; display:inline-block;\">" .
296 return $this->textMsg
;
299 function getHtmlMsg() {
300 return $this->htmlMsg
;
309 * Shortcut class for parameter validation errors
313 class TransformParameterError
extends MediaTransformError
{
314 function __construct( $params ) {
315 parent
::__construct( 'thumbnail_error',
316 max( isset( $params['width'] ) ?
$params['width'] : 0, 120 ),
317 max( isset( $params['height'] ) ?
$params['height'] : 0, 120 ),
318 wfMsg( 'thumbnail_invalid_params' ) );