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 array 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 * @param $linkAttribs array
83 * @param $contents string
87 protected function linkWrap( $linkAttribs, $contents ) {
89 return Xml
::tags( 'a', $linkAttribs, $contents );
96 * @param $title string
97 * @param $params array
100 function getDescLinkAttribs( $title = null, $params = '' ) {
101 $query = $this->page ?
( 'page=' . urlencode( $this->page
) ) : '';
103 $query .= $query ?
'&'.$params : $params;
106 'href' => $this->file
->getTitle()->getLocalURL( $query ),
110 $attribs['title'] = $title;
117 * Media transform output for images
121 class ThumbnailImage
extends MediaTransformOutput
{
124 * @param $file File object
125 * @param $url String: URL path to the thumb
126 * @param $width Integer: file's width
127 * @param $height Integer: file's height
128 * @param $path String: filesystem path to the thumb
129 * @param $page Integer: page number, for multipage files
132 function __construct( $file, $url, $width, $height, $path = false, $page = false ) {
135 # These should be integers when they get here.
136 # If not, there's a bug somewhere. But let's at
137 # least produce valid HTML code regardless.
138 $this->width
= round( $width );
139 $this->height
= round( $height );
145 * Return HTML <img ... /> tag for the thumbnail, will include
146 * width and height attributes and a blank alt text (as required).
148 * @param $options array Associative array of options. Boolean options
149 * should be indicated with a value of true for true, and false or
152 * alt HTML alt attribute
153 * title HTML title attribute
154 * desc-link Boolean, show a description link
155 * file-link Boolean, show a file download link
156 * valign vertical-align property, if the output is an inline element
157 * img-class Class applied to the \<img\> tag, if there is such a tag
158 * desc-query String, description link query params
159 * custom-url-link Custom URL to link to
160 * custom-title-link Custom Title object to link to
161 * custom target-link Value of the target attribute, for custom-target-link
163 * For images, desc-link and file-link are implemented as a click-through. For
164 * sounds and videos, they may be displayed in other ways.
168 function toHtml( $options = array() ) {
169 if ( count( func_get_args() ) == 2 ) {
170 throw new MWException( __METHOD__
.' called in the old style' );
173 $alt = empty( $options['alt'] ) ?
'' : $options['alt'];
175 $query = empty( $options['desc-query'] ) ?
'' : $options['desc-query'];
177 if ( !empty( $options['custom-url-link'] ) ) {
178 $linkAttribs = array( 'href' => $options['custom-url-link'] );
179 if ( !empty( $options['title'] ) ) {
180 $linkAttribs['title'] = $options['title'];
182 if ( !empty( $options['custom-target-link'] ) ) {
183 $linkAttribs['target'] = $options['custom-target-link'];
185 } elseif ( !empty( $options['custom-title-link'] ) ) {
186 $title = $options['custom-title-link'];
187 $linkAttribs = array(
188 'href' => $title->getLinkURL(),
189 'title' => empty( $options['title'] ) ?
$title->getFullText() : $options['title']
191 } elseif ( !empty( $options['desc-link'] ) ) {
192 $linkAttribs = $this->getDescLinkAttribs( empty( $options['title'] ) ?
null : $options['title'], $query );
193 } elseif ( !empty( $options['file-link'] ) ) {
194 $linkAttribs = array( 'href' => $this->file
->getURL() );
196 $linkAttribs = false;
202 'width' => $this->width
,
203 'height' => $this->height
,
205 if ( !empty( $options['valign'] ) ) {
206 $attribs['style'] = "vertical-align: {$options['valign']}";
208 if ( !empty( $options['img-class'] ) ) {
209 $attribs['class'] = $options['img-class'];
211 return $this->linkWrap( $linkAttribs, Xml
::element( 'img', $attribs ) );
217 * Basic media transform error class
221 class MediaTransformError
extends MediaTransformOutput
{
222 var $htmlMsg, $textMsg, $width, $height, $url, $path;
224 function __construct( $msg, $width, $height /*, ... */ ) {
225 $args = array_slice( func_get_args(), 3 );
226 $htmlArgs = array_map( 'htmlspecialchars', $args );
227 $htmlArgs = array_map( 'nl2br', $htmlArgs );
229 $this->htmlMsg
= wfMessage( $msg )->rawParams( $htmlArgs )->escaped();
230 $this->textMsg
= wfMessage( $msg )->rawParams( $htmlArgs )->text();
231 $this->width
= intval( $width );
232 $this->height
= intval( $height );
237 function toHtml( $options = array() ) {
238 return "<div class=\"MediaTransformError\" style=\"" .
239 "width: {$this->width}px; height: {$this->height}px; display:inline-block;\">" .
245 return $this->textMsg
;
248 function getHtmlMsg() {
249 return $this->htmlMsg
;
258 * Shortcut class for parameter validation errors
262 class TransformParameterError
extends MediaTransformError
{
263 function __construct( $params ) {
264 parent
::__construct( 'thumbnail_error',
265 max( isset( $params['width'] ) ?
$params['width'] : 0, 120 ),
266 max( isset( $params['height'] ) ?
$params['height'] : 0, 120 ),
267 wfMsg( 'thumbnail_invalid_params' ) );