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
143 public static function getImageLimitsFromOption( UserIdentity $user, string $optionName ) {
144 $imageLimits = MediaWikiServices::getInstance()->getMainConfig()
146 $optionsLookup = MediaWikiServices::getInstance()->getUserOptionsLookup();
147 $option = $optionsLookup->getIntOption( $user, $optionName );
148 if ( !isset( $imageLimits[$option] ) ) {
149 $option = $optionsLookup->getDefaultOption( $optionName, $user );
150 }
151
152 // The user offset might still be incorrect, specially if
153 // $wgImageLimits got changed (see T10858).
154 if ( !isset( $imageLimits[$option] ) ) {
155 // Default to the first offset in $wgImageLimits
156 $option = 0;
157 }
158
159 // if nothing is set, fallback to a hardcoded default
160 return $imageLimits[$option] ?? [ 800, 600 ];
161 }
162
176 public static function getNormalizedThumbLimits( $width ) {
177 $thumbSteps = MediaWikiServices::getInstance()->getMainConfig()
179 if ( !is_array( $thumbSteps ) ) {
180 // Sanity check: If ThumbnailSteps does not exist in the config,
181 // or is empty, we need to fall back on an array. In that case,
182 // in order to not have an empty value, we will just use the
183 // requested width as the only "step".
184 // This shouldn't happen in actual wikis, since ThumbnailSteps
185 // has a default value, but it has happened in CI wikis, so it's
186 // better to be safe in case of misconfiguration.
187 $thumbSteps = [ $width ];
188 }
189 sort( $thumbSteps, SORT_NUMERIC );
190
191 // Find the smallest thumbnail step that is at least as large
192 // as the requested width
193 foreach ( $thumbSteps as $thumbWidth ) {
194 if ( $thumbWidth >= $width ) {
195 return [ $thumbWidth, $thumbWidth ];
196 }
197 }
198
199 // if none was found to be at least as large,
200 // return the largest thumbnail step
201 $normalizedWidth = end( $thumbSteps );
202
203 if ( $normalizedWidth <= 0 ) {
204 // Sanity check: if the config is not set properly
205 // just return the original width.
206 return [ $width, $width ];
207 }
208
209 return [ $normalizedWidth, $normalizedWidth ];
210 }
211}
212
214class_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()
const ThumbnailSteps
Name constant for the ThumbnailSteps 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.