Dan-nl has uploaded a new change for review.

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

Change subject: Make template wrapping optional
......................................................................

Make template wrapping optional

adjusted the template wrapping so that:

* wrapping a creator parameter in a creator template is optional.
* reversing last name, first name of the creator value is optional.
* wrapping the institution value in an institution template is optional.
* wrapping of elements using lang= in a language template is optional.
* detecting and creating a permission template is optional.
* a global permission can be used instead of a metadata value.

* added new i18n messages
* added new form fields
* adjusted logic accordingly

Bug: 64060, 65248

Change-Id: Ia5f7f54f17c19f7373cc264eee5329565d585f54
---
M i18n/en.json
M i18n/qqq.json
M includes/Forms/MetadataMappingForm.php
M includes/Handlers/Forms/MetadataMappingHandler.php
M includes/Handlers/Xml/XmlMappingHandler.php
M includes/Models/MediawikiTemplate.php
6 files changed, 454 insertions(+), 128 deletions(-)


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

diff --git a/i18n/en.json b/i18n/en.json
index 3a6258b..c4dfd70 100644
--- a/i18n/en.json
+++ b/i18n/en.json
@@ -16,7 +16,7 @@
        "group-gwtoolset": "GWToolset users",
        "group-gwtoolset-member": "{{GENDER:$1|GWToolset user}}",
        "grouppage-gwtoolset": "{{ns:project}}:GWToolset users",
-       "gwtoolset-back-to-top": "⇪ top of the page",
+       "gwtoolset-back-to-top": "top of the page",
        "gwtoolset-batchjob-creation-failure": "Could not create a batch job of 
type \"$1\".",
        "gwtoolset-could-not-close-xml": "Could not close the XML reader.",
        "gwtoolset-could-not-open-xml": "Could not open the XML file for 
reading.",
@@ -201,5 +201,21 @@
        "gwtoolset-verify-php-version": "The $1 extension requires PHP >= 
5.3.3.",
        "gwtoolset-verify-uploads-enabled": "The $1 extension requires that 
file uploads are enabled.\n\nPlease make sure that 
<code>$wgEnableUploads</code> is set to <code>true</code> in 
<code>LocalSettings.php</code>.",
        "gwtoolset-verify-xmlreader": "The $1 extension requires that PHP 
[http://www.php.net/manual/en/xmlreader.setup.php XMLReader] be installed.",
-       "gwtoolset-wiki-checks-not-passed": "Wiki checks did not pass"
+       "gwtoolset-wiki-checks-not-passed": "Wiki checks did not pass",
+       "gwtoolset-reverse-creator": "Reverse last name, first name",
+       "gwtoolset-reverse-creator-explanation": "Sometimes the creator value 
is listed as last name, first name in the metadata; checking this checkbox will 
reverse that order and remove the comma; e.g., da Vinci, Leonardo would become 
Leonardo da Vinci. Note, this relies on a single comma in the value; if there 
is no comma in the value, no change will be made. If there is more than one 
comma, the change will only be made using the first comma.",
+       "gwtoolset-wrap-creator": "Wrap creator in the creator template",
+       "gwtoolset-wrap-creator-explanation": "The MediaWiki template may have 
an <code>author</code>, <code>artist</code>, <code>publisher</code> or similar 
parameter; when it does, the metadata value mapped to it can automatically be 
wrapped in <code>{{Creator:<value>}}</code>.",
+       "gwtoolset-wrap-creator-heading": "Creator template",
+       "gwtoolset-wrap-institution": "Wrap institution in the institution 
template",
+       "gwtoolset-wrap-institution-explanation": "The MediaWiki template may 
have an institution parameter; when it does, the metadata value mapped to it 
can automatically be wrapped in <code>{{Institution:<value>}}</code>.",
+       "gwtoolset-wrap-institution-heading": "Institution template",
+       "gwtoolset-wrap-language": "Wrap metadata in a language template",
+       "gwtoolset-wrap-language-explanation": "Sometimes a metadata element 
may contain a <code>lang</code> attribute; when it does, the value can be 
wrapped in a language template; e.g., <code><dc:description lang=\"en\">A 
sentence written in English.</dc:description></code> would become {{en|A 
sentence written in English.}}",
+       "gwtoolset-wrap-language-heading": "Language template",
+       "gwtoolset-detect-license": "Attempt to detect the license",
+       "gwtoolset-detect-license-explanation": "The MediaWiki template may 
have a <code>permission</code> parameter; when it does, an attempt to detect 
the copyright license based on the metadata value can be made. GWToolset looks 
for Creative Commons URLs that match possible copyright licenses; e.g., 
https://creativecommons.org/licenses/by-sa/3.0/ matches the MediaWiki license 
template <code>{{tl|Template:Cc-by-sa-3.0}}</code>. If this checkbox is left 
unchecked, the raw metadata value for each item in the batch upload will be 
placed in the permission parameter.",
+       "gwtoolset-detect-license-heading": "License template",
+       "gwtoolset-global-license": "Global license",
+       "gwtoolset-global-license-explanation": "Or you can specific a global 
license below that will be applied to all items in the batch upload. This value 
can be a free text value or a MediaWiki template. If this field has a value in 
it then any metadata value mapped to the permission parameter will be ignored."
 }
