Merge "Update some minor type hints"
[lhc/web/wiklou.git] / includes / media / MediaTransformOutput.php
1 <?php
2 /**
3 * Base class for the output of file transformation methods.
4 *
5 * This program is free software; you can redistribute it and/or modify
6 * it under the terms of the GNU General Public License as published by
7 * the Free Software Foundation; either version 2 of the License, or
8 * (at your option) any later version.
9 *
10 * This program is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 * GNU General Public License for more details.
14 *
15 * You should have received a copy of the GNU General Public License along
16 * with this program; if not, write to the Free Software Foundation, Inc.,
17 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
18 * http://www.gnu.org/copyleft/gpl.html
19 *
20 * @file
21 * @ingroup Media
22 */
23
24 /**
25 * Base class for the output of MediaHandler::doTransform() and File::transform().
26 *
27 * @ingroup Media
28 */
29 abstract class MediaTransformOutput {
30 /** @var array Associative array mapping optional supplementary image files
31 * from pixel density (eg 1.5 or 2) to additional URLs.
32 */
33 public $responsiveUrls = [];
34
35 /** @var File */
36 protected $file;
37
38 /** @var int Image width */
39 protected $width;
40
41 /** @var int Image height */
42 protected $height;
43
44 /** @var string URL path to the thumb */
45 protected $url;
46
47 /** @var bool|string */
48 protected $page;
49
50 /** @var bool|string Filesystem path to the thumb */
51 protected $path;
52
53 /** @var bool|string Language code, false if not set */
54 protected $lang;
55
56 /** @var bool|string Permanent storage path */
57 protected $storagePath = false;
58
59 /**
60 * @return int Width of the output box
61 */
62 public function getWidth() {
63 return $this->width;
64 }
65
66 /**
67 * @return int Height of the output box
68 */
69 public function getHeight() {
70 return $this->height;
71 }
72
73 /**
74 * @return File
75 */
76 public function getFile() {
77 return $this->file;
78 }
79
80 /**
81 * Get the final extension of the thumbnail.
82 * Returns false for scripted transformations.
83 * @return string|bool
84 */
85 public function getExtension() {
86 return $this->path ? FileBackend::extensionFromPath( $this->path ) : false;
87 }
88
89 /**
90 * @return string|bool The thumbnail URL
91 */
92 public function getUrl() {
93 return $this->url;
94 }
95
96 /**
97 * @return string|bool The permanent thumbnail storage path
98 */
99 public function getStoragePath() {
100 return $this->storagePath;
101 }
102
103 /**
104 * @param string $storagePath The permanent storage path
105 * @return void
106 */
107 public function setStoragePath( $storagePath ) {
108 $this->storagePath = $storagePath;
109 if ( $this->path === false ) {
110 $this->path = $storagePath;
111 }
112 }
113
114 /**
115 * Fetch HTML for this transform output
116 *
117 * @param array $options Associative array of options. Boolean options
118 * should be indicated with a value of true for true, and false or
119 * absent for false.
120 *
121 * alt Alternate text or caption
122 * desc-link Boolean, show a description link
123 * file-link Boolean, show a file download link
124 * custom-url-link Custom URL to link to
125 * custom-title-link Custom Title object to link to
126 * valign vertical-align property, if the output is an inline element
127 * img-class Class applied to the "<img>" tag, if there is such a tag
128 *
129 * For images, desc-link and file-link are implemented as a click-through. For
130 * sounds and videos, they may be displayed in other ways.
131 *
132 * @return string
133 */
134 abstract public function toHtml( $options = [] );
135
136 /**
137 * This will be overridden to return true in error classes
138 * @return bool
139 */
140 public function isError() {
141 return false;
142 }
143
144 /**
145 * Check if an output thumbnail file actually exists.
146 *
147 * This will return false if there was an error, the
148 * thumbnail is to be handled client-side only, or if
149 * transformation was deferred via TRANSFORM_LATER.
150 * This file may exist as a new file in /tmp, a file
151 * in permanent storage, or even refer to the original.
152 *
153 * @return bool
154 */
155 public function hasFile() {
156 // If TRANSFORM_LATER, $this->path will be false.
157 // Note: a null path means "use the source file".
158 return ( !$this->isError() && ( $this->path || $this->path === null ) );
159 }
160
161 /**
162 * Check if the output thumbnail is the same as the source.
163 * This can occur if the requested width was bigger than the source.
164 *
165 * @return bool
166 */
167 public function fileIsSource() {
168 return ( !$this->isError() && $this->path === null );
169 }
170
171 /**
172 * Get the path of a file system copy of the thumbnail.
173 * Callers should never write to this path.
174 *
175 * @return string|bool Returns false if there isn't one
176 */
177 public function getLocalCopyPath() {
178 if ( $this->isError() ) {
179 return false;
180 } elseif ( $this->path === null ) {
181 return $this->file->getLocalRefPath(); // assume thumb was not scaled
182 } elseif ( FileBackend::isStoragePath( $this->path ) ) {
183 $be = $this->file->getRepo()->getBackend();
184 // The temp file will be process cached by FileBackend
185 $fsFile = $be->getLocalReference( [ 'src' => $this->path ] );
186
187 return $fsFile ? $fsFile->getPath() : false;
188 } else {
189 return $this->path; // may return false
190 }
191 }
192
193 /**
194 * Stream the file if there were no errors
195 *
196 * @param array $headers Additional HTTP headers to send on success
197 * @return Status
198 * @since 1.27
199 */
200 public function streamFileWithStatus( $headers = [] ) {
201 if ( !$this->path ) {
202 return Status::newFatal( 'backend-fail-stream', '<no path>' );
203 } elseif ( FileBackend::isStoragePath( $this->path ) ) {
204 $be = $this->file->getRepo()->getBackend();
205 return $be->streamFile( [ 'src' => $this->path, 'headers' => $headers ] );
206 } else { // FS-file
207 $success = StreamFile::stream( $this->getLocalCopyPath(), $headers );
208 return $success ? Status::newGood() : Status::newFatal( 'backend-fail-stream', $this->path );
209 }
210 }
211
212 /**
213 * Stream the file if there were no errors
214 *
215 * @deprecated since 1.26, use streamFileWithStatus
216 * @param array $headers Additional HTTP headers to send on success
217 * @return bool Success
218 */
219 public function streamFile( $headers = [] ) {
220 return $this->streamFileWithStatus( $headers )->isOK();
221 }
222
223 /**
224 * Wrap some XHTML text in an anchor tag with the given attributes
225 *
226 * @param array $linkAttribs
227 * @param string $contents
228 * @return string
229 */
230 protected function linkWrap( $linkAttribs, $contents ) {
231 if ( $linkAttribs ) {
232 return Xml::tags( 'a', $linkAttribs, $contents );
233 } else {
234 return $contents;
235 }
236 }
237
238 /**
239 * @param string|null $title
240 * @param string|array $params Query parameters to add
241 * @return array
242 */
243 public function getDescLinkAttribs( $title = null, $params = [] ) {
244 if ( is_array( $params ) ) {
245 $query = $params;
246 } else {
247 $query = [];
248 }
249 if ( $this->page && $this->page !== 1 ) {
250 $query['page'] = $this->page;
251 }
252 if ( $this->lang ) {
253 $query['lang'] = $this->lang;
254 }
255
256 if ( is_string( $params ) && $params !== '' ) {
257 $query = $params . '&' . wfArrayToCgi( $query );
258 }
259
260 $attribs = [
261 'href' => $this->file->getTitle()->getLocalURL( $query ),
262 'class' => 'image',
263 ];
264 if ( $title ) {
265 $attribs['title'] = $title;
266 }
267
268 return $attribs;
269 }
270 }
271
272 /**
273 * Media transform output for images
274 *
275 * @ingroup Media
276 */
277 class ThumbnailImage extends MediaTransformOutput {
278 private static $firstNonIconImageRendered = false;
279
280 /**
281 * Get a thumbnail object from a file and parameters.
282 * If $path is set to null, the output file is treated as a source copy.
283 * If $path is set to false, no output file will be created.
284 * $parameters should include, as a minimum, (file) 'width' and 'height'.
285 * It may also include a 'page' parameter for multipage files.
286 *
287 * @param File $file
288 * @param string $url URL path to the thumb
289 * @param string|bool $path Filesystem path to the thumb
290 * @param array $parameters Associative array of parameters
291 */
292 function __construct( $file, $url, $path = false, $parameters = [] ) {
293 # Previous parameters:
294 # $file, $url, $width, $height, $path = false, $page = false
295
296 $defaults = [
297 'page' => false,
298 'lang' => false
299 ];
300
301 if ( is_array( $parameters ) ) {
302 $actualParams = $parameters + $defaults;
303 } else {
304 # Using old format, should convert. Later a warning could be added here.
305 $numArgs = func_num_args();
306 $actualParams = [
307 'width' => $path,
308 'height' => $parameters,
309 'page' => ( $numArgs > 5 ) ? func_get_arg( 5 ) : false
310 ] + $defaults;
311 $path = ( $numArgs > 4 ) ? func_get_arg( 4 ) : false;
312 }
313
314 $this->file = $file;
315 $this->url = $url;
316 $this->path = $path;
317
318 # These should be integers when they get here.
319 # If not, there's a bug somewhere. But let's at
320 # least produce valid HTML code regardless.
321 $this->width = round( $actualParams['width'] );
322 $this->height = round( $actualParams['height'] );
323
324 $this->page = $actualParams['page'];
325 $this->lang = $actualParams['lang'];
326 }
327
328 /**
329 * Return HTML <img ... /> tag for the thumbnail, will include
330 * width and height attributes and a blank alt text (as required).
331 *
332 * @param array $options Associative array of options. Boolean options
333 * should be indicated with a value of true for true, and false or
334 * absent for false.
335 *
336 * alt HTML alt attribute
337 * title HTML title attribute
338 * desc-link Boolean, show a description link
339 * file-link Boolean, show a file download link
340 * valign vertical-align property, if the output is an inline element
341 * img-class Class applied to the \<img\> tag, if there is such a tag
342 * desc-query String, description link query params
343 * override-width Override width attribute. Should generally not set
344 * override-height Override height attribute. Should generally not set
345 * no-dimensions Boolean, skip width and height attributes (useful if
346 * set in CSS)
347 * custom-url-link Custom URL to link to
348 * custom-title-link Custom Title object to link to
349 * custom target-link Value of the target attribute, for custom-target-link
350 * parser-extlink-* Attributes added by parser for external links:
351 * parser-extlink-rel: add rel="nofollow"
352 * parser-extlink-target: link target, but overridden by custom-target-link
353 *
354 * For images, desc-link and file-link are implemented as a click-through. For
355 * sounds and videos, they may be displayed in other ways.
356 *
357 * @throws MWException
358 * @return string
359 */
360 function toHtml( $options = [] ) {
361 global $wgPriorityHints, $wgElementTiming;
362
363 if ( count( func_get_args() ) == 2 ) {
364 throw new MWException( __METHOD__ . ' called in the old style' );
365 }
366
367 $alt = $options['alt'] ?? '';
368
369 $query = $options['desc-query'] ?? '';
370
371 $attribs = [
372 'alt' => $alt,
373 'src' => $this->url,
374 'decoding' => 'async',
375 ];
376
377 $elementTimingName = 'thumbnail';
378
379 if ( $wgPriorityHints
380 && !self::$firstNonIconImageRendered
381 && $this->width * $this->height > 100 * 100 ) {
382 self::$firstNonIconImageRendered = true;
383
384 $attribs['importance'] = 'high';
385 $elementTimingName = 'thumbnail-high';
386 }
387
388 if ( $wgElementTiming ) {
389 $attribs['elementtiming'] = $elementTimingName;
390 }
391
392 if ( !empty( $options['custom-url-link'] ) ) {
393 $linkAttribs = [ 'href' => $options['custom-url-link'] ];
394 if ( !empty( $options['title'] ) ) {
395 $linkAttribs['title'] = $options['title'];
396 }
397 if ( !empty( $options['custom-target-link'] ) ) {
398 $linkAttribs['target'] = $options['custom-target-link'];
399 } elseif ( !empty( $options['parser-extlink-target'] ) ) {
400 $linkAttribs['target'] = $options['parser-extlink-target'];
401 }
402 if ( !empty( $options['parser-extlink-rel'] ) ) {
403 $linkAttribs['rel'] = $options['parser-extlink-rel'];
404 }
405 } elseif ( !empty( $options['custom-title-link'] ) ) {
406 /** @var Title $title */
407 $title = $options['custom-title-link'];
408 $linkAttribs = [
409 'href' => $title->getLinkURL(),
410 'title' => empty( $options['title'] ) ? $title->getFullText() : $options['title']
411 ];
412 } elseif ( !empty( $options['desc-link'] ) ) {
413 $linkAttribs = $this->getDescLinkAttribs(
414 empty( $options['title'] ) ? null : $options['title'],
415 $query
416 );
417 } elseif ( !empty( $options['file-link'] ) ) {
418 $linkAttribs = [ 'href' => $this->file->getUrl() ];
419 } else {
420 $linkAttribs = false;
421 if ( !empty( $options['title'] ) ) {
422 $attribs['title'] = $options['title'];
423 }
424 }
425
426 if ( empty( $options['no-dimensions'] ) ) {
427 $attribs['width'] = $this->width;
428 $attribs['height'] = $this->height;
429 }
430 if ( !empty( $options['valign'] ) ) {
431 $attribs['style'] = "vertical-align: {$options['valign']}";
432 }
433 if ( !empty( $options['img-class'] ) ) {
434 $attribs['class'] = $options['img-class'];
435 }
436 if ( isset( $options['override-height'] ) ) {
437 $attribs['height'] = $options['override-height'];
438 }
439 if ( isset( $options['override-width'] ) ) {
440 $attribs['width'] = $options['override-width'];
441 }
442
443 // Additional densities for responsive images, if specified.
444 // If any of these urls is the same as src url, it'll be excluded.
445 $responsiveUrls = array_diff( $this->responsiveUrls, [ $this->url ] );
446 if ( !empty( $responsiveUrls ) ) {
447 $attribs['srcset'] = Html::srcSet( $responsiveUrls );
448 }
449
450 Hooks::run( 'ThumbnailBeforeProduceHTML', [ $this, &$attribs, &$linkAttribs ] );
451
452 return $this->linkWrap( $linkAttribs, Xml::element( 'img', $attribs ) );
453 }
454 }
455
456 /**
457 * Basic media transform error class
458 *
459 * @ingroup Media
460 */
461 class MediaTransformError extends MediaTransformOutput {
462 /** @var Message */
463 private $msg;
464
465 function __construct( $msg, $width, $height /*, ... */ ) {
466 $args = array_slice( func_get_args(), 3 );
467 $this->msg = wfMessage( $msg )->params( $args );
468 $this->width = intval( $width );
469 $this->height = intval( $height );
470 $this->url = false;
471 $this->path = false;
472 }
473
474 function toHtml( $options = [] ) {
475 return "<div class=\"MediaTransformError\" style=\"" .
476 "width: {$this->width}px; height: {$this->height}px; display:inline-block;\">" .
477 $this->getHtmlMsg() .
478 "</div>";
479 }
480
481 function toText() {
482 return $this->msg->text();
483 }
484
485 function getHtmlMsg() {
486 return $this->msg->escaped();
487 }
488
489 function getMsg() {
490 return $this->msg;
491 }
492
493 function isError() {
494 return true;
495 }
496
497 function getHttpStatusCode() {
498 return 500;
499 }
500 }
501
502 /**
503 * Shortcut class for parameter validation errors
504 *
505 * @ingroup Media
506 */
507 class TransformParameterError extends MediaTransformError {
508 function __construct( $params ) {
509 parent::__construct( 'thumbnail_error',
510 max( $params['width'] ?? 0, 120 ),
511 max( $params['height'] ?? 0, 120 ),
512 wfMessage( 'thumbnail_invalid_params' )
513 );
514 }
515
516 function getHttpStatusCode() {
517 return 400;
518 }
519 }
520
521 /**
522 * Shortcut class for parameter file size errors
523 *
524 * @ingroup Media
525 * @since 1.25
526 */
527 class TransformTooBigImageAreaError extends MediaTransformError {
528 function __construct( $params, $maxImageArea ) {
529 $msg = wfMessage( 'thumbnail_toobigimagearea' );
530 $msg->params(
531 $msg->getLanguage()->formatComputingNumbers( $maxImageArea, 1000, "size-$1pixel" )
532 );
533
534 parent::__construct( 'thumbnail_error',
535 max( $params['width'] ?? 0, 120 ),
536 max( $params['height'] ?? 0, 120 ),
537 $msg
538 );
539 }
540
541 function getHttpStatusCode() {
542 return 400;
543 }
544 }