Dan-nl has uploaded a new change for review.

  https://gerrit.wikimedia.org/r/98807


Change subject: derivative-request
......................................................................

derivative-request

chris steipp requested that:

* includes/Adapters/Php/MediawikiTemplatePhpAdapter.php should use a derivative 
request
  object instead of curling the api. If this isn't possible for some reason, it 
needs to
  use https instead of http.

while working on this request i also found an issue with the retrieval of 
MediaWiki templates
that contain a space in their title, Musical work, or do not exist; corrected 
the issue.

Change-Id: I493df13429abe5f05b7bfb7d75a7633cde5c8657
---
M GWToolset.i18n.php
M includes/Adapters/Php/MediawikiTemplatePhpAdapter.php
M includes/Config.php
M includes/Handlers/Forms/MetadataDetectHandler.php
M includes/Models/MediawikiTemplate.php
M includes/Utils.php
6 files changed, 46 insertions(+), 60 deletions(-)


  git pull ssh://gerrit.wikimedia.org:29418/mediawiki/extensions/GWToolset 
refs/changes/07/98807/1

diff --git a/GWToolset.i18n.php b/GWToolset.i18n.php
index 42dd59a..f7b2d71 100644
--- a/GWToolset.i18n.php
+++ b/GWToolset.i18n.php
@@ -29,7 +29,6 @@
        /**
         * developer issues
         */
-       'gwtoolset-api-call-unsuccessful' => 'The API call to retrieve ($1) was 
unsuccessful. The API error message is ($2).',
        'gwtoolset-batchjob-creation-failure' => 'Could not create a ($1) batch 
job.',
        'gwtoolset-could-not-close-xml' => 'Could not close the XMLReader.',
        'gwtoolset-could-not-open-xml' => 'Could not open the XML File for 
reading.',
@@ -277,7 +276,6 @@
 $messages['qqq'] = array(
        'gwtoolset' => 'extension name',
        'gwtoolset-accepted-file-types' => 'Label for accepted file types in 
the HTML form.',
-       'gwtoolset-api-call-unsuccessful' => 'Message that appears when an API 
call was unsuccessful.',
        'gwtoolset-back-text' => 'User message telling the user to use the 
browser back button to go back to the HTML form. When JavaScript is active this 
message is replaced with an anchor tag using gwtoolset-back-text-link.',
        'gwtoolset-back-text-link' => 'gwtoolset-back-text is replaced by an 
anchor tag when JavaScript is active; this text is used as the text of the 
anchor tag.',
        'gwtoolset-batchjob-creation-failure' => 'Message that appears when the 
extention could not create a batch job. Parameter $1 is the type of batch job.',
diff --git a/includes/Adapters/Php/MediawikiTemplatePhpAdapter.php 
b/includes/Adapters/Php/MediawikiTemplatePhpAdapter.php
index 5566195..321ac4e 100644
--- a/includes/Adapters/Php/MediawikiTemplatePhpAdapter.php
+++ b/includes/Adapters/Php/MediawikiTemplatePhpAdapter.php
@@ -8,7 +8,9 @@
  */
 
 namespace GWToolset\Adapters\Php;
-use GWToolset\Adapters\DataAdapterInterface,
+use ApiMain,
+       DerivativeRequest,
+       GWToolset\Adapters\DataAdapterInterface,
        GWToolset\Config,
        GWToolset\GWTException,
        GWToolset\Utils,
@@ -66,11 +68,11 @@
                        NS_TEMPLATE
                );
 
