Undo previous commit. Schema changes are a no-no without permission first :-)
[mediawiki.git] / includes / MediaTransformOutput.php
blob9e94f06b9360b9e958fddfe74ec5b9bb19d29506
1 <?php
2 /**
3 * @file
4 * @ingroup Media
5 */
7 /**
8 * Base class for the output of MediaHandler::doTransform() and File::transform().
10 * @ingroup Media
12 abstract class MediaTransformOutput {
13 var $file, $width, $height, $url, $page, $path;
15 /**
16 * Get the width of the output box
18 function getWidth() {
19 return $this->width;
22 /**
23 * Get the height of the output box
25 function getHeight() {
26 return $this->height;
29 /**
30 * @return string The thumbnail URL
32 function getUrl() {
33 return $this->url;
36 /**
37 * @return string Destination file path (local filesystem)
39 function getPath() {
40 return $this->path;
43 /**
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
48 * absent for false.
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.
59 * @return string
61 abstract function toHtml( $options = array() );
63 /**
64 * This will be overridden to return true in error classes
66 function isError() {
67 return false;
70 /**
71 * Wrap some XHTML text in an anchor tag with the given attributes
73 protected function linkWrap( $linkAttribs, $contents ) {
74 if ( $linkAttribs ) {
75 return Xml::tags( 'a', $linkAttribs, $contents );
76 } else {
77 return $contents;
81 function getDescLinkAttribs( $alt = false, $params = '' ) {
82 $query = $this->page ? ( 'page=' . urlencode( $this->page ) ) : '';
83 if( $params ) {
84 $query .= $query ? '&'.$params : $params;
86 $title = $this->file->getTitle();
87 if ( strval( $alt ) === '' ) {
88 $alt = $title->getText();
90 return array(
91 'href' => $this->file->getTitle()->getLocalURL( $query ),
92 'class' => 'image',
93 'title' => $alt
99 /**
100 * Media transform output for images
102 * @ingroup Media
104 class ThumbnailImage extends MediaTransformOutput {
106 * @param string $path Filesystem path to the thumb
107 * @param string $url URL path to the thumb
108 * @private
110 function ThumbnailImage( $file, $url, $width, $height, $path = false, $page = false ) {
111 $this->file = $file;
112 $this->url = $url;
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 );
118 $this->path = $path;
119 $this->page = $page;
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
128 * absent for false.
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.
140 * @return string
141 * @public
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() );
154 } else {
155 $linkAttribs = false;
158 $attribs = array(
159 'alt' => $alt,
160 'src' => $this->url,
161 'width' => $this->width,
162 'height' => $this->height,
163 'border' => 0,
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
179 * @ingroup Media
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 );
193 $this->url = false;
194 $this->path = false;
197 function toHtml( $options = array() ) {
198 return "<table class=\"MediaTransformError\" style=\"" .
199 "width: {$this->width}px; height: {$this->height}px;\"><tr><td>" .
200 $this->htmlMsg .
201 "</td></tr></table>";
204 function toText() {
205 return $this->textMsg;
208 function getHtmlMsg() {
209 return $this->htmlMsg;
212 function isError() {
213 return true;
218 * Shortcut class for parameter validation errors
220 * @ingroup Media
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' ) );