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;
23 * Get the width of the output box
25 public function getWidth() {
30 * Get the height of the output box
32 public function getHeight() {
37 * @return string The thumbnail URL
39 public function getUrl() {
44 * Fetch HTML for this transform output
46 * @param $options array Associative array of options. Boolean options
47 * should be indicated with a value of true for true, and false or
50 * alt Alternate text or caption
51 * desc-link Boolean, show a description link
52 * file-link Boolean, show a file download link
53 * custom-url-link Custom URL to link to
54 * custom-title-link Custom Title object to link to
55 * valign vertical-align property, if the output is an inline element
56 * img-class Class applied to the <img> tag, if there is such a tag
58 * For images, desc-link and file-link are implemented as a click-through. For
59 * sounds and videos, they may be displayed in other ways.
63 abstract public function toHtml( $options = array() );
66 * This will be overridden to return true in error classes
68 public function isError() {
73 * Check if an output thumbnail file was actually made.
74 * This will return false if there was an error, the
75 * thumnail is to be handled client-side only, or if
76 * transformation was deferred via TRANSFORM_LATER.
80 public function hasFile() {
81 // If TRANSFORM_LATER, $this->path will be false
82 return ( !$this->isError() && $this->path
);
86 * Check if the output thumbnail file is the same as the source.
87 * This can occur if the requested width was bigger than the source.
91 public function fileIsSource() {
92 return ( !$this->isError() && $this->path
=== $this->file
->getLocalRefPath() );
96 * Get the path of a file system copy of the thumbnail
98 * @return string|false Returns false if there isn't one
100 public function getLocalCopyPath() {
105 * Stream the file if there were no errors
107 * @param $headers Array Additional HTTP headers to send on success
108 * @return Bool success
110 public function streamFile( $headers = array() ) {
111 return $this->path
&& StreamFile
::stream( $this->path
, $headers );
115 * Wrap some XHTML text in an anchor tag with the given attributes
117 * @param $linkAttribs array
118 * @param $contents string
122 protected function linkWrap( $linkAttribs, $contents ) {
123 if ( $linkAttribs ) {
124 return Xml
::tags( 'a', $linkAttribs, $contents );
131 * @param $title string
132 * @param $params array
135 public function getDescLinkAttribs( $title = null, $params = '' ) {
136 $query = $this->page ?
( 'page=' . urlencode( $this->page
) ) : '';
138 $query .= $query ?
'&'.$params : $params;
141 'href' => $this->file
->getTitle()->getLocalURL( $query ),
145 $attribs['title'] = $title;
152 * Media transform output for images
156 class ThumbnailImage
extends MediaTransformOutput
{
159 * @param $file File object
160 * @param $url String: URL path to the thumb
161 * @param $width Integer: file's width
162 * @param $height Integer: file's height
163 * @param $path String: filesystem path to the thumb
164 * @param $page Integer: page number, for multipage files
167 function __construct( $file, $url, $width, $height, $path = false, $page = false ) {
170 # These should be integers when they get here.
171 # If not, there's a bug somewhere. But let's at
172 # least produce valid HTML code regardless.
173 $this->width
= round( $width );
174 $this->height
= round( $height );
180 * Return HTML <img ... /> tag for the thumbnail, will include
181 * width and height attributes and a blank alt text (as required).
183 * @param $options array Associative array of options. Boolean options
184 * should be indicated with a value of true for true, and false or
187 * alt HTML alt attribute
188 * title HTML title attribute
189 * desc-link Boolean, show a description link
190 * file-link Boolean, show a file download link
191 * valign vertical-align property, if the output is an inline element
192 * img-class Class applied to the \<img\> tag, if there is such a tag
193 * desc-query String, description link query params
194 * custom-url-link Custom URL to link to
195 * custom-title-link Custom Title object to link to
196 * custom target-link Value of the target attribute, for custom-target-link
198 * For images, desc-link and file-link are implemented as a click-through. For
199 * sounds and videos, they may be displayed in other ways.
203 function toHtml( $options = array() ) {
204 if ( count( func_get_args() ) == 2 ) {
205 throw new MWException( __METHOD__
.' called in the old style' );
208 $alt = empty( $options['alt'] ) ?
'' : $options['alt'];
210 $query = empty( $options['desc-query'] ) ?
'' : $options['desc-query'];
212 if ( !empty( $options['custom-url-link'] ) ) {
213 $linkAttribs = array( 'href' => $options['custom-url-link'] );
214 if ( !empty( $options['title'] ) ) {
215 $linkAttribs['title'] = $options['title'];
217 if ( !empty( $options['custom-target-link'] ) ) {
218 $linkAttribs['target'] = $options['custom-target-link'];
220 } elseif ( !empty( $options['custom-title-link'] ) ) {
221 $title = $options['custom-title-link'];
222 $linkAttribs = array(
223 'href' => $title->getLinkURL(),
224 'title' => empty( $options['title'] ) ?
$title->getFullText() : $options['title']
226 } elseif ( !empty( $options['desc-link'] ) ) {
227 $linkAttribs = $this->getDescLinkAttribs( empty( $options['title'] ) ?
null : $options['title'], $query );
228 } elseif ( !empty( $options['file-link'] ) ) {
229 $linkAttribs = array( 'href' => $this->file
->getURL() );
231 $linkAttribs = false;
237 'width' => $this->width
,
238 'height' => $this->height
,
240 if ( !empty( $options['valign'] ) ) {
241 $attribs['style'] = "vertical-align: {$options['valign']}";
243 if ( !empty( $options['img-class'] ) ) {
244 $attribs['class'] = $options['img-class'];
246 return $this->linkWrap( $linkAttribs, Xml
::element( 'img', $attribs ) );
252 * Basic media transform error class
256 class MediaTransformError
extends MediaTransformOutput
{
257 var $htmlMsg, $textMsg, $width, $height, $url, $path;
259 function __construct( $msg, $width, $height /*, ... */ ) {
260 $args = array_slice( func_get_args(), 3 );
261 $htmlArgs = array_map( 'htmlspecialchars', $args );
262 $htmlArgs = array_map( 'nl2br', $htmlArgs );
264 $this->htmlMsg
= wfMessage( $msg )->rawParams( $htmlArgs )->escaped();
265 $this->textMsg
= wfMessage( $msg )->rawParams( $htmlArgs )->text();
266 $this->width
= intval( $width );
267 $this->height
= intval( $height );
272 function toHtml( $options = array() ) {
273 return "<div class=\"MediaTransformError\" style=\"" .
274 "width: {$this->width}px; height: {$this->height}px; display:inline-block;\">" .
280 return $this->textMsg
;
283 function getHtmlMsg() {
284 return $this->htmlMsg
;
293 * Shortcut class for parameter validation errors
297 class TransformParameterError
extends MediaTransformError
{
298 function __construct( $params ) {
299 parent
::__construct( 'thumbnail_error',
300 max( isset( $params['width'] ) ?
$params['width'] : 0, 120 ),
301 max( isset( $params['height'] ) ?
$params['height'] : 0, 120 ),
302 wfMsg( 'thumbnail_invalid_params' ) );