diff --git a/i18n/qqq.json b/i18n/qqq.json
index 0794712..675e03e 100644
--- a/i18n/qqq.json
+++ b/i18n/qqq.json
@@ -198,5 +198,21 @@
        "gwtoolset-verify-php-version": "Message that appears when the PHP 
version is less than version 5.3.3. Parameters:\n* $1 - \"GWToolset\" 
(untranslatable)",
        "gwtoolset-verify-uploads-enabled": "Message that appears when the wiki 
does not allow file uploads. Parameters:\n* $1 - \"GWToolset\" 
(untranslatable)",
        "gwtoolset-verify-xmlreader": "Message that appears when PHP XMLReader 
is not available. Parameters:\n* $1 - \"GWToolset\" (untranslatable)",
-       "gwtoolset-wiki-checks-not-passed": "Heading used when a wiki 
requirement is not met."
+       "gwtoolset-wiki-checks-not-passed": "Heading used when a wiki 
requirement is not met.",
+       "gwtoolset-reverse-creator": "Label used for the corresponding checkbox 
in the Step 2: Metadata mapping form.",
+       "gwtoolset-reverse-creator-explanation": "Explanation of the 
corresponding checkbox in the Step 2: Metadata mapping form.",
+       "gwtoolset-wrap-creator": "Label used for the corresponding checkbox in 
the Step 2: Metadata mapping form.",
+       "gwtoolset-wrap-creator-explanation": "Explanation of the corresponding 
checkbox in the Step 2: Metadata mapping form.",
+       "gwtoolset-wrap-creator-heading": "Heading for the corresponding 
section in the Step 2: Metadata mapping form.",
+       "gwtoolset-wrap-institution": "Label used for the corresponding 
checkbox in the Step 2: Metadata mapping form.",
+       "gwtoolset-wrap-institution-explanation": "Explanation of the 
corresponding checkbox in the Step 2: Metadata mapping form.",
+       "gwtoolset-wrap-institution-heading": "Heading for the corresponding 
section in the Step 2: Metadata mapping form.",
+       "gwtoolset-wrap-language": "Label used for the corresponding checkbox 
in the Step 2: Metadata mapping form.",
+       "gwtoolset-wrap-language-explanation": "Explanation of the 
corresponding checkbox in the Step 2: Metadata mapping form.",
+       "gwtoolset-wrap-language-heading": "Heading for the corresponding 
section in the Step 2: Metadata mapping form.",
+       "gwtoolset-detect-license": "Label used for the corresponding checkbox 
in the Step 2: Metadata mapping form.",
+       "gwtoolset-detect-license-explanation": "Explanation of the 
corresponding checkbox in the Step 2: Metadata mapping form.",
+       "gwtoolset-detect-license-heading": "Heading for the corresponding 
section in the Step 2: Metadata mapping form.",
+       "gwtoolset-global-license": "Label used for the corresponding input 
field in the Step 2: Metadata mapping form.",
+       "gwtoolset-global-license-explanation": "Explanation of the 
corresponding input field in the Step 2: Metadata mapping form."
 }
diff --git a/includes/Forms/MetadataMappingForm.php 
b/includes/Forms/MetadataMappingForm.php
index d98888f..dca19b4 100644
--- a/includes/Forms/MetadataMappingForm.php
+++ b/includes/Forms/MetadataMappingForm.php
@@ -342,6 +342,157 @@
 
                        wfMessage( 'copyrightwarning2' )->parseAsBlock() .
 
