M4tx has uploaded a new change for review.
https://gerrit.wikimedia.org/r/181978
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
4 files changed, 32 insertions(+), 2 deletions(-)
git pull
ssh://gerrit.wikimedia.org:29418/mediawiki/extensions/CommonsMetadata
refs/changes/78/181978/1
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' =>
--
To view, visit https://gerrit.wikimedia.org/r/181978
To unsubscribe, visit https://gerrit.wikimedia.org/r/settings
Gerrit-MessageType: newchange
Gerrit-Change-Id: Ib86865b9d049cace5e8710d2eb6c7d4a451cc673
Gerrit-PatchSet: 1
Gerrit-Project: mediawiki/extensions/CommonsMetadata
Gerrit-Branch: master
Gerrit-Owner: M4tx <[email protected]>
_______________________________________________
MediaWiki-commits mailing list
[email protected]
https://lists.wikimedia.org/mailman/listinfo/mediawiki-commits