MarkTraceur has uploaded a new change for review.

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

Change subject: Consolidate some of the photo handling
......................................................................

Consolidate some of the photo handling

And more common sense changes too.

Change-Id: I06dbb018b3d6e12364151719d60e6f49cda5caf7
---
M resources/mw.FlickrChecker.js
1 file changed, 146 insertions(+), 122 deletions(-)


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

diff --git a/resources/mw.FlickrChecker.js b/resources/mw.FlickrChecker.js
index a893a70..23a80c7 100644
--- a/resources/mw.FlickrChecker.js
+++ b/resources/mw.FlickrChecker.js
@@ -119,13 +119,17 @@
                 * Usually the filename is just the Flickr title plus an 
extension, but in case of name conflicts
                 * or empty title a unique filename is generated.
                 *
-                * @param {string} title image title on Flickr
+                * @param {string} title image title on Flickr - sometimes they 
give us an object instead of a string, this method handles that
                 * @param {number} id image id on Flickr
                 * @param {string} ownername owner name on Flickr
                 * @return {string}
                 */
                getFilenameFromItem: function ( title, id, ownername ) {
                        var fileName;
+
+                       if ( title && title._content ) {
+                               title = title._content;
+                       }
 
                        if ( title === '' ) {
                                fileName = ownername + ' - ' + id + '.jpg';
@@ -372,29 +376,12 @@
                        // would be better to use isBlacklisted(), but didn't 
find a nice way of combining it with $.each
                        return $.when( flickrPromise, this.getBlacklist() 
).then( function ( photoset, blacklist ) {
                                var fileName, sourceURL,
+                                       promises = [],
                                        imagesHTML = '',
                                        x = 0;
 
                                $.each( photoset.photo, function ( i, item ) {
-                                       var flickrUpload, license, 
licenseValue, ownerId;
-
-                                       license = checker.checkLicense( 
item.license, i );
-                                       licenseValue = license.licenseValue;
-                                       if ( licenseValue === 'invalid' ) {
-                                               return;
-                                       }
-
-                                       if ( mode === 'photoset' ) {
-                                               ownerId = photoset.owner;
-                                               sourceURL = 
'http://www.flickr.com/photos/' + photoset.owner + '/' + item.id + '/';
-                                       } else if ( mode === 'photos' ) {
-                                               ownerId = item.owner;
-                                               sourceURL = 
'http://www.flickr.com/photos/' + item.owner + '/' + item.id + '/';
-                                       }
-
-                                       if ( ownerId in blacklist || 
item.pathalias in blacklist ) {
-                                               return;
-                                       }
+                                       var flickrUpload, license, licenseValue;
 
                                        // Limit to maximum of 500 valid images
                                        // (Flickr's API returns a maximum of 
500 images anyway.)
@@ -402,77 +389,75 @@
                                                return false;
                                        }
 
-                                       fileName = checker.getFilenameFromItem( 
item.title, item.id, item.ownername );
-
-                                       flickrUpload = {
-                                               name: fileName,
-                                               url: '',
-                                               type: 'JPEG',
-                                               fromURL: true,
-                                               licenseValue: licenseValue,
-                                               licenseMessage: 
license.licenseMessage,
-                                               license: license.licenseName 
!== 'Public Domain Mark',
-                                               photoId: item.id,
-                                               location: {
-                                                       latitude: item.latitude,
-                                                       longitude: 
item.longitude
-                                               },
-                                               author: item.ownername,
-                                               date: item.datetaken,
-                                               originalFormat: 
item.originalformat,
-                                               sourceURL: sourceURL,
-                                               index: i
-                                       };
-                                       // Adding all the Photoset files which 
have a valid license with the required info to an array so that they can be 
referenced later
-                                       checker.imageUploads[ i ] = 
flickrUpload;
-                                       checker.reserveFileName( fileName );
-
-                                       // setting up the thumbnail previews in 
the Selection list
-                                       if ( item.url_sq ) {
-                                               imagesHTML += '<li id="upload-' 
+ i + '" class="ui-state-default"><img class="lazy-thumbnail" data-original="' 
+ item.url_sq + '"></li>';
+                                       if ( mode === 'photoset' ) {
+                                               sourceURL = 
'http://www.flickr.com/photos/' + photoset.owner + '/' + item.id + '/';
+                                       } else if ( mode === 'photos' ) {
+                                               sourceURL = 
'http://www.flickr.com/photos/' + item.owner + '/' + item.id + '/';
                                        }
+
+                                       promises.push( checker.handlePhoto( 
item, sourceURL ).then( function ( upload ) {
+                                               // This is fine.
+                                               return upload;
+                                       }, function () {
+                                               // Can't reject the promise, or 
$.when will fail, just skip
+                                               return { skip: true };
+                                       } ) );
                                } );
-                               $( '#mwe-upwiz-flickr-select-list' ).append( 
imagesHTML );
-                               // Lazy-load images
-                               $( 'img.lazy-thumbnail' ).lazyload( {
-                                       // jQuery considers all images without 
'src' to not be ':visible'
-                                       skip_invisible: false
-                               } );
-                               // Trigger initial update (HACK)
-                               setTimeout( function () {
-                                       $( window ).triggerHandler( 'resize' );
-                               } );
-                               // Calling jquery ui selectable
-                               $( '#mwe-upwiz-flickr-select-list' 
).selectable( {
-                                       filter: 'li',
-                                       stop: function () {
-                                               // If at least one item is 
selected, activate the upload button
-                                               
checker.selectButton.setDisabled( $( '.ui-selected' ).length === 0 );
-                                       },
-                                       selecting: function ( event, ui ) {
-                                               // Limit the number of 
selectable images
-                                               if ( $( '.ui-selected, 
.ui-selecting' ).length > mw.UploadWizard.config.maxUploads ) {
-                                                       $( ui.selecting 
).removeClass( 'ui-selecting' );
+
+                               return $.when.apply( $, promises ).then( 
function () {
+                                       var i, uploads = [];
+
+                                       for ( i = 0; i < arguments.length; i++ 
) {
+                                               if ( arguments[i].skip ) {
+                                                       continue;
                                                }
+
+                                               uploads.push( arguments[i] );
+                                       }
+
+                                       return uploads;
+                               } ).then( function ( uploads ) {
+                                       // Lazy-load images
+                                       $( 'img.lazy-thumbnail' ).lazyload( {
+                                               // jQuery considers all images 
without 'src' to not be ':visible'
+                                               skip_invisible: false
+                                       } );
+                                       // Trigger initial update (HACK)
+                                       setTimeout( function () {
+                                               $( window ).triggerHandler( 
'resize' );
+                                       } );
+                                       // Calling jquery ui selectable
+                                       $( '#mwe-upwiz-flickr-select-list' 
).selectable( {
+                                               filter: 'li',
+                                               stop: function () {
+                                                       // If at least one item 
is selected, activate the upload button
+                                                       
checker.selectButton.setDisabled( $( '.ui-selected' ).length === 0 );
+                                               },
+                                               selecting: function ( event, ui 
) {
+                                                       // Limit the number of 
selectable images
+                                                       if ( $( '.ui-selected, 
.ui-selecting' ).length > mw.UploadWizard.config.maxUploads ) {
+                                                               $( ui.selecting 
).removeClass( 'ui-selecting' );
+                                                       }
+                                               }
+                                       } );
+                                       // Set up action for 'Upload selected 
images' button
+                                       checker.selectButton.on( 'click', 
function () {
+                                               $( 
'#mwe-upwiz-flickr-select-list-container' ).hide();
+                                               $( '#mwe-upwiz-upload-ctrls' 
).show();
+                                               $( '.ui-selected' ).each( 
function ( index, image ) {
+                                                       image = $( this ).attr( 
'id' );
+                                                       image = image.split( 
'-' )[ 1 ];
+                                                       
checker.setUploadDescription( checker.imageUploads[ image ] );
+                                                       checker.setImageURL( 
image );
+                                               } );
+                                       } );
+
+                                       if ( checker.imageUploads.length === 0 
) {
+                                               return $.Deferred().reject( 
mw.message( 'mwe-upwiz-license-photoset-invalid' ).escaped() );
+                                       } else {
+                                               $( 
'#mwe-upwiz-flickr-select-list-container' ).show();
                                        }
                                } );
-                               // Set up action for 'Upload selected images' 
button
-                               checker.selectButton.on( 'click', function () {
-                                       $( 
'#mwe-upwiz-flickr-select-list-container' ).hide();
-                                       $( '#mwe-upwiz-upload-ctrls' ).show();
-                                       $( '.ui-selected' ).each( function ( 
index, image ) {
-                                               image = $( this ).attr( 'id' );
-                                               image = image.split( '-' )[ 1 ];
-                                               checker.setUploadDescription( 
checker.imageUploads[ image ] );
-                                               checker.setImageURL( image );
-                                       } );
-                               } );
-
-                               if ( checker.imageUploads.length === 0 ) {
-                                       return $.Deferred().reject( mw.message( 
'mwe-upwiz-license-photoset-invalid' ).escaped() );
-                               } else {
-                                       $( 
'#mwe-upwiz-flickr-select-list-container' ).show();
-                               }
                        }, function ( message ) {
                                checker.showErrorDialog( message );
                                checker.wizard.flickrInterfaceReset();
@@ -509,23 +494,6 @@
                                        }
                                } );
                        } ).then( function ( photo ) {
-                               var license, flickrUpload;
-
-                               license = checker.checkLicense( photo.license );
-                               if ( license.licenseValue === 'invalid' ) {
-                                       return $.Deferred().reject( 
license.licenseMessage );
-                               }
-
-                               fileName = checker.getFilenameFromItem( 
photo.title._content, photo.id,
-                                       photo.owner.username );
-
-                               // if owner doesn't have a real name, use 
username
-                               if ( photo.owner.realname !== '' ) {
-                                       photoAuthor = photo.owner.realname;
-                               } else {
-                                       photoAuthor = photo.owner.username;
-                               }
-                               // get the URL of the photo page
                                $.each( photo.urls.url, function ( index, url ) 
{
                                        if ( url.type === 'photopage' ) {
                                                sourceURL = url._content;
@@ -533,25 +501,10 @@
                                                return false;
                                        }
                                } );
-                               flickrUpload = {
-                                       name: fileName,
-                                       url: '',
-                                       type: 'JPEG',
-                                       fromURL: true,
-                                       licenseValue: license.licenseValue,
-                                       licenseMessage: license.licenseMessage,
-                                       license: license.licenseName !== 
'Public Domain Mark',
-                                       author: photoAuthor,
-                                       originalFormat: photo.originalformat,
-                                       date: photo.dates.taken,
-                                       location: photo.location,
-                                       photoId: photo.id,
-                                       sourceURL: sourceURL
-                               };
-                               checker.setUploadDescription( flickrUpload, 
photo.description._content );
-                               checker.imageUploads.push( flickrUpload );
-                               checker.setImageURL( 0, checker );
-                               checker.reserveFileName( fileName );
+
+                               return checker.handlePhoto( photo, sourceURL 
).then( function ( upload ) {
+                                       checker.setImageURL( 0 );
+                               } );
                        }, function ( message ) {
                                checker.showErrorDialog( message );
                                checker.wizard.flickrInterfaceReset();
@@ -607,6 +560,77 @@
                },
 
                /**
+                * Process a single photo from Flickr
+                *
+                * @param {Object} photo
+                * @param {string} sourceURL
+                * @return {jQuery.Promise} Resolved with upload, rejected if 
license is
+                *   invalid.
+                */
+               handlePhoto: function ( photo, sourceURL ) {
+                       var license,
+                               checker = this;
+
+                       license = this.checkLicense( photo.license );
+
+                       if ( license.licenseValue === 'invalid' ) {
+                               return $.Deferred().reject( 
license.licenseMessage );
+                       }
+
+                       return this.isBlacklisted( photo.owner.nsid, 
photo.owner.path_alias ).then( function ( isBlacklisted ) {
+                               if ( isBlacklisted ) {
+                                       return $.Deferred().reject( mw.message( 
'mwe-upwiz-user-blacklisted', 'Flickr' ).text() );
+                               }
+
+                               return photo;
+                       } ).then( function ( photo ) {
+                               var filename = checker.getFilenameFromItem( 
photo.title, photo.id, photo.ownername ),
+                                       photoAuthor = photo.owner.realname || 
photo.owner.username,
+                                       upload = {
+                                               name: filename,
+                                               url: '',
+                                               type: 'JPEG',
+                                               fromURL: true,
+                                               licenseValue: 
license.licenseValue,
+                                               licenseMessage: 
license.licenseMessage,
+                                               license: license.licenseName 
!== 'Public Domain Mark',
+                                               photoId: photo.id,
+                                               location: photo.location || {
+                                                       latitude: 
photo.latitude,
+                                                       longitude: 
photo.longitude
+                                               },
+                                               author: photoAuthor,
+                                               date: photo.datetaken || ( 
photo.dates && photo.dates.taken ),
+                                               originalFormat: 
photo.originalformat,
+                                               sourceURL: sourceURL,
+                                               index: 
mw.UploadWizardUpload.prototype.count++
+                                       };
+
+                               if ( photo.description ) {
+                                       checker.setUploadDescription( upload, 
photo.description._content );
+                               }
+
+                               checker.imageUploads.push( upload );
+                               checker.reserveFileName( filename );
+
+                               if ( photo.url_sq ) {
+                                       $( '#mwe-upwiz-flickr-select-list' 
).append(
+                                               $( '<li>' )
+                                                       .attr( 'id', 'upload-' 
+ ( checker.imageUploads.length - 1 ) )
+                                                       .addClass( 
'ui-state-default' )
+                                                       .append(
+                                                               $( '<img>' )
+                                                                       
.addClass( 'lazy-thumbnail' )
+                                                                       .attr( 
'data-original', photo.url_sq )
+                                                       )
+                                       );
+                               }
+
+                               return upload;
+                       } );
+               },
+
+               /**
                 * Retrieve the list of all current Flickr licenses and store 
it in an array (`mw.FlickrChecker.licenseList`)
                 *
                 * @return {jQuery.Promise}

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

Gerrit-MessageType: newchange
Gerrit-Change-Id: I06dbb018b3d6e12364151719d60e6f49cda5caf7
Gerrit-PatchSet: 1
Gerrit-Project: mediawiki/extensions/UploadWizard
Gerrit-Branch: master
Gerrit-Owner: MarkTraceur <mtrac...@member.fsf.org>

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

Reply via email to