Gergő Tisza has uploaded a new change for review.
https://gerrit.wikimedia.org/r/92532
Change subject: Add language handling to imageinfo/extmetadata API
......................................................................
Add language handling to imageinfo/extmetadata API
Adds options to return metadata in all available languages, or in
a single selected language.
Change-Id: I78f096318904a08abd317a5ed3f74ee33d3289cb
---
M docs/hooks.txt
M includes/api/ApiQueryImageInfo.php
M includes/filerepo/file/ForeignAPIFile.php
M includes/media/FormatMetadata.php
4 files changed, 55 insertions(+), 13 deletions(-)
git pull ssh://gerrit.wikimedia.org:29418/mediawiki/core
refs/changes/32/92532/1
diff --git a/docs/hooks.txt b/docs/hooks.txt
index fe6cd5e..0c39380 100644
--- a/docs/hooks.txt
+++ b/docs/hooks.txt
@@ -1162,6 +1162,9 @@
'value' => prop value, 'source' => 'name of hook' ).
$file: File object of file in question
$context: RequestContext (including language to use)
+$single: Only extract the current language; if false, the prop value should
+be in the metadata multi-language array format:
+mediawiki.org/wiki/Manual:File_metadata_handling#Multi-language_array_format
'GetFullURL': Modify fully-qualified URLs used in redirects/export/offsite
data.
$title: Title object of page
diff --git a/includes/api/ApiQueryImageInfo.php
b/includes/api/ApiQueryImageInfo.php
index 1e80f57..81c9faf 100755
--- a/includes/api/ApiQueryImageInfo.php
+++ b/includes/api/ApiQueryImageInfo.php
@@ -51,6 +51,8 @@
$metadataOpts = array(
'version' => $params['metadataversion'],
+ 'language' => $params['extmetadatalanguage'],
+ 'multilang' => $params['extmetadatamultilang'],
);
$pageIds = $this->getPageSet()->getAllTitlesByNamespace();
@@ -305,12 +307,18 @@
* @param string|array $metadataOpts Options for metadata fetching.
* This is an array consisting of the keys:
* 'version': The metadata version for the metadata option
+ * 'language': The language for extmetadata property
+ * 'multilang': Return all translations in extmetadata property
* @return Array: result array
*/
static function getInfo( $file, $prop, $result, $thumbParams = null,
$metadataOpts = false ) {
+ global $wgContLang;
+
if ( !$metadataOpts || is_string( $metadataOpts ) ) {
$metadataOpts = array(
'version' => $metadataOpts ?: 'latest',
+ 'language' => $wgContLang,
+ 'multilang' => false,
);
}
$version = $metadataOpts['version'];
@@ -437,6 +445,8 @@
// start with a letter, and all the values are strings.
// Thus there should be no issue with format=xml.
$format = new FormatMetadata;
+ $format->setSingleLanguage( !$metadataOpts['multilang']
);
+ $format->getContext()->setLanguage(
$metadataOpts['language'] );
$extmetaArray = $format->fetchExtendedMetadata( $file );
$vals['extmetadata'] = $extmetaArray;
}
@@ -515,6 +525,7 @@
}
public function getAllowedParams() {
+ global $wgContLang;
return array(
'prop' => array(
ApiBase::PARAM_ISMULTI => true,
@@ -545,6 +556,14 @@
'metadataversion' => array(
ApiBase::PARAM_TYPE => 'string',
ApiBase::PARAM_DFLT => '1',
+ ),
+ 'extmetadatalanguage' => array(
+ ApiBase::PARAM_TYPE => 'string',
+ ApiBase::PARAM_DFLT => $wgContLang->getCode(),
+ ),
+ 'extmetadatamultilang' => array(
+ ApiBase::PARAM_TYPE => 'boolean',
+ ApiBase::PARAM_DFLT => false,
),
'urlparam' => array(
ApiBase::PARAM_DFLT => '',
@@ -628,6 +647,10 @@
'end' => 'Timestamp to stop listing at',
'metadataversion' => array( "Version of metadata to
use. if 'latest' is specified, use latest version.",
"Defaults to '1' for backwards
compatibility" ),
+ 'extmetadatalanguage' => array( 'What language to fetch
extmetadata in. This affects both which',
+ 'translation to fetch, if
multiple are available, as well as how things',
+ 'like numbers and various
values are formatted.' ),
+ 'extmetadatamultilang' => 'If translations for
extmetadata property are available, fetch all of them.',
'continue' => 'If the query response includes a
continue value, use it here to get another page of results',
'localonly' => 'Look only for files in the local
repository',
);
diff --git a/includes/filerepo/file/ForeignAPIFile.php
b/includes/filerepo/file/ForeignAPIFile.php
index fc9e176..0b3d5df 100755
--- a/includes/filerepo/file/ForeignAPIFile.php
+++ b/includes/filerepo/file/ForeignAPIFile.php
@@ -57,7 +57,10 @@
'titles' => 'File:' . $title->getDBkey(),
'iiprop' => self::getProps(),
'prop' => 'imageinfo',
- 'iimetadataversion' =>
MediaHandler::getMetadataVersion()
+ 'iimetadataversion' =>
MediaHandler::getMetadataVersion(),
+ // extmetadata is language-dependant, accessing the
current language here
+ // would be problematic, so we just get them all
+ 'iiextmetadatamultilang' => 1,
) );
$info = $repo->getImageInfo( $data );
diff --git a/includes/media/FormatMetadata.php
b/includes/media/FormatMetadata.php
index 45b492a..c278526 100755
--- a/includes/media/FormatMetadata.php
+++ b/includes/media/FormatMetadata.php
@@ -957,7 +957,7 @@
$content = '';
- $cLang = $this->getLanguage()->getCode();
+ $priorityLanguages =
$this->getPriorityLanguages();
$defaultItem = false;
$defaultLang = false;
@@ -972,18 +972,24 @@
$defaultItem = $vals['x-default'];
unset( $vals['x-default'] );
}
- // Do contentLanguage.
- if ( isset( $vals[$cLang] ) ) {
- $isDefault = false;
- if ( $vals[$cLang] === $defaultItem ) {
- $defaultItem = false;
- $isDefault = true;
- }
- $content .= $this->langItem(
- $vals[$cLang], $cLang,
- $isDefault, $noHtml );
+ foreach( $priorityLanguages as $pLang ) {
+ if ( isset( $vals[$pLang] ) ) {
+ $isDefault = false;
+ if ( $vals[$pLang] ===
$defaultItem ) {
+ $defaultItem = false;
+ $isDefault = true;
+ }
+ $content .= $this->langItem(
+ $vals[$pLang], $pLang,
+ $isDefault, $noHtml );
- unset( $vals[$cLang] );
+ unset( $vals[$pLang] );
+
+ if ( $this->singleLang ) {
+ return
Html::rawElement( 'span',
+ array( 'lang'
=> $pLang ), $vals[$pLang] );
+ }
+ }
}
// Now do the rest.
@@ -994,11 +1000,18 @@
}
$content .= $this->langItem( $item,
$lang, false, $noHtml );
+ if ( $this->singleLang ) {
+ return Html::rawElement( 'span',
+ array( 'lang' => $lang
), $item );
+ }
}
if ( $defaultItem !== false ) {
$content = $this->langItem(
$defaultItem,
$defaultLang, true, $noHtml ) .
$content;
+ if ( $this->singleLang ) {
+ return $defaultItem;
+ }
}
if ( $noHtml ) {
return $content;
--
To view, visit https://gerrit.wikimedia.org/r/92532
To unsubscribe, visit https://gerrit.wikimedia.org/r/settings
Gerrit-MessageType: newchange
Gerrit-Change-Id: I78f096318904a08abd317a5ed3f74ee33d3289cb
Gerrit-PatchSet: 1
Gerrit-Project: mediawiki/core
Gerrit-Branch: master
Gerrit-Owner: Gergő Tisza <[email protected]>
_______________________________________________
MediaWiki-commits mailing list
[email protected]
https://lists.wikimedia.org/mailman/listinfo/mediawiki-commits