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
{
15 var $file, $width, $height, $url, $page, $path;
18 * Get the width of the output box
25 * Get the height of the output box
27 function getHeight() {
32 * @return string The thumbnail URL
39 * @return String: destination file path (local filesystem)
46 * Fetch HTML for this transform output
48 * @param $options Associative array of options. Boolean options
49 * should be indicated with a value of true for true, and false or
52 * alt Alternate text or caption
53 * desc-link Boolean, show a description link
54 * file-link Boolean, show a file download link
55 * custom-url-link Custom URL to link to
56 * custom-title-link Custom Title object to link to
57 * valign vertical-align property, if the output is an inline element
58 * img-class Class applied to the <img> tag, if there is such a tag
60 * For images, desc-link and file-link are implemented as a click-through. For
61 * sounds and videos, they may be displayed in other ways.
65 abstract function toHtml( $options = array() );
68 * This will be overridden to return true in error classes
75 * Wrap some XHTML text in an anchor tag with the given attributes
77 protected function linkWrap( $linkAttribs, $contents ) {
79 return Xml
::tags( 'a', $linkAttribs, $contents );
85 function getDescLinkAttribs( $title = null, $params = '' ) {
86 $query = $this->page ?
( 'page=' . urlencode( $this->page
) ) : '';
88 $query .= $query ?
'&'.$params : $params;
91 'href' => $this->file
->getTitle()->getLocalURL( $query ),
95 $attribs['title'] = $title;
103 * Media transform output for images
107 class ThumbnailImage
extends MediaTransformOutput
{
110 * @param $file File object
111 * @param $url String: URL path to the thumb
112 * @param $width Integer: file's width
113 * @param $height Integer: file's height
114 * @param $path String: filesystem path to the thumb
115 * @param $page Integer: page number, for multipage files
118 function __construct( $file, $url, $width, $height, $path = false, $page = false ) {
121 # These should be integers when they get here.
122 # If not, there's a bug somewhere. But let's at
123 # least produce valid HTML code regardless.
124 $this->width
= round( $width );
125 $this->height
= round( $height );
131 * Return HTML <img ... /> tag for the thumbnail, will include
132 * width and height attributes and a blank alt text (as required).
134 * @param $options Associative array of options. Boolean options
135 * should be indicated with a value of true for true, and false or
138 * alt HTML alt attribute
139 * title HTML title attribute
140 * desc-link Boolean, show a description link
141 * file-link Boolean, show a file download link
142 * valign vertical-align property, if the output is an inline element
143 * img-class Class applied to the \<img\> tag, if there is such a tag
144 * desc-query String, description link query params
145 * custom-url-link Custom URL to link to
146 * custom-title-link Custom Title object to link to
147 * custom target-link Value of the target attribute, for custom-target-link
149 * For images, desc-link and file-link are implemented as a click-through. For
150 * sounds and videos, they may be displayed in other ways.
154 function toHtml( $options = array() ) {
155 if ( count( func_get_args() ) == 2 ) {
156 throw new MWException( __METHOD__
.' called in the old style' );
159 $alt = empty( $options['alt'] ) ?
'' : $options['alt'];
161 $query = empty( $options['desc-query'] ) ?
'' : $options['desc-query'];
163 if ( !empty( $options['custom-url-link'] ) ) {
164 $linkAttribs = array( 'href' => $options['custom-url-link'] );
165 if ( !empty( $options['title'] ) ) {
166 $linkAttribs['title'] = $options['title'];
168 if ( !empty( $options['custom-target-link'] ) ) {
169 $linkAttribs['target'] = $options['custom-target-link'];
171 } elseif ( !empty( $options['custom-title-link'] ) ) {
172 $title = $options['custom-title-link'];
173 $linkAttribs = array(
174 'href' => $title->getLinkUrl(),
175 'title' => empty( $options['title'] ) ?
$title->getFullText() : $options['title']
177 } elseif ( !empty( $options['desc-link'] ) ) {
178 $linkAttribs = $this->getDescLinkAttribs( empty( $options['title'] ) ?
null : $options['title'], $query );
179 } elseif ( !empty( $options['file-link'] ) ) {
180 $linkAttribs = array( 'href' => $this->file
->getURL() );
182 $linkAttribs = false;
188 'width' => $this->width
,
189 'height' => $this->height
,
191 if ( !empty( $options['valign'] ) ) {
192 $attribs['style'] = "vertical-align: {$options['valign']}";
194 if ( !empty( $options['img-class'] ) ) {
195 $attribs['class'] = $options['img-class'];
197 return $this->linkWrap( $linkAttribs, Xml
::element( 'img', $attribs ) );
203 * Basic media transform error class
207 class MediaTransformError
extends MediaTransformOutput
{
208 var $htmlMsg, $textMsg, $width, $height, $url, $path;
210 function __construct( $msg, $width, $height /*, ... */ ) {
211 $args = array_slice( func_get_args(), 3 );
212 $htmlArgs = array_map( 'htmlspecialchars', $args );
213 $htmlArgs = array_map( 'nl2br', $htmlArgs );
215 $this->htmlMsg
= wfMsgReplaceArgs( htmlspecialchars( wfMsgGetKey( $msg, true ) ), $htmlArgs );
216 $this->textMsg
= wfMsgReal( $msg, $args );
217 $this->width
= intval( $width );
218 $this->height
= intval( $height );
223 function toHtml( $options = array() ) {
224 return "<div class=\"MediaTransformError\" style=\"" .
225 "width: {$this->width}px; height: {$this->height}px; display:inline-block;\">" .
231 return $this->textMsg
;
234 function getHtmlMsg() {
235 return $this->htmlMsg
;
244 * Shortcut class for parameter validation errors
248 class TransformParameterError
extends MediaTransformError
{
249 function __construct( $params ) {
250 parent
::__construct( 'thumbnail_error',
251 max( isset( $params['width'] ) ?
$params['width'] : 0, 120 ),
252 max( isset( $params['height'] ) ?
$params['height'] : 0, 120 ),
253 wfMsg( 'thumbnail_invalid_params' ) );