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