+                       // creator template
+                       Html::rawElement(
+                               'h3',
+                               array( 'style' => 'margin-top:1em;'),
+                               wfMessage( 'gwtoolset-wrap-creator-heading' 
)->escaped()
+                       ) .
+
+                       Html::rawElement(
+                               'p',
+                               array(),
+                               Html::rawElement(
+                                       'label',
+                                       array(),
+                                       Html::rawElement(
+                                               'input',
+                                               array(
+                                                       'type' => 'checkbox',
+                                                       'name' => 
'gwtoolset-wrap-creator',
+                                                       'value' => 'true'
+                                               )
+                                       ) .
+                                       ' ' . wfMessage( 
'gwtoolset-wrap-creator' )->escaped() .
+                                       Html::rawElement( 'br' ) .
+                                       wfMessage( 
'gwtoolset-wrap-creator-explanation' )->parse()
+                               )
+                       ) .
+
+                       Html::rawElement(
+                               'p',
+                               array(),
+                               Html::rawElement(
+                                       'label',
+                                       array(),
+                                       Html::rawElement(
+                                               'input',
+                                               array(
+                                                       'type' => 'checkbox',
+                                                       'name' => 
'gwtoolset-reverse-creator',
+                                                       'value' => 'true'
+                                               )
+                                       ) .
+                                       ' ' . wfMessage( 
'gwtoolset-reverse-creator' )->escaped() .
+                                       Html::rawElement( 'br' ) .
+                                       wfMessage( 
'gwtoolset-reverse-creator-explanation' )->escaped()
+                               )
+                       ) .
+
+                       // institution template
+                       Html::rawElement(
+                               'h3',
+                               array( 'style' => 'margin-top:1em;'),
+                               wfMessage( 'gwtoolset-wrap-institution-heading' 
)->escaped()
+                       ) .
+
+                       Html::rawElement(
+                               'p',
+                               array(),
+                               Html::rawElement(
+                                       'label',
+                                       array(),
+                                       Html::rawElement(
+                                               'input',
+                                               array(
+                                                       'type' => 'checkbox',
+                                                       'name' => 
'gwtoolset-wrap-institution',
+                                                       'value' => 'true'
+                                               )
+                                       ) .
+                                       ' ' . wfMessage( 
'gwtoolset-wrap-institution' )->escaped() .
+                                       Html::rawElement( 'br' ) .
+                                       wfMessage( 
'gwtoolset-wrap-institution-explanation' )->parse()
+                               )
+                       ) .
+
+                       // language template
+                       Html::rawElement(
+                               'h3',
+                               array( 'style' => 'margin-top:1em;'),
+                               wfMessage( 'gwtoolset-wrap-language-heading' 
)->escaped()
+                       ) .
+
+                       Html::rawElement(
+                               'p',
+                               array(),
+                               Html::rawElement(
+                                       'label',
+                                       array(),
+                                       Html::rawElement(
+                                               'input',
+                                               array(
+                                                       'type' => 'checkbox',
+                                                       'name' => 
'gwtoolset-wrap-language',
+                                                       'value' => 'true'
+                                               )
+                                       ) .
+                                       ' ' . wfMessage( 
'gwtoolset-wrap-language' )->escaped() .
+                                       Html::rawElement( 'br' ) .
+                                       wfMessage( 
'gwtoolset-wrap-language-explanation' )->parse()
+                               )
+                       ) .
+
+                       // permission/license template
+                       Html::rawElement(
+                               'h3',
+                               array( 'style' => 'margin-top:1em;'),
+                               wfMessage( 'gwtoolset-detect-license-heading' 
)->escaped()
+                       ) .
+
+                       Html::rawElement(
+                               'p',
+                               array(),
+                               Html::rawElement(
+                                       'label',
+                                       array(),
+                                       Html::rawElement(
+                                               'input',
+                                               array(
+                                                       'type' => 'checkbox',
+                                                       'name' => 
'gwtoolset-detect-license',
+                                                       'value' => 'true'
+                                               )
+                                       ) .
+                                       ' ' . wfMessage( 
'gwtoolset-detect-license' )->escaped() .
+                                       Html::rawElement( 'br' ) .
+                                       wfMessage( 
'gwtoolset-detect-license-explanation' )->parse()
+                               )
+                       ) .
+
+                       Html::rawElement(
+                               'p',
+                               array(),
+                               Html::rawElement(
+                                       'span',
+                                       array( 'style' => 
'font-style:italic;text-decoration:underline;' ),
+                                       wfMessage( 'gwtoolset-global-license' 
)->escaped()
+                               ) .
+                               Html::rawElement( 'br' ) .
+                               wfMessage( 
'gwtoolset-global-license-explanation' )->escaped()
+                       ) .
+
+                       Html::rawElement(
+                               'input',
+                               array(
+                                       'type' => 'text',
+                                       'name' => 'gwtoolset-global-license',
+                                       'placeholder' => wfMessage( 
'gwtoolset-global-license' ),
+                                       'class' => 'gwtoolset-url-input'
+                               )
+                       ) .
+
+                       // global categories
                        Html::rawElement(
                                'h3',
                                array( 'style' => 'margin-top:1em;'),
diff --git a/includes/Handlers/Forms/MetadataMappingHandler.php 
b/includes/Handlers/Forms/MetadataMappingHandler.php
index a9dbe98..51cc025 100644
--- a/includes/Handlers/Forms/MetadataMappingHandler.php
+++ b/includes/Handlers/Forms/MetadataMappingHandler.php
@@ -55,7 +55,13 @@
                'gwtoolset-record-begin' => array( 'size' => 255 ),
                'gwtoolset-record-count' => array( 'size' => 255 ),
                'gwtoolset-record-element-name' => array( 'size' => 255 ),
-               'gwtoolset-reupload-media' => array( 'size' => 255 ),
+               'gwtoolset-reupload-media' => array( 'size' => 4 ),
+               'gwtoolset-reverse-creator' => array( 'size' => 4 ),
+               'gwtoolset-wrap-creator' => array( 'size' => 4 ),
+               'gwtoolset-wrap-institution' => array( 'size' => 4 ),
+               'gwtoolset-wrap-language' => array( 'size' => 4 ),
+               'gwtoolset-detect-license' => array( 'size' => 4 ),
+               'gwtoolset-global-license' => array( 'size' => 255 ),
                'wpEditToken' => array( 'size' => 255 ),
                'wpSummary' => array( 'size' => 255 )
        );