-               if ( !$Title->isKnown() ) {
+               if ( $Title === null || !$Title->isKnown() ) {
                        throw new GWTException(
                                array(
                                        
'gwtoolset-mediawiki-template-does-not-exist' =>
-                                       array( $Title->getBaseTitle() )
+                                       array( 
$options['mediawiki_template_name'] )
                                )
                        );
                }
@@ -100,49 +102,29 @@
        protected function retrieveTemplateData( Title $Title ) {
                $result = null;
                $api_result = array();
-               global $wgServer, $wgScriptPath;
+               global $wgRequest;
 
-               $url = $wgServer .
-                       $wgScriptPath .
-                       '/api.php?action=templatedata&titles=' .
-                       $Title->getBaseTitle();
+               $Api = new ApiMain(
+                       new DerivativeRequest(
+                               $wgRequest,
+                               array(
+                                       'action' => 'templatedata',
+                                       'titles' => $Title->getBaseTitle()
+                               ),
+                               false // not posted
+                       ),
+                       false // disable write
+               );
 
-               $Http = MWHttpRequest::factory( $url );
+               $Api->execute();
 
-               $Status = $Http->execute();
+               $api_result = $Api->getResultData();
+               $api_result = Utils::objectToArray( $api_result );
 
-               if ( !$Status->ok ) {
-                       throw new MWException(
-                               wfMessage( 'gwtoolset-developer-issue' )
-                                       ->params(
-                                               wfMessage( 
'gwtoolset-api-call-unsuccessful' )
-                                                       ->params( 
$Title->getBaseTitle(), $Status->getMessage() )
-                                                       ->escaped()
-                                       )
-                               ->parse()
-                       );
-               }
-
-               $api_result = json_decode( $Http->getContent(), true );
-
-               try {
-                       Utils::jsonCheckForError();
-               } catch ( GWTException $e ) {
-                       throw new MWException(
-                               wfMessage( 'gwtoolset-developer-issue' )
-                                       ->params(
-                                               wfMessage( 
'gwtoolset-json-error' )
-                                                       ->params( 
$e->getMessage() )
-                                                       ->escaped()
-                                       )
-                               ->parse()
-                       );
-               }
-
-               if ( isset ( $api_result['pages'] ) && count ( 
$api_result['pages'] ) === 1 ) {
+               if ( isset( $api_result['pages'] ) && count( 
$api_result['pages'] ) === 1 ) {
                        $api_result = array_shift( $api_result['pages'] );
 
-                       if ( count ( $api_result['params'] ) > 0 ) {
+                       if ( count( $api_result['params'] ) > 0 ) {
                                foreach ( $api_result['params'] as $key => 
$value ) {
                                        if ( !$value['deprecated'] ) {
                                                $result[$key] = '';
diff --git a/includes/Config.php b/includes/Config.php
index b47fbd5..323f69c 100644
--- a/includes/Config.php
+++ b/includes/Config.php
@@ -34,18 +34,6 @@
        );
 
        /**
-        * @var {array}
-        * which MediaWiki Templates are allowed for mapping
-        */
-       public static $allowed_templates = array(
-               'Artwork',
-               'Book',
-               'Musical_work',
-               'Photograph',
-               'Specimen'
-       );
-
-       /**
         * @var {string}
         */
        public static $category_separator = '|';
@@ -92,13 +80,13 @@
 
        /**
         * @var {array}
-        * fallbacks in case there’s no template data for these MediaWiki 
templates
-        * http://gwtoolset/api.php?action=templatedata&titles=Template:Artwork
+        * - which MediaWiki Templates are allowed for mapping
+        * - fallbacks in case there’s no template data for these MediaWiki 
templates
         */
        public static $mediawiki_templates = array(
                'Artwork' => 
'{"artist":"","title":"","description":"","date":"","medium":"","dimensions":"","institution":"","location":"","references":"","object
 history":"","exhibition history":"","credit 
line":"","inscriptions":"","notes":"","accession 
number":"","source":"","permission":"","other_versions":""}',
                'Book' => 
'{"Author":"","Translator":"","Editor":"","Illustrator":"","Title":"","Subtitle":"","Series
 
title":"","Volume":"","Edition":"","Publisher":"","Printer":"","Date":"","City":"","Language":"","Description":"","Source":"","Permission":"","Image":"","Image
 
page":"","Pageoverview":"","Wikisource":"","Homecat":"","Other_versions":"","ISBN":"","LCCN":"","OCLC":""}',
-               'Musical work' => 
'{"composer":"","lyrics_writer":"","performer":"","title":"","description":"","composition_date":"","performance_date":"","notes":"","record_ID":"","image":"","references":"","source":"","permission":"","other_versions":""}',
+               'Musical_work' => 
'{"composer":"","lyrics_writer":"","performer":"","title":"","description":"","composition_date":"","performance_date":"","notes":"","record_ID":"","image":"","references":"","source":"","permission":"","other_versions":""}',
                'Photograph' => 
'{"photographer":"","title":"","description":"","depicted people":"","depicted 
place":"","date":"","medium":"","dimensions":"","institution":"","department":"","references":"","object
 history":"","exhibition history":"","credit 
line":"","inscriptions":"","notes":"","accession 
number":"","source":"","permission":"","other_versions":""}',
                'Specimen' => 
'{"taxon":"","authority":"","institution":"","accession 
number":"","sex":"","discovery 
place":"","cultivar":"","author":"","source":"","date":"","description":"","period":"","depicted
 place":"","camera 
coord":"","dimensions":"","institution":"","location":"","object 
history":"","exhibition history":"","credit 
line":"","notes":"","references":"","permission":"","other 
versions":"","photographer":"","source":""}'
        );
diff --git a/includes/Handlers/Forms/MetadataDetectHandler.php 
b/includes/Handlers/Forms/MetadataDetectHandler.php
index bb00446..afeec20 100644
--- a/includes/Handlers/Forms/MetadataDetectHandler.php
+++ b/includes/Handlers/Forms/MetadataDetectHandler.php
@@ -113,7 +113,7 @@
                        'gwtoolset-mediawiki-template-name' => !empty(
                                        
$this->_whitelisted_post['gwtoolset-mediawiki-template-name']
                                )
-                               ? 
$this->_whitelisted_post['gwtoolset-mediawiki-template-name']
+                               ? Utils::normalizeSpace( 
$this->_whitelisted_post['gwtoolset-mediawiki-template-name'] )
                                : null,
 
                        'gwtoolset-metadata-file-url' => !empty( 
$this->_whitelisted_post['gwtoolset-metadata-file-url'] )
@@ -212,7 +212,7 @@
 
                $this->_MediawikiTemplate = new MediawikiTemplate( new 
MediawikiTemplatePhpAdapter() );
                $this->_MediawikiTemplate->getMediaWikiTemplate(
-                       
$this->_whitelisted_post['gwtoolset-mediawiki-template-name']
+                       $user_options['gwtoolset-mediawiki-template-name']
                );
 
                $this->_Mapping = new Mapping( new MappingPhpAdapter() );
diff --git a/includes/Models/MediawikiTemplate.php 
b/includes/Models/MediawikiTemplate.php
index 4750a25..8df46f6 100644
--- a/includes/Models/MediawikiTemplate.php
+++ b/includes/Models/MediawikiTemplate.php
@@ -406,7 +406,7 @@
                                );
                }
 
-               if ( in_array( $mediawiki_template_name, 
Config::$allowed_templates ) ) {
+               if ( key_exists( $mediawiki_template_name, 
Config::$mediawiki_templates ) ) {
                        $this->mediawiki_template_name = 
$mediawiki_template_name;
                        $this->retrieve();
                } else {
diff --git a/includes/Utils.php b/includes/Utils.php
index 09f979f..6f7fa63 100644
--- a/includes/Utils.php
+++ b/includes/Utils.php
@@ -43,6 +43,24 @@
        }
 
        /**
+        * GlobalFunctions->wfObjectToArray() doesn’t work here
+        *
+        * @param {array|object} $data
+        * @return {array}
+        */
+       public static function objectToArray( $data ) {
+               if ( is_object( $data ) ) {
+                       $data = (array)$data;
+               }
+
+               if ( is_array( $data ) ) {
+                       return array_map( array( __CLASS__, __FUNCTION__ ), 
$data );
+               } else {
+                       return $data;
+               }
+       }
+
+       /**
         * takes a php ini value that contains a letter for Kilobytes, 
Megabytes, etc.
         * and converts it to bytes
         *

-- 
To view, visit https://gerrit.wikimedia.org/r/98807
To unsubscribe, visit https://gerrit.wikimedia.org/r/settings

Gerrit-MessageType: newchange
Gerrit-Change-Id: I493df13429abe5f05b7bfb7d75a7633cde5c8657
Gerrit-PatchSet: 1
Gerrit-Project: mediawiki/extensions/GWToolset
Gerrit-Branch: master
Gerrit-Owner: Dan-nl <d_ent...@yahoo.com>

_______________________________________________
MediaWiki-commits mailing list
MediaWiki-commits@lists.wikimedia.org
https://lists.wikimedia.org/mailman/listinfo/mediawiki-commits

Reply via email to