jenkins-bot has submitted this change and it was merged.

Change subject: Allow for custom templates
......................................................................


Allow for custom templates

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

Bug: 63889
Change-Id: Ibab2b75e018409e8699a72b9c90cb2eb6a7babf5
---
M i18n/en.json
M i18n/qqq.json
M includes/Adapters/Php/MediawikiTemplatePhpAdapter.php
M includes/Forms/MetadataDetectForm.php
M includes/Forms/MetadataMappingForm.php
M includes/Handlers/Forms/MetadataDetectHandler.php
M includes/Models/MediawikiTemplate.php
M includes/Utils.php
M resources/css/ext.gwtoolset.css
9 files changed, 119 insertions(+), 41 deletions(-)

Approvals:
  Dan-nl: Verified; Looks good to me, approved
  jenkins-bot: Verified



diff --git a/i18n/en.json b/i18n/en.json
index 1d9f391..2a01e99 100644
--- a/i18n/en.json
+++ b/i18n/en.json
@@ -1,7 +1,7 @@
 {
        "@metadata": {
                "authors": [
-                       "dan-nl",
+                       "dan entous",
                        "Brian Wolff",
                        "Siebrand Mazeland",
                        "Shirayuki",
@@ -86,7 +86,6 @@
        "gwtoolset-file-interpretation-error": "There was a problem processing 
the metadata file.",
        "gwtoolset-mediawiki-template": "Template $1",
        "gwtoolset-metadata-user-options-error": "The following form 
{{PLURAL:$2|field|fields}} must be filled in:\n$1",
-       "gwtoolset-metadata-invalid-template": "No valid MediaWiki template 
found.",
        "gwtoolset-menu": "* $1",
        "gwtoolset-menu-1": "Metadata mapping",
        "gwtoolset-technical-error": "There was a technical error.",
@@ -220,5 +219,10 @@
        "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 box 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 specify below a 
global license that will be applied to all the 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."
+       "gwtoolset-global-license-explanation": "Or you can specify below a 
global license that will be applied to all the 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.",
+       "gwtoolset-select-template": "Select a template:",
+       "gwtoolset-select-custom-template": "or enter a custom template:",
+       "gwtoolset-no-templatedata": "There is no 
<code>&lt;templatedata></code> block for template $1.\n\nIf possible, add a $2 
to the template, or to the template's $3.",
+       "gwtoolset-templatedata-link-text": "<code>&lt;templatedata></code> 
block",
+       "gwtoolset-templatebox-link-text": "<code>TemplateBox</code>"
 }
diff --git a/i18n/qqq.json b/i18n/qqq.json
index 70a8905..a358abd 100644
--- a/i18n/qqq.json
+++ b/i18n/qqq.json
@@ -216,5 +216,10 @@
        "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."
+       "gwtoolset-global-license-explanation": "Explanation of the 
corresponding input field in the Step 2: Metadata mapping form.",
+       "gwtoolset-select-template": "Label for the corresponding field in Step 
1: Metadata detection.",
+       "gwtoolset-select-custom-template": "Label for the corresponding field 
in Step 1: Metadata detection.",
+       "gwtoolset-no-templatedata": "Message that appears when there is no 
templatedata for a MediaWiki template chosen in Step 1: Metadata detection. 
Parameters:\n* $1 - the name of the chosen template\n* $2 - a link to the 
MediaWiki templatedata help page for adding a templatedata block to a 
template\nLink text used is {{msg-mw|Gwtoolset-templatedata-link-text}}\n* $3 - 
a link to the commons help page for adding templatedata to a template's 
templatebox\nLink text used is {{msg-mw|Gwtoolset-templatebox-link-text}}",
+       "gwtoolset-templatedata-link-text": "Text for the templatedata link. 
See {{msg-mw|Gwtoolset-no-templatedata}}",
+       "gwtoolset-templatebox-link-text": "Text for the templatebox link. See 
{{msg-mw|Gwtoolset-no-templatedata}}"
 }
diff --git a/includes/Adapters/Php/MediawikiTemplatePhpAdapter.php 
b/includes/Adapters/Php/MediawikiTemplatePhpAdapter.php
index 206dd35..ca83e39 100644
--- a/includes/Adapters/Php/MediawikiTemplatePhpAdapter.php
+++ b/includes/Adapters/Php/MediawikiTemplatePhpAdapter.php
@@ -14,6 +14,7 @@
        GWToolset\Config,
        GWToolset\GWTException,
        GWToolset\Utils,
+       Linker,
        Title;
 
 class MediawikiTemplatePhpAdapter implements DataAdapterInterface {
@@ -47,19 +48,6 @@
                $result = array( 'mediawiki_template_json' => '' );
                $template_data = null;
 
-               // should we limit the mw templates we allow?
-               // 2013-09-26 w/david haskia, for now, yes
-               if ( !isset(
-                       Config::$mediawiki_templates[Utils::sanitizeString( 
$options['mediawiki_template_name'] )] )
-               ) {
-                       throw new GWTException(
-                               array(
-                                       
'gwtoolset-mediawiki-template-not-found' =>
-                                       array( 
$options['mediawiki_template_name'] )
-                               )
-                       );
-               }
-
                $Title = Utils::getTitle(
                        $options['mediawiki_template_name'],
                        NS_TEMPLATE
@@ -76,11 +64,32 @@
 
                $template_data = $this->retrieveTemplateData( $Title );
 
-               if ( empty ( $template_data ) ) {
-                       $result['mediawiki_template_json'] =
-                               
Config::$mediawiki_templates[Utils::sanitizeString( 
$options['mediawiki_template_name'] )];
-               } else {
+               if ( !empty( $template_data ) ) {
                        $result['mediawiki_template_json'] = $template_data;
+               } else if (
+                       in_array(
+                               $options['mediawiki_template_name'],
+                               Config::$mediawiki_templates
+                       )
+               ) {
+                       $result['mediawiki_template_json'] =
+                               Config::$mediawiki_templates[ 
$options['mediawiki_template_name'] ];
+               } else {
+                       throw new GWTException(
+                               array(
+                                       'gwtoolset-no-templatedata' => array(
+                                               
$options['mediawiki_template_name'],
+                                               '[[' .
+                                                       
'mw:Extension:TemplateData#Defining_a_TemplateData_block|' .
+                                                       wfMessage( 
'gwtoolset-templatedata-link-text' )->parse() .
+                                               ']]',
+                                               '[[' .
+                                                       
'commons:Commons:TemplateData#Using_TemplateBox|' .
+                                                       wfMessage( 
'gwtoolset-templatebox-link-text' )->parse() .
+                                               ']]'
+                                       )
+                               )
+                       );
                }
 
                return $result;
diff --git a/includes/Forms/MetadataDetectForm.php 
b/includes/Forms/MetadataDetectForm.php
index 9636542..08e976d 100644
--- a/includes/Forms/MetadataDetectForm.php
+++ b/includes/Forms/MetadataDetectForm.php
@@ -195,13 +195,40 @@
                                        'label',
                                        array(),
                                        wfMessage( 
'gwtoolset-which-mediawiki-template' )->escaped() .
-                                       
$MediawikiTemplate->getTemplatesAsSelect( 'gwtoolset-mediawiki-template-name' ) 
.
                                        Html::rawElement(
                                                'span',
                                                array( 'class' => 'required' ),
                                                ' *'
                                        )
-                               )
+                               ) .
+
+                               Html::openElement( 'ul' ) .
+
+                               Html::rawElement(
+                                       'li',
+                                       array(),
+                                       wfMessage( 'gwtoolset-select-template' 
)->escaped() .
+                                       Html::rawElement( 'br' ) .
+                                       
$MediawikiTemplate->getTemplatesAsSelect( 'gwtoolset-mediawiki-template-name' )
+                               ) .
+
+                               Html::rawElement(
+                                       'li',
+                                       array(),
+                                       wfMessage( 
'gwtoolset-select-custom-template' ) .
+                                       Html::rawElement( 'br' ) .
+                                       Html::rawElement(
+                                               'input',
+                                               array(
+                                                       'type' => 'text',
+                                                       'name' => 
'gwtoolset-mediawiki-template-custom',
+                                                       'class' => 
'gwtoolset-wider-input',
+                                                       'placeholder' => 
'TorontoHollarCollection'
+                                               )
+                                       )
+                               ) .
+
+                               Html::closeElement( 'ul' )
                        ) .
 
                        Html::rawElement(
@@ -216,7 +243,7 @@
                                                array(
                                                        'type' => 'text',
                                                        'name' => 
'gwtoolset-metadata-mapping-url',
-                                                       'class' => 
'gwtoolset-url-input',
+                                                       'class' => 
'gwtoolset-wider-input',
                                                        'placeholder' => 
$namespace .
                                                                
Utils::sanitizeString( Config::$metadata_mapping_subpage ) .
                                                                
'/User-name/mapping-name.json'
@@ -370,7 +397,7 @@
                                        array(
                                                'type' => 'text',
                                                'name' => 
'gwtoolset-metadata-file-url',
-                                               'class' => 
'gwtoolset-url-input',
+                                               'class' => 
'gwtoolset-wider-input',
                                                'placeholder' => 
'Two-images.xml'
                                        )
                                ) .
diff --git a/includes/Forms/MetadataMappingForm.php 
b/includes/Forms/MetadataMappingForm.php
index dca19b4..a05435a 100644
--- a/includes/Forms/MetadataMappingForm.php
+++ b/includes/Forms/MetadataMappingForm.php
@@ -645,7 +645,7 @@
                                                        'type' => 'text',
                                                        'name' => 
'gwtoolset-partner-template-url',
                                                        'placeholder' => 
'Template:Europeana',
-                                                       'class' => 
'gwtoolset-url-input'
+                                                       'class' => 
'gwtoolset-wider-input'
                                                )
                                        )
                                ) .
