Matthias Mullie has uploaded a new change for review. ( 
https://gerrit.wikimedia.org/r/401541 )

Change subject: Add patent selection to Special:Upload
......................................................................

Add patent selection to Special:Upload

* Patent templates can be selected from a list of radio buttons
* Selected patent will also be previewed, like licenses
* When the file uploaded is not an STL file, the patent selection
  will be hidden, as it is irrelevant.

Like MediaWiki:Licenses, MediaWiki:Patents can be altered to
add a list of possible patent templates.

Bug: T182683
Depends-On: I0c097442aa557dd90eb5825553ebf892f9af8a01
Change-Id: Iafb1e7e5da4b67f4c5ae7dda511d130ae10f748c
---
M extension.json
M i18n/en.json
M i18n/qqq.json
M modules/ext.3d.js
A modules/ext.3d.special.upload.js
M modules/mmv.3d.head.js
M modules/mmv.3d.js
M src/Hooks.php
A src/PatentFormField.php
9 files changed, 255 insertions(+), 7 deletions(-)


  git pull ssh://gerrit.wikimedia.org:29418/mediawiki/extensions/3D 
refs/changes/41/401541/1

diff --git a/extension.json b/extension.json
index 50a6c70..054dc89 100644
--- a/extension.json
+++ b/extension.json
@@ -64,6 +64,14 @@
                                "oojs-ui",
                                "oojs-ui.styles.icons-accessibility"
                        ]
+               },
+               "ext.3d.special.upload": {
+                       "scripts": [
+                               "ext.3d.special.upload.js"
+                       ],
+                       "dependencies": [
+                               "mediawiki.special.upload"
+                       ]
                }
        },
        "ResourceFileModulePaths": {
@@ -73,6 +81,12 @@
        "Hooks": {
                "BeforePageDisplay": [
                        
"MediaWiki\\Extensions\\ThreeD\\Hooks::onBeforePageDisplay"
+               ],
+               "UploadFormInitDescriptor": [
+                       
"MediaWiki\\Extensions\\ThreeD\\Hooks::onUploadFormInitDescriptor"
+               ],
+               "UploadForm:getInitialPageText": [
+                       
"MediaWiki\\Extensions\\ThreeD\\Hooks::onGetInitialPageText"
                ]
        },
        "AutoloadNamespaces": {
@@ -82,6 +96,7 @@
                "application/sla": 
"MediaWiki\\Extensions\\ThreeD\\ThreeDHandler"
        },
        "config": {
-               "Max3d2pngMemory": "10000000"
+               "Max3d2pngMemory": "10000000",
+               "AjaxPatentPreview": true
        }
 }
diff --git a/i18n/en.json b/i18n/en.json
index ae681da..2d329e2 100644
--- a/i18n/en.json
+++ b/i18n/en.json
@@ -6,5 +6,8 @@
        },
        "3d": "3d",
        "3d-desc": "Provides support for 3d file formats",
-       "3d-badge-text": "3D"
+       "3d-badge-text": "3D",
+       "patent": "Patent permissions:",
+       "nopatent": "None selected",
+       "patents": "-"
 }
diff --git a/i18n/qqq.json b/i18n/qqq.json
index c6937a2..561a115 100644
--- a/i18n/qqq.json
+++ b/i18n/qqq.json
@@ -8,5 +8,8 @@
        },
        "3d": "{{name}}",
        "3d-desc": 
"{{desc|name=3d|url=https://www.mediawiki.org/wiki/Extension:3d}}\n\n[[wikipedia:3D
 computer graphics|Read more about 3D]]",
-       "3d-badge-text": "{{optional}}\nText for the badge shown in the top 
left corner of images to identify 3D files"
+       "3d-badge-text": "{{optional}}\nText for the badge shown in the top 
left corner of images to identify 3D files",
+       "patent": "This appears in the upload form for the patent radio 
inputs.",
+       "nopatent": "{{Identical|None selected}}",
+       "patents": "{{notranslate}}"
 }
