/* eslint-disable camelcase, no-underscore-dangle */ ( function ( mw, $, OO ) { mw.FlickrChecker = function ( ui, selectButton ) { this.ui = ui; this.imageUploads = []; this.apiUrl = mw.UploadWizard.config.flickrApiUrl; this.apiKey = mw.UploadWizard.config.flickrApiKey; this.selectButton = selectButton; }; /** * Static list of all Flickr upload filenames. * Used to avoid name conflicts. Filenames are not removed when an upload is cancelled, so this can * contain fakes. Since we only use the list to choose an ugly but more unique file format on conflict, * and never refuse an upload based on it, that is not really a problem. * @type {Object} */ mw.FlickrChecker.fileNames = {}; /** * Cache for Flickr blacklist lookups. * Resolves to a hash whose keys are the blacklisted Flickr NSIDs. * Use `FlickrChecker.getBlacklist()` instead of accessing this directly. * @type {jQuery.Promise} */ mw.FlickrChecker.blacklist = null; /** * Cache for Flickr license lookups. * @type {jQuery.Promise} */ mw.FlickrChecker.licensePromise = null; /** * Flickr licenses. */ mw.FlickrChecker.licenseList = []; // Map each Flickr license name to the equivalent templates. // These are the current Flickr license names as of April 26, 2011. // Live list at http://api.flickr.com/services/rest/?&method=flickr.photos.licenses.getInfo&api_key=... mw.FlickrChecker.licenseMaps = { 'All Rights Reserved': 'invalid', 'Attribution License': '{{FlickrVerifiedByUploadWizard|cc-by-2.0}}{{cc-by-2.0}}', 'Attribution-NoDerivs License': 'invalid', 'Attribution-NonCommercial-NoDerivs License': 'invalid', 'Attribution-NonCommercial License': 'invalid', 'Attribution-NonCommercial-ShareAlike License': 'invalid', 'Attribution-ShareAlike License': '{{FlickrVerifiedByUploadWizard|cc-by-sa-2.0}}{{cc-by-sa-2.0}}', 'No known copyright restrictions': '{{FlickrVerifiedByUploadWizard|Flickr-no known copyright restrictions}}{{Flickr-no known copyright restrictions}}', 'United States Government Work': '{{FlickrVerifiedByUploadWizard|PD-USGov}}{{PD-USGov}}', 'Public Domain Dedication (CC0)': '{{FlickrVerifiedByUploadWizard|cc-zero}}{{cc-zero}}', 'Public Domain Mark': '{{FlickrVerifiedByUploadWizard|Public Domain Mark}}' // T105629 }; mw.FlickrChecker.prototype = { /** * If a photo is from Flickr, retrieve its license. If the license is valid, display the license * to the user, hide the normal license selection interface, and set it as the deed for the upload. * If the license is not valid, alert the user with an error message. If no recognized license is * retrieved, do nothing. Note that the license look-up system is fragile on purpose. If Flickr * changes the name associated with a license ID, it's better for the lookup to fail than to use * an incorrect license. * * @param {string} flickrInputUrl The source URL to check */ checkFlickr: function ( flickrInputUrl ) { var photoIdMatches, albumIdMatches, userCollectionMatches, userPhotostreamMatches, groupPoolMatches, userGalleryMatches, userFavoritesMatches; photoIdMatches = flickrInputUrl.match( /flickr\.com\/(?:x\/t\/[^/]+\/)?photos\/[^/]+\/([0-9]+)/ ); albumIdMatches = flickrInputUrl.match( /flickr\.com\/photos\/[^/]+\/(sets|albums)\/([0-9]+)/ ); userCollectionMatches = flickrInputUrl.match( /flickr\.com\/(?:x\/t\/[^/]+\/)?photos\/[^/]+\/collections\/?([0-9]+)?/ ); userPhotostreamMatches = flickrInputUrl.match( /flickr\.com\/(?:x\/t\/[^/]+\/)?photos\/([^/]+)/ ); groupPoolMatches = flickrInputUrl.match( /flickr\.com\/groups\/[^/]+(?:\/pool\/([^/]+))?/ ); userGalleryMatches = flickrInputUrl.match( /flickr\.com\/(?:x\/t\/[^/]+\/)?photos\/[^/]+\/galleries\/([0-9]+)/ ); userFavoritesMatches = flickrInputUrl.match( /flickr\.com\/(?:x\/t\/[^/]+\/)?photos\/([^/]+)\/favorites/ ); this.$spinner = $.createSpinner( { size: 'large', type: 'block' } ); $( '#mwe-upwiz-flickr-select-list-container' ).after( this.$spinner ); if ( photoIdMatches === null ) { // try static urls photoIdMatches = flickrInputUrl.match( /static\.?flickr\.com\/[^/]+\/([0-9]+)_/ ); } if ( albumIdMatches || photoIdMatches || userCollectionMatches || userPhotostreamMatches || groupPoolMatches || userGalleryMatches || userFavoritesMatches ) { $( '#mwe-upwiz-upload-add-flickr-container' ).hide(); this.imageUploads = []; if ( albumIdMatches && albumIdMatches[ 2 ] > 0 ) { this.getPhotoset( albumIdMatches, flickrInputUrl ); } else if ( photoIdMatches && photoIdMatches[ 1 ] > 0 ) { this.getPhoto( photoIdMatches, flickrInputUrl ); } else if ( userCollectionMatches ) { this.getCollection( userCollectionMatches, flickrInputUrl ); } else if ( userFavoritesMatches && userFavoritesMatches[ 1 ] ) { this.getPhotostream( 'favorites', userPhotostreamMatches, flickrInputUrl ); } else if ( userGalleryMatches && userGalleryMatches[ 1 ] ) { this.getGallery( flickrInputUrl ); } else if ( userPhotostreamMatches && userPhotostreamMatches[ 1 ] ) { this.getPhotostream( 'stream', flickrInputUrl ); } else if ( groupPoolMatches ) { this.getGroupPool( groupPoolMatches, flickrInputUrl ); } } else { // XXX show user the message that the URL entered was not valid mw.errorDialog( mw.message( 'mwe-upwiz-url-invalid', 'Flickr' ).escaped() ); this.$spinner.remove(); this.ui.flickrInterfaceReset(); } }, /** * Returns a suggested filename for the image. * 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 {number} id image id on Flickr * @param {string} ownername owner name on Flickr * @return {string} */ getFilenameFromItem: function ( title, id, ownername ) { var fileName; if ( title === '' ) { fileName = ownername + ' - ' + id + '.jpg'; } else if ( mw.FlickrChecker.fileNames[ title + '.jpg' ] ) { fileName = title + ' - ' + id + '.jpg'; } else { fileName = title + '.jpg'; } return fileName; }, /** * Reserves a filename; used by `mw.FlickrChecker.getFileNameFromItem()` which tries to * avoid returning a filename which is already reserved. * This works even when the filename was reserved in a different FlickrChecker instance. * * @param {string} fileName */ reserveFileName: function ( fileName ) { mw.FlickrChecker.fileNames[ fileName ] = true; }, /** * @param {Object} params * @return {jQuery.Promise} a promise with the response data */ flickrRequest: function ( params ) { params = $.extend( { api_key: this.apiKey, format: 'json', nojsoncallback: 1 }, params ); return $.getJSON( this.apiUrl, params ); }, /* * Retrieves a list of photos in photostream and displays it. * @see {@link getPhotos} * @param {string} mode may be: 'favorites' - user's favorites are retrieved, * or 'stream' - user's photostream is retrieved * @param {string} url URL to get the user from. * @return {jQuery.Promise} */ getPhotostream: function ( mode, url ) { var checker = this; return this.flickrRequest( { method: 'flickr.urls.lookupUser', url: url } ).then( function ( data ) { var method; if ( mode === 'stream' ) { method = 'flickr.people.getPublicPhotos'; } else if ( mode === 'favorites' ) { method = 'flickr.favorites.getPublicList'; } return checker.getPhotos( 'photos', { method: method, user_id: data.user.id } ); } ); }, /** * Retrieves a list of photos in group pool and displays it. * * @param {Object} groupPoolMatches Groups in the input URL * @param {string} url The URL from which to get the group. * @see {@link getPhotos} * @return {jQuery.Promise} */ getGroupPool: function ( groupPoolMatches, url ) { var checker = this; return this.flickrRequest( { method: 'flickr.urls.lookupGroup', url: url } ).then( function ( data ) { var gid = data.group.id; if ( groupPoolMatches[ 1 ] ) { // URL contains a user ID return checker.flickrRequest( { method: 'flickr.urls.lookupUser', url: 'http://www.flickr.com/photos/' + groupPoolMatches[ 1 ] } ).then( function ( data ) { return checker.getPhotos( 'photos', { method: 'flickr.groups.pools.getPhotos', group_id: gid, user_id: data.user.id } ); } ); } return checker.getPhotos( 'photos', { method: 'flickr.groups.pools.getPhotos', group_id: gid } ); } ); }, /** * Constructs an unordered list of sets in the collection. * * @param {boolean} appendId True if you want to append * id="mwe-upwiz-files-collection-chooser"; false otherwise * @param {Object} data The retrieved data * @see {@link getCollection} * @return {jQuery} */ buildCollectionLinks: function ( appendId, data ) { var $elem = $( '