37 private const MAX_ERR_LOG_SIZE = 65535;
46 return MediaWikiServices::getInstance()
47 ->getMediaHandlerFactory()->getHandler( $type );
184 $rc =
new ReflectionClass( $this );
185 $rm =
new ReflectionMethod( $this, $name );
186 return $rm->getDeclaringClass()->getName() === $rc->getName();
217 if ( $info ===
false ) {
220 if ( $info !==
null ) {
221 $info += [
'width' => 0,
'height' => 0,
'metadata' => [] ];
222 if ( !is_array( $info[
'metadata'] ) ) {
223 throw new InvalidArgumentException(
'Media handler ' .
224 static::class .
' returned ' . get_debug_type( $info[
'metadata'] ) .
225 ' for metadata, should be array' );
238 if ( $blob ===
false && $size ===
false ) {
243 'width' => $size[0] ?? 0,
244 'height' => $size[1] ?? 0
246 if ( isset( $size[
'bits'] ) ) {
247 $info[
'bits'] = $size[
'bits'];
250 $info = [
'width' => 0,
'height' => 0 ];
252 if ( $blob !==
false ) {
254 $metadata = @unserialize( $blob );
255 if ( $metadata ===
false ) {
257 $metadata = [
'_error' => $blob ];
258 } elseif ( !is_array( $metadata ) ) {
261 $info[
'metadata'] = $metadata;
263 $info[
'metadata'] = [];
287 (
new HookRunner( MediaWikiServices::getInstance()->getHookContainer() ) )->onGetMetadataVersion( $version );
289 return implode(
';', $version );
338 return self::METADATA_GOOD;
367 return self::METADATA_GOOD;
439 final public function getTransform( $image, $dstPath, $dstUrl, $params ) {
440 return $this->
doTransform( $image, $dstPath, $dstUrl, $params, self::TRANSFORM_LATER );
457 abstract public function doTransform( $image, $dstPath, $dstUrl, $params, $flags = 0 );
470 $magic = MediaWikiServices::getInstance()->getMimeAnalyzer();
471 if ( !$ext || $magic->isMatchingExtension( $ext, $mime ) ===
false ) {
474 $knownExt = $magic->getExtensionFromMimeTypeOrNull( $mime );
475 if ( $knownExt !==
null ) {
476 return [ $knownExt, $mime ];
482 return [ $ext, $mime ];
630 for ( $i = 1; $i <= $numPages; $i++ ) {
632 if ( is_string( $curPage ) ) {
633 $document .= $curPage .
"\n";
636 if ( $document !==
'' ) {
695 foreach ( $metadataArray as $tag => $vals ) {
696 $v = $this->
formatTag( $tag, $vals, $context );
697 if ( $v ===
false ) {
703 unset( $metadataArray[$tag] );
706 $metadataArray[$tag] = [
'_formatted' => $v ];
707 if ( isset( $v[
'_type'] ) ) {
708 $metadataArray[$tag][
'_type'] = $v[
'_type'];
709 unset( $metadataArray[$tag][
'_formatted'][
'_type'] );
714 $formatted = FormatMetadata::getFormattedData( $metadataArray, $context );
717 foreach ( $formatted as $name => $value ) {
718 $tag = strtolower( $name );
719 self::addMeta( $result,
720 in_array( $tag, $visibleFields ) ?
'visible' :
'collapsed',
742 protected function formatTag(
string $key, $vals, $context =
false ) {
755 return FormatMetadata::getVisibleFields();
781 protected static function addMeta( &$array, $visibility, $type, $id, $value, $param =
false ) {
782 $msg =
wfMessage(
"$type-$id", (
string)$param );
783 if ( $msg->exists() ) {
784 $name = $msg->text();
791 wfDebug( __METHOD__ .
' Unknown metadata name: ' . $id );
794 $array[$visibility][] = [
816 return self::getGeneralShortDesc( $file );
834 return self::getGeneralLongDesc( $file );
846 return htmlspecialchars(
$wgLang->formatSize( $file->getSize() ), ENT_QUOTES );
857 ->sizeParams( $file->getSize() )
858 ->params(
'<span class="mime-type">' . $file->getMimeType() .
'</span>' )
870 public static function fitBoxWidth( $boxWidth, $boxHeight, $maxHeight ) {
871 $idealWidth = $boxWidth * $maxHeight / $boxHeight;
872 $roundedUp = ceil( $idealWidth );
873 if ( round( $roundedUp * $boxHeight / $boxWidth ) > $maxHeight ) {
874 return (
int)floor( $idealWidth );
919 return Status::newGood();
933 if ( file_exists( $dstPath ) ) {
934 $thumbstat = stat( $dstPath );
935 if ( $thumbstat[
'size'] == 0 || $retval != 0 ) {
936 $result = unlink( $dstPath );
940 sprintf(
'Removing bad %d-byte thumbnail "%s". unlink() succeeded',
941 $thumbstat[
'size'], $dstPath ) );
944 sprintf(
'Removing bad %d-byte thumbnail "%s". unlink() failed',
945 $thumbstat[
'size'], $dstPath ) );
1016 # Keep error output limited (T59985)
1017 $errMessage = trim( substr( $err, 0, self::MAX_ERR_LOG_SIZE ) );
1020 sprintf(
'thumbnail failed on %s: error %d "%s" from "%s"',
1021 wfHostname(), $retval, $errMessage, $cmd ) );
1160 $pageRangesByDimensions = [];
1162 foreach ( $pagesByDimensions as $dimensions => $pageList ) {
1164 $firstPage = $pageList[0];
1165 $lastPage = $firstPage - 1;
1167 foreach ( $pageList as $page ) {
1168 if ( $page > $lastPage + 1 ) {
1169 if ( $firstPage !== $lastPage ) {
1170 $ranges[] =
"$firstPage-$lastPage";
1172 $ranges[] =
"$firstPage";
1181 if ( $firstPage != $lastPage ) {
1182 $ranges[] =
"$firstPage-$lastPage";
1184 $ranges[] =
"$firstPage";
1187 $pageRangesByDimensions[ $dimensions ] = $ranges;
1190 $dimensionsString = [];
1191 foreach ( $pageRangesByDimensions as $dimensions => $pageRanges ) {
1192 $dimensionsString[] =
"$dimensions:" . implode(
',', $pageRanges );
1195 return implode(
'/', $dimensionsString );
1207 return [
'X-Content-Dimensions' =>
'' ];
wfDebug( $text, $dest='all', array $context=[])
Sends a line to the debug log if enabled or, optionally, to a comment in output.
wfEscapeWikiText( $input)
Escapes the given text so that it may be output using addWikiText() without any linking,...
wfHostname()
Get host name of the current machine, for use in error reporting.
wfDebugLog( $logGroup, $text, $dest='all', array $context=[])
Send a line to a supplementary debug log file, if configured, or main debug log if not.
wfMessage( $key,... $params)
This is the function for getting translated interface messages.
if(MW_ENTRY_POINT==='index') if(!defined( 'MW_NO_SESSION') &&MW_ENTRY_POINT !=='cli' $wgLang
Interface for objects which can provide a MediaWiki context on request.