diff --git a/includes/Handlers/Forms/MetadataDetectHandler.php 
b/includes/Handlers/Forms/MetadataDetectHandler.php
index 712bd35..c5d3955 100644
--- a/includes/Handlers/Forms/MetadataDetectHandler.php
+++ b/includes/Handlers/Forms/MetadataDetectHandler.php
@@ -19,6 +19,7 @@
        GWToolset\Models\MediawikiTemplate,
        GWToolset\Utils,
        FSFile,
+       Language,
        MWException,
        Php\File;
 
@@ -30,6 +31,7 @@
        protected $_expected_post_fields = array(
                'gwtoolset-form' => array( 'size' => 255 ),
                'gwtoolset-mediawiki-template-name' => array( 'size' => 255 ),
+               'gwtoolset-mediawiki-template-custom' => array( 'size' => 255 ),
                'gwtoolset-mediafile-throttle' => array( 'size' => 2 ),
                'gwtoolset-metadata-file-upload' => array( 'size' => 255 ),
                'gwtoolset-metadata-mapping-url' => array( 'size' => 255 ),
@@ -118,11 +120,7 @@
                                        )
                                : Config::$mediafile_job_throttle_default,
 
-                       'gwtoolset-mediawiki-template-name' => !empty(
-                                       
$this->_whitelisted_post['gwtoolset-mediawiki-template-name']
-                               )
-                               ? Utils::normalizeSpace( 
$this->_whitelisted_post['gwtoolset-mediawiki-template-name'] )
-                               : null,
+                       'gwtoolset-mediawiki-template-name' => 
$this->setTemplateName(),
 
                        'gwtoolset-metadata-file-url' => !empty( 
$this->_whitelisted_post['gwtoolset-metadata-file-url'] )
                                ? 
