4 * Base class for the output of MediaHandler::doTransform() and File::transform().
8 abstract class MediaTransformOutput
{
9 var $file, $width, $height, $url, $page, $path;
12 * Get the width of the output box
19 * Get the height of the output box
21 function getHeight() {
26 * @return string The thumbnail URL
33 * @return string Destination file path (local filesystem)
40 * Fetch HTML for this transform output
42 * @param array $options Associative array of options. Boolean options
43 * should be indicated with a value of true for true, and false or
46 * alt Alternate text or caption
47 * desc-link Boolean, show a description link
48 * file-link Boolean, show a file download link
49 * valign vertical-align property, if the output is an inline element
50 * img-class Class applied to the <img> tag, if there is such a tag
52 * For images, desc-link and file-link are implemented as a click-through. For
53 * sounds and videos, they may be displayed in other ways.
57 abstract function toHtml( $options = array() );
60 * This will be overridden to return true in error classes
67 * Wrap some XHTML text in an anchor tag with the given attributes
69 protected function linkWrap( $linkAttribs, $contents ) {
71 return Xml
::tags( 'a', $linkAttribs, $contents );
77 function getDescLinkAttribs( $alt = false ) {
78 $query = $this->page ?
( 'page=' . urlencode( $this->page
) ) : '';
79 $title = $this->file
->getTitle();
80 if ( strval( $alt ) === '' ) {
81 $alt = $title->getText();
84 'href' => $this->file
->getTitle()->getLocalURL( $query ),
93 * Media transform output for images
97 class ThumbnailImage
extends MediaTransformOutput
{
99 * @param string $path Filesystem path to the thumb
100 * @param string $url URL path to the thumb
103 function ThumbnailImage( $file, $url, $width, $height, $path = false, $page = false ) {
106 # These should be integers when they get here.
107 # If not, there's a bug somewhere. But let's at
108 # least produce valid HTML code regardless.
109 $this->width
= round( $width );
110 $this->height
= round( $height );
116 * Return HTML <img ... /> tag for the thumbnail, will include
117 * width and height attributes and a blank alt text (as required).
119 * @param array $options Associative array of options. Boolean options
120 * should be indicated with a value of true for true, and false or
123 * alt Alternate text or caption
124 * desc-link Boolean, show a description link
125 * file-link Boolean, show a file download link
126 * valign vertical-align property, if the output is an inline element
127 * img-class Class applied to the <img> tag, if there is such a tag
129 * For images, desc-link and file-link are implemented as a click-through. For
130 * sounds and videos, they may be displayed in other ways.
135 function toHtml( $options = array() ) {
136 if ( count( func_get_args() ) == 2 ) {
137 throw new MWException( __METHOD__
.' called in the old style' );
140 $alt = empty( $options['alt'] ) ?
'' : $options['alt'];
141 if ( !empty( $options['desc-link'] ) ) {
142 $linkAttribs = $this->getDescLinkAttribs( $alt );
143 } elseif ( !empty( $options['file-link'] ) ) {
144 $linkAttribs = array( 'href' => $this->file
->getURL() );
146 $linkAttribs = false;
152 'width' => $this->width
,
153 'height' => $this->height
,
156 if ( !empty( $options['valign'] ) ) {
157 $attribs['style'] = "vertical-align: {$options['valign']}";
159 if ( !empty( $options['img-class'] ) ) {
160 $attribs['class'] = $options['img-class'];
162 return $this->linkWrap( $linkAttribs, Xml
::element( 'img', $attribs ) );
168 * Basic media transform error class
172 class MediaTransformError
extends MediaTransformOutput
{
173 var $htmlMsg, $textMsg, $width, $height, $url, $path;
175 function __construct( $msg, $width, $height /*, ... */ ) {
176 $args = array_slice( func_get_args(), 3 );
177 $htmlArgs = array_map( 'htmlspecialchars', $args );
178 $htmlArgs = array_map( 'nl2br', $htmlArgs );
180 $this->htmlMsg
= wfMsgReplaceArgs( htmlspecialchars( wfMsgGetKey( $msg, true ) ), $htmlArgs );
181 $this->textMsg
= wfMsgReal( $msg, $args );
182 $this->width
= intval( $width );
183 $this->height
= intval( $height );
188 function toHtml( $options = array() ) {
189 return "<table class=\"MediaTransformError\" style=\"" .
190 "width: {$this->width}px; height: {$this->height}px;\"><tr><td>" .
192 "</td></tr></table>";
196 return $this->textMsg
;
199 function getHtmlMsg() {
200 return $this->htmlMsg
;
209 * Shortcut class for parameter validation errors
213 class TransformParameterError
extends MediaTransformError
{
214 function __construct( $params ) {
215 parent
::__construct( 'thumbnail_error',
216 max( isset( $params['width'] ) ?
$params['width'] : 0, 180 ),
217 max( isset( $params['height'] ) ?
$params['height'] : 0, 180 ),
218 wfMsg( 'thumbnail_invalid_params' ) );