Commit graph

11 commits

Author SHA1 Message Date
Tim Starling
b4849e03b7 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-06-08 17:04:01 +10:00
Reedy
b3b78a0e4b Fix Extract typo
Change-Id: I34b2b214034c2e8f297399be6efb6805895b0853
2021-05-20 19:42:27 +01:00
Thiemo Kreuz
9489ff2f53 Document and fix UploadBase::$mFinalExtension being null
While trying to understand T273249, I followed all code
paths and found one that can result in mFinalExtension being
null: when a file is stashed, it gets a temporary name
without an extension.

This patch fixes a bunch of usages where this null might
cause type errors.

Bug: T273249
Change-Id: Ie8bd84d4637aec5a65ae8b220d9266d36873acc0
2021-02-03 19:12:07 +01:00
Timo Tijhof
6b77a3e3c8 MWFileProps: Hotfix getPropsFromPath() to treat null as empty string
Follows-up 3d84030478. It is unclear what null should do, but
it is a fact that it used to behave similar to an empty string
within MimeAnalyzer so keep that for now, until we figure out
what to do with it.

Bug: T273249
Change-Id: Ic174253dbe019794f88bdc0f9bbe1e688dda4894
2021-02-02 00:51:53 +00:00
Ori Livneh
7c9e19ed5e mime: Document null return from MimeAnalyzer::improveTypeFromExtension()
This method returns null when $mime is 'unknown/unknown' and the file
extension is unknown to MediaWiki. The inline documentation and @return
annotation omitted this.

I don't think this was an intentional design choice, but it's the
existing behavior and I'm not sure it's safe to change.

Since it is the existing behavior, document it and add a test case, to
ensure that any changes to this behavior are intentional.

Bug: T253483
Change-Id: Ie6615a4bd9ae77e9ab59cfe76edb237cace693b1
2020-05-24 15:51:08 -04:00
Daimona Eaytoy
69cadf44a9 Unsuppress PhanParamsTooMany
This is the last repo-wide suppressed issue. Hurrah.

Bug: T231636
Change-Id: I3dc939f115bea14848c9c40bc52a7601f86ca0a7
2019-10-10 12:44:08 -07:00
Fomafix
43244db9a2 Use PHP 7 '??' operator instead of if-then-else
Change-Id: If9d4be5d88c8927f63cbb84dfc8181baf62ea3eb
2018-10-21 21:46:46 +02:00
Reedy
6aaaa070f3 Remove MimeMagic
Deprecated since 1.28

Change-Id: I793deb8669e353525464ccd840801e5015c545c8
2018-09-23 20:13:29 +00:00
Reedy
c16af68fb6 Replace MimeMagic::singleton() calls
Change-Id: Ieed41b5d6b0f568fe2872e7754f2feae7868fe7a
2017-11-27 02:13:51 +00:00
Aaron Schulz
724b2cf912 Remove MimeMagic depedency from FSFile and move it to /libs
Change-Id: Ieaae909b49c798b6e9bed65d15961cefbdaec49b
2016-09-22 03:17:04 +00:00
Aaron Schulz
ff5abb66b4 Migrate callers to new MWFileProps::getPropsFromPath() method
* FSFile should not be responsible for handling this much logic.
* Make more MediaHandler classes aware of the fact that an object
  other than File might be passed in. Use the FSFile instead of a
  useless empty stdClass object.
* Also added more fields to FSFile::placeholderProps to make it
  more complete.

Change-Id: I9fe764b2a7261af507c6555e6a57273cf7d00d36
2016-09-22 00:33:46 +00:00