diff --git a/modules/ext.3d.js b/modules/ext.3d.js
index bfb53c3..7d31604 100644
--- a/modules/ext.3d.js
+++ b/modules/ext.3d.js
@@ -18,7 +18,9 @@
 ( function ( mw, $ ) {
        'use strict';
 
-       mw.threed = {
+       mw.threed = mw.threed || {};
+
+       mw.threed.base = {
                wrap: function ( $element ) {
                        if ( !$element.parent().hasClass( 'mw-3d-wrapper' ) ) {
                                $element.wrap( $( '<span>' ).addClass( 
'mw-3d-wrapper' ) );
@@ -40,5 +42,5 @@
                }
        };
 
-       mw.threed.attachBadge( $( 'img[src$=".stl.png"]' ) );
+       mw.threed.base.attachBadge( $( 'img[src$=".stl.png"]' ) );
 }( mediaWiki, jQuery ) );
diff --git a/modules/ext.3d.special.upload.js b/modules/ext.3d.special.upload.js
new file mode 100644
index 0000000..e469a73
--- /dev/null
+++ b/modules/ext.3d.special.upload.js
@@ -0,0 +1,89 @@
+/*
+ * This file is part of the MediaWiki extension 3D.
+ *
+ * The 3D extension is free software: you can redistribute it and/or modify
+ * it under the terms of the GNU General Public License as published by
+ * the Free Software Foundation, either version 2 of the License, or
+ * (at your option) any later version.
+ *
+ * The 3D extension is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+ * GNU General Public License for more details.
+ *
+ * You should have received a copy of the GNU General Public License
+ * along with The 3D extension. If not, see <http://www.gnu.org/licenses/>.
+ */
+
+( function ( mw, $ ) {
+       'use strict';
+
+       mw.threed = mw.threed || {};
+
+       mw.threed.specialUpload = {
+               $patent: $( 'input[name=wpPatent]' ),
+               $patentPreview: $( '<td>' ).attr( 'id', 'mw-patent-preview' ),
+               uploadTemplatePreview: window.wgUploadTemplatePreviewObj,
+
+               addPatentPreview: function () {
+                       // Patent selector table row
+                       this.$patent.closest( 'tr' ).after(
+                               $( '<tr>' ).append(
+                                       $( '<td>' ),
+                                       this.$patentPreview
+                               )
+                       );
+
+                       // Patent selector check
+                       this.$patent.change( function ( e ) {
+                               // We might show a preview
+                               this.uploadTemplatePreview.getPreview( $( 
e.currentTarget ), this.$patentPreview );
+                       }.bind( this ) );
+               },
+
+               /**
+                * @param {boolean} show True to show, false to hide
+                */
+               togglePatentSelector: function ( show ) {
+                       // select default value & show/hide the options
+                       this.$patent.eq( 0 ).prop( 'checked', true );
+                       this.$patent.closest( 'tr' ).toggle( show );
+                       this.$patentPreview.closest( 'tr' ).toggle( show );
+               },
+
+               onChangeFile: function () {
+                       var files = $( '#wpUploadFile' )[ 0 ].files,
+                               stlFiles = [];
+
+                       if ( !files ) {
+                               return;
+                       }
+
+                       $.each( files, function ( i, file ) {
+                               if ( file.name.split( '.' ).pop().toLowerCase() 
=== 'stl' ) {
+                                       stlFiles.push( file );
+                               }
+                       } );
+
+                       // only show patent selector when the upload is an STL 
file
+                       this.togglePatentSelector( stlFiles.length > 0 );
+               },
+
+               init: function () {
+                       if ( mw.config.get( 'wgAjaxPatentPreview' ) && 
this.$patent.length ) {
+                               this.addPatentPreview();
+                       }
+
+                       // If there is only 1 (default = no patent) patent 
option, then don't show it
+                       if ( this.$patent.length <= 1 ) {
+                               this.togglePatentSelector( false );
+                       }
+
+                       $( '#wpUploadFile' ).change( function () {
+                               this.onChangeFile()
+                       }.bind( this ) );
+               }
+       };
+
+       mw.threed.specialUpload.init();
+}( mediaWiki, jQuery ) );
diff --git a/modules/mmv.3d.head.js b/modules/mmv.3d.head.js
index 8fb6823..84ccac0 100644
--- a/modules/mmv.3d.head.js
+++ b/modules/mmv.3d.head.js
@@ -26,7 +26,7 @@
                 * @param {jQuery} $link
                 */
                attachControls: function ( $image, $link ) {
-                       var $wrap = mw.threed.wrap( $image ),
+                       var $wrap = mw.threed.base.wrap( $image ),
                                view = new OO.ui.ButtonWidget( {
                                        icon: 'eye',
                                        flags: [ 'progressive' ],
diff --git a/modules/mmv.3d.js b/modules/mmv.3d.js
index 7bfd18b..fa667ec 100644
--- a/modules/mmv.3d.js
+++ b/modules/mmv.3d.js
@@ -150,7 +150,7 @@
                        threed.camera.lookAt( threed.scene.position );
                        threed.render( threed.renderer, threed.scene, 
threed.camera );
 
-                       mw.threed.attachBadge( threed.$container );
+                       mw.threed.base.attachBadge( threed.$container );
                } ).progress( function ( progress ) {
                        threed.progressBar.animateTo( progress );
                } ).fail( function ( /* error */ ) {
diff --git a/src/Hooks.php b/src/Hooks.php
index a317e78..7be0878 100644
--- a/src/Hooks.php
+++ b/src/Hooks.php
@@ -39,4 +39,74 @@
 
                return true;
        }
+
+       /**
+        * @param array &$descriptor
+        * @return bool
+        */
+       public static function onUploadFormInitDescriptor( array &$descriptor ) 
{
+               global $wgOut;
+
+               if ( !array_key_exists( 'License', $descriptor ) ) {
+                       return true;
+               }
+
+               if ( empty( PatentFormField::getLinesFromMessage() ) ) {
+                       return true;
+               }
+
+               $patentDescriptor = [
+                       'Patent' => [
+                               'type' => 'select',
+                               'class' => PatentFormField::class,
+                               'section' => 'description',
+                               'id' => 'wpPatent',
+                               'label-message' => 'patent',
+                       ]
+               ];
+
+               // $descriptor is an associative array, but the order of the 
items matters for where in
+               // the form they will appear; we want it right before 'License'
+               $position = array_search( 'License', array_keys( $descriptor ), 
true );
+               $descriptor = array_slice( $descriptor, 0, $position, true ) +
+                       $patentDescriptor +
+                       array_slice( $descriptor, $position, null, true );
+
+               $context = \RequestContext::getMain();
+               $config = $context->getConfig();
+               $useAjaxPatentPreview = $config->get( 'UseAjax' ) &&
+                       $config->get( 'AjaxPatentPreview' ) && $config->get( 
'EnableAPI' );
+
+               $wgOut->addModules( [ 'ext.3d.special.upload' ] );
+               $wgOut->addJsConfigVars( [ 'wgAjaxPatentPreview' => 
$useAjaxPatentPreview ] );
+
+               return true;
+       }
+
+       /**
+        * @param string &$pageText
+        * @param array $msg
+        * @param \Config $config
+        * @return bool
+        */
+       public static function onGetInitialPageText( &$pageText, array $msg, 
\Config $config ) {
+               global $wgRequest;
+               $patent = $wgRequest->getText( 'wpPatent' );
+               if ( $patent === '' ) {
+                       // no patent text to be added
+                       return true;
+               }
+
+               $licenseHeader = '== ' . $msg['license-header'] . " ==\n";
+               $patentText = '{{' . $patent . "}}\n";
+               if ( strpos( $pageText, $licenseHeader ) >= 0 ) {
+                       // license header already exists; add it right there
+                       $pageText = str_replace( $licenseHeader, $licenseHeader 
. $patentText, $pageText );
+               } else {
+                       // license header does not already exist; create it & 
add patent info
+                       $pageText .= $licenseHeader . $patentText;
+               }
+
+               return true;
+       }
 }
diff --git a/src/PatentFormField.php b/src/PatentFormField.php
new file mode 100644
index 0000000..017205e
--- /dev/null
+++ b/src/PatentFormField.php
@@ -0,0 +1,66 @@
+<?php
+/**
+ *
+ * This program is free software; you can redistribute it and/or modify
+ * it under the terms of the GNU General Public License as published by
+ * the Free Software Foundation; either version 2 of the License, or
+ * (at your option) any later version.
+ *
+ * This program is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
+ * GNU General Public License for more details.
+ *
+ * You should have received a copy of the GNU General Public License along
+ * with this program; if not, write to the Free Software Foundation, Inc.,
+ * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
+ * http://www.gnu.org/copyleft/gpl.html
+ */
+
+namespace MediaWiki\Extensions\ThreeD;
+
+use HTMLRadioField;
+use TemplateSelector;
+
+class PatentFormField extends TemplateSelector {
+       /**
+        * {@inheritdoc}
+        */
+       public function __construct( $params ) {
+               $params['message'] = empty( $params['patents'] )
+                       ? wfMessage( 'patents' )->inContentLanguage()->plain()
+                       : $params['patents'];
+
+               parent::__construct( $params );
+       }
+
+       /**
+        * {@inheritdoc}
+        */
+       public function getInputHTML( $value ) {
+               $options[$this->msg( 'nopatent' )->text()] = '';
+               $options += $this->getOptionsArray();
+
+               $field = new HTMLRadioField( [
+                       'name' => 'wpPatent',
+                       'id' => 'wpPatent',
+                       'options' => $options,
+               ] );
+               return $field->getInputHTML( $value );
+       }
+
+       /**
+        * @return array
+        */
+       protected function getOptionsArray() {
+               $lines = $this->getLines();
+               $options = [];
+               foreach ( $lines as $line ) {
+                       $msgObj = $this->msg( $line->text );
+                       $text = $msgObj->exists() ? $msgObj->text() : 
$line->text;
+
+                       $options[$text] = $line->template;
+               }
+               return $options;
+       }
+}

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

Gerrit-MessageType: newchange
Gerrit-Change-Id: Iafb1e7e5da4b67f4c5ae7dda511d130ae10f748c
Gerrit-PatchSet: 1
Gerrit-Project: mediawiki/extensions/3D
Gerrit-Branch: master
Gerrit-Owner: Matthias Mullie <[email protected]>

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

Reply via email to