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
30 * Get the height of the output box
32 function getHeight() {
37 * @return string The thumbnail URL
44 * @return String: destination file path (local filesystem)
51 * Fetch HTML for this transform output
53 * @param $options Associative array of options. Boolean options
54 * should be indicated with a value of true for true, and false or
57 * alt Alternate text or caption
58 * desc-link Boolean, show a description link
59 * file-link Boolean, show a file download link
60 * custom-url-link Custom URL to link to
61 * custom-title-link Custom Title object to link to
62 * valign vertical-align property, if the output is an inline element
63 * img-class Class applied to the <img> tag, if there is such a tag
65 * For images, desc-link and file-link are implemented as a click-through. For
66 * sounds and videos, they may be displayed in other ways.
70 abstract function toHtml( $options = array() );
73 * This will be overridden to return true in error classes
80 * Wrap some XHTML text in an anchor tag with the given attributes
82 protected function linkWrap( $linkAttribs, $contents ) {
84 return Xml
::tags( 'a', $linkAttribs, $contents );
90 function getDescLinkAttribs( $title = null, $params = '' ) {
91 $query = $this->page ?
( 'page=' . urlencode( $this->page
) ) : '';
93 $query .= $query ?
'&'.$params : $params;
96 'href' => $this->file
->getTitle()->getLocalURL( $query ),
100 $attribs['title'] = $title;
108 * Media transform output for images
112 class ThumbnailImage
extends MediaTransformOutput
{
115 * @param $file File object
116 * @param $url String: URL path to the thumb
117 * @param $width Integer: file's width
118 * @param $height Integer: file's height
119 * @param $path String: filesystem path to the thumb
120 * @param $page Integer: page number, for multipage files
123 function __construct( $file, $url, $width, $height, $path = false, $page = false ) {
126 # These should be integers when they get here.
127 # If not, there's a bug somewhere. But let's at
128 # least produce valid HTML code regardless.
129 $this->width
= round( $width );
130 $this->height
= round( $height );
136 * Return HTML <img ... /> tag for the thumbnail, will include
137 * width and height attributes and a blank alt text (as required).
139 * @param $options Associative array of options. Boolean options
140 * should be indicated with a value of true for true, and false or
143 * alt HTML alt attribute
144 * title HTML title attribute
145 * desc-link Boolean, show a description link
146 * file-link Boolean, show a file download link
147 * valign vertical-align property, if the output is an inline element
148 * img-class Class applied to the \<img\> tag, if there is such a tag
149 * desc-query String, description link query params
150 * custom-url-link Custom URL to link to
151 * custom-title-link Custom Title object to link to
152 * custom target-link Value of the target attribute, for custom-target-link
154 * For images, desc-link and file-link are implemented as a click-through. For
155 * sounds and videos, they may be displayed in other ways.
159 function toHtml( $options = array() ) {
160 if ( count( func_get_args() ) == 2 ) {
161 throw new MWException( __METHOD__
.' called in the old style' );
164 $alt = empty( $options['alt'] ) ?
'' : $options['alt'];
166 $query = empty( $options['desc-query'] ) ?
'' : $options['desc-query'];
168 if ( !empty( $options['custom-url-link'] ) ) {
169 $linkAttribs = array( 'href' => $options['custom-url-link'] );
170 if ( !empty( $options['title'] ) ) {
171 $linkAttribs['title'] = $options['title'];
173 if ( !empty( $options['custom-target-link'] ) ) {
174 $linkAttribs['target'] = $options['custom-target-link'];
176 } elseif ( !empty( $options['custom-title-link'] ) ) {
177 $title = $options['custom-title-link'];
178 $linkAttribs = array(
179 'href' => $title->getLinkUrl(),
180 'title' => empty( $options['title'] ) ?
$title->getFullText() : $options['title']
182 } elseif ( !empty( $options['desc-link'] ) ) {
183 $linkAttribs = $this->getDescLinkAttribs( empty( $options['title'] ) ?
null : $options['title'], $query );
184 } elseif ( !empty( $options['file-link'] ) ) {
185 $linkAttribs = array( 'href' => $this->file
->getURL() );
187 $linkAttribs = false;
193 'width' => $this->width
,
194 'height' => $this->height
,
196 if ( !empty( $options['valign'] ) ) {
197 $attribs['style'] = "vertical-align: {$options['valign']}";
199 if ( !empty( $options['img-class'] ) ) {
200 $attribs['class'] = $options['img-class'];
202 return $this->linkWrap( $linkAttribs, Xml
::element( 'img', $attribs ) );
208 * Basic media transform error class
212 class MediaTransformError
extends MediaTransformOutput
{
213 var $htmlMsg, $textMsg, $width, $height, $url, $path;
215 function __construct( $msg, $width, $height /*, ... */ ) {
216 $args = array_slice( func_get_args(), 3 );
217 $htmlArgs = array_map( 'htmlspecialchars', $args );
218 $htmlArgs = array_map( 'nl2br', $htmlArgs );
220 $this->htmlMsg
= wfMessage( $msg )->rawParams( $htmlArgs )->escaped();
221 $this->textMsg
= wfMessage( $msg )->rawParams( $htmlArgs )->text();
222 $this->width
= intval( $width );
223 $this->height
= intval( $height );
228 function toHtml( $options = array() ) {
229 return "<div class=\"MediaTransformError\" style=\"" .
230 "width: {$this->width}px; height: {$this->height}px; display:inline-block;\">" .
236 return $this->textMsg
;
239 function getHtmlMsg() {
240 return $this->htmlMsg
;
249 * Shortcut class for parameter validation errors
253 class TransformParameterError
extends MediaTransformError
{
254 function __construct( $params ) {
255 parent
::__construct( 'thumbnail_error',
256 max( isset( $params['width'] ) ?
$params['width'] : 0, 120 ),
257 max( isset( $params['height'] ) ?
$params['height'] : 0, 120 ),
258 wfMsg( 'thumbnail_invalid_params' ) );