MediaWiki REL1_40
MediaFileTrait.php
Go to the documentation of this file.
1<?php
25
32trait MediaFileTrait {
39 private function getFileInfo( $file, Authority $performer, $transforms ) {
40 $urlUtils = MediaWikiServices::getInstance()->getUrlUtils();
41 // If there is a problem with the file, there is very little info we can reliably
42 // return (T228286, T239213), but we do what we can (T201205).
43 $responseFile = [
44 'title' => $file->getTitle()->getText(),
45 'file_description_url' => $urlUtils->expand( $file->getDescriptionUrl(), PROTO_RELATIVE ),
46 'latest' => null,
47 'preferred' => null,
48 'original' => null,
49 ];
50
51 foreach ( array_keys( $transforms ) as $transformType ) {
52 $responseFile[$transformType] = null;
53 }
54
55 if ( $file->exists() ) {
56 $uploader = $file->getUploader( File::FOR_THIS_USER, $performer );
57 if ( $uploader ) {
58 $fileUser = [
59 'id' => $uploader->getId(),
60 'name' => $uploader->getName(),
61 ];
62 } else {
63 $fileUser = [
64 'id' => null,
65 'name' => null,
66 ];
67 }
68 $responseFile['latest'] = [
69 'timestamp' => wfTimestamp( TS_ISO_8601, $file->getTimestamp() ),
70 'user' => $fileUser,
71 ];
72
73 // If the file doesn't and shouldn't have a duration, return null instead of 0.
74 // Testing for 0 first, then checking mediatype, makes gifs behave as desired for
75 // both still and animated cases.
76 $duration = $file->getLength();
77 $mediaTypesWithDurations = [ MEDIATYPE_AUDIO, MEDIATYPE_VIDEO, MEDIATYPE_MULTIMEDIA ];
78 if ( $duration == 0 && !in_array( $file->getMediaType(), $mediaTypesWithDurations ) ) {
79 $duration = null;
80 }
81
82 if ( $file->allowInlineDisplay() ) {
83 foreach ( $transforms as $transformType => $transform ) {
84 $responseFile[$transformType] = $this->getTransformInfo(
85 $file,
86 // @phan-suppress-next-line PhanTypeMismatchArgumentNullable False positive
87 $duration,
88 $transform['maxWidth'],
89 $transform['maxHeight']
90 );
91 }
92 }
93
94 $responseFile['original'] = [
95 'mediatype' => $file->getMediaType(),
96 'size' => $file->getSize(),
97 'width' => $file->getWidth() ?: null,
98 'height' => $file->getHeight() ?: null,
99 'duration' => $duration,
100 'url' => $urlUtils->expand( $file->getUrl(), PROTO_RELATIVE ),
101 ];
102 }
103
104 return $responseFile;
105 }
106
114 private function getTransformInfo( $file, $duration, $maxWidth, $maxHeight ) {
115 $transformInfo = null;
116
117 try {
118 [ $width, $height ] = $file->getDisplayWidthHeight( $maxWidth, $maxHeight );
119 $transform = $file->transform( [ 'width' => $width, 'height' => $height ] );
120 if ( $transform && !$transform->isError() ) {
121 // $file->getSize() returns original size. Only include if dimensions match.
122 $size = null;
123 if ( $file->getWidth() == $transform->getWidth() &&
124 $file->getHeight() == $transform->getHeight()
125 ) {
126 $size = $file->getSize();
127 }
128
129 $transformInfo = [
130 'mediatype' => $transform->getFile()->getMediaType(),
131 'size' => $size,
132 'width' => $transform->getWidth() ?: null,
133 'height' => $transform->getHeight() ?: null,
134 'duration' => $duration,
135 'url' => MediaWikiServices::getInstance()->getUrlUtils()
136 ->expand( $transform->getUrl(), PROTO_RELATIVE ),
137 ];
138 }
139 } catch ( MWException $e ) {
140 // Caller decides what to do on failure
141 }
142
143 return $transformInfo;
144 }
145
154 public static function getImageLimitsFromOption( UserIdentity $user, string $optionName ) {
155 $imageLimits = MediaWikiServices::getInstance()->getMainConfig()
156 ->get( MainConfigNames::ImageLimits );
157 $optionsLookup = MediaWikiServices::getInstance()->getUserOptionsLookup();
158 $option = $optionsLookup->getIntOption( $user, $optionName );
159 if ( !isset( $imageLimits[$option] ) ) {
160 $option = $optionsLookup->getDefaultOption( $optionName );
161 }
162
163 // The user offset might still be incorrect, specially if
164 // $wgImageLimits got changed (see T10858).
165 if ( !isset( $imageLimits[$option] ) ) {
166 // Default to the first offset in $wgImageLimits
167 $option = 0;
168 }
169
170 // if nothing is set, fallback to a hardcoded default
171 return $imageLimits[$option] ?? [ 800, 600 ];
172 }
173}
const PROTO_RELATIVE
Definition Defines.php:195
wfTimestamp( $outputtype=TS_UNIX, $ts=0)
Get a timestamp string in one of various formats.
MediaWiki exception.
A class containing constants representing the names of configuration variables.
Service locator for MediaWiki core services.
This interface represents the authority associated the current execution context, such as a web reque...
Definition Authority.php:37
Interface for objects representing user identity.
const MEDIATYPE_VIDEO
Definition defines.php:35
const MEDIATYPE_AUDIO
Definition defines.php:32
const MEDIATYPE_MULTIMEDIA
Definition defines.php:37
if(PHP_SAPI !='cli-server') if(!isset( $_SERVER['SCRIPT_FILENAME'])) $file
Item class for a filearchive table row.
Definition router.php:42