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 ThumbnailImage( $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
148 * For images, desc-link and file-link are implemented as a click-through. For
149 * sounds and videos, they may be displayed in other ways.
153 function toHtml( $options = array() ) {
154 if ( count( func_get_args() ) == 2 ) {
155 throw new MWException( __METHOD__
.' called in the old style' );
158 $alt = empty( $options['alt'] ) ?
'' : $options['alt'];
160 $query = empty( $options['desc-query'] ) ?
'' : $options['desc-query'];
162 if ( !empty( $options['custom-url-link'] ) ) {
163 $linkAttribs = array( 'href' => $options['custom-url-link'] );
164 if ( !empty( $options['title'] ) ) {
165 $linkAttribs['title'] = $options['title'];
167 } elseif ( !empty( $options['custom-title-link'] ) ) {
168 $title = $options['custom-title-link'];
169 $linkAttribs = array(
170 'href' => $title->getLinkUrl(),
171 'title' => empty( $options['title'] ) ?
$title->getFullText() : $options['title']
173 } elseif ( !empty( $options['desc-link'] ) ) {
174 $linkAttribs = $this->getDescLinkAttribs( empty( $options['title'] ) ?
null : $options['title'], $query );
175 } elseif ( !empty( $options['file-link'] ) ) {
176 $linkAttribs = array( 'href' => $this->file
->getURL() );
178 $linkAttribs = false;
184 'width' => $this->width
,
185 'height' => $this->height
,
187 if ( !empty( $options['valign'] ) ) {
188 $attribs['style'] = "vertical-align: {$options['valign']}";
190 if ( !empty( $options['img-class'] ) ) {
191 $attribs['class'] = $options['img-class'];
193 return $this->linkWrap( $linkAttribs, Xml
::element( 'img', $attribs ) );
199 * Basic media transform error class
203 class MediaTransformError
extends MediaTransformOutput
{
204 var $htmlMsg, $textMsg, $width, $height, $url, $path;
206 function __construct( $msg, $width, $height /*, ... */ ) {
207 $args = array_slice( func_get_args(), 3 );
208 $htmlArgs = array_map( 'htmlspecialchars', $args );
209 $htmlArgs = array_map( 'nl2br', $htmlArgs );
211 $this->htmlMsg
= wfMsgReplaceArgs( htmlspecialchars( wfMsgGetKey( $msg, true ) ), $htmlArgs );
212 $this->textMsg
= wfMsgReal( $msg, $args );
213 $this->width
= intval( $width );
214 $this->height
= intval( $height );
219 function toHtml( $options = array() ) {
220 return "<div class=\"MediaTransformError\" style=\"" .
221 "width: {$this->width}px; height: {$this->height}px; display:inline-block;\">" .
227 return $this->textMsg
;
230 function getHtmlMsg() {
231 return $this->htmlMsg
;
240 * Shortcut class for parameter validation errors
244 class TransformParameterError
extends MediaTransformError
{
245 function __construct( $params ) {
246 parent
::__construct( 'thumbnail_error',
247 max( isset( $params['width'] ) ?
$params['width'] : 0, 120 ),
248 max( isset( $params['height'] ) ?
$params['height'] : 0, 120 ),
249 wfMsg( 'thumbnail_invalid_params' ) );