$this->_whitelisted_post['gwtoolset-metadata-file-url']
@@ -158,10 +156,12 @@
         */
        protected function processRequest() {
                $result = null;
+
                $this->_whitelisted_post = Utils::getWhitelistedPost(
                        $_POST,
                        $this->_expected_post_fields
                );
+
                $user_options = $this->getUserOptions();
 
                $this->checkForRequiredFormFields(
@@ -239,4 +239,38 @@
 
                return $result;
        }
+
+       /**
+        * the application uses gwtoolset-mediawiki-template-name to fetch a 
mediawiki
+        * template to use for metadata matching.
+        *
+        * if a value for gwtoolset-mediawiki-template-custom is given, it is 
assumed
+        * that the application should use it instead, even a value is also 
provided
+        * in gwtoolset-mediawiki-template-name.
+        *
+        * @return {string}
+        */
+       protected function setTemplateName() {
+               global $wgLanguageCode;
+
+               if (
+                       !empty( 
$this->_whitelisted_post['gwtoolset-mediawiki-template-custom'] )
+               ) {
+                       $Language = Language::factory( $wgLanguageCode );
+
+                       return
+                               Utils::normalizeSpace(
+                                       str_replace(
+                                               $Language->getNsText( 
NS_TEMPLATE ) . ':',
+                                               '',
+                                               
$this->_whitelisted_post['gwtoolset-mediawiki-template-custom']
+                                       )
+                               );
+               } else {
+                       return
+                               Utils::normalizeSpace(
+                                       
$this->_whitelisted_post['gwtoolset-mediawiki-template-name']
+                               );
+               }
+       }
 }
