API: Allow filtering keys in iiprop=extmetadata
authorBrad Jorsch <bjorsch@wikimedia.org>
Thu, 14 Nov 2013 19:26:00 +0000 (14:26 -0500)
committerBrad Jorsch <bjorsch@wikimedia.org>
Thu, 14 Nov 2013 19:27:33 +0000 (14:27 -0500)
Requesting iiprop=extmetadata can return a *lot* of meta data, when the
client may only be interested in certain keys. This adds a
"iiextmetadatafilter" parameter to allow the client to indicate which
keys it cares about.

Change-Id: Ie2185ec285e40359fffab4932bff8cab16071a53

includes/api/ApiQueryImageInfo.php

index 990130f..96840e8 100644 (file)
@@ -53,6 +53,7 @@ class ApiQueryImageInfo extends ApiQueryBase {
                        'version' => $params['metadataversion'],
                        'language' => $params['extmetadatalanguage'],
                        'multilang' => $params['extmetadatamultilang'],
+                       'extmetadatafilter' => $params['extmetadatafilter'],
                );
 
                $pageIds = $this->getPageSet()->getAllTitlesByNamespace();
@@ -321,6 +322,7 @@ class ApiQueryImageInfo extends ApiQueryBase {
                                'version' => $metadataOpts ?: 'latest',
                                'language' => $wgContLang,
                                'multilang' => false,
+                               'extmetadatafilter' => array(),
                        );
                }
                $version = $metadataOpts['version'];
@@ -451,6 +453,11 @@ class ApiQueryImageInfo extends ApiQueryBase {
                        $format->setSingleLanguage( !$metadataOpts['multilang'] );
                        $format->getContext()->setLanguage( $metadataOpts['language'] );
                        $extmetaArray = $format->fetchExtendedMetadata( $file );
+                       if ( $metadataOpts['extmetadatafilter'] ) {
+                               $extmetaArray = array_intersect_key(
+                                       $extmetaArray, array_flip( $metadataOpts['extmetadatafilter'] )
+                               );
+                       }
                        $vals['extmetadata'] = $extmetaArray;
                }
 
@@ -571,6 +578,10 @@ class ApiQueryImageInfo extends ApiQueryBase {
                                ApiBase::PARAM_TYPE => 'boolean',
                                ApiBase::PARAM_DFLT => false,
                        ),
+                       'extmetadatafilter' => array(
+                               ApiBase::PARAM_TYPE => 'string',
+                               ApiBase::PARAM_ISMULTI => true,
+                       ),
                        'urlparam' => array(
                                ApiBase::PARAM_DFLT => '',
                                ApiBase::PARAM_TYPE => 'string',
@@ -658,6 +669,7 @@ class ApiQueryImageInfo extends ApiQueryBase {
                                '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.',
+                       'extmetadatafilter' => "If specified and non-empty, only these keys will be returned for {$p}prop=extmetadata",
                        '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',
                );