MediaWiki REL1_35
MediaFileTrait.php
Go to the documentation of this file.
1<?php
26trait MediaFileTrait {
33 private function getFileInfo( $file, $user, $transforms ) {
34 // If there is a problem with the file, there is very little info we can reliably
35 // return (T228286, T239213), but we do what we can (T201205).
36 $responseFile = [
37 'title' => $file->getTitle()->getText(),
38 'file_description_url' => wfExpandUrl( $file->getDescriptionUrl(), PROTO_RELATIVE ),
39 'latest' => null,
40 'preferred' => null,
41 'original' => null,
42 ];
43
44 foreach ( array_keys( $transforms ) as $transformType ) {
45 $responseFile[$transformType] = null;
46 }
47
48 if ( $file->exists() ) {
49 if ( $file->userCan( File::DELETED_USER, $user ) ) {
50 $fileUser = [
51 'id' => $file->getUser( 'id' ),
52 'name' => $file->getUser( 'text' ),
53 ];
54 } else {
55 $fileUser = [
56 'id' => null,
57 'name' => null,
58 ];
59 }
60 $responseFile['latest'] = [
61 'timestamp' => wfTimestamp( TS_ISO_8601, $file->getTimestamp() ),
62 'user' => $fileUser,
63 ];
64
65 // If the file doesn't and shouldn't have a duration, return null instead of 0.
66 // Testing for 0 first, then checking mediatype, makes gifs behave as desired for
67 // both still and animated cases.
68 $duration = $file->getLength();
69 $mediaTypesWithDurations = [ MEDIATYPE_AUDIO, MEDIATYPE_VIDEO, MEDIATYPE_MULTIMEDIA ];
70 if ( $duration == 0 && !in_array( $file->getMediaType(), $mediaTypesWithDurations ) ) {
71 $duration = null;
72 }
73
74 if ( $file->allowInlineDisplay() ) {
75 foreach ( $transforms as $transformType => $transform ) {
76 $responseFile[$transformType] = $this->getTransformInfo(
77 $file,
78 $duration,
79 $transform['maxWidth'],
80 $transform['maxHeight']
81 );
82 }
83 }
84
85 $responseFile['original'] = [
86 'mediatype' => $file->getMediaType(),
87 'size' => $file->getSize(),
88 'width' => $file->getWidth() ?: null,
89 'height' => $file->getHeight() ?: null,
90 'duration' => $duration,
91 'url' => wfExpandUrl( $file->getUrl(), PROTO_RELATIVE ),
92 ];
93 }
94
95 return $responseFile;
96 }
97
105 private function getTransformInfo( $file, $duration, $maxWidth, $maxHeight ) {
106 $transformInfo = null;
107
108 try {
109 list( $width, $height ) = $file->getDisplayWidthHeight( $maxWidth, $maxHeight );
110 $transform = $file->transform( [ 'width' => $width, 'height' => $height ] );
111 if ( $transform && !$transform->isError() ) {
112 // $file->getSize() returns original size. Only include if dimensions match.
113 $size = null;
114 if ( $file->getWidth() == $transform->getWidth() &&
115 $file->getHeight() == $transform->getHeight()
116 ) {
117 $size = $file->getSize();
118 }
119
120 $transformInfo = [
121 'mediatype' => $transform->getFile()->getMediaType(),
122 'size' => $size,
123 'width' => $transform->getWidth() ?: null,
124 'height' => $transform->getHeight() ?: null,
125 'duration' => $duration,
126 'url' => wfExpandUrl( $transform->getUrl(), PROTO_RELATIVE ),
127 ];
128 }
129 } catch ( MWException $e ) {
130 // Caller decides what to do on failure
131 }
132
133 return $transformInfo;
134 }
135
144 public static function getImageLimitsFromOption( $user, $optionName ) {
145 global $wgImageLimits;
146
147 $option = $user->getIntOption( $optionName );
148 if ( !isset( $wgImageLimits[$option] ) ) {
149 $option = User::getDefaultOption( $optionName );
150 }
151
152 // The user offset might still be incorrect, specially if
153 // $wgImageLimits got changed (see T10858).
154 if ( !isset( $wgImageLimits[$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 $wgImageLimits[$option] ?? [ 800, 600 ];
161 }
162}
$wgImageLimits
Limit images on image description pages to a user-selectable limit.
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.
static getDefaultOption( $opt)
Get a given default option value.
Definition User.php:1553
const PROTO_RELATIVE
Definition Defines.php:211
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