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