Bartosz Dziewoński has uploaded a new change for review.

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

Change subject: mw.DestinationChecker: Check for non-exact extension matches too
......................................................................

mw.DestinationChecker: Check for non-exact extension matches too

This is uglier than I hoped it would be. We have to fire two API requests,
since there's no way to check both for duplicates in a foreign repo and
for duplicates with a different extension. (And there's no way at all to
check for duplicates with a different extension in a foreign repo, but we
don't really care about these, we just want to stop the user from
accidentally shadowing the foreign file.)

Since the matched title can now be different from the inputted title,
changed uw.TitleDetailsWidget to use the matched one, if any.

Bug: T132356
Change-Id: I0a29d9ff20fe71fd9557feb1430164afdff9a151
---
M resources/details/uw.TitleDetailsWidget.js
M resources/mw.DestinationChecker.js
2 files changed, 90 insertions(+), 58 deletions(-)


  git pull ssh://gerrit.wikimedia.org:29418/mediawiki/extensions/UploadWizard 
refs/changes/05/283005/1

diff --git a/resources/details/uw.TitleDetailsWidget.js 
b/resources/details/uw.TitleDetailsWidget.js
index 8fb3b17..dd084ed 100644
--- a/resources/details/uw.TitleDetailsWidget.js
+++ b/resources/details/uw.TitleDetailsWidget.js
@@ -119,7 +119,8 @@
                errors = [];
 
                try {
-                       titleString = mw.UploadWizardDetails.makeTitleInFileNS( 
result.title ).getPrefixedText();
+                       titleString = result.unique.title || result.title;
+                       titleString = mw.UploadWizardDetails.makeTitleInFileNS( 
titleString ).getPrefixedText();
                } catch ( e ) {
                        // Unparseable result? This shouldn't happen, we 
checked for that earlier...
                        errors.push( mw.message( 'mwe-upwiz-unparseable-title' 
) );
diff --git a/resources/mw.DestinationChecker.js 
b/resources/mw.DestinationChecker.js
index 2cc12cb..10d17be 100644
--- a/resources/mw.DestinationChecker.js
+++ b/resources/mw.DestinationChecker.js
@@ -102,72 +102,76 @@
                 * @return {string} [return.done.href] URL to file description 
page
                 */
                checkUnique: function ( title ) {
-                       var checker = this;
+                       var checker = this,
+                               NS_FILE = mw.config.get( 'wgNamespaceIds' 
).file,
+                               titleObj, prefix, ext;
+
+                       titleObj = mw.Title.newFromText( title );
+                       ext = mw.Title.normalizeExtension( 
titleObj.getExtension() );
+                       // Strip namespace and file extension
+                       prefix = titleObj.getNameText();
 
                        function checkUniqueProcessor( data ) {
                                var result, protection, pageId, ntitle, img;
 
-                               if ( !data || !data.query || !data.query.pages 
) {
-                                       // Ignore a null result
-                                       mw.log( 
'mw.DestinationChecker::checkUnique> No data in checkUnique result' );
-                                       return $.Deferred().reject();
-                               }
+                               result = { isUnique: true };
 
-                               // The API will check for files with that 
filename.
-                               // If no file found: a page with a key of -1 
and no imageinfo
-                               // If file found on another repository, such as 
when the wiki is using InstantCommons: page with a key of -1, plus imageinfo
-                               // If file found on this repository: page with 
some positive numeric key
-                               if ( data.query.pages[ -1 ] && 
!data.query.pages[ -1 ].imageinfo ) {
-                                       protection = data.query.pages[ -1 
].protection;
-                                       if ( protection && protection.length > 
0 ) {
-                                               $.each( protection, function ( 
i, val ) {
-                                                       if ( $.inArray( 
val.level, mw.config.get( 'wgUserGroups' ) ) === -1 ) {
-                                                               result = {
-                                                                       
isUnique: true,
-                                                                       
isProtected: true
-                                                               };
-                                                       }
-                                               } );
-                                       } else {
-                                               // No conflict found on any 
repository this wiki uses
-                                               result = { isUnique: true };
-                                       }
-                               } else {
-                                       for ( pageId in data.query.pages ) {
-                                               // Conflict found, this 
filename is NOT unique
-                                               if ( data.query.normalized ) {
-                                                       ntitle = 
data.query.normalized[ 0 ].to;
+                               if ( data.query && data.query.pages ) {
+                                       // The API will check for files with 
that filename.
+                                       // If no file found: a page with a key 
of -1 and no imageinfo
+                                       // If file found on another repository, 
such as when the wiki is using InstantCommons: page with a key of -1, plus 
imageinfo
+                                       // If file found on this repository: 
page with some positive numeric key
+                                       if ( data.query.pages[ -1 ] && 
!data.query.pages[ -1 ].imageinfo ) {
+                                               protection = data.query.pages[ 
-1 ].protection;
+                                               if ( protection && 
protection.length > 0 ) {
+                                                       $.each( protection, 
function ( i, val ) {
+                                                               if ( $.inArray( 
val.level, mw.config.get( 'wgUserGroups' ) ) === -1 ) {
+                                                                       result 
= {
+                                                                               
isUnique: true,
+                                                                               
isProtected: true
+                                                                       };
+                                                               }
+                                                       } );
                                                } else {
-                                                       ntitle = 
data.query.pages[ pageId ].title;
+                                                       // No conflict found on 
any repository this wiki uses
+                                                       result = { isUnique: 
true };
                                                }
+                                       } else {
+                                               for ( pageId in 
data.query.pages ) {
+                                                       // Conflict found, this 
filename is NOT unique
+                                                       ntitle = 
data.query.pages[ pageId ].title;
+                                                       if ( ext !== 
mw.Title.newFromText( ntitle ).getExtension() ) {
+                                                               // Unless it's 
a different extension, that's fine (e.g. to upload a SVG version of a PNG file)
+                                                               continue;
+                                                       }
 
-                                               if ( !data.query.pages[ pageId 
].imageinfo ) {
-                                                       // This means that 
there's a page, but it's not a file. Well,
-                                                       // we should really 
report that anyway, but we shouldn't process
-                                                       // it like a file, and 
we should defer to other entries that may be files.
+                                                       if ( !data.query.pages[ 
pageId ].imageinfo ) {
+                                                               // This means 
that there's a page, but it's not a file. Well,
+                                                               // we should 
really report that anyway, but we shouldn't process
+                                                               // it like a 
file, and we should defer to other entries that may be files.
+                                                               result = {
+                                                                       
isUnique: false,
+                                                                       title: 
ntitle,
+                                                                       img: 
null,
+                                                                       href: 
null
+                                                               };
+                                                               continue;
+                                                       }
+
+                                                       img = data.query.pages[ 
pageId ].imageinfo[ 0 ];
+
                                                        result = {
                                                                isUnique: false,
+                                                               img: img,
                                                                title: ntitle,
-                                                               img: null,
-                                                               href: null
+                                                               href: 
img.descriptionurl
                                                        };
-                                                       continue;
+
+                                                       break;
                                                }
-
-                                               img = data.query.pages[ pageId 
].imageinfo[ 0 ];
-
-                                               result = {
-                                                       isUnique: false,
-                                                       img: img,
-                                                       title: ntitle,
-                                                       href: img.descriptionurl
-                                               };
-
-                                               break;
                                        }
                                }
 
-                               checker.cachedResult[ title ] = result;
                                return result;
                        }
 
@@ -177,13 +181,40 @@
 
                        // Setup the request -- will return thumbnail data if 
it finds one
                        // XXX do not use iiurlwidth as it will create a 
thumbnail
-                       return this.api.get( {
-                               titles: title,
-                               prop: 'info|imageinfo',
-                               inprop: 'protection',
-                               iiprop: 'url|mime|size',
-                               iiurlwidth: 150
-                       } ).then( checkUniqueProcessor );
+                       return $.when(
+                               // Checks for exact matches on this wiki and 
foreign file repos
+                               this.api.get( {
+                                       action: 'query',
+                                       titles: title,
+                                       prop: 'info|imageinfo',
+                                       inprop: 'protection',
+                                       iiprop: 'url|mime|size',
+                                       iiurlwidth: 150
+                               } ).then( checkUniqueProcessor ),
+                               // Checks for matches with different versions 
of the file extension on this wiki only
+                               this.api.get( {
+                                       action: 'query',
+                                       generator: 'allpages',
+                                       gapnamespace: NS_FILE,
+                                       gapprefix: prefix,
+                                       prop: 'info|imageinfo',
+                                       inprop: 'protection',
+                                       iiprop: 'url|mime|size',
+                                       iiurlwidth: 150
+                               } ).then( checkUniqueProcessor )
+                       ).then( function ( exact, fuzzy ) {
+                               var result;
+                               if ( !exact.isUnique || exact.isProtected ) {
+                                       result = exact;
+                               } else if ( !fuzzy.isUnique || 
fuzzy.isProtected ) {
+                                       result = fuzzy;
+                               } else {
+                                       result = { isUnique: true };
+                               }
+
+                               checker.cachedResult[ title ] = result;
+                               return result;
+                       } );
                }
 
        };

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

Gerrit-MessageType: newchange
Gerrit-Change-Id: I0a29d9ff20fe71fd9557feb1430164afdff9a151
Gerrit-PatchSet: 1
Gerrit-Project: mediawiki/extensions/UploadWizard
Gerrit-Branch: master
Gerrit-Owner: Bartosz Dziewoński <[email protected]>

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

Reply via email to