Merged FileBackend branch. Manually avoiding merging the many prop-only changes SVN...
[lhc/web/wiklou.git] / includes / media / MediaTransformOutput.php
1 <?php
2 /**
3 * Base class for the output of file transformation methods.
4 *
5 * @file
6 * @ingroup Media
7 */
8
9 /**
10 * Base class for the output of MediaHandler::doTransform() and File::transform().
11 *
12 * @ingroup Media
13 */
14 abstract class MediaTransformOutput {
15 /**
16 * @var File
17 */
18 var $file;
19
20 var $width, $height, $url, $page, $path;
21
22 /**
23 * Get the width of the output box
24 */
25 public function getWidth() {
26 return $this->width;
27 }
28
29 /**
30 * Get the height of the output box
31 */
32 public function getHeight() {
33 return $this->height;
34 }
35
36 /**
37 * @return string The thumbnail URL
38 */
39 public function getUrl() {
40 return $this->url;
41 }
42
43 /**
44 * Fetch HTML for this transform output
45 *
46 * @param $options array Associative array of options. Boolean options
47 * should be indicated with a value of true for true, and false or
48 * absent for false.
49 *
50 * alt Alternate text or caption
51 * desc-link Boolean, show a description link
52 * file-link Boolean, show a file download link
53 * custom-url-link Custom URL to link to
54 * custom-title-link Custom Title object to link to
55 * valign vertical-align property, if the output is an inline element
56 * img-class Class applied to the <img> tag, if there is such a tag
57 *
58 * For images, desc-link and file-link are implemented as a click-through. For
59 * sounds and videos, they may be displayed in other ways.
60 *
61 * @return string
62 */
63 abstract public function toHtml( $options = array() );
64
65 /**
66 * This will be overridden to return true in error classes
67 */
68 public function isError() {
69 return false;
70 }
71
72 /**
73 * Check if an output thumbnail file was actually made.
74 * This will return false if there was an error, the
75 * thumnail is to be handled client-side only, or if
76 * transformation was deferred via TRANSFORM_LATER.
77 *
78 * @return Bool
79 */
80 public function hasFile() {
81 // If TRANSFORM_LATER, $this->path will be false
82 return ( !$this->isError() && $this->path );
83 }
84
85 /**
86 * Check if the output thumbnail file is the same as the source.
87 * This can occur if the requested width was bigger than the source.
88 *
89 * @return Bool
90 */
91 public function fileIsSource() {
92 return ( !$this->isError() && $this->path === $this->file->getLocalRefPath() );
93 }
94
95 /**
96 * Get the path of a file system copy of the thumbnail
97 *
98 * @return string|false Returns false if there isn't one
99 */
100 public function getLocalCopyPath() {
101 return $this->path;
102 }
103
104 /**
105 * Stream the file if there were no errors
106 *
107 * @param $headers Array Additional HTTP headers to send on success
108 * @return Bool success
109 */
110 public function streamFile( $headers = array() ) {
111 return $this->path && StreamFile::stream( $this->path, $headers );
112 }
113
114 /**
115 * Wrap some XHTML text in an anchor tag with the given attributes
116 *
117 * @param $linkAttribs array
118 * @param $contents string
119 *
120 * @return string
121 */
122 protected function linkWrap( $linkAttribs, $contents ) {
123 if ( $linkAttribs ) {
124 return Xml::tags( 'a', $linkAttribs, $contents );
125 } else {
126 return $contents;
127 }
128 }
129
130 /**
131 * @param $title string
132 * @param $params array
133 * @return array
134 */
135 public function getDescLinkAttribs( $title = null, $params = '' ) {
136 $query = $this->page ? ( 'page=' . urlencode( $this->page ) ) : '';
137 if( $params ) {
138 $query .= $query ? '&'.$params : $params;
139 }
140 $attribs = array(
141 'href' => $this->file->getTitle()->getLocalURL( $query ),
142 'class' => 'image',
143 );
144 if ( $title ) {
145 $attribs['title'] = $title;
146 }
147 return $attribs;
148 }
149 }
150
151 /**
152 * Media transform output for images
153 *
154 * @ingroup Media
155 */
156 class ThumbnailImage extends MediaTransformOutput {
157
158 /**
159 * @param $file File object
160 * @param $url String: URL path to the thumb
161 * @param $width Integer: file's width
162 * @param $height Integer: file's height
163 * @param $path String: filesystem path to the thumb
164 * @param $page Integer: page number, for multipage files
165 * @private
166 */
167 function __construct( $file, $url, $width, $height, $path = false, $page = false ) {
168 $this->file = $file;
169 $this->url = $url;
170 # These should be integers when they get here.
171 # If not, there's a bug somewhere. But let's at
172 # least produce valid HTML code regardless.
173 $this->width = round( $width );
174 $this->height = round( $height );
175 $this->path = $path;
176 $this->page = $page;
177 }
178
179 /**
180 * Return HTML <img ... /> tag for the thumbnail, will include
181 * width and height attributes and a blank alt text (as required).
182 *
183 * @param $options array Associative array of options. Boolean options
184 * should be indicated with a value of true for true, and false or
185 * absent for false.
186 *
187 * alt HTML alt attribute
188 * title HTML title attribute
189 * desc-link Boolean, show a description link
190 * file-link Boolean, show a file download link
191 * valign vertical-align property, if the output is an inline element
192 * img-class Class applied to the \<img\> tag, if there is such a tag
193 * desc-query String, description link query params
194 * custom-url-link Custom URL to link to
195 * custom-title-link Custom Title object to link to
196 * custom target-link Value of the target attribute, for custom-target-link
197 *
198 * For images, desc-link and file-link are implemented as a click-through. For
199 * sounds and videos, they may be displayed in other ways.
200 *
201 * @return string
202 */
203 function toHtml( $options = array() ) {
204 if ( count( func_get_args() ) == 2 ) {
205 throw new MWException( __METHOD__ .' called in the old style' );
206 }
207
208 $alt = empty( $options['alt'] ) ? '' : $options['alt'];
209
210 $query = empty( $options['desc-query'] ) ? '' : $options['desc-query'];
211
212 if ( !empty( $options['custom-url-link'] ) ) {
213 $linkAttribs = array( 'href' => $options['custom-url-link'] );
214 if ( !empty( $options['title'] ) ) {
215 $linkAttribs['title'] = $options['title'];
216 }
217 if ( !empty( $options['custom-target-link'] ) ) {
218 $linkAttribs['target'] = $options['custom-target-link'];
219 }
220 } elseif ( !empty( $options['custom-title-link'] ) ) {
221 $title = $options['custom-title-link'];
222 $linkAttribs = array(
223 'href' => $title->getLinkURL(),
224 'title' => empty( $options['title'] ) ? $title->getFullText() : $options['title']
225 );
226 } elseif ( !empty( $options['desc-link'] ) ) {
227 $linkAttribs = $this->getDescLinkAttribs( empty( $options['title'] ) ? null : $options['title'], $query );
228 } elseif ( !empty( $options['file-link'] ) ) {
229 $linkAttribs = array( 'href' => $this->file->getURL() );
230 } else {
231 $linkAttribs = false;
232 }
233
234 $attribs = array(
235 'alt' => $alt,
236 'src' => $this->url,
237 'width' => $this->width,
238 'height' => $this->height,
239 );
240 if ( !empty( $options['valign'] ) ) {
241 $attribs['style'] = "vertical-align: {$options['valign']}";
242 }
243 if ( !empty( $options['img-class'] ) ) {
244 $attribs['class'] = $options['img-class'];
245 }
246 return $this->linkWrap( $linkAttribs, Xml::element( 'img', $attribs ) );
247 }
248
249 }
250
251 /**
252 * Basic media transform error class
253 *
254 * @ingroup Media
255 */
256 class MediaTransformError extends MediaTransformOutput {
257 var $htmlMsg, $textMsg, $width, $height, $url, $path;
258
259 function __construct( $msg, $width, $height /*, ... */ ) {
260 $args = array_slice( func_get_args(), 3 );
261 $htmlArgs = array_map( 'htmlspecialchars', $args );
262 $htmlArgs = array_map( 'nl2br', $htmlArgs );
263
264 $this->htmlMsg = wfMessage( $msg )->rawParams( $htmlArgs )->escaped();
265 $this->textMsg = wfMessage( $msg )->rawParams( $htmlArgs )->text();
266 $this->width = intval( $width );
267 $this->height = intval( $height );
268 $this->url = false;
269 $this->path = false;
270 }
271
272 function toHtml( $options = array() ) {
273 return "<div class=\"MediaTransformError\" style=\"" .
274 "width: {$this->width}px; height: {$this->height}px; display:inline-block;\">" .
275 $this->htmlMsg .
276 "</div>";
277 }
278
279 function toText() {
280 return $this->textMsg;
281 }
282
283 function getHtmlMsg() {
284 return $this->htmlMsg;
285 }
286
287 function isError() {
288 return true;
289 }
290 }
291
292 /**
293 * Shortcut class for parameter validation errors
294 *
295 * @ingroup Media
296 */
297 class TransformParameterError extends MediaTransformError {
298 function __construct( $params ) {
299 parent::__construct( 'thumbnail_error',
300 max( isset( $params['width'] ) ? $params['width'] : 0, 120 ),
301 max( isset( $params['height'] ) ? $params['height'] : 0, 120 ),
302 wfMsg( 'thumbnail_invalid_params' ) );
303 }
304 }