Localisation updates for core and extension messages from translatewiki.net (2011...
[mediawiki.git] / includes / media / MediaTransformOutput.php
blobc441f06c92118605a4448849c038763f0202e526
1 <?php
2 /**
3 * Base class for the output of file transformation methods.
5 * @file
6 * @ingroup Media
7 */
9 /**
10 * Base class for the output of MediaHandler::doTransform() and File::transform().
12 * @ingroup Media
14 abstract class MediaTransformOutput {
15 var $file, $width, $height, $url, $page, $path;
17 /**
18 * Get the width of the output box
20 function getWidth() {
21 return $this->width;
24 /**
25 * Get the height of the output box
27 function getHeight() {
28 return $this->height;
31 /**
32 * @return string The thumbnail URL
34 function getUrl() {
35 return $this->url;
38 /**
39 * @return String: destination file path (local filesystem)
41 function getPath() {
42 return $this->path;
45 /**
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
50 * absent for false.
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.
63 * @return string
65 abstract function toHtml( $options = array() );
67 /**
68 * This will be overridden to return true in error classes
70 function isError() {
71 return false;
74 /**
75 * Wrap some XHTML text in an anchor tag with the given attributes
77 protected function linkWrap( $linkAttribs, $contents ) {
78 if ( $linkAttribs ) {
79 return Xml::tags( 'a', $linkAttribs, $contents );
80 } else {
81 return $contents;
85 function getDescLinkAttribs( $title = null, $params = '' ) {
86 $query = $this->page ? ( 'page=' . urlencode( $this->page ) ) : '';
87 if( $params ) {
88 $query .= $query ? '&'.$params : $params;
90 $attribs = array(
91 'href' => $this->file->getTitle()->getLocalURL( $query ),
92 'class' => 'image',
94 if ( $title ) {
95 $attribs['title'] = $title;
97 return $attribs;
103 * Media transform output for images
105 * @ingroup Media
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
116 * @private
118 function __construct( $file, $url, $width, $height, $path = false, $page = false ) {
119 $this->file = $file;
120 $this->url = $url;
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 );
126 $this->path = $path;
127 $this->page = $page;
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
136 * absent for false.
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.
152 * @return string
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() );
181 } else {
182 $linkAttribs = false;
185 $attribs = array(
186 'alt' => $alt,
187 'src' => $this->url,
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
205 * @ingroup Media
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 );
219 $this->url = false;
220 $this->path = false;
223 function toHtml( $options = array() ) {
224 return "<div class=\"MediaTransformError\" style=\"" .
225 "width: {$this->width}px; height: {$this->height}px; display:inline-block;\">" .
226 $this->htmlMsg .
227 "</div>";
230 function toText() {
231 return $this->textMsg;
234 function getHtmlMsg() {
235 return $this->htmlMsg;
238 function isError() {
239 return true;
244 * Shortcut class for parameter validation errors
246 * @ingroup Media
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' ) );