diff --git a/includes/Models/MediawikiTemplate.php 
b/includes/Models/MediawikiTemplate.php
index c1c8974..4c5044d 100644
--- a/includes/Models/MediawikiTemplate.php
+++ b/includes/Models/MediawikiTemplate.php
@@ -542,12 +542,8 @@
                                );
                }
 
-               if ( array_key_exists( $mediawiki_template_name, 
Config::$mediawiki_templates ) ) {
-                       $this->mediawiki_template_name = 
$mediawiki_template_name;
-                       $this->retrieve();
-               } else {
-                       throw new GWTException( 
'gwtoolset-metadata-invalid-template' );
-               }
+               $this->mediawiki_template_name = $mediawiki_template_name;
+               $this->retrieve();
        }
 
        /**
diff --git a/includes/Utils.php b/includes/Utils.php
index 318e205..af9a9a2 100644
--- a/includes/Utils.php
+++ b/includes/Utils.php
@@ -79,13 +79,14 @@
         * the result is not filtered
         */
        public static function getNamespaceName( $namespace = 0 ) {
+               global $wgLanguageCode;
                $result = null;
 
                if ( !is_int( $namespace ) ) {
                        return $result;
                }
 
-               $Languages = new Language();
+               $Languages = Language::factory( $wgLanguageCode );
                $namespaces = $Languages->getNamespaces();
 
                if ( isset( $namespaces[$namespace] ) ) {
@@ -142,7 +143,9 @@
                if ( !empty( $namespace )
                                && $namespace !== $Title->getNamespace()
                ) {
-                       $Language = new Language();
+                       global $wgLanguageCode;
+                       $Language = Language::factory( $wgLanguageCode );
+
                        throw new GWTException(
                                array(
                                        'gwtoolset-namespace-mismatch' => array(
diff --git a/resources/css/ext.gwtoolset.css b/resources/css/ext.gwtoolset.css
index 45d2800..8352cfb 100644
--- a/resources/css/ext.gwtoolset.css
+++ b/resources/css/ext.gwtoolset.css
@@ -55,8 +55,8 @@
 
 }
 
-.gwtoolset-url-input {
-       width: 300px;
+.gwtoolset-wider-input {
+       width: 350px;
 }
 
 .no-close .ui-dialog-titlebar-close {

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

Gerrit-MessageType: merged
Gerrit-Change-Id: Ibab2b75e018409e8699a72b9c90cb2eb6a7babf5
Gerrit-PatchSet: 9
Gerrit-Project: mediawiki/extensions/GWToolset
Gerrit-Branch: master
Gerrit-Owner: Dan-nl <[email protected]>
Gerrit-Reviewer: Dan-nl <[email protected]>
Gerrit-Reviewer: Gergő Tisza <[email protected]>
Gerrit-Reviewer: Gilles <[email protected]>
Gerrit-Reviewer: Jarekt <[email protected]>
Gerrit-Reviewer: Jean-Frédéric <[email protected]>
Gerrit-Reviewer: MarkTraceur <[email protected]>
Gerrit-Reviewer: Nikerabbit <[email protected]>
Gerrit-Reviewer: Raimond Spekking <[email protected]>
Gerrit-Reviewer: Siebrand <[email protected]>
Gerrit-Reviewer: Steinsplitter <[email protected]>
Gerrit-Reviewer: jenkins-bot <>

_______________________________________________
MediaWiki-commits mailing list
[email protected]
https://lists.wikimedia.org/mailman/listinfo/mediawiki-commits

Reply via email to