| 1 |
<?php |
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
|
| 6 |
|
| 7 |
|
| 8 |
|
| 9 |
|
| 10 |
|
| 11 |
|
| 12 |
|
| 13 |
|
| 14 |
|
| 15 |
|
| 16 |
|
| 17 |
|
| 18 |
|
| 19 |
|
| 20 |
|
| 21 |
use MediaWiki\MainConfigNames; |
| 22 |
use MediaWiki\MediaWikiServices; |
| 23 |
use MediaWiki\Permissions\Authority; |
| 24 |
use MediaWiki\User\UserIdentity; |
| 25 |
|
| 26 |
|
| 27 |
|
| 28 |
|
| 29 |
|
| 30 |
|
| 31 |
|
| 32 |
trait MediaFileTrait { |
| 33 |
|
| 34 |
|
| 35 |
|
| 36 |
|
| 37 |
|
| 38 |
|
| 39 |
private function getFileInfo( $file, Authority $performer, $transforms ) { |
| 40 |
$urlUtils = MediaWikiServices::getInstance()->getUrlUtils(); |
| 41 |
|
| 42 |
|
| 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 ( $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 |
|
| 74 |
|
| 75 |
|
| 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 |
|
| 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 |
|
| 107 |
|
| 108 |
|
| 109 |
|
| 110 |
|
| 111 |
|
| 112 |
|
| 113 |
|
| 114 |
private function getTransformInfo( $file, $duration, $maxWidth, $maxHeight ) { |
| 115 |
$transformInfo = null; |
| 116 |
|
| 117 |
[ $width, $height ] = $file->getDisplayWidthHeight( $maxWidth, $maxHeight ); |
| 118 |
$transform = $file->transform( [ 'width' => $width, 'height' => $height ] ); |
| 119 |
if ( $transform && !$transform->isError() ) { |
| 120 |
|
| 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' => MediaWikiServices::getInstance()->getUrlUtils() |
| 135 |
->expand( $transform->getUrl(), PROTO_RELATIVE ), |
| 136 |
]; |
| 137 |
} |
| 138 |
|
| 139 |
return $transformInfo; |
| 140 |
} |
| 141 |
|
| 142 |
|
| 143 |
|
| 144 |
|
| 145 |
|
| 146 |
|
| 147 |
|
| 148 |
|
| 149 |
|
| 150 |
public static function getImageLimitsFromOption( UserIdentity $user, string $optionName ) { |
| 151 |
$imageLimits = MediaWikiServices::getInstance()->getMainConfig() |
| 152 |
->get( MainConfigNames::ImageLimits ); |
| 153 |
$optionsLookup = MediaWikiServices::getInstance()->getUserOptionsLookup(); |
| 154 |
$option = $optionsLookup->getIntOption( $user, $optionName ); |
| 155 |
if ( !isset( $imageLimits[$option] ) ) { |
| 156 |
$option = $optionsLookup->getDefaultOption( $optionName, $user ); |
| 157 |
} |
| 158 |
|
| 159 |
|
| 160 |
|
| 161 |
if ( !isset( $imageLimits[$option] ) ) { |
| 162 |
|
| 163 |
$option = 0; |
| 164 |
} |
| 165 |
|
| 166 |
|
| 167 |
return $imageLimits[$option] ?? [ 800, 600 ]; |
| 168 |
} |
| 169 |
} |