@@ -184,10 +190,20 @@
                                ? 
$this->_whitelisted_post['gwtoolset-category-metadata']
                                : array(),
 
+                       'gwtoolset-detect-license' =>
+                               !empty( 
$this->_whitelisted_post['gwtoolset-detect-license'] )
+                               ? 
(bool)$this->_whitelisted_post['gwtoolset-detect-license']
+                               : false,
+
                        'comment' =>
                                !empty( $this->_whitelisted_post['wpSummary'] )
                                ? $this->_whitelisted_post['wpSummary']
                                : '',
+
+                       'gwtoolset-global-license' =>
+                               !empty( 
$this->_whitelisted_post['gwtoolset-global-license'] )
+                               ? 
$this->_whitelisted_post['gwtoolset-global-license']
+                               : null,
 
                        'gwtoolset-mediafile-throttle' =>
                                !empty( 
$this->_whitelisted_post['gwtoolset-mediafile-throttle'] )
@@ -262,6 +278,26 @@
                                ? 
(bool)$this->_whitelisted_post['gwtoolset-reupload-media']
                                : false,
 
+                       'gwtoolset-reverse-creator' =>
+                               !empty( 
$this->_whitelisted_post['gwtoolset-reverse-creator'] )
+                               ? 
(bool)$this->_whitelisted_post['gwtoolset-reverse-creator']
+                               : false,
+
+                       'gwtoolset-wrap-creator' =>
+                               !empty( 
$this->_whitelisted_post['gwtoolset-wrap-creator'] )
+                               ? 
(bool)$this->_whitelisted_post['gwtoolset-wrap-creator']
+                               : false,
+
+                       'gwtoolset-wrap-institution' =>
+                               !empty( 
$this->_whitelisted_post['gwtoolset-wrap-institution'] )
+                               ? 
(bool)$this->_whitelisted_post['gwtoolset-wrap-institution']
+                               : false,
+
+                       'gwtoolset-wrap-language' =>
+                               !empty( 
$this->_whitelisted_post['gwtoolset-wrap-language'] )
+                               ? 
(bool)$this->_whitelisted_post['gwtoolset-wrap-language']
+                               : false,
+
                        'gwtoolset-url-to-the-media-file' =>
                                !empty( 
$this->_whitelisted_post['gwtoolset-url-to-the-media-file'] )
                                ? 
