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