MediaWiki  1.28.0
XMPTest.php
Go to the documentation of this file.
1 <?php
2 
7 class XMPTest extends PHPUnit_Framework_TestCase {
8 
9  protected function setUp() {
10  parent::setUp();
11  # Requires libxml to do XMP parsing
12  if ( !extension_loaded( 'exif' ) ) {
13  $this->markTestSkipped( "PHP extension 'exif' is not loaded, skipping." );
14  }
15  }
16 
29  public function testXMPParse( $xmp, $expected, $info ) {
30  if ( !is_string( $xmp ) || !is_array( $expected ) ) {
31  throw new Exception( "Invalid data provided to " . __METHOD__ );
32  }
33  $reader = new XMPReader;
34  $reader->parse( $xmp );
35  $this->assertEquals( $expected, $reader->getResults(), $info, 0.0000000001 );
36  }
37 
38  public static function provideXMPParse() {
39  $xmpPath = __DIR__ . '/../../../data/xmp/';
40  $data = [];
41 
42  // $xmpFiles format: array of arrays with first arg file base name,
43  // with the actual file having .xmp on the end for the xmp
44  // and .result.php on the end for a php file containing the result
45  // array. Second argument is some info on what's being tested.
46  $xmpFiles = [
47  [ '1', 'parseType=Resource test' ],
48  [ '2', 'Structure with mixed attribute and element props' ],
49  [ '3', 'Extra qualifiers (that should be ignored)' ],
50  [ '3-invalid', 'Test ignoring qualifiers that look like normal props' ],
51  [ '4', 'Flash as qualifier' ],
52  [ '5', 'Flash as qualifier 2' ],
53  [ '6', 'Multiple rdf:Description' ],
54  [ '7', 'Generic test of several property types' ],
55  [ 'flash', 'Test of Flash property' ],
56  [ 'invalid-child-not-struct', 'Test child props not in struct or ignored' ],
57  [ 'no-recognized-props', 'Test namespace and no recognized props' ],
58  [ 'no-namespace', 'Test non-namespaced attributes are ignored' ],
59  [ 'bag-for-seq', "Allow bag's instead of seq's. (bug 27105)" ],
60  [ 'utf16BE', 'UTF-16BE encoding' ],
61  [ 'utf16LE', 'UTF-16LE encoding' ],
62  [ 'utf32BE', 'UTF-32BE encoding' ],
63  [ 'utf32LE', 'UTF-32LE encoding' ],
64  [ 'xmpExt', 'Extended XMP missing second part' ],
65  [ 'gps', 'Handling of exif GPS parameters in XMP' ],
66  ];
67 
68  $xmpFiles[] = [ 'doctype-included', 'XMP includes doctype' ];
69 
70  foreach ( $xmpFiles as $file ) {
71  $xmp = file_get_contents( $xmpPath . $file[0] . '.xmp' );
72  // I'm not sure if this is the best way to handle getting the
73  // result array, but it seems kind of big to put directly in the test
74  // file.
75  $result = null;
76  include $xmpPath . $file[0] . '.result.php';
77  $data[] = [ $xmp, $result, '[' . $file[0] . '.xmp] ' . $file[1] ];
78  }
79 
80  return $data;
81  }
82 
91  public function testExtendedXMP() {
92  $xmpPath = __DIR__ . '/../../../data/xmp/';
93  $standardXMP = file_get_contents( $xmpPath . 'xmpExt.xmp' );
94  $extendedXMP = file_get_contents( $xmpPath . 'xmpExt2.xmp' );
95 
96  $md5sum = '28C74E0AC2D796886759006FBE2E57B7'; // of xmpExt2.xmp
97  $length = pack( 'N', strlen( $extendedXMP ) );
98  $offset = pack( 'N', 0 );
99  $extendedPacket = $md5sum . $length . $offset . $extendedXMP;
100 
101  $reader = new XMPReader();
102  $reader->parse( $standardXMP );
103  $reader->parseExtended( $extendedPacket );
104  $actual = $reader->getResults();
105 
106  $expected = [
107  'xmp-exif' => [
108  'DigitalZoomRatio' => '0/10',
109  'Flash' => 9,
110  'FNumber' => '2/10',
111  ]
112  ];
113 
114  $this->assertEquals( $expected, $actual );
115  }
116 
123  public function testExtendedXMPWithWrongGUID() {
124  $xmpPath = __DIR__ . '/../../../data/xmp/';
125  $standardXMP = file_get_contents( $xmpPath . 'xmpExt.xmp' );
126  $extendedXMP = file_get_contents( $xmpPath . 'xmpExt2.xmp' );
127 
128  $md5sum = '28C74E0AC2D796886759006FBE2E57B9'; // Note last digit.
129  $length = pack( 'N', strlen( $extendedXMP ) );
130  $offset = pack( 'N', 0 );
131  $extendedPacket = $md5sum . $length . $offset . $extendedXMP;
132 
133  $reader = new XMPReader();
134  $reader->parse( $standardXMP );
135  $reader->parseExtended( $extendedPacket );
136  $actual = $reader->getResults();
137 
138  $expected = [
139  'xmp-exif' => [
140  'DigitalZoomRatio' => '0/10',
141  'Flash' => 9,
142  ]
143  ];
144 
145  $this->assertEquals( $expected, $actual );
146  }
147 
154  public function testExtendedXMPMissingPacket() {
155  $xmpPath = __DIR__ . '/../../../data/xmp/';
156  $standardXMP = file_get_contents( $xmpPath . 'xmpExt.xmp' );
157  $extendedXMP = file_get_contents( $xmpPath . 'xmpExt2.xmp' );
158 
159  $md5sum = '28C74E0AC2D796886759006FBE2E57B7'; // of xmpExt2.xmp
160  $length = pack( 'N', strlen( $extendedXMP ) );
161  $offset = pack( 'N', 2048 );
162  $extendedPacket = $md5sum . $length . $offset . $extendedXMP;
163 
164  $reader = new XMPReader();
165  $reader->parse( $standardXMP );
166  $reader->parseExtended( $extendedPacket );
167  $actual = $reader->getResults();
168 
169  $expected = [
170  'xmp-exif' => [
171  'DigitalZoomRatio' => '0/10',
172  'Flash' => 9,
173  ]
174  ];
175 
176  $this->assertEquals( $expected, $actual );
177  }
178 
183  public function testCheckParseSafety() {
184 
185  // Test for detection
186  $xmpPath = __DIR__ . '/../../../data/xmp/';
187  $file = fopen( $xmpPath . 'doctype-included.xmp', 'rb' );
188  $valid = false;
189  $reader = new XMPReader();
190  do {
191  $chunk = fread( $file, 10 );
192  $valid = $reader->parse( $chunk, feof( $file ) );
193  } while ( !feof( $file ) );
194  $this->assertFalse( $valid, 'Check that doctype is detected in fragmented XML' );
195  $this->assertEquals(
196  [],
197  $reader->getResults(),
198  'Check that doctype is detected in fragmented XML'
199  );
200  fclose( $file );
201  unset( $reader );
202 
203  // Test for false positives
204  $file = fopen( $xmpPath . 'doctype-not-included.xmp', 'rb' );
205  $valid = false;
206  $reader = new XMPReader();
207  do {
208  $chunk = fread( $file, 10 );
209  $valid = $reader->parse( $chunk, feof( $file ) );
210  } while ( !feof( $file ) );
211  $this->assertTrue(
212  $valid,
213  'Check for false-positive detecting doctype in fragmented XML'
214  );
215  $this->assertEquals(
216  [
217  'xmp-exif' => [
218  'DigitalZoomRatio' => '0/10',
219  'Flash' => '9'
220  ]
221  ],
222  $reader->getResults(),
223  'Check that doctype is detected in fragmented XML'
224  );
225  }
226 }
setUp()
Definition: XMPTest.php:9
testXMPParse($xmp, $expected, $info)
Put XMP in, compare what comes out...
Definition: XMPTest.php:29
Media XMPReader.
Definition: XMPTest.php:7
testExtendedXMPWithWrongGUID()
This test has an extended XMP block with a wrong guid (md5sum) and thus should only return the Standa...
Definition: XMPTest.php:123
testExtendedXMP()
Test ExtendedXMP block support.
Definition: XMPTest.php:91
The index of the header message $result[1]=The index of the body text message $result[2 through n]=Parameters passed to body text message.Please note the header message cannot receive/use parameters. 'ImportHandleLogItemXMLTag':When parsing a XML tag in a log item.Return false to stop further processing of the tag $reader:XMLReader object $logInfo:Array of information 'ImportHandlePageXMLTag':When parsing a XML tag in a page.Return false to stop further processing of the tag $reader:XMLReader object &$pageInfo:Array of information 'ImportHandleRevisionXMLTag':When parsing a XML tag in a page revision.Return false to stop further processing of the tag $reader:XMLReader object $pageInfo:Array of page information $revisionInfo:Array of revision information 'ImportHandleToplevelXMLTag':When parsing a top level XML tag.Return false to stop further processing of the tag $reader:XMLReader object 'ImportHandleUploadXMLTag':When parsing a XML tag in a file upload.Return false to stop further processing of the tag $reader:XMLReader object $revisionInfo:Array of information 'ImportLogInterwikiLink':Hook to change the interwiki link used in log entries and edit summaries for transwiki imports.&$fullInterwikiPrefix:Interwiki prefix, may contain colons.&$pageTitle:String that contains page title. 'ImportSources':Called when reading from the $wgImportSources configuration variable.Can be used to lazy-load the import sources list.&$importSources:The value of $wgImportSources.Modify as necessary.See the comment in DefaultSettings.php for the detail of how to structure this array. 'InfoAction':When building information to display on the action=info page.$context:IContextSource object &$pageInfo:Array of information 'InitializeArticleMaybeRedirect':MediaWiki check to see if title is a redirect.&$title:Title object for the current page &$request:WebRequest &$ignoreRedirect:boolean to skip redirect check &$target:Title/string of redirect target &$article:Article object 'InternalParseBeforeLinks':during Parser's internalParse method before links but after nowiki/noinclude/includeonly/onlyinclude and other processings.&$parser:Parser object &$text:string containing partially parsed text &$stripState:Parser's internal StripState object 'InternalParseBeforeSanitize':during Parser's internalParse method just before the parser removes unwanted/dangerous HTML tags and after nowiki/noinclude/includeonly/onlyinclude and other processings.Ideal for syntax-extensions after template/parser function execution which respect nowiki and HTML-comments.&$parser:Parser object &$text:string containing partially parsed text &$stripState:Parser's internal StripState object 'InterwikiLoadPrefix':When resolving if a given prefix is an interwiki or not.Return true without providing an interwiki to continue interwiki search.$prefix:interwiki prefix we are looking for.&$iwData:output array describing the interwiki with keys iw_url, iw_local, iw_trans and optionally iw_api and iw_wikiid. 'InvalidateEmailComplete':Called after a user's email has been invalidated successfully.$user:user(object) whose email is being invalidated 'IRCLineURL':When constructing the URL to use in an IRC notification.Callee may modify $url and $query, URL will be constructed as $url.$query &$url:URL to index.php &$query:Query string $rc:RecentChange object that triggered url generation 'IsFileCacheable':Override the result of Article::isFileCacheable()(if true) &$article:article(object) being checked 'IsTrustedProxy':Override the result of IP::isTrustedProxy() &$ip:IP being check &$result:Change this value to override the result of IP::isTrustedProxy() 'IsUploadAllowedFromUrl':Override the result of UploadFromUrl::isAllowedUrl() $url:URL used to upload from &$allowed:Boolean indicating if uploading is allowed for given URL 'isValidEmailAddr':Override the result of Sanitizer::validateEmail(), for instance to return false if the domain name doesn't match your organization.$addr:The e-mail address entered by the user &$result:Set this and return false to override the internal checks 'isValidPassword':Override the result of User::isValidPassword() $password:The password entered by the user &$result:Set this and return false to override the internal checks $user:User the password is being validated for 'Language::getMessagesFileName':$code:The language code or the language we're looking for a messages file for &$file:The messages file path, you can override this to change the location. 'LanguageGetMagic':DEPRECATED!Use $magicWords in a file listed in $wgExtensionMessagesFiles instead.Use this to define synonyms of magic words depending of the language &$magicExtensions:associative array of magic words synonyms $lang:language code(string) 'LanguageGetNamespaces':Provide custom ordering for namespaces or remove namespaces.Do not use this hook to add namespaces.Use CanonicalNamespaces for that.&$namespaces:Array of namespaces indexed by their numbers 'LanguageGetSpecialPageAliases':DEPRECATED!Use $specialPageAliases in a file listed in $wgExtensionMessagesFiles instead.Use to define aliases of special pages names depending of the language &$specialPageAliases:associative array of magic words synonyms $lang:language code(string) 'LanguageGetTranslatedLanguageNames':Provide translated language names.&$names:array of language code=> language name $code:language of the preferred translations 'LanguageLinks':Manipulate a page's language links.This is called in various places to allow extensions to define the effective language links for a page.$title:The page's Title.&$links:Associative array mapping language codes to prefixed links of the form"language:title".&$linkFlags:Associative array mapping prefixed links to arrays of flags.Currently unused, but planned to provide support for marking individual language links in the UI, e.g.for featured articles. 'LanguageSelector':Hook to change the language selector available on a page.$out:The output page.$cssClassName:CSS class name of the language selector. 'LinkBegin':DEPRECATED!Use HtmlPageLinkRendererBegin instead.Used when generating internal and interwiki links in Linker::link(), before processing starts.Return false to skip default processing and return $ret.See documentation for Linker::link() for details on the expected meanings of parameters.$skin:the Skin object $target:the Title that the link is pointing to &$html:the contents that the< a > tag should have(raw HTML) $result
Definition: hooks.txt:1934
testExtendedXMPMissingPacket()
Have a high offset to simulate a missing packet, which should cause it to ignore the ExtendedXMP pack...
Definition: XMPTest.php:154
testCheckParseSafety()
Test for multi-section, hostile XML XMPReader::checkParseSafety.
Definition: XMPTest.php:183
This document is intended to provide useful advice for parties seeking to redistribute MediaWiki to end users It s targeted particularly at maintainers for Linux since it s been observed that distribution packages of MediaWiki often break We ve consistently had to recommend that users seeking support use official tarballs instead of their distribution s and this often solves whatever problem the user is having It would be nice if this could such as
Definition: distributors.txt:9
Class for reading xmp data containing properties relevant to images, and spitting out an array that F...
Definition: XMP.php:54
parse($content, $allOfIt=true)
Main function to call to parse XMP.
Definition: XMP.php:303
injection txt This is an overview of how MediaWiki makes use of dependency injection The design described here grew from the discussion of RFC T384 The term dependency this means that anything an object needs to operate should be injected from the the object itself should only know narrow no concrete implementation of the logic it relies on The requirement to inject everything typically results in an architecture that based on two main types of and essentially stateless service objects that use other service objects to operate on the value objects As of the beginning MediaWiki is only starting to use the DI approach Much of the code still relies on global state or direct resulting in a highly cyclical dependency which acts as the top level factory for services in MediaWiki which can be used to gain access to default instances of various services MediaWikiServices however also allows new services to be defined and default services to be redefined Services are defined or redefined by providing a callback the instantiator that will return a new instance of the service When it will create an instance of MediaWikiServices and populate it with the services defined in the files listed by thereby bootstrapping the DI framework Per $wgServiceWiringFiles lists includes ServiceWiring php
Definition: injection.txt:35
static provideXMPParse()
Definition: XMPTest.php:38