2007-04-20 12:31:36 +00:00
|
|
|
<?php
|
WARNING: HUGE COMMIT
Doxygen documentation update:
* Changed alls @addtogroup to @ingroup. @addtogroup adds the comment to the group description, but doesn't add the file, class, function, ... to the group like @ingroup does. See for example http://svn.wikimedia.org/doc/group__SpecialPage.html where it's impossible to see related files, classes, ... that should belong to that group.
* Added @file to file description, it seems that it should be explicitely decalred for file descriptions, otherwise doxygen will think that the comment document the first class, variabled, function, ... that is in that file.
* Removed some empty comments
* Removed some ?>
Added following groups:
* ExternalStorage
* JobQueue
* MaintenanceLanguage
One more thing: there are still a lot of warnings when generating the doc.
2008-05-20 17:13:28 +00:00
|
|
|
/**
|
2010-08-15 17:27:41 +00:00
|
|
|
* Handler for SVG images.
|
|
|
|
|
*
|
2012-05-03 20:13:10 +00:00
|
|
|
* This program is free software; you can redistribute it and/or modify
|
|
|
|
|
* it under the terms of the GNU General Public License as published by
|
|
|
|
|
* the Free Software Foundation; either version 2 of the License, or
|
|
|
|
|
* (at your option) any later version.
|
|
|
|
|
*
|
|
|
|
|
* This program is distributed in the hope that it will be useful,
|
|
|
|
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
|
* GNU General Public License for more details.
|
|
|
|
|
*
|
|
|
|
|
* You should have received a copy of the GNU General Public License along
|
|
|
|
|
* with this program; if not, write to the Free Software Foundation, Inc.,
|
|
|
|
|
* 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
|
|
|
|
|
* http://www.gnu.org/copyleft/gpl.html
|
|
|
|
|
*
|
WARNING: HUGE COMMIT
Doxygen documentation update:
* Changed alls @addtogroup to @ingroup. @addtogroup adds the comment to the group description, but doesn't add the file, class, function, ... to the group like @ingroup does. See for example http://svn.wikimedia.org/doc/group__SpecialPage.html where it's impossible to see related files, classes, ... that should belong to that group.
* Added @file to file description, it seems that it should be explicitely decalred for file descriptions, otherwise doxygen will think that the comment document the first class, variabled, function, ... that is in that file.
* Removed some empty comments
* Removed some ?>
Added following groups:
* ExternalStorage
* JobQueue
* MaintenanceLanguage
One more thing: there are still a lot of warnings when generating the doc.
2008-05-20 17:13:28 +00:00
|
|
|
* @file
|
|
|
|
|
* @ingroup Media
|
|
|
|
|
*/
|
2019-04-07 11:36:22 +00:00
|
|
|
|
2020-01-03 23:03:14 +00:00
|
|
|
use MediaWiki\MediaWikiServices;
|
2019-04-07 11:36:22 +00:00
|
|
|
use MediaWiki\Shell\Shell;
|
2016-10-12 05:36:03 +00:00
|
|
|
use Wikimedia\ScopedCallback;
|
2007-04-20 12:31:36 +00:00
|
|
|
|
2007-04-24 06:53:31 +00:00
|
|
|
/**
|
2010-08-15 17:27:41 +00:00
|
|
|
* Handler for SVG images.
|
|
|
|
|
*
|
WARNING: HUGE COMMIT
Doxygen documentation update:
* Changed alls @addtogroup to @ingroup. @addtogroup adds the comment to the group description, but doesn't add the file, class, function, ... to the group like @ingroup does. See for example http://svn.wikimedia.org/doc/group__SpecialPage.html where it's impossible to see related files, classes, ... that should belong to that group.
* Added @file to file description, it seems that it should be explicitely decalred for file descriptions, otherwise doxygen will think that the comment document the first class, variabled, function, ... that is in that file.
* Removed some empty comments
* Removed some ?>
Added following groups:
* ExternalStorage
* JobQueue
* MaintenanceLanguage
One more thing: there are still a lot of warnings when generating the doc.
2008-05-20 17:13:28 +00:00
|
|
|
* @ingroup Media
|
2007-04-24 06:53:31 +00:00
|
|
|
*/
|
2007-04-20 12:31:36 +00:00
|
|
|
class SvgHandler extends ImageHandler {
|
2019-11-12 01:46:51 +00:00
|
|
|
public const SVG_METADATA_VERSION = 2;
|
2010-11-01 23:57:09 +00:00
|
|
|
|
2013-12-05 19:27:27 +00:00
|
|
|
/** @var array A list of metadata tags that can be converted
|
|
|
|
|
* to the commonly used exif tags. This allows messages
|
|
|
|
|
* to be reused, and consistent tag names for {{#formatmetadata:..}}
|
2013-08-28 23:09:07 +00:00
|
|
|
*/
|
2016-02-17 09:09:32 +00:00
|
|
|
private static $metaConversion = [
|
2013-08-28 23:09:07 +00:00
|
|
|
'originalwidth' => 'ImageWidth',
|
|
|
|
|
'originalheight' => 'ImageLength',
|
|
|
|
|
'description' => 'ImageDescription',
|
|
|
|
|
'title' => 'ObjectName',
|
2016-02-17 09:09:32 +00:00
|
|
|
];
|
2013-08-28 23:09:07 +00:00
|
|
|
|
2019-02-25 09:16:30 +00:00
|
|
|
public function isEnabled() {
|
2007-04-20 12:31:36 +00:00
|
|
|
global $wgSVGConverters, $wgSVGConverter;
|
|
|
|
|
if ( !isset( $wgSVGConverters[$wgSVGConverter] ) ) {
|
2020-06-01 05:00:39 +00:00
|
|
|
wfDebug( "\$wgSVGConverter is invalid, disabling SVG rendering." );
|
2013-12-05 10:05:05 +00:00
|
|
|
|
2007-04-20 12:31:36 +00:00
|
|
|
return false;
|
|
|
|
|
} else {
|
|
|
|
|
return true;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2016-02-23 23:47:02 +00:00
|
|
|
public function mustRender( $file ) {
|
2007-04-20 12:31:36 +00:00
|
|
|
return true;
|
|
|
|
|
}
|
|
|
|
|
|
2020-05-17 23:57:36 +00:00
|
|
|
public function isVectorized( $file ) {
|
2010-10-31 21:49:25 +00:00
|
|
|
return true;
|
|
|
|
|
}
|
|
|
|
|
|
2011-02-18 23:34:24 +00:00
|
|
|
/**
|
2013-12-05 19:27:27 +00:00
|
|
|
* @param File $file
|
2011-02-18 23:34:24 +00:00
|
|
|
* @return bool
|
|
|
|
|
*/
|
2020-05-17 23:57:36 +00:00
|
|
|
public function isAnimatedImage( $file ) {
|
2013-12-05 19:27:27 +00:00
|
|
|
# @todo Detect animated SVGs
|
Use the unserialized form of image metadata internally
Image metadata is usually a serialized string representing an array.
Passing the string around internally and having everything unserialize
it is an awkward convention.
Also, many image handlers were reading the file twice: once for
getMetadata() and again for getImageSize(). Often getMetadata()
would actually read the width and height and then throw it away.
So, in filerepo:
* Add File::getMetadataItem(), which promises to allow partial
loading of metadata per my proposal on T275268 in a future commit.
* Add File::getMetadataArray(), which returns the unserialized array.
Some file handlers were returning non-serializable strings from
getMetadata(), so I gave them a legacy array form ['_error' => ...]
* Changed MWFileProps to return the array form of metadata.
* Deprecate the weird File::getImageSize(). It was apparently not
called by anything, but was overridden by UnregisteredLocalFile.
* Wrap serialize/unserialize with File::getMetadataForDb() and
File::loadMetadataFromDb() in preparation for T275268.
In MediaHandler:
* Merged MediaHandler::getImageSize() and MediaHandler::getMetadata()
into getSizeAndMetadata(). Deprecated the old methods.
* Instead of isMetadataValid() we now have isFileMetadataValid(), which
only gets a File object, so it can decide what data it needs to load.
* Simplified getPageDimensions() by having it return false for non-paged
media. It was not called in that case, but was implemented anyway.
In specific handlers:
* Rename DjVuHandler::getUnserializedMetadata() and
extractTreesFromMetadata() for clarity. "Metadata" in these function
names meant an XML string.
* Updated DjVuImage::getImageSize() to provide image sizes in the new
style.
* In ExifBitmapHandler, getRotationForExif() now takes just the
Orientation tag, rather than a serialized string. Also renamed for
clarity.
* In GIFMetadataExtractor, return the width, height and bits per channel
instead of throwing them away. There was some conflation in
decodeBPP() which I picked apart. Refer to GIF89a section 18.
* In JpegMetadataExtractor, process the SOF0/SOF2 segment to extract
bits per channel, width, height and components (channel count). This
is essentially a port of PHP's getimagesize(), so should be bugwards
compatible.
* In PNGMetadataExtractor, return the width and height, which were
previously assigned to unused local variables. I verified the
implementation by referring to the specification.
* In SvgHandler, retain the version validation from unpackMetadata(),
but rename the function since it now takes an array as input.
In tests:
* In ExifBitmapTest, refactored some tests by using a provider.
* In GIFHandlerTest and PNGHandlerTest, I removed the tests in which
getMetadata() returns null, since it doesn't make sense when ported to
getMetadataArray(). I added tests for empty arrays instead.
* In tests, I retained serialization of input data since I figure it's
useful to confirm that existing database rows will continue to be read
correctly. I removed serialization of expected values, replacing them
with plain data.
* In tests, I replaced access to private class constants like
BROKEN_FILE with string literals, since stability is essential. If
the class constant changes, the test should fail.
Elsewhere:
* In maintenance/refreshImageMetadata.php, I removed the check for
shrinking image metadata, since it's not easy to implement and is
not future compatible. Image metadata is expected to shrink in
future.
Bug: T275268
Change-Id: I039785d5b6439d71dcc21dcb972177dba5c3a67d
2021-05-19 00:24:32 +00:00
|
|
|
$metadata = $this->validateMetadata( $file->getMetadataArray() );
|
|
|
|
|
if ( isset( $metadata['animated'] ) ) {
|
|
|
|
|
return $metadata['animated'];
|
2010-11-04 00:35:29 +00:00
|
|
|
}
|
2013-12-05 10:05:05 +00:00
|
|
|
|
2010-11-01 23:57:09 +00:00
|
|
|
return false;
|
|
|
|
|
}
|
|
|
|
|
|
2013-11-16 01:47:51 +00:00
|
|
|
/**
|
|
|
|
|
* Which languages (systemLanguage attribute) is supported.
|
|
|
|
|
*
|
|
|
|
|
* @note This list is not guaranteed to be exhaustive.
|
|
|
|
|
* To avoid OOM errors, we only look at first bit of a file.
|
|
|
|
|
* Thus all languages on this list are present in the file,
|
|
|
|
|
* but its possible for the file to have a language not on
|
|
|
|
|
* this list.
|
|
|
|
|
*
|
|
|
|
|
* @param File $file
|
2020-10-28 10:01:33 +00:00
|
|
|
* @return string[] Array of language codes, or empty if no language switching supported.
|
2013-11-16 01:47:51 +00:00
|
|
|
*/
|
|
|
|
|
public function getAvailableLanguages( File $file ) {
|
2016-02-17 09:09:32 +00:00
|
|
|
$langList = [];
|
Use the unserialized form of image metadata internally
Image metadata is usually a serialized string representing an array.
Passing the string around internally and having everything unserialize
it is an awkward convention.
Also, many image handlers were reading the file twice: once for
getMetadata() and again for getImageSize(). Often getMetadata()
would actually read the width and height and then throw it away.
So, in filerepo:
* Add File::getMetadataItem(), which promises to allow partial
loading of metadata per my proposal on T275268 in a future commit.
* Add File::getMetadataArray(), which returns the unserialized array.
Some file handlers were returning non-serializable strings from
getMetadata(), so I gave them a legacy array form ['_error' => ...]
* Changed MWFileProps to return the array form of metadata.
* Deprecate the weird File::getImageSize(). It was apparently not
called by anything, but was overridden by UnregisteredLocalFile.
* Wrap serialize/unserialize with File::getMetadataForDb() and
File::loadMetadataFromDb() in preparation for T275268.
In MediaHandler:
* Merged MediaHandler::getImageSize() and MediaHandler::getMetadata()
into getSizeAndMetadata(). Deprecated the old methods.
* Instead of isMetadataValid() we now have isFileMetadataValid(), which
only gets a File object, so it can decide what data it needs to load.
* Simplified getPageDimensions() by having it return false for non-paged
media. It was not called in that case, but was implemented anyway.
In specific handlers:
* Rename DjVuHandler::getUnserializedMetadata() and
extractTreesFromMetadata() for clarity. "Metadata" in these function
names meant an XML string.
* Updated DjVuImage::getImageSize() to provide image sizes in the new
style.
* In ExifBitmapHandler, getRotationForExif() now takes just the
Orientation tag, rather than a serialized string. Also renamed for
clarity.
* In GIFMetadataExtractor, return the width, height and bits per channel
instead of throwing them away. There was some conflation in
decodeBPP() which I picked apart. Refer to GIF89a section 18.
* In JpegMetadataExtractor, process the SOF0/SOF2 segment to extract
bits per channel, width, height and components (channel count). This
is essentially a port of PHP's getimagesize(), so should be bugwards
compatible.
* In PNGMetadataExtractor, return the width and height, which were
previously assigned to unused local variables. I verified the
implementation by referring to the specification.
* In SvgHandler, retain the version validation from unpackMetadata(),
but rename the function since it now takes an array as input.
In tests:
* In ExifBitmapTest, refactored some tests by using a provider.
* In GIFHandlerTest and PNGHandlerTest, I removed the tests in which
getMetadata() returns null, since it doesn't make sense when ported to
getMetadataArray(). I added tests for empty arrays instead.
* In tests, I retained serialization of input data since I figure it's
useful to confirm that existing database rows will continue to be read
correctly. I removed serialization of expected values, replacing them
with plain data.
* In tests, I replaced access to private class constants like
BROKEN_FILE with string literals, since stability is essential. If
the class constant changes, the test should fail.
Elsewhere:
* In maintenance/refreshImageMetadata.php, I removed the check for
shrinking image metadata, since it's not easy to implement and is
not future compatible. Image metadata is expected to shrink in
future.
Bug: T275268
Change-Id: I039785d5b6439d71dcc21dcb972177dba5c3a67d
2021-05-19 00:24:32 +00:00
|
|
|
$metadata = $this->validateMetadata( $file->getMetadataArray() );
|
|
|
|
|
if ( isset( $metadata['translations'] ) ) {
|
|
|
|
|
foreach ( $metadata['translations'] as $lang => $langType ) {
|
|
|
|
|
if ( $langType === SVGReader::LANG_FULL_MATCH ) {
|
|
|
|
|
$langList[] = strtolower( $lang );
|
2013-11-16 01:47:51 +00:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
2017-10-18 12:38:48 +00:00
|
|
|
return array_unique( $langList );
|
2013-11-16 01:47:51 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
2017-10-18 12:38:48 +00:00
|
|
|
* SVG's systemLanguage matching rules state:
|
|
|
|
|
* 'The `systemLanguage` attribute ... [e]valuates to "true" if one of the languages indicated
|
|
|
|
|
* by user preferences exactly equals one of the languages given in the value of this parameter,
|
|
|
|
|
* or if one of the languages indicated by user preferences exactly equals a prefix of one of
|
|
|
|
|
* the languages given in the value of this parameter such that the first tag character
|
|
|
|
|
* following the prefix is "-".'
|
2013-11-16 01:47:51 +00:00
|
|
|
*
|
2017-10-18 12:38:48 +00:00
|
|
|
* Return the first element of $svgLanguages that matches $userPreferredLanguage
|
|
|
|
|
*
|
|
|
|
|
* @see https://www.w3.org/TR/SVG/struct.html#SystemLanguageAttribute
|
|
|
|
|
* @param string $userPreferredLanguage
|
|
|
|
|
* @param array $svgLanguages
|
|
|
|
|
* @return string|null
|
|
|
|
|
*/
|
|
|
|
|
public function getMatchedLanguage( $userPreferredLanguage, array $svgLanguages ) {
|
|
|
|
|
foreach ( $svgLanguages as $svgLang ) {
|
|
|
|
|
if ( strcasecmp( $svgLang, $userPreferredLanguage ) === 0 ) {
|
|
|
|
|
return $svgLang;
|
|
|
|
|
}
|
|
|
|
|
$trimmedSvgLang = $svgLang;
|
|
|
|
|
while ( strpos( $trimmedSvgLang, '-' ) !== false ) {
|
|
|
|
|
$trimmedSvgLang = substr( $trimmedSvgLang, 0, strrpos( $trimmedSvgLang, '-' ) );
|
|
|
|
|
if ( strcasecmp( $trimmedSvgLang, $userPreferredLanguage ) === 0 ) {
|
|
|
|
|
return $svgLang;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
return null;
|
|
|
|
|
}
|
|
|
|
|
|
2018-10-06 00:56:53 +00:00
|
|
|
/**
|
|
|
|
|
* Determines render language from image parameters
|
|
|
|
|
*
|
|
|
|
|
* @param array $params
|
|
|
|
|
* @return string
|
|
|
|
|
*/
|
|
|
|
|
protected function getLanguageFromParams( array $params ) {
|
|
|
|
|
return $params['lang'] ?? $params['targetlang'] ?? 'en';
|
|
|
|
|
}
|
|
|
|
|
|
2017-10-18 12:38:48 +00:00
|
|
|
/**
|
|
|
|
|
* What language to render file in if none selected
|
|
|
|
|
*
|
|
|
|
|
* @param File $file Language code
|
|
|
|
|
* @return string
|
2013-11-16 01:47:51 +00:00
|
|
|
*/
|
|
|
|
|
public function getDefaultRenderLanguage( File $file ) {
|
|
|
|
|
return 'en';
|
|
|
|
|
}
|
|
|
|
|
|
2012-08-19 01:19:53 +00:00
|
|
|
/**
|
|
|
|
|
* We do not support making animated svg thumbnails
|
2014-08-14 19:34:55 +00:00
|
|
|
* @param File $file
|
|
|
|
|
* @return bool
|
2012-08-19 01:19:53 +00:00
|
|
|
*/
|
2020-05-17 23:57:36 +00:00
|
|
|
public function canAnimateThumbnail( $file ) {
|
2012-08-19 01:19:53 +00:00
|
|
|
return false;
|
|
|
|
|
}
|
|
|
|
|
|
2011-02-18 23:34:24 +00:00
|
|
|
/**
|
2013-12-05 19:27:27 +00:00
|
|
|
* @param File $image
|
2017-08-11 00:23:16 +00:00
|
|
|
* @param array &$params
|
2011-02-18 23:34:24 +00:00
|
|
|
* @return bool
|
|
|
|
|
*/
|
2018-10-06 00:56:53 +00:00
|
|
|
public function normaliseParams( $image, &$params ) {
|
|
|
|
|
if ( parent::normaliseParams( $image, $params ) ) {
|
|
|
|
|
$params = $this->normaliseParamsInternal( $image, $params );
|
|
|
|
|
return true;
|
2007-04-20 12:31:36 +00:00
|
|
|
}
|
2018-10-06 00:56:53 +00:00
|
|
|
|
|
|
|
|
return false;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Code taken out of normaliseParams() for testability
|
|
|
|
|
*
|
|
|
|
|
* @since 1.33
|
|
|
|
|
*
|
|
|
|
|
* @param File $image
|
|
|
|
|
* @param array $params
|
|
|
|
|
* @return array Modified $params
|
|
|
|
|
*/
|
|
|
|
|
protected function normaliseParamsInternal( $image, $params ) {
|
|
|
|
|
global $wgSVGMaxSize;
|
|
|
|
|
|
2011-08-02 14:13:51 +00:00
|
|
|
# Don't make an image bigger than wgMaxSVGSize on the smaller side
|
|
|
|
|
if ( $params['physicalWidth'] <= $params['physicalHeight'] ) {
|
|
|
|
|
if ( $params['physicalWidth'] > $wgSVGMaxSize ) {
|
|
|
|
|
$srcWidth = $image->getWidth( $params['page'] );
|
|
|
|
|
$srcHeight = $image->getHeight( $params['page'] );
|
|
|
|
|
$params['physicalWidth'] = $wgSVGMaxSize;
|
|
|
|
|
$params['physicalHeight'] = File::scaleHeight( $srcWidth, $srcHeight, $wgSVGMaxSize );
|
|
|
|
|
}
|
2019-03-29 20:12:24 +00:00
|
|
|
} elseif ( $params['physicalHeight'] > $wgSVGMaxSize ) {
|
|
|
|
|
$srcWidth = $image->getWidth( $params['page'] );
|
|
|
|
|
$srcHeight = $image->getHeight( $params['page'] );
|
|
|
|
|
$params['physicalWidth'] = File::scaleHeight( $srcHeight, $srcWidth, $wgSVGMaxSize );
|
|
|
|
|
$params['physicalHeight'] = $wgSVGMaxSize;
|
2007-04-20 12:31:36 +00:00
|
|
|
}
|
2018-10-06 00:56:53 +00:00
|
|
|
// To prevent the proliferation of thumbnails in languages not present in SVGs, unless
|
|
|
|
|
// explicitly forced by user.
|
2019-03-29 20:12:24 +00:00
|
|
|
if ( isset( $params['targetlang'] ) && !$image->getMatchedLanguage( $params['targetlang'] ) ) {
|
|
|
|
|
unset( $params['targetlang'] );
|
2018-10-06 00:56:53 +00:00
|
|
|
}
|
2013-12-05 10:05:05 +00:00
|
|
|
|
2018-10-06 00:56:53 +00:00
|
|
|
return $params;
|
2007-04-20 12:31:36 +00:00
|
|
|
}
|
2008-04-14 07:45:50 +00:00
|
|
|
|
2011-02-18 23:34:24 +00:00
|
|
|
/**
|
2013-12-05 19:27:27 +00:00
|
|
|
* @param File $image
|
|
|
|
|
* @param string $dstPath
|
|
|
|
|
* @param string $dstUrl
|
|
|
|
|
* @param array $params
|
2011-02-18 23:34:24 +00:00
|
|
|
* @param int $flags
|
|
|
|
|
* @return bool|MediaTransformError|ThumbnailImage|TransformParameterError
|
|
|
|
|
*/
|
2020-05-17 23:57:36 +00:00
|
|
|
public function doTransform( $image, $dstPath, $dstUrl, $params, $flags = 0 ) {
|
2007-04-20 12:31:36 +00:00
|
|
|
if ( !$this->normaliseParams( $image, $params ) ) {
|
|
|
|
|
return new TransformParameterError( $params );
|
|
|
|
|
}
|
|
|
|
|
$clientWidth = $params['width'];
|
|
|
|
|
$clientHeight = $params['height'];
|
|
|
|
|
$physicalWidth = $params['physicalWidth'];
|
|
|
|
|
$physicalHeight = $params['physicalHeight'];
|
2018-10-06 00:56:53 +00:00
|
|
|
$lang = $this->getLanguageFromParams( $params );
|
2007-04-20 12:31:36 +00:00
|
|
|
|
|
|
|
|
if ( $flags & self::TRANSFORM_LATER ) {
|
2012-09-01 16:12:48 +00:00
|
|
|
return new ThumbnailImage( $image, $dstUrl, $dstPath, $params );
|
2007-04-20 12:31:36 +00:00
|
|
|
}
|
|
|
|
|
|
Use the unserialized form of image metadata internally
Image metadata is usually a serialized string representing an array.
Passing the string around internally and having everything unserialize
it is an awkward convention.
Also, many image handlers were reading the file twice: once for
getMetadata() and again for getImageSize(). Often getMetadata()
would actually read the width and height and then throw it away.
So, in filerepo:
* Add File::getMetadataItem(), which promises to allow partial
loading of metadata per my proposal on T275268 in a future commit.
* Add File::getMetadataArray(), which returns the unserialized array.
Some file handlers were returning non-serializable strings from
getMetadata(), so I gave them a legacy array form ['_error' => ...]
* Changed MWFileProps to return the array form of metadata.
* Deprecate the weird File::getImageSize(). It was apparently not
called by anything, but was overridden by UnregisteredLocalFile.
* Wrap serialize/unserialize with File::getMetadataForDb() and
File::loadMetadataFromDb() in preparation for T275268.
In MediaHandler:
* Merged MediaHandler::getImageSize() and MediaHandler::getMetadata()
into getSizeAndMetadata(). Deprecated the old methods.
* Instead of isMetadataValid() we now have isFileMetadataValid(), which
only gets a File object, so it can decide what data it needs to load.
* Simplified getPageDimensions() by having it return false for non-paged
media. It was not called in that case, but was implemented anyway.
In specific handlers:
* Rename DjVuHandler::getUnserializedMetadata() and
extractTreesFromMetadata() for clarity. "Metadata" in these function
names meant an XML string.
* Updated DjVuImage::getImageSize() to provide image sizes in the new
style.
* In ExifBitmapHandler, getRotationForExif() now takes just the
Orientation tag, rather than a serialized string. Also renamed for
clarity.
* In GIFMetadataExtractor, return the width, height and bits per channel
instead of throwing them away. There was some conflation in
decodeBPP() which I picked apart. Refer to GIF89a section 18.
* In JpegMetadataExtractor, process the SOF0/SOF2 segment to extract
bits per channel, width, height and components (channel count). This
is essentially a port of PHP's getimagesize(), so should be bugwards
compatible.
* In PNGMetadataExtractor, return the width and height, which were
previously assigned to unused local variables. I verified the
implementation by referring to the specification.
* In SvgHandler, retain the version validation from unpackMetadata(),
but rename the function since it now takes an array as input.
In tests:
* In ExifBitmapTest, refactored some tests by using a provider.
* In GIFHandlerTest and PNGHandlerTest, I removed the tests in which
getMetadata() returns null, since it doesn't make sense when ported to
getMetadataArray(). I added tests for empty arrays instead.
* In tests, I retained serialization of input data since I figure it's
useful to confirm that existing database rows will continue to be read
correctly. I removed serialization of expected values, replacing them
with plain data.
* In tests, I replaced access to private class constants like
BROKEN_FILE with string literals, since stability is essential. If
the class constant changes, the test should fail.
Elsewhere:
* In maintenance/refreshImageMetadata.php, I removed the check for
shrinking image metadata, since it's not easy to implement and is
not future compatible. Image metadata is expected to shrink in
future.
Bug: T275268
Change-Id: I039785d5b6439d71dcc21dcb972177dba5c3a67d
2021-05-19 00:24:32 +00:00
|
|
|
$metadata = $this->validateMetadata( $image->getMetadataArray() );
|
2012-11-26 21:17:41 +00:00
|
|
|
if ( isset( $metadata['error'] ) ) { // sanity check
|
2016-11-01 19:08:38 +00:00
|
|
|
$err = wfMessage( 'svg-long-error', $metadata['error']['message'] );
|
2013-12-05 10:05:05 +00:00
|
|
|
|
2012-11-26 21:17:41 +00:00
|
|
|
return new MediaTransformError( 'thumbnail_error', $clientWidth, $clientHeight, $err );
|
|
|
|
|
}
|
|
|
|
|
|
2011-07-25 22:01:19 +00:00
|
|
|
if ( !wfMkdirParents( dirname( $dstPath ), null, __METHOD__ ) ) {
|
2008-04-14 07:45:50 +00:00
|
|
|
return new MediaTransformError( 'thumbnail_error', $clientWidth, $clientHeight,
|
2016-11-01 19:08:38 +00:00
|
|
|
wfMessage( 'thumbnail_dest_directory' ) );
|
2007-04-20 12:31:36 +00:00
|
|
|
}
|
2010-11-04 00:35:29 +00:00
|
|
|
|
2012-08-17 03:23:37 +00:00
|
|
|
$srcPath = $image->getLocalRefPath();
|
2014-07-10 07:14:45 +00:00
|
|
|
if ( $srcPath === false ) { // Failed to get local copy
|
|
|
|
|
wfDebugLog( 'thumbnail',
|
|
|
|
|
sprintf( 'Thumbnail failed on %s: could not get local copy of "%s"',
|
|
|
|
|
wfHostname(), $image->getName() ) );
|
|
|
|
|
|
|
|
|
|
return new MediaTransformError( 'thumbnail_error',
|
|
|
|
|
$params['width'], $params['height'],
|
2016-11-01 19:08:38 +00:00
|
|
|
wfMessage( 'filemissing' )
|
2014-07-10 07:14:45 +00:00
|
|
|
);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// Make a temp dir with a symlink to the local copy in it.
|
|
|
|
|
// This plays well with rsvg-convert policy for external entities.
|
|
|
|
|
// https://git.gnome.org/browse/librsvg/commit/?id=f01aded72c38f0e18bc7ff67dee800e380251c8e
|
|
|
|
|
$tmpDir = wfTempDir() . '/svg_' . wfRandomString( 24 );
|
|
|
|
|
$lnPath = "$tmpDir/" . basename( $srcPath );
|
2016-02-04 04:21:22 +00:00
|
|
|
$ok = mkdir( $tmpDir, 0771 );
|
|
|
|
|
if ( !$ok ) {
|
|
|
|
|
wfDebugLog( 'thumbnail',
|
|
|
|
|
sprintf( 'Thumbnail failed on %s: could not create temporary directory %s',
|
|
|
|
|
wfHostname(), $tmpDir ) );
|
|
|
|
|
return new MediaTransformError( 'thumbnail_error',
|
|
|
|
|
$params['width'], $params['height'],
|
|
|
|
|
wfMessage( 'thumbnail-temp-create' )->text()
|
|
|
|
|
);
|
|
|
|
|
}
|
|
|
|
|
$ok = symlink( $srcPath, $lnPath );
|
2015-07-15 01:01:11 +00:00
|
|
|
/** @noinspection PhpUnusedLocalVariableInspection */
|
2021-02-10 22:31:02 +00:00
|
|
|
$cleaner = new ScopedCallback( static function () use ( $tmpDir, $lnPath ) {
|
2018-02-10 07:52:26 +00:00
|
|
|
Wikimedia\suppressWarnings();
|
2014-07-10 07:14:45 +00:00
|
|
|
unlink( $lnPath );
|
|
|
|
|
rmdir( $tmpDir );
|
2018-02-10 07:52:26 +00:00
|
|
|
Wikimedia\restoreWarnings();
|
2014-07-10 07:14:45 +00:00
|
|
|
} );
|
|
|
|
|
if ( !$ok ) {
|
|
|
|
|
wfDebugLog( 'thumbnail',
|
|
|
|
|
sprintf( 'Thumbnail failed on %s: could not link %s to %s',
|
|
|
|
|
wfHostname(), $lnPath, $srcPath ) );
|
|
|
|
|
return new MediaTransformError( 'thumbnail_error',
|
|
|
|
|
$params['width'], $params['height'],
|
2016-11-01 19:08:38 +00:00
|
|
|
wfMessage( 'thumbnail-temp-create' )
|
2014-07-10 07:14:45 +00:00
|
|
|
);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
$status = $this->rasterize( $lnPath, $dstPath, $physicalWidth, $physicalHeight, $lang );
|
2012-11-26 21:17:41 +00:00
|
|
|
if ( $status === true ) {
|
2012-09-01 16:12:48 +00:00
|
|
|
return new ThumbnailImage( $image, $dstUrl, $dstPath, $params );
|
2008-11-18 01:18:12 +00:00
|
|
|
} else {
|
|
|
|
|
return $status; // MediaTransformError
|
|
|
|
|
}
|
|
|
|
|
}
|
2010-11-04 00:35:29 +00:00
|
|
|
|
2011-05-21 19:35:16 +00:00
|
|
|
/**
|
2012-10-07 23:35:26 +00:00
|
|
|
* Transform an SVG file to PNG
|
|
|
|
|
* This function can be called outside of thumbnail contexts
|
|
|
|
|
* @param string $srcPath
|
|
|
|
|
* @param string $dstPath
|
|
|
|
|
* @param string $width
|
|
|
|
|
* @param string $height
|
2021-09-20 22:18:39 +00:00
|
|
|
* @param string|false $lang Language code of the language to render the SVG in
|
2012-10-07 23:35:26 +00:00
|
|
|
* @throws MWException
|
|
|
|
|
* @return bool|MediaTransformError
|
|
|
|
|
*/
|
2013-05-24 12:56:06 +00:00
|
|
|
public function rasterize( $srcPath, $dstPath, $width, $height, $lang = false ) {
|
2008-11-18 01:18:12 +00:00
|
|
|
global $wgSVGConverters, $wgSVGConverter, $wgSVGConverterPath;
|
2007-04-20 12:31:36 +00:00
|
|
|
$err = false;
|
2010-09-02 22:15:20 +00:00
|
|
|
$retval = '';
|
2008-11-18 01:18:12 +00:00
|
|
|
if ( isset( $wgSVGConverters[$wgSVGConverter] ) ) {
|
2011-03-12 19:59:41 +00:00
|
|
|
if ( is_array( $wgSVGConverters[$wgSVGConverter] ) ) {
|
|
|
|
|
// This is a PHP callable
|
|
|
|
|
$func = $wgSVGConverters[$wgSVGConverter][0];
|
|
|
|
|
if ( !is_callable( $func ) ) {
|
|
|
|
|
throw new MWException( "$func is not callable" );
|
|
|
|
|
}
|
2018-06-09 23:26:32 +00:00
|
|
|
$err = $func( $srcPath,
|
|
|
|
|
$dstPath,
|
|
|
|
|
$width,
|
|
|
|
|
$height,
|
|
|
|
|
$lang,
|
|
|
|
|
...array_slice( $wgSVGConverters[$wgSVGConverter], 1 )
|
|
|
|
|
);
|
2011-03-12 19:59:41 +00:00
|
|
|
$retval = (bool)$err;
|
|
|
|
|
} else {
|
|
|
|
|
// External command
|
|
|
|
|
$cmd = str_replace(
|
2016-02-17 09:09:32 +00:00
|
|
|
[ '$path/', '$width', '$height', '$input', '$output' ],
|
2019-04-07 11:36:22 +00:00
|
|
|
[ $wgSVGConverterPath ? Shell::escape( "$wgSVGConverterPath/" ) : "",
|
2013-02-03 19:42:08 +00:00
|
|
|
intval( $width ),
|
|
|
|
|
intval( $height ),
|
2019-04-07 11:36:22 +00:00
|
|
|
Shell::escape( $srcPath ),
|
|
|
|
|
Shell::escape( $dstPath ) ],
|
2011-03-12 19:59:41 +00:00
|
|
|
$wgSVGConverters[$wgSVGConverter]
|
2013-09-12 01:40:56 +00:00
|
|
|
);
|
2013-05-24 12:56:06 +00:00
|
|
|
|
2016-02-17 09:09:32 +00:00
|
|
|
$env = [];
|
2013-08-24 15:06:25 +00:00
|
|
|
if ( $lang !== false ) {
|
2013-05-24 12:56:06 +00:00
|
|
|
$env['LANG'] = $lang;
|
|
|
|
|
}
|
|
|
|
|
|
2020-06-01 05:00:39 +00:00
|
|
|
wfDebug( __METHOD__ . ": $cmd" );
|
2013-09-12 01:40:56 +00:00
|
|
|
$err = wfShellExecWithStderr( $cmd, $retval, $env );
|
2011-03-12 19:59:41 +00:00
|
|
|
}
|
2007-04-20 12:31:36 +00:00
|
|
|
}
|
|
|
|
|
$removed = $this->removeBadFile( $dstPath, $retval );
|
|
|
|
|
if ( $retval != 0 || $removed ) {
|
2013-12-04 16:28:12 +00:00
|
|
|
$this->logErrorForExternalProcess( $retval, $err, $cmd );
|
2008-11-25 18:26:21 +00:00
|
|
|
return new MediaTransformError( 'thumbnail_error', $width, $height, $err );
|
2007-04-20 12:31:36 +00:00
|
|
|
}
|
2013-12-05 10:05:05 +00:00
|
|
|
|
2008-11-18 01:18:12 +00:00
|
|
|
return true;
|
2007-04-20 12:31:36 +00:00
|
|
|
}
|
2011-07-25 22:01:19 +00:00
|
|
|
|
2011-03-12 19:59:41 +00:00
|
|
|
public static function rasterizeImagickExt( $srcPath, $dstPath, $width, $height ) {
|
|
|
|
|
$im = new Imagick( $srcPath );
|
|
|
|
|
$im->setImageFormat( 'png' );
|
|
|
|
|
$im->setBackgroundColor( 'transparent' );
|
|
|
|
|
$im->setImageDepth( 8 );
|
2011-07-25 22:01:19 +00:00
|
|
|
|
2011-03-12 19:59:41 +00:00
|
|
|
if ( !$im->thumbnailImage( intval( $width ), intval( $height ), /* fit */ false ) ) {
|
|
|
|
|
return 'Could not resize image';
|
|
|
|
|
}
|
|
|
|
|
if ( !$im->writeImage( $dstPath ) ) {
|
|
|
|
|
return "Could not write to $dstPath";
|
|
|
|
|
}
|
|
|
|
|
}
|
2007-04-20 12:31:36 +00:00
|
|
|
|
2019-02-25 09:16:30 +00:00
|
|
|
public function getThumbType( $ext, $mime, $params = null ) {
|
2016-02-17 09:09:32 +00:00
|
|
|
return [ 'png', 'image/png' ];
|
2007-04-20 12:31:36 +00:00
|
|
|
}
|
Basic integrated audio/video support, with Ogg implementation.
* JavaScript video player based loosely on Greg Maxwell's player
* Image page text snippet customisation
* Abstraction of transform parameters in the parser. Introduced Linker::makeImageLink2().
* Made canRender(), mustRender() depend on file, not just on handler. Moved width=0, height=0 checking to ImageHandler::canRender(), since audio streams have width=height=0 but should be rendered.
Also:
* Automatic upgrade for oldimage rows on image page view, allows media handler selection based on oi_*_mime
* oi_*_mime unconditionally referenced, REQUIRES SCHEMA UPGRADE
* Don't destroy file info for missing files on upgrade
* Simple, centralised extension message file handling
* Made MessageCache::loadAllMessages non-static, optimised for repeated-call case due to abuse in User.php
* Support for lightweight parser output hooks, with callback whitelist for security
* Moved Linker::formatSize() to Language, to join the new formatTimePeriod() and formatBitrate()
* Introduced MagicWordArray, regex capture trick requires that magic word IDs DO NOT CONTAIN HYPHENS.
2007-08-15 10:50:09 +00:00
|
|
|
|
2011-02-18 23:34:24 +00:00
|
|
|
/**
|
2012-08-17 02:18:39 +00:00
|
|
|
* Subtitle for the image. Different from the base
|
|
|
|
|
* class so it can be denoted that SVG's have
|
|
|
|
|
* a "nominal" resolution, and not a fixed one,
|
|
|
|
|
* as well as so animation can be denoted.
|
|
|
|
|
*
|
2013-12-05 19:27:27 +00:00
|
|
|
* @param File $file
|
2011-02-18 23:34:24 +00:00
|
|
|
* @return string
|
|
|
|
|
*/
|
2019-02-25 09:16:30 +00:00
|
|
|
public function getLongDesc( $file ) {
|
Use the unserialized form of image metadata internally
Image metadata is usually a serialized string representing an array.
Passing the string around internally and having everything unserialize
it is an awkward convention.
Also, many image handlers were reading the file twice: once for
getMetadata() and again for getImageSize(). Often getMetadata()
would actually read the width and height and then throw it away.
So, in filerepo:
* Add File::getMetadataItem(), which promises to allow partial
loading of metadata per my proposal on T275268 in a future commit.
* Add File::getMetadataArray(), which returns the unserialized array.
Some file handlers were returning non-serializable strings from
getMetadata(), so I gave them a legacy array form ['_error' => ...]
* Changed MWFileProps to return the array form of metadata.
* Deprecate the weird File::getImageSize(). It was apparently not
called by anything, but was overridden by UnregisteredLocalFile.
* Wrap serialize/unserialize with File::getMetadataForDb() and
File::loadMetadataFromDb() in preparation for T275268.
In MediaHandler:
* Merged MediaHandler::getImageSize() and MediaHandler::getMetadata()
into getSizeAndMetadata(). Deprecated the old methods.
* Instead of isMetadataValid() we now have isFileMetadataValid(), which
only gets a File object, so it can decide what data it needs to load.
* Simplified getPageDimensions() by having it return false for non-paged
media. It was not called in that case, but was implemented anyway.
In specific handlers:
* Rename DjVuHandler::getUnserializedMetadata() and
extractTreesFromMetadata() for clarity. "Metadata" in these function
names meant an XML string.
* Updated DjVuImage::getImageSize() to provide image sizes in the new
style.
* In ExifBitmapHandler, getRotationForExif() now takes just the
Orientation tag, rather than a serialized string. Also renamed for
clarity.
* In GIFMetadataExtractor, return the width, height and bits per channel
instead of throwing them away. There was some conflation in
decodeBPP() which I picked apart. Refer to GIF89a section 18.
* In JpegMetadataExtractor, process the SOF0/SOF2 segment to extract
bits per channel, width, height and components (channel count). This
is essentially a port of PHP's getimagesize(), so should be bugwards
compatible.
* In PNGMetadataExtractor, return the width and height, which were
previously assigned to unused local variables. I verified the
implementation by referring to the specification.
* In SvgHandler, retain the version validation from unpackMetadata(),
but rename the function since it now takes an array as input.
In tests:
* In ExifBitmapTest, refactored some tests by using a provider.
* In GIFHandlerTest and PNGHandlerTest, I removed the tests in which
getMetadata() returns null, since it doesn't make sense when ported to
getMetadataArray(). I added tests for empty arrays instead.
* In tests, I retained serialization of input data since I figure it's
useful to confirm that existing database rows will continue to be read
correctly. I removed serialization of expected values, replacing them
with plain data.
* In tests, I replaced access to private class constants like
BROKEN_FILE with string literals, since stability is essential. If
the class constant changes, the test should fail.
Elsewhere:
* In maintenance/refreshImageMetadata.php, I removed the check for
shrinking image metadata, since it's not easy to implement and is
not future compatible. Image metadata is expected to shrink in
future.
Bug: T275268
Change-Id: I039785d5b6439d71dcc21dcb972177dba5c3a67d
2021-05-19 00:24:32 +00:00
|
|
|
$metadata = $this->validateMetadata( $file->getMetadataArray() );
|
2012-11-26 21:17:41 +00:00
|
|
|
if ( isset( $metadata['error'] ) ) {
|
|
|
|
|
return wfMessage( 'svg-long-error', $metadata['error']['message'] )->text();
|
|
|
|
|
}
|
|
|
|
|
|
2012-08-17 02:18:39 +00:00
|
|
|
if ( $this->isAnimatedImage( $file ) ) {
|
|
|
|
|
$msg = wfMessage( 'svg-long-desc-animated' );
|
|
|
|
|
} else {
|
|
|
|
|
$msg = wfMessage( 'svg-long-desc' );
|
|
|
|
|
}
|
|
|
|
|
|
2021-09-20 22:35:30 +00:00
|
|
|
return $msg->numParams( $file->getWidth(), $file->getHeight() )->sizeParams( $file->getSize() )->parse();
|
Basic integrated audio/video support, with Ogg implementation.
* JavaScript video player based loosely on Greg Maxwell's player
* Image page text snippet customisation
* Abstraction of transform parameters in the parser. Introduced Linker::makeImageLink2().
* Made canRender(), mustRender() depend on file, not just on handler. Moved width=0, height=0 checking to ImageHandler::canRender(), since audio streams have width=height=0 but should be rendered.
Also:
* Automatic upgrade for oldimage rows on image page view, allows media handler selection based on oi_*_mime
* oi_*_mime unconditionally referenced, REQUIRES SCHEMA UPGRADE
* Don't destroy file info for missing files on upgrade
* Simple, centralised extension message file handling
* Made MessageCache::loadAllMessages non-static, optimised for repeated-call case due to abuse in User.php
* Support for lightweight parser output hooks, with callback whitelist for security
* Moved Linker::formatSize() to Language, to join the new formatTimePeriod() and formatBitrate()
* Introduced MagicWordArray, regex capture trick requires that magic word IDs DO NOT CONTAIN HYPHENS.
2007-08-15 10:50:09 +00:00
|
|
|
}
|
2010-11-01 23:57:09 +00:00
|
|
|
|
2013-12-05 19:27:27 +00:00
|
|
|
/**
|
Use the unserialized form of image metadata internally
Image metadata is usually a serialized string representing an array.
Passing the string around internally and having everything unserialize
it is an awkward convention.
Also, many image handlers were reading the file twice: once for
getMetadata() and again for getImageSize(). Often getMetadata()
would actually read the width and height and then throw it away.
So, in filerepo:
* Add File::getMetadataItem(), which promises to allow partial
loading of metadata per my proposal on T275268 in a future commit.
* Add File::getMetadataArray(), which returns the unserialized array.
Some file handlers were returning non-serializable strings from
getMetadata(), so I gave them a legacy array form ['_error' => ...]
* Changed MWFileProps to return the array form of metadata.
* Deprecate the weird File::getImageSize(). It was apparently not
called by anything, but was overridden by UnregisteredLocalFile.
* Wrap serialize/unserialize with File::getMetadataForDb() and
File::loadMetadataFromDb() in preparation for T275268.
In MediaHandler:
* Merged MediaHandler::getImageSize() and MediaHandler::getMetadata()
into getSizeAndMetadata(). Deprecated the old methods.
* Instead of isMetadataValid() we now have isFileMetadataValid(), which
only gets a File object, so it can decide what data it needs to load.
* Simplified getPageDimensions() by having it return false for non-paged
media. It was not called in that case, but was implemented anyway.
In specific handlers:
* Rename DjVuHandler::getUnserializedMetadata() and
extractTreesFromMetadata() for clarity. "Metadata" in these function
names meant an XML string.
* Updated DjVuImage::getImageSize() to provide image sizes in the new
style.
* In ExifBitmapHandler, getRotationForExif() now takes just the
Orientation tag, rather than a serialized string. Also renamed for
clarity.
* In GIFMetadataExtractor, return the width, height and bits per channel
instead of throwing them away. There was some conflation in
decodeBPP() which I picked apart. Refer to GIF89a section 18.
* In JpegMetadataExtractor, process the SOF0/SOF2 segment to extract
bits per channel, width, height and components (channel count). This
is essentially a port of PHP's getimagesize(), so should be bugwards
compatible.
* In PNGMetadataExtractor, return the width and height, which were
previously assigned to unused local variables. I verified the
implementation by referring to the specification.
* In SvgHandler, retain the version validation from unpackMetadata(),
but rename the function since it now takes an array as input.
In tests:
* In ExifBitmapTest, refactored some tests by using a provider.
* In GIFHandlerTest and PNGHandlerTest, I removed the tests in which
getMetadata() returns null, since it doesn't make sense when ported to
getMetadataArray(). I added tests for empty arrays instead.
* In tests, I retained serialization of input data since I figure it's
useful to confirm that existing database rows will continue to be read
correctly. I removed serialization of expected values, replacing them
with plain data.
* In tests, I replaced access to private class constants like
BROKEN_FILE with string literals, since stability is essential. If
the class constant changes, the test should fail.
Elsewhere:
* In maintenance/refreshImageMetadata.php, I removed the check for
shrinking image metadata, since it's not easy to implement and is
not future compatible. Image metadata is expected to shrink in
future.
Bug: T275268
Change-Id: I039785d5b6439d71dcc21dcb972177dba5c3a67d
2021-05-19 00:24:32 +00:00
|
|
|
* @param MediaHandlerState $state
|
2013-12-05 19:27:27 +00:00
|
|
|
* @param string $filename
|
Use the unserialized form of image metadata internally
Image metadata is usually a serialized string representing an array.
Passing the string around internally and having everything unserialize
it is an awkward convention.
Also, many image handlers were reading the file twice: once for
getMetadata() and again for getImageSize(). Often getMetadata()
would actually read the width and height and then throw it away.
So, in filerepo:
* Add File::getMetadataItem(), which promises to allow partial
loading of metadata per my proposal on T275268 in a future commit.
* Add File::getMetadataArray(), which returns the unserialized array.
Some file handlers were returning non-serializable strings from
getMetadata(), so I gave them a legacy array form ['_error' => ...]
* Changed MWFileProps to return the array form of metadata.
* Deprecate the weird File::getImageSize(). It was apparently not
called by anything, but was overridden by UnregisteredLocalFile.
* Wrap serialize/unserialize with File::getMetadataForDb() and
File::loadMetadataFromDb() in preparation for T275268.
In MediaHandler:
* Merged MediaHandler::getImageSize() and MediaHandler::getMetadata()
into getSizeAndMetadata(). Deprecated the old methods.
* Instead of isMetadataValid() we now have isFileMetadataValid(), which
only gets a File object, so it can decide what data it needs to load.
* Simplified getPageDimensions() by having it return false for non-paged
media. It was not called in that case, but was implemented anyway.
In specific handlers:
* Rename DjVuHandler::getUnserializedMetadata() and
extractTreesFromMetadata() for clarity. "Metadata" in these function
names meant an XML string.
* Updated DjVuImage::getImageSize() to provide image sizes in the new
style.
* In ExifBitmapHandler, getRotationForExif() now takes just the
Orientation tag, rather than a serialized string. Also renamed for
clarity.
* In GIFMetadataExtractor, return the width, height and bits per channel
instead of throwing them away. There was some conflation in
decodeBPP() which I picked apart. Refer to GIF89a section 18.
* In JpegMetadataExtractor, process the SOF0/SOF2 segment to extract
bits per channel, width, height and components (channel count). This
is essentially a port of PHP's getimagesize(), so should be bugwards
compatible.
* In PNGMetadataExtractor, return the width and height, which were
previously assigned to unused local variables. I verified the
implementation by referring to the specification.
* In SvgHandler, retain the version validation from unpackMetadata(),
but rename the function since it now takes an array as input.
In tests:
* In ExifBitmapTest, refactored some tests by using a provider.
* In GIFHandlerTest and PNGHandlerTest, I removed the tests in which
getMetadata() returns null, since it doesn't make sense when ported to
getMetadataArray(). I added tests for empty arrays instead.
* In tests, I retained serialization of input data since I figure it's
useful to confirm that existing database rows will continue to be read
correctly. I removed serialization of expected values, replacing them
with plain data.
* In tests, I replaced access to private class constants like
BROKEN_FILE with string literals, since stability is essential. If
the class constant changes, the test should fail.
Elsewhere:
* In maintenance/refreshImageMetadata.php, I removed the check for
shrinking image metadata, since it's not easy to implement and is
not future compatible. Image metadata is expected to shrink in
future.
Bug: T275268
Change-Id: I039785d5b6439d71dcc21dcb972177dba5c3a67d
2021-05-19 00:24:32 +00:00
|
|
|
* @return array
|
2013-12-05 19:27:27 +00:00
|
|
|
*/
|
Use the unserialized form of image metadata internally
Image metadata is usually a serialized string representing an array.
Passing the string around internally and having everything unserialize
it is an awkward convention.
Also, many image handlers were reading the file twice: once for
getMetadata() and again for getImageSize(). Often getMetadata()
would actually read the width and height and then throw it away.
So, in filerepo:
* Add File::getMetadataItem(), which promises to allow partial
loading of metadata per my proposal on T275268 in a future commit.
* Add File::getMetadataArray(), which returns the unserialized array.
Some file handlers were returning non-serializable strings from
getMetadata(), so I gave them a legacy array form ['_error' => ...]
* Changed MWFileProps to return the array form of metadata.
* Deprecate the weird File::getImageSize(). It was apparently not
called by anything, but was overridden by UnregisteredLocalFile.
* Wrap serialize/unserialize with File::getMetadataForDb() and
File::loadMetadataFromDb() in preparation for T275268.
In MediaHandler:
* Merged MediaHandler::getImageSize() and MediaHandler::getMetadata()
into getSizeAndMetadata(). Deprecated the old methods.
* Instead of isMetadataValid() we now have isFileMetadataValid(), which
only gets a File object, so it can decide what data it needs to load.
* Simplified getPageDimensions() by having it return false for non-paged
media. It was not called in that case, but was implemented anyway.
In specific handlers:
* Rename DjVuHandler::getUnserializedMetadata() and
extractTreesFromMetadata() for clarity. "Metadata" in these function
names meant an XML string.
* Updated DjVuImage::getImageSize() to provide image sizes in the new
style.
* In ExifBitmapHandler, getRotationForExif() now takes just the
Orientation tag, rather than a serialized string. Also renamed for
clarity.
* In GIFMetadataExtractor, return the width, height and bits per channel
instead of throwing them away. There was some conflation in
decodeBPP() which I picked apart. Refer to GIF89a section 18.
* In JpegMetadataExtractor, process the SOF0/SOF2 segment to extract
bits per channel, width, height and components (channel count). This
is essentially a port of PHP's getimagesize(), so should be bugwards
compatible.
* In PNGMetadataExtractor, return the width and height, which were
previously assigned to unused local variables. I verified the
implementation by referring to the specification.
* In SvgHandler, retain the version validation from unpackMetadata(),
but rename the function since it now takes an array as input.
In tests:
* In ExifBitmapTest, refactored some tests by using a provider.
* In GIFHandlerTest and PNGHandlerTest, I removed the tests in which
getMetadata() returns null, since it doesn't make sense when ported to
getMetadataArray(). I added tests for empty arrays instead.
* In tests, I retained serialization of input data since I figure it's
useful to confirm that existing database rows will continue to be read
correctly. I removed serialization of expected values, replacing them
with plain data.
* In tests, I replaced access to private class constants like
BROKEN_FILE with string literals, since stability is essential. If
the class constant changes, the test should fail.
Elsewhere:
* In maintenance/refreshImageMetadata.php, I removed the check for
shrinking image metadata, since it's not easy to implement and is
not future compatible. Image metadata is expected to shrink in
future.
Bug: T275268
Change-Id: I039785d5b6439d71dcc21dcb972177dba5c3a67d
2021-05-19 00:24:32 +00:00
|
|
|
public function getSizeAndMetadata( $state, $filename ) {
|
2016-02-17 09:09:32 +00:00
|
|
|
$metadata = [ 'version' => self::SVG_METADATA_VERSION ];
|
2019-03-30 20:14:52 +00:00
|
|
|
|
2010-11-01 23:57:09 +00:00
|
|
|
try {
|
2019-03-30 20:14:52 +00:00
|
|
|
$svgReader = new SVGReader( $filename );
|
|
|
|
|
$metadata += $svgReader->getMetadata();
|
2015-01-09 23:44:47 +00:00
|
|
|
} catch ( Exception $e ) { // @todo SVG specific exceptions
|
2012-11-26 21:17:41 +00:00
|
|
|
// File not found, broken, etc.
|
2016-02-17 09:09:32 +00:00
|
|
|
$metadata['error'] = [
|
2012-11-26 21:17:41 +00:00
|
|
|
'message' => $e->getMessage(),
|
2013-04-20 21:11:46 +00:00
|
|
|
'code' => $e->getCode()
|
2016-02-17 09:09:32 +00:00
|
|
|
];
|
2020-06-01 05:00:39 +00:00
|
|
|
wfDebug( __METHOD__ . ': ' . $e->getMessage() );
|
2010-11-01 23:57:09 +00:00
|
|
|
}
|
2013-12-05 10:05:05 +00:00
|
|
|
|
Use the unserialized form of image metadata internally
Image metadata is usually a serialized string representing an array.
Passing the string around internally and having everything unserialize
it is an awkward convention.
Also, many image handlers were reading the file twice: once for
getMetadata() and again for getImageSize(). Often getMetadata()
would actually read the width and height and then throw it away.
So, in filerepo:
* Add File::getMetadataItem(), which promises to allow partial
loading of metadata per my proposal on T275268 in a future commit.
* Add File::getMetadataArray(), which returns the unserialized array.
Some file handlers were returning non-serializable strings from
getMetadata(), so I gave them a legacy array form ['_error' => ...]
* Changed MWFileProps to return the array form of metadata.
* Deprecate the weird File::getImageSize(). It was apparently not
called by anything, but was overridden by UnregisteredLocalFile.
* Wrap serialize/unserialize with File::getMetadataForDb() and
File::loadMetadataFromDb() in preparation for T275268.
In MediaHandler:
* Merged MediaHandler::getImageSize() and MediaHandler::getMetadata()
into getSizeAndMetadata(). Deprecated the old methods.
* Instead of isMetadataValid() we now have isFileMetadataValid(), which
only gets a File object, so it can decide what data it needs to load.
* Simplified getPageDimensions() by having it return false for non-paged
media. It was not called in that case, but was implemented anyway.
In specific handlers:
* Rename DjVuHandler::getUnserializedMetadata() and
extractTreesFromMetadata() for clarity. "Metadata" in these function
names meant an XML string.
* Updated DjVuImage::getImageSize() to provide image sizes in the new
style.
* In ExifBitmapHandler, getRotationForExif() now takes just the
Orientation tag, rather than a serialized string. Also renamed for
clarity.
* In GIFMetadataExtractor, return the width, height and bits per channel
instead of throwing them away. There was some conflation in
decodeBPP() which I picked apart. Refer to GIF89a section 18.
* In JpegMetadataExtractor, process the SOF0/SOF2 segment to extract
bits per channel, width, height and components (channel count). This
is essentially a port of PHP's getimagesize(), so should be bugwards
compatible.
* In PNGMetadataExtractor, return the width and height, which were
previously assigned to unused local variables. I verified the
implementation by referring to the specification.
* In SvgHandler, retain the version validation from unpackMetadata(),
but rename the function since it now takes an array as input.
In tests:
* In ExifBitmapTest, refactored some tests by using a provider.
* In GIFHandlerTest and PNGHandlerTest, I removed the tests in which
getMetadata() returns null, since it doesn't make sense when ported to
getMetadataArray(). I added tests for empty arrays instead.
* In tests, I retained serialization of input data since I figure it's
useful to confirm that existing database rows will continue to be read
correctly. I removed serialization of expected values, replacing them
with plain data.
* In tests, I replaced access to private class constants like
BROKEN_FILE with string literals, since stability is essential. If
the class constant changes, the test should fail.
Elsewhere:
* In maintenance/refreshImageMetadata.php, I removed the check for
shrinking image metadata, since it's not easy to implement and is
not future compatible. Image metadata is expected to shrink in
future.
Bug: T275268
Change-Id: I039785d5b6439d71dcc21dcb972177dba5c3a67d
2021-05-19 00:24:32 +00:00
|
|
|
return [
|
|
|
|
|
'width' => $metadata['width'] ?? 0,
|
|
|
|
|
'height' => $metadata['height'] ?? 0,
|
|
|
|
|
'metadata' => $metadata
|
|
|
|
|
];
|
2010-11-01 23:57:09 +00:00
|
|
|
}
|
2010-11-04 00:35:29 +00:00
|
|
|
|
Use the unserialized form of image metadata internally
Image metadata is usually a serialized string representing an array.
Passing the string around internally and having everything unserialize
it is an awkward convention.
Also, many image handlers were reading the file twice: once for
getMetadata() and again for getImageSize(). Often getMetadata()
would actually read the width and height and then throw it away.
So, in filerepo:
* Add File::getMetadataItem(), which promises to allow partial
loading of metadata per my proposal on T275268 in a future commit.
* Add File::getMetadataArray(), which returns the unserialized array.
Some file handlers were returning non-serializable strings from
getMetadata(), so I gave them a legacy array form ['_error' => ...]
* Changed MWFileProps to return the array form of metadata.
* Deprecate the weird File::getImageSize(). It was apparently not
called by anything, but was overridden by UnregisteredLocalFile.
* Wrap serialize/unserialize with File::getMetadataForDb() and
File::loadMetadataFromDb() in preparation for T275268.
In MediaHandler:
* Merged MediaHandler::getImageSize() and MediaHandler::getMetadata()
into getSizeAndMetadata(). Deprecated the old methods.
* Instead of isMetadataValid() we now have isFileMetadataValid(), which
only gets a File object, so it can decide what data it needs to load.
* Simplified getPageDimensions() by having it return false for non-paged
media. It was not called in that case, but was implemented anyway.
In specific handlers:
* Rename DjVuHandler::getUnserializedMetadata() and
extractTreesFromMetadata() for clarity. "Metadata" in these function
names meant an XML string.
* Updated DjVuImage::getImageSize() to provide image sizes in the new
style.
* In ExifBitmapHandler, getRotationForExif() now takes just the
Orientation tag, rather than a serialized string. Also renamed for
clarity.
* In GIFMetadataExtractor, return the width, height and bits per channel
instead of throwing them away. There was some conflation in
decodeBPP() which I picked apart. Refer to GIF89a section 18.
* In JpegMetadataExtractor, process the SOF0/SOF2 segment to extract
bits per channel, width, height and components (channel count). This
is essentially a port of PHP's getimagesize(), so should be bugwards
compatible.
* In PNGMetadataExtractor, return the width and height, which were
previously assigned to unused local variables. I verified the
implementation by referring to the specification.
* In SvgHandler, retain the version validation from unpackMetadata(),
but rename the function since it now takes an array as input.
In tests:
* In ExifBitmapTest, refactored some tests by using a provider.
* In GIFHandlerTest and PNGHandlerTest, I removed the tests in which
getMetadata() returns null, since it doesn't make sense when ported to
getMetadataArray(). I added tests for empty arrays instead.
* In tests, I retained serialization of input data since I figure it's
useful to confirm that existing database rows will continue to be read
correctly. I removed serialization of expected values, replacing them
with plain data.
* In tests, I replaced access to private class constants like
BROKEN_FILE with string literals, since stability is essential. If
the class constant changes, the test should fail.
Elsewhere:
* In maintenance/refreshImageMetadata.php, I removed the check for
shrinking image metadata, since it's not easy to implement and is
not future compatible. Image metadata is expected to shrink in
future.
Bug: T275268
Change-Id: I039785d5b6439d71dcc21dcb972177dba5c3a67d
2021-05-19 00:24:32 +00:00
|
|
|
protected function validateMetadata( $unser ) {
|
2010-11-01 23:57:09 +00:00
|
|
|
if ( isset( $unser['version'] ) && $unser['version'] == self::SVG_METADATA_VERSION ) {
|
|
|
|
|
return $unser;
|
|
|
|
|
} else {
|
Use the unserialized form of image metadata internally
Image metadata is usually a serialized string representing an array.
Passing the string around internally and having everything unserialize
it is an awkward convention.
Also, many image handlers were reading the file twice: once for
getMetadata() and again for getImageSize(). Often getMetadata()
would actually read the width and height and then throw it away.
So, in filerepo:
* Add File::getMetadataItem(), which promises to allow partial
loading of metadata per my proposal on T275268 in a future commit.
* Add File::getMetadataArray(), which returns the unserialized array.
Some file handlers were returning non-serializable strings from
getMetadata(), so I gave them a legacy array form ['_error' => ...]
* Changed MWFileProps to return the array form of metadata.
* Deprecate the weird File::getImageSize(). It was apparently not
called by anything, but was overridden by UnregisteredLocalFile.
* Wrap serialize/unserialize with File::getMetadataForDb() and
File::loadMetadataFromDb() in preparation for T275268.
In MediaHandler:
* Merged MediaHandler::getImageSize() and MediaHandler::getMetadata()
into getSizeAndMetadata(). Deprecated the old methods.
* Instead of isMetadataValid() we now have isFileMetadataValid(), which
only gets a File object, so it can decide what data it needs to load.
* Simplified getPageDimensions() by having it return false for non-paged
media. It was not called in that case, but was implemented anyway.
In specific handlers:
* Rename DjVuHandler::getUnserializedMetadata() and
extractTreesFromMetadata() for clarity. "Metadata" in these function
names meant an XML string.
* Updated DjVuImage::getImageSize() to provide image sizes in the new
style.
* In ExifBitmapHandler, getRotationForExif() now takes just the
Orientation tag, rather than a serialized string. Also renamed for
clarity.
* In GIFMetadataExtractor, return the width, height and bits per channel
instead of throwing them away. There was some conflation in
decodeBPP() which I picked apart. Refer to GIF89a section 18.
* In JpegMetadataExtractor, process the SOF0/SOF2 segment to extract
bits per channel, width, height and components (channel count). This
is essentially a port of PHP's getimagesize(), so should be bugwards
compatible.
* In PNGMetadataExtractor, return the width and height, which were
previously assigned to unused local variables. I verified the
implementation by referring to the specification.
* In SvgHandler, retain the version validation from unpackMetadata(),
but rename the function since it now takes an array as input.
In tests:
* In ExifBitmapTest, refactored some tests by using a provider.
* In GIFHandlerTest and PNGHandlerTest, I removed the tests in which
getMetadata() returns null, since it doesn't make sense when ported to
getMetadataArray(). I added tests for empty arrays instead.
* In tests, I retained serialization of input data since I figure it's
useful to confirm that existing database rows will continue to be read
correctly. I removed serialization of expected values, replacing them
with plain data.
* In tests, I replaced access to private class constants like
BROKEN_FILE with string literals, since stability is essential. If
the class constant changes, the test should fail.
Elsewhere:
* In maintenance/refreshImageMetadata.php, I removed the check for
shrinking image metadata, since it's not easy to implement and is
not future compatible. Image metadata is expected to shrink in
future.
Bug: T275268
Change-Id: I039785d5b6439d71dcc21dcb972177dba5c3a67d
2021-05-19 00:24:32 +00:00
|
|
|
return null;
|
2010-11-01 23:57:09 +00:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2020-05-17 23:57:36 +00:00
|
|
|
public function getMetadataType( $image ) {
|
2010-11-01 23:57:09 +00:00
|
|
|
return 'parsed-svg';
|
|
|
|
|
}
|
|
|
|
|
|
Use the unserialized form of image metadata internally
Image metadata is usually a serialized string representing an array.
Passing the string around internally and having everything unserialize
it is an awkward convention.
Also, many image handlers were reading the file twice: once for
getMetadata() and again for getImageSize(). Often getMetadata()
would actually read the width and height and then throw it away.
So, in filerepo:
* Add File::getMetadataItem(), which promises to allow partial
loading of metadata per my proposal on T275268 in a future commit.
* Add File::getMetadataArray(), which returns the unserialized array.
Some file handlers were returning non-serializable strings from
getMetadata(), so I gave them a legacy array form ['_error' => ...]
* Changed MWFileProps to return the array form of metadata.
* Deprecate the weird File::getImageSize(). It was apparently not
called by anything, but was overridden by UnregisteredLocalFile.
* Wrap serialize/unserialize with File::getMetadataForDb() and
File::loadMetadataFromDb() in preparation for T275268.
In MediaHandler:
* Merged MediaHandler::getImageSize() and MediaHandler::getMetadata()
into getSizeAndMetadata(). Deprecated the old methods.
* Instead of isMetadataValid() we now have isFileMetadataValid(), which
only gets a File object, so it can decide what data it needs to load.
* Simplified getPageDimensions() by having it return false for non-paged
media. It was not called in that case, but was implemented anyway.
In specific handlers:
* Rename DjVuHandler::getUnserializedMetadata() and
extractTreesFromMetadata() for clarity. "Metadata" in these function
names meant an XML string.
* Updated DjVuImage::getImageSize() to provide image sizes in the new
style.
* In ExifBitmapHandler, getRotationForExif() now takes just the
Orientation tag, rather than a serialized string. Also renamed for
clarity.
* In GIFMetadataExtractor, return the width, height and bits per channel
instead of throwing them away. There was some conflation in
decodeBPP() which I picked apart. Refer to GIF89a section 18.
* In JpegMetadataExtractor, process the SOF0/SOF2 segment to extract
bits per channel, width, height and components (channel count). This
is essentially a port of PHP's getimagesize(), so should be bugwards
compatible.
* In PNGMetadataExtractor, return the width and height, which were
previously assigned to unused local variables. I verified the
implementation by referring to the specification.
* In SvgHandler, retain the version validation from unpackMetadata(),
but rename the function since it now takes an array as input.
In tests:
* In ExifBitmapTest, refactored some tests by using a provider.
* In GIFHandlerTest and PNGHandlerTest, I removed the tests in which
getMetadata() returns null, since it doesn't make sense when ported to
getMetadataArray(). I added tests for empty arrays instead.
* In tests, I retained serialization of input data since I figure it's
useful to confirm that existing database rows will continue to be read
correctly. I removed serialization of expected values, replacing them
with plain data.
* In tests, I replaced access to private class constants like
BROKEN_FILE with string literals, since stability is essential. If
the class constant changes, the test should fail.
Elsewhere:
* In maintenance/refreshImageMetadata.php, I removed the check for
shrinking image metadata, since it's not easy to implement and is
not future compatible. Image metadata is expected to shrink in
future.
Bug: T275268
Change-Id: I039785d5b6439d71dcc21dcb972177dba5c3a67d
2021-05-19 00:24:32 +00:00
|
|
|
public function isFileMetadataValid( $image ) {
|
|
|
|
|
$meta = $this->validateMetadata( $image->getMetadataArray() );
|
|
|
|
|
if ( !$meta ) {
|
2012-08-19 18:18:50 +00:00
|
|
|
return self::METADATA_BAD;
|
|
|
|
|
}
|
|
|
|
|
if ( !isset( $meta['originalWidth'] ) ) {
|
|
|
|
|
// Old but compatible
|
|
|
|
|
return self::METADATA_COMPATIBLE;
|
|
|
|
|
}
|
2013-12-05 10:05:05 +00:00
|
|
|
|
2012-08-19 18:18:50 +00:00
|
|
|
return self::METADATA_GOOD;
|
2010-11-01 23:57:09 +00:00
|
|
|
}
|
2010-11-04 00:35:29 +00:00
|
|
|
|
2013-12-05 19:40:38 +00:00
|
|
|
protected function visibleMetadataFields() {
|
2016-02-17 09:09:32 +00:00
|
|
|
$fields = [ 'objectname', 'imagedescription' ];
|
2013-12-05 10:05:05 +00:00
|
|
|
|
2010-11-04 00:35:29 +00:00
|
|
|
return $fields;
|
|
|
|
|
}
|
|
|
|
|
|
2011-02-18 23:34:24 +00:00
|
|
|
/**
|
2013-12-05 19:27:27 +00:00
|
|
|
* @param File $file
|
2020-05-15 05:34:04 +00:00
|
|
|
* @param IContextSource|false $context
|
|
|
|
|
* @return array[]|false
|
2011-02-18 23:34:24 +00:00
|
|
|
*/
|
2019-02-25 09:16:30 +00:00
|
|
|
public function formatMetadata( $file, $context = false ) {
|
2016-02-17 09:09:32 +00:00
|
|
|
$result = [
|
|
|
|
|
'visible' => [],
|
|
|
|
|
'collapsed' => []
|
|
|
|
|
];
|
Use the unserialized form of image metadata internally
Image metadata is usually a serialized string representing an array.
Passing the string around internally and having everything unserialize
it is an awkward convention.
Also, many image handlers were reading the file twice: once for
getMetadata() and again for getImageSize(). Often getMetadata()
would actually read the width and height and then throw it away.
So, in filerepo:
* Add File::getMetadataItem(), which promises to allow partial
loading of metadata per my proposal on T275268 in a future commit.
* Add File::getMetadataArray(), which returns the unserialized array.
Some file handlers were returning non-serializable strings from
getMetadata(), so I gave them a legacy array form ['_error' => ...]
* Changed MWFileProps to return the array form of metadata.
* Deprecate the weird File::getImageSize(). It was apparently not
called by anything, but was overridden by UnregisteredLocalFile.
* Wrap serialize/unserialize with File::getMetadataForDb() and
File::loadMetadataFromDb() in preparation for T275268.
In MediaHandler:
* Merged MediaHandler::getImageSize() and MediaHandler::getMetadata()
into getSizeAndMetadata(). Deprecated the old methods.
* Instead of isMetadataValid() we now have isFileMetadataValid(), which
only gets a File object, so it can decide what data it needs to load.
* Simplified getPageDimensions() by having it return false for non-paged
media. It was not called in that case, but was implemented anyway.
In specific handlers:
* Rename DjVuHandler::getUnserializedMetadata() and
extractTreesFromMetadata() for clarity. "Metadata" in these function
names meant an XML string.
* Updated DjVuImage::getImageSize() to provide image sizes in the new
style.
* In ExifBitmapHandler, getRotationForExif() now takes just the
Orientation tag, rather than a serialized string. Also renamed for
clarity.
* In GIFMetadataExtractor, return the width, height and bits per channel
instead of throwing them away. There was some conflation in
decodeBPP() which I picked apart. Refer to GIF89a section 18.
* In JpegMetadataExtractor, process the SOF0/SOF2 segment to extract
bits per channel, width, height and components (channel count). This
is essentially a port of PHP's getimagesize(), so should be bugwards
compatible.
* In PNGMetadataExtractor, return the width and height, which were
previously assigned to unused local variables. I verified the
implementation by referring to the specification.
* In SvgHandler, retain the version validation from unpackMetadata(),
but rename the function since it now takes an array as input.
In tests:
* In ExifBitmapTest, refactored some tests by using a provider.
* In GIFHandlerTest and PNGHandlerTest, I removed the tests in which
getMetadata() returns null, since it doesn't make sense when ported to
getMetadataArray(). I added tests for empty arrays instead.
* In tests, I retained serialization of input data since I figure it's
useful to confirm that existing database rows will continue to be read
correctly. I removed serialization of expected values, replacing them
with plain data.
* In tests, I replaced access to private class constants like
BROKEN_FILE with string literals, since stability is essential. If
the class constant changes, the test should fail.
Elsewhere:
* In maintenance/refreshImageMetadata.php, I removed the check for
shrinking image metadata, since it's not easy to implement and is
not future compatible. Image metadata is expected to shrink in
future.
Bug: T275268
Change-Id: I039785d5b6439d71dcc21dcb972177dba5c3a67d
2021-05-19 00:24:32 +00:00
|
|
|
$metadata = $this->validateMetadata( $file->getMetadataArray() );
|
2012-11-26 21:17:41 +00:00
|
|
|
if ( !$metadata || isset( $metadata['error'] ) ) {
|
2010-11-04 00:35:29 +00:00
|
|
|
return false;
|
|
|
|
|
}
|
|
|
|
|
|
2013-12-05 19:27:27 +00:00
|
|
|
/* @todo Add a formatter
|
2010-11-04 00:35:29 +00:00
|
|
|
$format = new FormatSVG( $metadata );
|
|
|
|
|
$formatted = $format->getFormattedData();
|
|
|
|
|
*/
|
|
|
|
|
|
|
|
|
|
// Sort fields into visible and collapsed
|
|
|
|
|
$visibleFields = $this->visibleMetadataFields();
|
(Follow-up r75968) r75968 started extracting and displaying some svg metadata, but didn't add the messages it used.
It started to use the messages svg-description, svg-title, svg-width, and svg-height in the metadata table, without
defining those messages. I changed it to use the same messages as are used when displaying the same table on
the image description pages of jpeg files. ( exif-imagewidth and friends ). Personally I feel the metadata
should be about the image, not what format the metadata was stored in (a description of an image is still a
description of the image, regardless of the format of that image. JPEG images for example can have image descriptions in
3 different metadata formats [exif, iptc, xmp], it would be insane to make different messages for them). Furthermore,
I think we should rename all the exif-* messages to something like filemeta-property (but thats an issue for later).
I added one message, 'exif-objectname', since the svg title property didn't really correspond to anything in exif.
However, it does correspond very well to IPTC's object name property, which i hope we will start supporting in 1.18,
so I used that message for that.
2011-02-13 04:55:35 +00:00
|
|
|
|
2013-08-24 17:51:07 +00:00
|
|
|
$showMeta = false;
|
2010-11-04 00:35:29 +00:00
|
|
|
foreach ( $metadata as $name => $value ) {
|
|
|
|
|
$tag = strtolower( $name );
|
2013-08-28 23:09:07 +00:00
|
|
|
if ( isset( self::$metaConversion[$tag] ) ) {
|
|
|
|
|
$tag = strtolower( self::$metaConversion[$tag] );
|
2012-08-17 02:18:39 +00:00
|
|
|
} else {
|
|
|
|
|
// Do not output other metadata not in list
|
|
|
|
|
continue;
|
(Follow-up r75968) r75968 started extracting and displaying some svg metadata, but didn't add the messages it used.
It started to use the messages svg-description, svg-title, svg-width, and svg-height in the metadata table, without
defining those messages. I changed it to use the same messages as are used when displaying the same table on
the image description pages of jpeg files. ( exif-imagewidth and friends ). Personally I feel the metadata
should be about the image, not what format the metadata was stored in (a description of an image is still a
description of the image, regardless of the format of that image. JPEG images for example can have image descriptions in
3 different metadata formats [exif, iptc, xmp], it would be insane to make different messages for them). Furthermore,
I think we should rename all the exif-* messages to something like filemeta-property (but thats an issue for later).
I added one message, 'exif-objectname', since the svg title property didn't really correspond to anything in exif.
However, it does correspond very well to IPTC's object name property, which i hope we will start supporting in 1.18,
so I used that message for that.
2011-02-13 04:55:35 +00:00
|
|
|
}
|
2013-08-24 17:51:07 +00:00
|
|
|
$showMeta = true;
|
2010-11-04 00:35:29 +00:00
|
|
|
self::addMeta( $result,
|
|
|
|
|
in_array( $tag, $visibleFields ) ? 'visible' : 'collapsed',
|
(Follow-up r75968) r75968 started extracting and displaying some svg metadata, but didn't add the messages it used.
It started to use the messages svg-description, svg-title, svg-width, and svg-height in the metadata table, without
defining those messages. I changed it to use the same messages as are used when displaying the same table on
the image description pages of jpeg files. ( exif-imagewidth and friends ). Personally I feel the metadata
should be about the image, not what format the metadata was stored in (a description of an image is still a
description of the image, regardless of the format of that image. JPEG images for example can have image descriptions in
3 different metadata formats [exif, iptc, xmp], it would be insane to make different messages for them). Furthermore,
I think we should rename all the exif-* messages to something like filemeta-property (but thats an issue for later).
I added one message, 'exif-objectname', since the svg title property didn't really correspond to anything in exif.
However, it does correspond very well to IPTC's object name property, which i hope we will start supporting in 1.18,
so I used that message for that.
2011-02-13 04:55:35 +00:00
|
|
|
'exif',
|
2010-11-04 00:35:29 +00:00
|
|
|
$tag,
|
|
|
|
|
$value
|
|
|
|
|
);
|
|
|
|
|
}
|
2013-12-05 10:05:05 +00:00
|
|
|
|
2013-08-24 17:51:07 +00:00
|
|
|
return $showMeta ? $result : false;
|
2010-11-04 00:35:29 +00:00
|
|
|
}
|
2013-05-24 12:56:06 +00:00
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* @param string $name Parameter name
|
2013-12-05 19:27:27 +00:00
|
|
|
* @param mixed $value Parameter value
|
2013-05-24 12:56:06 +00:00
|
|
|
* @return bool Validity
|
|
|
|
|
*/
|
2016-02-23 23:47:02 +00:00
|
|
|
public function validateParam( $name, $value ) {
|
2016-02-17 09:09:32 +00:00
|
|
|
if ( in_array( $name, [ 'width', 'height' ] ) ) {
|
2013-05-24 12:56:06 +00:00
|
|
|
// Reject negative heights, widths
|
|
|
|
|
return ( $value > 0 );
|
2013-08-24 15:06:25 +00:00
|
|
|
} elseif ( $name == 'lang' ) {
|
2013-05-24 12:56:06 +00:00
|
|
|
// Validate $code
|
2020-01-03 23:03:14 +00:00
|
|
|
if ( $value === ''
|
|
|
|
|
|| !MediaWikiServices::getInstance()->getLanguageNameUtils()
|
|
|
|
|
->isValidCode( $value )
|
|
|
|
|
) {
|
2013-05-24 12:56:06 +00:00
|
|
|
return false;
|
|
|
|
|
}
|
2013-12-05 10:05:05 +00:00
|
|
|
|
2013-05-24 12:56:06 +00:00
|
|
|
return true;
|
|
|
|
|
}
|
2013-12-05 10:05:05 +00:00
|
|
|
|
2013-05-24 12:56:06 +00:00
|
|
|
// Only lang, width and height are acceptable keys
|
|
|
|
|
return false;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
2014-07-24 17:43:25 +00:00
|
|
|
* @param array $params Name=>value pairs of parameters
|
2021-09-20 22:18:39 +00:00
|
|
|
* @return string|false Filename to use
|
2013-05-24 12:56:06 +00:00
|
|
|
*/
|
2016-02-23 23:47:02 +00:00
|
|
|
public function makeParamString( $params ) {
|
2013-05-24 12:56:06 +00:00
|
|
|
$lang = '';
|
2018-10-06 00:56:53 +00:00
|
|
|
$code = $this->getLanguageFromParams( $params );
|
|
|
|
|
if ( $code !== 'en' ) {
|
|
|
|
|
$lang = 'lang' . strtolower( $code ) . '-';
|
2013-05-24 12:56:06 +00:00
|
|
|
}
|
|
|
|
|
if ( !isset( $params['width'] ) ) {
|
|
|
|
|
return false;
|
|
|
|
|
}
|
2013-12-05 10:05:05 +00:00
|
|
|
|
2013-05-24 12:56:06 +00:00
|
|
|
return "$lang{$params['width']}px";
|
|
|
|
|
}
|
|
|
|
|
|
2016-02-23 23:47:02 +00:00
|
|
|
public function parseParamString( $str ) {
|
2013-05-24 12:56:06 +00:00
|
|
|
$m = false;
|
2017-10-18 12:38:48 +00:00
|
|
|
if ( preg_match( '/^lang([a-z]+(?:-[a-z]+)*)-(\d+)px$/i', $str, $m ) ) {
|
2016-02-17 09:09:32 +00:00
|
|
|
return [ 'width' => array_pop( $m ), 'lang' => $m[1] ];
|
2013-08-24 15:06:25 +00:00
|
|
|
} elseif ( preg_match( '/^(\d+)px$/', $str, $m ) ) {
|
2016-02-17 09:09:32 +00:00
|
|
|
return [ 'width' => $m[1], 'lang' => 'en' ];
|
2013-05-24 12:56:06 +00:00
|
|
|
} else {
|
|
|
|
|
return false;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2016-02-23 23:47:02 +00:00
|
|
|
public function getParamMap() {
|
2016-02-17 09:09:32 +00:00
|
|
|
return [ 'img_lang' => 'lang', 'img_width' => 'width' ];
|
2013-05-24 12:56:06 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
2013-12-05 19:27:27 +00:00
|
|
|
* @param array $params
|
2013-05-24 12:56:06 +00:00
|
|
|
* @return array
|
|
|
|
|
*/
|
2019-02-25 09:20:28 +00:00
|
|
|
protected function getScriptParams( $params ) {
|
2016-02-17 09:09:32 +00:00
|
|
|
$scriptParams = [ 'width' => $params['width'] ];
|
2013-12-06 23:45:15 +00:00
|
|
|
if ( isset( $params['lang'] ) ) {
|
|
|
|
|
$scriptParams['lang'] = $params['lang'];
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return $scriptParams;
|
2013-05-24 12:56:06 +00:00
|
|
|
}
|
2013-08-28 23:09:07 +00:00
|
|
|
|
|
|
|
|
public function getCommonMetaArray( File $file ) {
|
Use the unserialized form of image metadata internally
Image metadata is usually a serialized string representing an array.
Passing the string around internally and having everything unserialize
it is an awkward convention.
Also, many image handlers were reading the file twice: once for
getMetadata() and again for getImageSize(). Often getMetadata()
would actually read the width and height and then throw it away.
So, in filerepo:
* Add File::getMetadataItem(), which promises to allow partial
loading of metadata per my proposal on T275268 in a future commit.
* Add File::getMetadataArray(), which returns the unserialized array.
Some file handlers were returning non-serializable strings from
getMetadata(), so I gave them a legacy array form ['_error' => ...]
* Changed MWFileProps to return the array form of metadata.
* Deprecate the weird File::getImageSize(). It was apparently not
called by anything, but was overridden by UnregisteredLocalFile.
* Wrap serialize/unserialize with File::getMetadataForDb() and
File::loadMetadataFromDb() in preparation for T275268.
In MediaHandler:
* Merged MediaHandler::getImageSize() and MediaHandler::getMetadata()
into getSizeAndMetadata(). Deprecated the old methods.
* Instead of isMetadataValid() we now have isFileMetadataValid(), which
only gets a File object, so it can decide what data it needs to load.
* Simplified getPageDimensions() by having it return false for non-paged
media. It was not called in that case, but was implemented anyway.
In specific handlers:
* Rename DjVuHandler::getUnserializedMetadata() and
extractTreesFromMetadata() for clarity. "Metadata" in these function
names meant an XML string.
* Updated DjVuImage::getImageSize() to provide image sizes in the new
style.
* In ExifBitmapHandler, getRotationForExif() now takes just the
Orientation tag, rather than a serialized string. Also renamed for
clarity.
* In GIFMetadataExtractor, return the width, height and bits per channel
instead of throwing them away. There was some conflation in
decodeBPP() which I picked apart. Refer to GIF89a section 18.
* In JpegMetadataExtractor, process the SOF0/SOF2 segment to extract
bits per channel, width, height and components (channel count). This
is essentially a port of PHP's getimagesize(), so should be bugwards
compatible.
* In PNGMetadataExtractor, return the width and height, which were
previously assigned to unused local variables. I verified the
implementation by referring to the specification.
* In SvgHandler, retain the version validation from unpackMetadata(),
but rename the function since it now takes an array as input.
In tests:
* In ExifBitmapTest, refactored some tests by using a provider.
* In GIFHandlerTest and PNGHandlerTest, I removed the tests in which
getMetadata() returns null, since it doesn't make sense when ported to
getMetadataArray(). I added tests for empty arrays instead.
* In tests, I retained serialization of input data since I figure it's
useful to confirm that existing database rows will continue to be read
correctly. I removed serialization of expected values, replacing them
with plain data.
* In tests, I replaced access to private class constants like
BROKEN_FILE with string literals, since stability is essential. If
the class constant changes, the test should fail.
Elsewhere:
* In maintenance/refreshImageMetadata.php, I removed the check for
shrinking image metadata, since it's not easy to implement and is
not future compatible. Image metadata is expected to shrink in
future.
Bug: T275268
Change-Id: I039785d5b6439d71dcc21dcb972177dba5c3a67d
2021-05-19 00:24:32 +00:00
|
|
|
$metadata = $this->validateMetadata( $file->getMetadataArray() );
|
2013-08-28 23:09:07 +00:00
|
|
|
if ( !$metadata || isset( $metadata['error'] ) ) {
|
2016-02-17 09:09:32 +00:00
|
|
|
return [];
|
2013-08-28 23:09:07 +00:00
|
|
|
}
|
2016-02-17 09:09:32 +00:00
|
|
|
$stdMetadata = [];
|
2013-08-28 23:09:07 +00:00
|
|
|
foreach ( $metadata as $name => $value ) {
|
|
|
|
|
$tag = strtolower( $name );
|
|
|
|
|
if ( $tag === 'originalwidth' || $tag === 'originalheight' ) {
|
|
|
|
|
// Skip these. In the exif metadata stuff, it is assumed these
|
|
|
|
|
// are measured in px, which is not the case here.
|
|
|
|
|
continue;
|
|
|
|
|
}
|
|
|
|
|
if ( isset( self::$metaConversion[$tag] ) ) {
|
|
|
|
|
$tag = self::$metaConversion[$tag];
|
|
|
|
|
$stdMetadata[$tag] = $value;
|
|
|
|
|
}
|
|
|
|
|
}
|
2013-12-05 10:05:05 +00:00
|
|
|
|
2013-08-28 23:09:07 +00:00
|
|
|
return $stdMetadata;
|
|
|
|
|
}
|
2007-04-20 12:31:36 +00:00
|
|
|
}
|