jenkins-bot has submitted this change and it was merged.
Change subject: Parse <time> element in DateTimeOriginal
......................................................................
Parse <time> element in DateTimeOriginal
If one is found, then only the value of datetime attribute is returned.
Bug: T63701
Change-Id: Ib86865b9d049cace5e8710d2eb6c7d4a451cc673
---
M DomNavigator.php
M TemplateParser.php
M tests/data/File_Dala_Kyrka.JPG.php
M tests/data/File_Sunrise_over_fishing_boats_in_Kerala.jpg.php
M tests/phpunit/DomNavigatorTest.php
5 files changed, 38 insertions(+), 2 deletions(-)
Approvals:
Gergő Tisza: Looks good to me, approved
jenkins-bot: Verified
diff --git a/DomNavigator.php b/DomNavigator.php
index c3efee1..fa92f49 100644
--- a/DomNavigator.php
+++ b/DomNavigator.php
@@ -105,6 +105,21 @@
}
/**
+ * Returns a list of elements of the given type which have the given
attribute with any value.
+ * (In other words, this is equivalent to the CSS selector
'element[attribute]'.)
+ * When there are multiple elements with this attribute, all are
returned.
+ * @param string|array $element HTML tag name (* to accept all) or
array of tag names
+ * @param string $attribute
+ * @param DOMNode $context if present, the method will only search
inside this element
+ * @return DOMNodeList|DOMElement[]
+ */
+ public function findElementsWithAttribute( $element, $attribute,
DOMNode $context = null ) {
+ $element = $this->handleElementOrList( $element );
+ $xpath = "./descendant-or-self::{$element}[@{$attribute}]";
+ return $this->findByXpath( $xpath, $context );
+ }
+
+ /**
* Returns true if the node has all the specified classes.
* @param DOMNode $node
* @param string $classes one or more class names (separated with space)
diff --git a/TemplateParser.php b/TemplateParser.php
index ebfcf95..82cee16 100644
--- a/TemplateParser.php
+++ b/TemplateParser.php
@@ -272,6 +272,21 @@
}
/**
+ * Parses the DateTimeOriginal - finds <time> tag and returns the value
of datetime attribute
+ * @param DomNavigator $domNavigator
+ * @param DOMNode $node
+ * @return string
+ */
+ protected function parseFieldDateTimeOriginal( DomNavigator
$domNavigator, DOMNode $node ) {
+ $nodes = $domNavigator->findElementsWithAttribute( 'time',
'datetime', $node );
+ foreach ( $nodes as $time ) {
+ return $time->getAttribute( 'datetime' );
+ }
+
+ return $this->parseContents( $domNavigator, $node );
+ }
+
+ /**
* Extracts an hCard property from a DOMNode that contains an hCard
* @param DomNavigator $domNavigator
* @param DOMNode $node
diff --git a/tests/data/File_Dala_Kyrka.JPG.php
b/tests/data/File_Dala_Kyrka.JPG.php
index e24b365..a69dac2 100644
--- a/tests/data/File_Dala_Kyrka.JPG.php
+++ b/tests/data/File_Dala_Kyrka.JPG.php
@@ -7,7 +7,7 @@
return array (
'DateTimeOriginal' =>
array (
- 'value' => '<time class="dtstart"
datetime="2013-10-27">27 October 2013</time>, 10:27:44',
+ 'value' => '2013-10-27',
'source' => 'commons-desc-page',
),
'License' =>
diff --git a/tests/data/File_Sunrise_over_fishing_boats_in_Kerala.jpg.php
b/tests/data/File_Sunrise_over_fishing_boats_in_Kerala.jpg.php
index 5108133..65d3232 100644
--- a/tests/data/File_Sunrise_over_fishing_boats_in_Kerala.jpg.php
+++ b/tests/data/File_Sunrise_over_fishing_boats_in_Kerala.jpg.php
@@ -8,7 +8,7 @@
return array (
'DateTimeOriginal' =>
array (
- 'value' => '<time class="dtstart"
datetime="2009-02-18">18 February 2009</time>' . "\xc2\xa0" . '(according to <a
href="//en.wikipedia.org/wiki/Exchangeable_image_file_format" class="extiw"
title="en:Exchangeable image file format">EXIF</a> data)',
+ 'value' => '2009-02-18',
'source' => 'commons-desc-page',
),
'License' =>
diff --git a/tests/phpunit/DomNavigatorTest.php
b/tests/phpunit/DomNavigatorTest.php
index 187f544..97c20fe 100644
--- a/tests/phpunit/DomNavigatorTest.php
+++ b/tests/phpunit/DomNavigatorTest.php
@@ -140,6 +140,12 @@
$this->assertNodeListTextEquals( array( '1', '2' ), $nodes );
}
+ public function testFindElementsWithAttribute() {
+ $navigator = new DomNavigator( '<span class="foo">1</span><span
class>2</span><span id="bar">3</span>' );
+ $nodes = $navigator->findElementsWithAttribute( 'span', 'class'
);
+ $this->assertNodeListTextEquals( array( '1', '2' ), $nodes );
+ }
+
public function testClosest() {
$navigator = new DomNavigator( '<span><ul id="a"><li
id="b"><span id="c"><b></b></span></li></ul></span>' );
$node = $navigator->getByXpath( "//*[@id = 'c']" );
--
To view, visit https://gerrit.wikimedia.org/r/181978
To unsubscribe, visit https://gerrit.wikimedia.org/r/settings
Gerrit-MessageType: merged
Gerrit-Change-Id: Ib86865b9d049cace5e8710d2eb6c7d4a451cc673
Gerrit-PatchSet: 2
Gerrit-Project: mediawiki/extensions/CommonsMetadata
Gerrit-Branch: master
Gerrit-Owner: M4tx <[email protected]>
Gerrit-Reviewer: Gergő Tisza <[email protected]>
Gerrit-Reviewer: jenkins-bot <>
_______________________________________________
MediaWiki-commits mailing list
[email protected]
https://lists.wikimedia.org/mailman/listinfo/mediawiki-commits