MediaWiki master
MediaFileTrait.php
Go to the documentation of this file.
1<?php
8
13use Wikimedia\Timestamp\TimestampFormat as TS;
14
28 private function getFileInfo( $file, Authority $performer, $transforms ) {
29 $urlUtils = MediaWikiServices::getInstance()->getUrlUtils();
30 // If there is a problem with the file, there is very little info we can reliably
31 // return (T228286, T239213), but we do what we can (T201205).
32 $responseFile = [
33 'title' => $file->getTitle()->getText(),
34 'file_description_url' => $urlUtils->expand( $file->getDescriptionUrl(), PROTO_RELATIVE ),
35 'latest' => null,
36 'preferred' => null,
37 'original' => null,
38 ];
39
40 foreach ( $transforms as $transformType => $_ ) {
41 $responseFile[$transformType] = null;
42 }
43
44 if ( $file->exists() ) {
45 $uploader = $file->getUploader( File::FOR_THIS_USER, $performer );
46 if ( $uploader ) {
47 $fileUser = [
48 'id' => $uploader->getId(),
49 'name' => $uploader->getName(),
50 ];
51 } else {
52 $fileUser = [
53 'id' => null,
54 'name' => null,
55 ];
56 }
57 $responseFile['latest'] = [
58 'timestamp' => wfTimestamp( TS::ISO_8601, $file->getTimestamp() ),
59 'user' => $fileUser,
60 ];
61
62 // If the file doesn't and shouldn't have a duration, return null instead of 0.
63 // Testing for 0 first, then checking mediatype, makes gifs behave as desired for
64 // both still and animated cases.
65 $duration = $file->getLength();
66 $mediaTypesWithDurations = [ MEDIATYPE_AUDIO, MEDIATYPE_VIDEO, MEDIATYPE_MULTIMEDIA ];
67 if ( $duration == 0 && !in_array( $file->getMediaType(), $mediaTypesWithDurations ) ) {
68 $duration = null;
69 }
70
71 if ( $file->allowInlineDisplay() ) {
72 foreach ( $transforms as $transformType => $transform ) {
73 $responseFile[$transformType] = $this->getTransformInfo(
74 $file,
75 // @phan-suppress-next-line PhanTypeMismatchArgumentNullable False positive
76 $duration,
77 $transform['maxWidth'],
78 $transform['maxHeight']
79 );
80 }
81 }
82
83 $responseFile['original'] = [
84 'mediatype' => $file->getMediaType(),
85 'size' => $file->getSize(),
86 'width' => $file->getWidth() ?: null,
87 'height' => $file->getHeight() ?: null,
88 'duration' => $duration,
89 'url' => $urlUtils->expand( $file->getUrl(), PROTO_RELATIVE ),
90 ];
91 }
92
93 return $responseFile;
94 }
95
103 private function getTransformInfo( $file, $duration, $maxWidth, $maxHeight ) {
104 $transformInfo = null;
105
106 [ $width, $height ] = $file->getDisplayWidthHeight( $maxWidth, $maxHeight );
107 $transform = $file->transform( [ 'width' => $width, 'height' => $height ] );
108 if ( $transform && !$transform->isError() ) {
109 // $file->getSize() returns original size. Only include if dimensions match.
110 $size = null;
111 if ( $file->getWidth() == $transform->getWidth() &&
112 $file->getHeight() == $transform->getHeight()
113 ) {
114 $size = $file->getSize();
115 }
116
117 $transformInfo = [
118 'mediatype' => $transform->getFile()->getMediaType(),
119 'size' => $size,
120 'width' => $transform->getWidth() ?: null,
121 'height' => $transform->getHeight() ?: null,
122 'duration' => $duration,
123 'url' => MediaWikiServices::getInstance()->getUrlUtils()
124 ->expand( $transform->getUrl(), PROTO_RELATIVE ),
125 ];
126 }
127
128 return $transformInfo;
129 }
130
139 public static function getImageLimitsFromOption( UserIdentity $user, string $optionName ) {
140 $imageLimits = MediaWikiServices::getInstance()->getMainConfig()
142 $optionsLookup = MediaWikiServices::getInstance()->getUserOptionsLookup();
143 $option = $optionsLookup->getIntOption( $user, $optionName );
144 if ( !isset( $imageLimits[$option] ) ) {
145 $option = $optionsLookup->getDefaultOption( $optionName, $user );
146 }
147
148 // The user offset might still be incorrect, specially if
149 // $wgImageLimits got changed (see T10858).
150 if ( !isset( $imageLimits[$option] ) ) {
151 // Default to the first offset in $wgImageLimits
152 $option = 0;
153 }
154
155 // if nothing is set, fallback to a hardcoded default
156 return $imageLimits[$option] ?? [ 800, 600 ];
157 }
158}
159
161class_alias( MediaFileTrait::class, 'MediaFileTrait' );
const PROTO_RELATIVE
Definition Defines.php:219
wfTimestamp( $outputtype=TS::UNIX, $ts=0)
Get a timestamp string in one of various formats.
const MEDIATYPE_VIDEO
Definition defines.php:22
const MEDIATYPE_AUDIO
Definition defines.php:19
const MEDIATYPE_MULTIMEDIA
Definition defines.php:24
A class containing constants representing the names of configuration variables.
const ImageLimits
Name constant for the ImageLimits setting, for use with Config::get()
Service locator for MediaWiki core services.
static getInstance()
Returns the global default instance of the top level service locator.
trait MediaFileTrait
Trait for functionality related to media files.
This interface represents the authority associated with the current execution context,...
Definition Authority.php:23
Interface for objects representing user identity.