$this->_whitelisted_post['gwtoolset-url-to-the-media-file']
diff --git a/includes/Handlers/Xml/XmlMappingHandler.php 
b/includes/Handlers/Xml/XmlMappingHandler.php
index 890c0e0..ed4a842 100644
--- a/includes/Handlers/Xml/XmlMappingHandler.php
+++ b/includes/Handlers/Xml/XmlMappingHandler.php
@@ -135,13 +135,14 @@
         * @todo possibly refactor so that it works with getDOMElementAsArray
         *
         * @param {DOMELement} $DOMElement
+        * @param {array} $user_options
         *
         * @return {array}
         * the keys and values in the array have not been filtered
         * an array that maps mediawiki template parameters to the metadata 
record
         * values provided by the DOMElement
         */
-       protected function getDOMElementMapped( DOMElement $DOMElement ) {
+       protected function getDOMElementMapped( DOMElement $DOMElement, array 
$user_options ) {
                $elements_mapped = array();
                $DOMNodeList = $DOMElement->getElementsByTagName( '*' );
 
@@ -173,7 +174,7 @@
                        foreach ( $template_parameters as $template_parameter ) 
{
                                $is_url = strpos( $template_parameter, 'url' ) 
!== false;
 
-                               if ( !empty( $lang ) ) {
+                               if ( !empty( $lang ) && 
$user_options['gwtoolset-wrap-language'] ) {
                                        // within a record, multimple elements 
with the same element name, e.g., description
                                        // can exist. some may have a lang 
attribute and some may not. if the first element
                                        // found does not have a lang attribute 
it is stored as a value in
@@ -377,7 +378,8 @@
                                                $user_options,
                                                array(
                                                        'metadata-as-array' => 
$this->getDOMElementAsArray( $record ),
-                                                       
'metadata-mapped-to-mediawiki-template' => $this->getDOMElementMapped( $record 
),
+                                                       
'metadata-mapped-to-mediawiki-template' =>
+                                                               
$this->getDOMElementMapped( $record, $user_options ),
                                                        'metadata-raw' => 
$outer_xml
                                                )
                                        );
diff --git a/includes/Models/MediawikiTemplate.php 
b/includes/Models/MediawikiTemplate.php
index f6d783d..7f0176b 100644
--- a/includes/Models/MediawikiTemplate.php
+++ b/includes/Models/MediawikiTemplate.php
@@ -51,7 +51,6 @@
         */
        protected $_sub_templates = array(
                'language' => '{{%s|%s}}',
-               'institution' => '{{Institution:%s}}',
                'creator' => array(
                        'template' => '{{Creator:%s}}',
                        'parameters' => array(
@@ -79,6 +78,100 @@
        }
 
        public function delete( array &$options = array() ) {
+       }
+
+       /**
+        * determines the MediaWiki template creator parameter
+        * evaluates whether or not the user has chosen to:
+        *
+        * - use the metadata value mapped to the institution parameter as is
+        * - or wrap the metadata value in an institution template
+        *
+        * @param {string} $content
+        * @param {array} $user_options
+        * @return {string}
+        */
+       protected function getCreator( $content, array $user_options ) {
+               $result = '';
+
+               // assumes that there could be more than one creator in the 
string
+               // and uses the configured metadata separator for evaluation
+               $creators = explode( Config::$metadata_separator, $content );
+
+               foreach ( $creators as $creator ) {
+                       // assumes that a creator entry could be last name, 
first
+                       if ( $user_options['gwtoolset-reverse-creator'] ) {
+                               $creator = explode( ',', $creator, 2 );
+                       } else {
+                               $creator = array( $creator );
+                       }
+
+                       if ( count( $creator ) <= 1 && trim( $creator[0] ) === 
'' ) {
+                               return $result;
+                       } else {
+                               if ( count( $creator ) === 2 ) {
+                                       $creator = trim( $creator[1] ) . ' ' . 
trim( $creator[0] );
+                               } else {
+                                       $creator = $creator[0];
+                               }
+
+                               if ( $user_options['gwtoolset-wrap-creator'] ) {
+                                       $result .= sprintf(
+                                               
$this->_sub_templates['creator']['template'],
+                                               $creator
+                                       ) . PHP_EOL;
+                               } else {
+                                       $result .= $creator . PHP_EOL;
+                               }
+                       }
+               }
+
+               return $result;
+       }
+
+       /**
+        * creates wiki text for the GWToolset parameters
+        *
+        * @todo move this into a GWToolsetTemplate model
+        *
+        * @return {string}
+        * the result is sanitized
+        */
+       public function getGWToolsetTemplateAsWikiText() {
+               return
+                       '{{Uploaded with GWToolset' . PHP_EOL .
+                       ' | gwtoolset-title-identifier = ' .
+                                       Utils::sanitizeString(
+                                               
$this->mediawiki_template_array['gwtoolset-title-identifier']
+                                       ) . PHP_EOL .
+                       ' | gwtoolset-url-to-the-media-file = ' .
+                                       Utils::sanitizeString(
+                                               
$this->mediawiki_template_array['gwtoolset-url-to-the-media-file']
+                                       ) . PHP_EOL .
+                       '}}' .
+                       PHP_EOL . PHP_EOL . PHP_EOL . PHP_EOL;
+       }
+
+       /**
+        * determines the MediaWiki template institution parameter
+        * evaluates whether or not the user has chosen to:
+        *
+        * - use the metadata value mapped to the institution parameter as is
+        * - or wrap the metadata value in an institution template
+        *
+        * @param {string} $content
+        * @param {array} $user_options
+        * @return {string}
+        */
+       protected function getInstitution( $content, array $user_options ) {
+               if (
+                       trim( $content ) === ''
+                       || !$user_options['gwtoolset-wrap-institution']
+               ) {
+                       return $content;
+               }
+
+               return sprintf( '{{Institution:%s}}', $content );
        }
 
        /**
@@ -141,26 +234,103 @@
        }
 
        /**
-        * creates wiki text for the GWToolset parameters
+        * determines the MediaWiki template permission parameter
+        * evaluates whether or not the user has chosen to:
         *
-        * @todo move this into a GWToolsetTemplate model
+        * - use a free text global license
+        * - detect and create a license template based on a cc license URL
+        * - use the metadata value mapped to the permission parameter
         *
+        * e.g. https://creativecommons.org/licenses/by-sa/3.0/ corresponds with
+        * the MediaWiki license template  {{Template:Cc-by-sa-3.0}}
+        *
+        * @see 
http://commons.wikimedia.org/wiki/Category:Creative_Commons_licenses
+        *
+        * @param {string} $content
+        * @param {array} $user_options
         * @return {string}
-        * the result is sanitized
         */
-       public function getGWToolsetTemplateAsWikiText() {
-               return
-                       '{{Uploaded with GWToolset' . PHP_EOL .
-                       ' | gwtoolset-title-identifier = ' .
-                                       Utils::sanitizeString(
-                                               
$this->mediawiki_template_array['gwtoolset-title-identifier']
-                                       ) . PHP_EOL .
-                       ' | gwtoolset-url-to-the-media-file = ' .
-                                       Utils::sanitizeString(
-                                               
$this->mediawiki_template_array['gwtoolset-url-to-the-media-file']
-                                       ) . PHP_EOL .
-                       '}}' .
-                       PHP_EOL . PHP_EOL . PHP_EOL . PHP_EOL;
+       protected function getPermission( $content, array $user_options ) {
+               if ( !empty( $user_options['gwtoolset-global-license'] ) ) {
+                       return $user_options['gwtoolset-global-license'];
+               } else if ( $user_options['gwtoolset-detect-license'] ) {
+                       $permission = strtolower( $content );
+               } else {
+                       return $content;
+               }
+
+               if ( !strstr( $permission, 'creativecommons.org/' ) ) {
+                       return $content;
+               }
+
+               $patterns = array(
+                       
'/(http|https):\/\/(www\.|)creativecommons.org\/publicdomain\/mark\/1.0\//',
+                       
'/(http|https):\/\/(www\.|)creativecommons.org\/publicdomain\/zero\/1.0\//',
+                       
'/(http|https):\/\/(www\.|)creativecommons.org\/licenses\//',
+                       '/deed\.*/'
+               );
+
+               $replacements = array(
+                       '{{PD-US}}{{PD-old}}', // Public Domain Mark 1.0
+                       '{{Cc-zero}}', // CC0 1.0 Universal (CC0 1.0) Public 
Domain Dedication
+                       '',
+                       ''
+               );
+
+               $permission = preg_replace( $patterns, $replacements, 
$permission );
+               $permission = explode( '/', $permission );
+
+               if ( count( $permission ) > 1 ) {
+                       $i = 0;
+                       $string = '{{Cc-';
+
+                       foreach ( $permission as $piece ) {
+                               if ( !empty( $piece ) ) {
+                                       $string .= $piece . '-';
+                               }
+
+                               $i++;
+
+                               // limit licenses path depth to 3
+                               if ( $i == 3 ) {
+                                       break;
+                               }
+                       }
+
+                       $string = substr( $string, 0, strlen( $string ) - 1 );
+                       $string .= '}}';
+                       $permission = $string;
+               } else {
+                       $permission = $permission[0];
+               }
+
+               return $permission;
+       }
+
+       /**
+        * determines the MediaWiki template source parameter
+        * evaluates whether or not the user has chosen to:
+        *
+        * - use a free text source template
+        *   adds the metadata value mapped to the source parameter
+        *   and adds the free text value
+        *   assumes that the free text value must be wrapped in {{}}
+        * - use the metadata value mapped to the source parameter
+        *
+        * @param {string} $content
+        * @param {array} $user_options
+        * @return {string}
+        */
+       protected function getSource( $content, array $user_options ) {
+               if ( !empty( $user_options['partner-template-name'] ) ) {
+                       return
+                               $content .
+                               '{{' .
+                               Utils::sanitizeString( 
$user_options['partner-template-name'] ) .
+                               '}}';
+               } else {
+                       return $content;
+               }
        }
 
        /**
@@ -180,7 +350,7 @@
         * @return {string}
         * the resulting wiki text is filtered
         */
-       public function getTemplateAsWikiText( array &$user_options ) {
+       public function getTemplateAsWikiText( array $user_options ) {
                $result = '';
                $sections = null;
                $template = '{{' . $this->mediawiki_template_name . PHP_EOL . 
'%s}}';
@@ -194,16 +364,19 @@
 
                        $sections .= ' | ' . Utils::sanitizeString( $parameter 
) . ' = ';
 
-                       /**
-                        * sometimes the metadata element has several "shared" 
metadata
-                        * elements with the same element name. at the moment 
the
-                        * application will add elements that use lang= 
attribute to an
-                        * associative array element 'language' indicating that 
the mediawiki
-                        * template should use the language subtemplate
-                        */
+                       // sometimes a metadata element has an XML attribute 
that the tools
+                       // looks for in order to possibly place the metadata 
into a
+                       // sub-template, e.g., <dc:description lang="en">
+                       //
+                       // currently the application only looks for XML 
elements with the
+                       // attribute lang; those elements are placed into an 
associative array
+                       // 'language' and are processed here. no other array 
grouping is
+                       // currently created.
+                       //
+                       // this section is meant to handle this current 
scenario and any future
+                       // scenarios of this type.
                        if ( is_array( $content ) ) {
                                foreach ( $content as $sub_template_name => 
$sub_template_content ) {
-                                       // currently only language is handled 
as a sub-template
                                        if ( $sub_template_name === 'language' 
) {
                                                foreach ( $sub_template_content 
as $language => $language_content ) {
                                                        $sections .= sprintf(
@@ -212,112 +385,44 @@
                                                                        
Utils::sanitizeString( $language_content )
                                                                ) . PHP_EOL;
                                                }
-                                               /**
-                                                * sometimes the "shared" 
metadata element will indicate lang,
-                                                * sometimes not this section 
handles those "shared" metadata
-                                                * elements that do not specify 
a lang attribute
-                                                */
+
+                                       // sometimes there is more than one 
metadata element with the same
+                                       // element name that falls into this 
sub-templating scenario; one
+                                       // has an XML attribute the tool looks 
for, and another does not.
+                                       // when one of those "shared" elements 
does not have the XML
+                                       // attribute the tool is looking for, 
it falls into this section and
+                                       // is not wrapped in a sub-template.
                                        } else {
                                                $sections .= 
Utils::sanitizeString( $sub_template_content ) . PHP_EOL;
                                        }
                                }
                        } else {
-                               $content = trim( $content );
-
+                               // institution parameter
                                if ( $parameter === 'institution' ) {
-                                       if ( trim( $content ) === '' ) {
-                                               $sections .= PHP_EOL;
-                                       } else {
-                                               $sections .= sprintf(
-                                                       
$this->_sub_templates['institution'],
-                                                       Utils::sanitizeString( 
$content )
-                                               ) . PHP_EOL;
-                                       }
-                               } elseif ( in_array( $parameter, 
$this->_sub_templates['creator']['parameters'] ) ) {
-                                       // assumes that there could be more 
than one creator and uses the
-                                       // configured metadata separator to 
determine that
-                                       $creators = explode( 
Config::$metadata_separator, $content );
+                                       $institution = $this->getInstitution( 
$content, $user_options );
+                                       $sections .= Utils::sanitizeString( 
$institution ) . PHP_EOL;
 
-                                       foreach ( $creators as $creator ) {
-                                               // assumes that a creator entry 
could be last name, first
-                                               // no other assumptions are 
made other than this one
-                                               $creator = explode( ',', 
$creator, 2 );
+                               // creator parameter
+                               } elseif (
+                                       in_array(
+                                               $parameter,
+                                               
$this->_sub_templates['creator']['parameters']
+                                       )
+                               ) {
+                                       $creator = $this->getCreator( $content, 
$user_options );
+                                       $sections .= Utils::sanitizeString( 
$creator );
 
-                                               if ( count( $creator ) <= 1 && 
trim( $creator[0] ) === '' ) {
-                                                       $sections .= PHP_EOL;
-                                               } else {
-                                                       if ( count( $creator ) 
=== 2 ) {
-                                                               $creator = 
trim( $creator[1] ) . ' ' . trim( $creator[0] );
-                                                       } else {
-                                                               $creator = 
trim( $creator[0] );
-                                                       }
-
-                                                       $sections .= sprintf(
-                                                               
$this->_sub_templates['creator']['template'],
-                                                               
Utils::sanitizeString( $creator )
-                                                       ) . PHP_EOL;
-                                               }
-                                       }
+                               // permission parameter
                                } elseif ( $parameter === 'permission' ) {
-                                       // 
http://commons.wikimedia.org/wiki/Category:Creative_Commons_licenses
-                                       $permission = strtolower( $content );
-
-                                       if ( strstr( $permission, 
'creativecommons.org/' ) ) {
-                                               $patterns = array(
-                                                       
'/(http|https):\/\/(www\.|)creativecommons.org\/publicdomain\/mark\/1.0\//',
-                                                       
'/(http|https):\/\/(www\.|)creativecommons.org\/publicdomain\/zero\/1.0\//',
-                                                       
'/(http|https):\/\/(www\.|)creativecommons.org\/licenses\//',
-                                                       '/deed\.*/'
-                                               );
-
-                                               $replacements = array(
-                                                       '{{PD-US}}{{PD-old}}', 
// Public Domain Mark 1.0
-                                                       '{{Cc-zero}}', // CC0 
1.0 Universal (CC0 1.0) Public Domain Dedication
-                                                       '',
-                                                       ''
-                                               );
-
-                                               $permission = preg_replace( 
$patterns, $replacements, $permission );
-                                               $permission = explode( '/', 
$permission );
-
-                                               if ( count( $permission ) > 1 ) 
{
-                                                       $i = 0;
-                                                       $string = '{{Cc-';
-
-                                                       foreach ( $permission 
as $piece ) {
-                                                               if ( !empty( 
$piece ) ) {
-                                                                       $string 
.= $piece . '-';
-                                                               }
-
-                                                               $i++;
-
-                                                               // limit 
licenses path depth to 3
-                                                               if ( $i == 3 ) {
-                                                                       break;
-                                                               }
-                                                       }
-
-                                                       $string = substr( 
$string, 0, strlen( $string ) - 1 );
-                                                       $string .= '}}';
-                                                       $permission = $string;
-                                               } else {
-                                                       $permission = 
$permission[0];
-                                               }
-                                       } else {
-                                               $permission = $content;
-                                       }
-
+                                       $permission = $this->getPermission( 
$content, $user_options );
                                        $sections .= Utils::sanitizeString( 
$permission ) . PHP_EOL;
+
+                               // source parameter
                                } elseif ( $parameter === 'source' ) {
-                                       if ( !empty( 
$user_options['partner-template-name'] ) ) {
-                                               $sections .= 
Utils::sanitizeString( $content ) .
-                                                       '{{' .
-                                                       Utils::sanitizeString( 
$user_options['partner-template-name'] ) .
-                                                       '}}' .
-                                                       PHP_EOL;
-                                       } else {
-                                               $sections .= 
Utils::sanitizeString( $content ) . PHP_EOL;
-                                       }
+                                       $source = $this->getSource( $content, 
$user_options );
+                                       $sections .= Utils::sanitizeString( 
$source ) . PHP_EOL;
+
+                               // all other parameters
                                } else {
                                        $sections .= Utils::sanitizeString( 
$content ) . PHP_EOL;
                                }

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

Gerrit-MessageType: newchange
Gerrit-Change-Id: Ia5f7f54f17c19f7373cc264eee5329565d585f54
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