In SpecialUploadStash:
[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 was actually made.
90 * This will return false if there was an error, the
91 * thumnail 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 return ( !$this->isError() && $this->path );
99 }
100
101 /**
102 * Check if the output thumbnail file is the same as the source.
103 * This can occur if the requested width was bigger than the source.
104 *
105 * @return Bool
106 */
107 public function fileIsSource() {
108 return ( !$this->isError() && $this->path === $this->file->getLocalRefPath() );
109 }
110
111 /**
112 * Get the path of a file system copy of the thumbnail
113 *
114 * @return string|false Returns false if there isn't one
115 */
116 public function getLocalCopyPath() {
117 return $this->path;
118 }
119
120 /**
121 * Stream the file if there were no errors
122 *
123 * @param $headers Array Additional HTTP headers to send on success
124 * @return Bool success
125 */
126 public function streamFile( $headers = array() ) {
127 return $this->path && StreamFile::stream( $this->path, $headers );
128 }
129
130 /**
131 * Wrap some XHTML text in an anchor tag with the given attributes
132 *
133 * @param $linkAttribs array
134 * @param $contents string
135 *
136 * @return string
137 */
138 protected function linkWrap( $linkAttribs, $contents ) {
139 if ( $linkAttribs ) {
140 return Xml::tags( 'a', $linkAttribs, $contents );
141 } else {
142 return $contents;
143 }
144 }
145
146 /**
147 * @param $title string
148 * @param $params array
149 * @return array
150 */
151 public function getDescLinkAttribs( $title = null, $params = '' ) {
152 $query = $this->page ? ( 'page=' . urlencode( $this->page ) ) : '';
153 if( $params ) {
154 $query .= $query ? '&'.$params : $params;
155 }
156 $attribs = array(
157 'href' => $this->file->getTitle()->getLocalURL( $query ),
158 'class' => 'image',
159 );
160 if ( $title ) {
161 $attribs['title'] = $title;
162 }
163 return $attribs;
164 }
165 }
166
167 /**
168 * Media transform output for images
169 *
170 * @ingroup Media
171 */
172 class ThumbnailImage extends MediaTransformOutput {
173
174 /**
175 * @param $file File object
176 * @param $url String: URL path to the thumb
177 * @param $width Integer: file's width
178 * @param $height Integer: file's height
179 * @param $path String: filesystem path to the thumb
180 * @param $page Integer: page number, for multipage files
181 * @private
182 */
183 function __construct( $file, $url, $width, $height, $path = false, $page = false ) {
184 $this->file = $file;
185 $this->url = $url;
186 # These should be integers when they get here.
187 # If not, there's a bug somewhere. But let's at
188 # least produce valid HTML code regardless.
189 $this->width = round( $width );
190 $this->height = round( $height );
191 $this->path = $path;
192 $this->page = $page;
193 }
194
195 /**
196 * Return HTML <img ... /> tag for the thumbnail, will include
197 * width and height attributes and a blank alt text (as required).
198 *
199 * @param $options array Associative array of options. Boolean options
200 * should be indicated with a value of true for true, and false or
201 * absent for false.
202 *
203 * alt HTML alt attribute
204 * title HTML title attribute
205 * desc-link Boolean, show a description link
206 * file-link Boolean, show a file download link
207 * valign vertical-align property, if the output is an inline element
208 * img-class Class applied to the \<img\> tag, if there is such a tag
209 * desc-query String, description link query params
210 * custom-url-link Custom URL to link to
211 * custom-title-link Custom Title object to link to
212 * custom target-link Value of the target attribute, for custom-target-link
213 *
214 * For images, desc-link and file-link are implemented as a click-through. For
215 * sounds and videos, they may be displayed in other ways.
216 *
217 * @return string
218 */
219 function toHtml( $options = array() ) {
220 if ( count( func_get_args() ) == 2 ) {
221 throw new MWException( __METHOD__ .' called in the old style' );
222 }
223
224 $alt = empty( $options['alt'] ) ? '' : $options['alt'];
225
226 $query = empty( $options['desc-query'] ) ? '' : $options['desc-query'];
227
228 if ( !empty( $options['custom-url-link'] ) ) {
229 $linkAttribs = array( 'href' => $options['custom-url-link'] );
230 if ( !empty( $options['title'] ) ) {
231 $linkAttribs['title'] = $options['title'];
232 }
233 if ( !empty( $options['custom-target-link'] ) ) {
234 $linkAttribs['target'] = $options['custom-target-link'];
235 }
236 } elseif ( !empty( $options['custom-title-link'] ) ) {
237 $title = $options['custom-title-link'];
238 $linkAttribs = array(
239 'href' => $title->getLinkURL(),
240 'title' => empty( $options['title'] ) ? $title->getFullText() : $options['title']
241 );
242 } elseif ( !empty( $options['desc-link'] ) ) {
243 $linkAttribs = $this->getDescLinkAttribs( empty( $options['title'] ) ? null : $options['title'], $query );
244 } elseif ( !empty( $options['file-link'] ) ) {
245 $linkAttribs = array( 'href' => $this->file->getURL() );
246 } else {
247 $linkAttribs = false;
248 }
249
250 $attribs = array(
251 'alt' => $alt,
252 'src' => $this->url,
253 'width' => $this->width,
254 'height' => $this->height,
255 );
256 if ( !empty( $options['valign'] ) ) {
257 $attribs['style'] = "vertical-align: {$options['valign']}";
258 }
259 if ( !empty( $options['img-class'] ) ) {
260 $attribs['class'] = $options['img-class'];
261 }
262 return $this->linkWrap( $linkAttribs, Xml::element( 'img', $attribs ) );
263 }
264
265 }
266
267 /**
268 * Basic media transform error class
269 *
270 * @ingroup Media
271 */
272 class MediaTransformError extends MediaTransformOutput {
273 var $htmlMsg, $textMsg, $width, $height, $url, $path;
274
275 function __construct( $msg, $width, $height /*, ... */ ) {
276 $args = array_slice( func_get_args(), 3 );
277 $htmlArgs = array_map( 'htmlspecialchars', $args );
278 $htmlArgs = array_map( 'nl2br', $htmlArgs );
279
280 $this->htmlMsg = wfMessage( $msg )->rawParams( $htmlArgs )->escaped();
281 $this->textMsg = wfMessage( $msg )->rawParams( $htmlArgs )->text();
282 $this->width = intval( $width );
283 $this->height = intval( $height );
284 $this->url = false;
285 $this->path = false;
286 }
287
288 function toHtml( $options = array() ) {
289 return "<div class=\"MediaTransformError\" style=\"" .
290 "width: {$this->width}px; height: {$this->height}px; display:inline-block;\">" .
291 $this->htmlMsg .
292 "</div>";
293 }
294
295 function toText() {
296 return $this->textMsg;
297 }
298
299 function getHtmlMsg() {
300 return $this->htmlMsg;
301 }
302
303 function isError() {
304 return true;
305 }
306 }
307
308 /**
309 * Shortcut class for parameter validation errors
310 *
311 * @ingroup Media
312 */
313 class TransformParameterError extends MediaTransformError {
314 function __construct( $params ) {
315 parent::__construct( 'thumbnail_error',
316 max( isset( $params['width'] ) ? $params['width'] : 0, 120 ),
317 max( isset( $params['height'] ) ? $params['height'] : 0, 120 ),
318 wfMsg( 'thumbnail_invalid_params' ) );
319 }
320 }