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

Change subject: Allow pasting full Wikimedia Commons URLs
......................................................................


Allow pasting full Wikimedia Commons URLs

Slashes are never allowed in file names. This reduces full URLs to the
pure file name, when at least two characters are left.

Bug: T147917
Change-Id: Id8c6907f3a1b4301bdbf5f76240c0b8bfc75c8ee
---
M lib/jquery.ui/jquery.ui.commonssuggester.js
A tests/lib/jquery.ui/jquery.ui.commonssuggester.tests.js
M tests/lib/resources.php
3 files changed, 120 insertions(+), 2 deletions(-)

Approvals:
  Hoo man: Looks good to me, approved
  jenkins-bot: Verified



diff --git a/lib/jquery.ui/jquery.ui.commonssuggester.js 
b/lib/jquery.ui/jquery.ui.commonssuggester.js
index 89819a9..6979cf5 100644
--- a/lib/jquery.ui/jquery.ui.commonssuggester.js
+++ b/lib/jquery.ui/jquery.ui.commonssuggester.js
@@ -32,6 +32,8 @@
                 * @return {Function}
                 */
                _initDefaultSource: function() {
+                       var self = this;
+
                        return function( term ) {
                                var deferred = $.Deferred();
 
@@ -39,14 +41,14 @@
                                        url: 
'https://commons.wikimedia.org/w/api.php',
                                        dataType: 'jsonp',
                                        data: {
-                                               search: term,
+                                               search: 
self._grepFileTitleFromTerm( term ),
                                                action: 'opensearch',
                                                namespace: 6
                                        },
                                        timeout: 8000
                                } )
                                .done( function( response ) {
-                                       deferred.resolve( response[1], 
response[0] );
+                                       deferred.resolve( response[1], term );
                                } )
                                .fail( function( jqXHR, textStatus ) {
                                        // Since this is a JSONP request, this 
will always fail with a timeout...
@@ -58,6 +60,25 @@
                },
 
                /**
+                * @private
+                *
+                * @param {string} term
+                * @return {string}
+                */
+               _grepFileTitleFromTerm: function( term ) {
+                       try {
+                               // Make sure there are always at least 2 
characters left
+                               return decodeURIComponent( term
+                                       .replace( 
/^[^#]*\btitle=([^&#]{2,}).*/, '$1' )
+                                       .replace( /^[^#]*\/([^/?#]{2,}).*/, 
'$1' )
+                               );
+                       } catch ( ex ) {
+                               // Revert all replacements when the input was 
not a (fragment of a) valid URL
+                               return term;
+                       }
+               },
+
+               /**
                 * @inheritdoc
                 * @protected
                 */
diff --git a/tests/lib/jquery.ui/jquery.ui.commonssuggester.tests.js 
b/tests/lib/jquery.ui/jquery.ui.commonssuggester.tests.js
new file mode 100644
index 0000000..54b0d77
--- /dev/null
+++ b/tests/lib/jquery.ui/jquery.ui.commonssuggester.tests.js
@@ -0,0 +1,88 @@
+/**
+ * @license GPL-2.0+
+ * @author Thiemo Mättig
+ */
+( function( $, QUnit ) {
+       'use strict';
+
+       var newTestSuggester = function() {
+               return $( '<input>' )
+                       .addClass( 'test_suggester' )
+                       .appendTo( 'body' )
+                       .commonssuggester();
+       };
+
+       QUnit.module( 'jquery.ui.commonssuggester', {
+               teardown: function() {
+                       var $suggester = $( '.test_suggester' ),
+                               suggester = $suggester.data( 'commonssuggester' 
);
+                       if ( suggester ) {
+                               suggester.destroy();
+                       }
+                       $suggester.remove();
+               }
+       } );
+
+       QUnit.test( 'Create', function( assert ) {
+               assert.expect( 1 );
+               var $suggester = newTestSuggester();
+
+               assert.ok(
+                       $suggester.data( 'commonssuggester' ) instanceof 
$.ui.commonssuggester,
+                       'Instantiated commons suggester.'
+               );
+       } );
+
+       QUnit.test( '_grepFileTitleFromTerm', function( assert ) {
+               var $suggester = newTestSuggester(),
+                       suggester = $suggester.data( 'commonssuggester' ),
+                       testCases = {
+                               '': '',
+                               'File:A.jpg': 'File:A.jpg',
+                               '%41': 'A',
+                               'A &%26 B.jpg#not-sure-if-URL': 'A && 
B.jpg#not-sure-if-URL',
+
+                               // Find last title=… parameter
+                               'title=A.jpg&title=B.jpg': 'B.jpg',
+                               'title=A.jpg/title=%42.jpg': 'B.jpg',
+                               'title=File%3AA.jpg&oldid=1#title=/X.jpg': 
'File:A.jpg',
+                               
'https://commons.wikimedia.org/w/?title=File:%41.jpg&action=edit': 'File:A.jpg',
+                               
'https://commons.wikimedia.org/w/index.php?title=File:A.jpg&oldid=1': 
'File:A.jpg',
+                               
'https://commons.wikimedia.org/w/index.php?title=File%3AA.jpg&diff=1': 
'File:A.jpg',
+
+                               // Find last word after a slash
+                               'title=A.jpg/B.jpg': 'B.jpg',
+                               'w/A.jpg': 'A.jpg',
+                               '/w/A.jpg': 'A.jpg',
+                               'A.jpg/B.jpg': 'B.jpg',
+                               'wiki/File:%41.jpg#title=/X.jpg': 'File:A.jpg',
+                               '/wiki/File:A.jpg': 'File:A.jpg',
+                               '//commons.wikimedia.org/wiki/File:A.jpg': 
'File:A.jpg',
+                               
'https://commons.wikimedia.org/wiki/File:A.jpg': 'File:A.jpg',
+                               
'https://commons.wikimedia.org/wiki/File:A.jpg#filehistory': 'File:A.jpg',
+                               
'https://commons.wikimedia.org/wiki/File:A.jpg?action=history': 'File:A.jpg',
+                               
'https://upload.wikimedia.org/wikipedia/commons/6/66/A.jpg': 'A.jpg',
+                               
'/wikipedia/commons/thumb/6/66/A.jpg/100px-A.jpg': '100px-A.jpg',
+
+                               // Minimum is 2 characters
+                               '/w/': '/w/',
+                               'title=A': 'title=A',
+                               'A.jpg/B': 'A.jpg/B',
+                               'A.jpg/B.jpg/C': 'B.jpg',
+
+                               // Do not do anything with invalid URL encoding
+                               '1%': '1%',
+                               'title=1%.jpg': 'title=1%.jpg'
+                       };
+
+               assert.expect( Object.keys( testCases ).length );
+
+               for ( var input in testCases ) {
+                       var actual = suggester._grepFileTitleFromTerm( input ),
+                               expected = testCases[input];
+
+                       assert.strictEqual( actual, expected );
+               }
+       } );
+
+}( jQuery, QUnit ) );
diff --git a/tests/lib/resources.php b/tests/lib/resources.php
index 4a07785..306dce0 100644
--- a/tests/lib/resources.php
+++ b/tests/lib/resources.php
@@ -84,6 +84,15 @@
                ),
 
 
+               'jquery.ui.commonssuggester.tests' => $moduleTemplate + array(
+                       'scripts' => array(
+                               'jquery.ui/jquery.ui.commonssuggester.tests.js',
+                       ),
+                       'dependencies' => array(
+                               'jquery.ui.commonssuggester',
+                       ),
+               ),
+
                'jquery.ui.inputextender.tests' => $moduleTemplate + array(
                        'scripts' => array(
                                'jquery.ui/jquery.ui.inputextender.tests.js',

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

Gerrit-MessageType: merged
Gerrit-Change-Id: Id8c6907f3a1b4301bdbf5f76240c0b8bfc75c8ee
Gerrit-PatchSet: 4
Gerrit-Project: data-values/value-view
Gerrit-Branch: master
Gerrit-Owner: Thiemo Mättig (WMDE) <thiemo.maet...@wikimedia.de>
Gerrit-Reviewer: Aude <aude.w...@gmail.com>
Gerrit-Reviewer: Hoo man <h...@online.de>
Gerrit-Reviewer: Jonas Kress (WMDE) <jonas.kr...@wikimedia.de>
Gerrit-Reviewer: Thiemo Mättig (WMDE) <thiemo.maet...@wikimedia.de>
Gerrit-Reviewer: jenkins-bot <>

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

Reply via email to