8 * Base class for the output of MediaHandler::doTransform() and File::transform().
12 abstract class MediaTransformOutput
{
13 var $file, $width, $height, $url, $page, $path;
16 * Get the width of the output box
23 * Get the height of the output box
25 function getHeight() {
30 * @return string The thumbnail URL
37 * @return string Destination file path (local filesystem)
44 * Fetch HTML for this transform output
46 * @param array $options 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 * valign vertical-align property, if the output is an inline element
54 * img-class Class applied to the <img> tag, if there is such a tag
56 * For images, desc-link and file-link are implemented as a click-through. For
57 * sounds and videos, they may be displayed in other ways.
61 abstract function toHtml( $options = array() );
64 * This will be overridden to return true in error classes
71 * Wrap some XHTML text in an anchor tag with the given attributes
73 protected function linkWrap( $linkAttribs, $contents ) {
75 return Xml
::tags( 'a', $linkAttribs, $contents );
81 function getDescLinkAttribs( $alt = false, $params = '' ) {
82 $query = $this->page ?
( 'page=' . urlencode( $this->page
) ) : '';
84 $query .= $query ?
'&'.$params : $params;
86 $title = $this->file
->getTitle();
87 if ( strval( $alt ) === '' ) {
88 $alt = $title->getText();
91 'href' => $this->file
->getTitle()->getLocalURL( $query ),
100 * Media transform output for images
104 class ThumbnailImage
extends MediaTransformOutput
{
106 * @param string $path Filesystem path to the thumb
107 * @param string $url URL path to the thumb
110 function ThumbnailImage( $file, $url, $width, $height, $path = false, $page = false ) {
113 # These should be integers when they get here.
114 # If not, there's a bug somewhere. But let's at
115 # least produce valid HTML code regardless.
116 $this->width
= round( $width );
117 $this->height
= round( $height );
123 * Return HTML <img ... /> tag for the thumbnail, will include
124 * width and height attributes and a blank alt text (as required).
126 * @param array $options Associative array of options. Boolean options
127 * should be indicated with a value of true for true, and false or
130 * alt Alternate text or caption
131 * desc-link Boolean, show a description link
132 * file-link Boolean, show a file download link
133 * valign vertical-align property, if the output is an inline element
134 * img-class Class applied to the <img> tag, if there is such a tag
135 * desc-query String, description link query params
137 * For images, desc-link and file-link are implemented as a click-through. For
138 * sounds and videos, they may be displayed in other ways.
143 function toHtml( $options = array() ) {
144 if ( count( func_get_args() ) == 2 ) {
145 throw new MWException( __METHOD__
.' called in the old style' );
148 $alt = empty( $options['alt'] ) ?
'' : $options['alt'];
149 $query = empty($options['desc-query']) ?
'' : $options['desc-query'];
150 if ( !empty( $options['desc-link'] ) ) {
151 $linkAttribs = $this->getDescLinkAttribs( $alt, $query );
152 } elseif ( !empty( $options['file-link'] ) ) {
153 $linkAttribs = array( 'href' => $this->file
->getURL() );
155 $linkAttribs = false;
161 'width' => $this->width
,
162 'height' => $this->height
,
165 if ( !empty( $options['valign'] ) ) {
166 $attribs['style'] = "vertical-align: {$options['valign']}";
168 if ( !empty( $options['img-class'] ) ) {
169 $attribs['class'] = $options['img-class'];
171 return $this->linkWrap( $linkAttribs, Xml
::element( 'img', $attribs ) );
177 * Basic media transform error class
181 class MediaTransformError
extends MediaTransformOutput
{
182 var $htmlMsg, $textMsg, $width, $height, $url, $path;
184 function __construct( $msg, $width, $height /*, ... */ ) {
185 $args = array_slice( func_get_args(), 3 );
186 $htmlArgs = array_map( 'htmlspecialchars', $args );
187 $htmlArgs = array_map( 'nl2br', $htmlArgs );
189 $this->htmlMsg
= wfMsgReplaceArgs( htmlspecialchars( wfMsgGetKey( $msg, true ) ), $htmlArgs );
190 $this->textMsg
= wfMsgReal( $msg, $args );
191 $this->width
= intval( $width );
192 $this->height
= intval( $height );
197 function toHtml( $options = array() ) {
198 return "<table class=\"MediaTransformError\" style=\"" .
199 "width: {$this->width}px; height: {$this->height}px;\"><tr><td>" .
201 "</td></tr></table>";
205 return $this->textMsg
;
208 function getHtmlMsg() {
209 return $this->htmlMsg
;
218 * Shortcut class for parameter validation errors
222 class TransformParameterError
extends MediaTransformError
{
223 function __construct( $params ) {
224 parent
::__construct( 'thumbnail_error',
225 max( isset( $params['width'] ) ?
$params['width'] : 0, 180 ),
226 max( isset( $params['height'] ) ?
$params['height'] : 0, 180 ),
227 wfMsg( 'thumbnail_invalid_params' ) );