Index: trunk/extensions/UploadWizard/resources/mw.FlickrChecker.js |
— | — | @@ -0,0 +1,67 @@ |
| 2 | +( function( mw ) { |
| 3 | + |
| 4 | +mw.FlickrChecker = { |
| 5 | + |
| 6 | + apiUrl: 'http://api.flickr.com/services/rest/?', |
| 7 | + apiKey: 'e9d8174a79c782745289969a45d350e8', |
| 8 | + licenseList: new Array(), |
| 9 | + |
| 10 | + // Map each Flickr license name to the equivalent templates. |
| 11 | + // These are the current Flickr license names as of April 26, 2011. |
| 12 | + licenseMaps: { |
| 13 | + 'All Rights Reserved': 'invalid', |
| 14 | + 'Attribution-NonCommercial-ShareAlike License': 'invalid', |
| 15 | + 'Attribution-NonCommercial License': 'invalid', |
| 16 | + 'Attribution-NonCommercial-NoDerivs License': 'invalid', |
| 17 | + 'Attribution License': '{{flickrreview}}{{cc-by-2.0}}', |
| 18 | + 'Attribution-ShareAlike License': '{{flickrreview}}{{cc-by-sa-2.0}}', |
| 19 | + 'Attribution-NoDerivs License': 'invalid', |
| 20 | + 'No known copyright restrictions': '{{flickrreview}}{{PD-Old}}', |
| 21 | + 'United States Government Work': '{{flickrreview}}{{PD-USGov}}' |
| 22 | + }, |
| 23 | + |
| 24 | + /** |
| 25 | + * If a photo is from flickr, retrieve its license. If the license is valid, display the license |
| 26 | + * to the user, hide the normal license selection interface, and set it as the deed for the upload. |
| 27 | + * If the license is not valid, alert the user with an error message. If no recognized license is |
| 28 | + * retrieved, do nothing. |
| 29 | + * @param url - the source URL to check |
| 30 | + * @param $selector - the element to insert the license name into |
| 31 | + * @param upload - the upload object to set the deed for |
| 32 | + */ |
| 33 | + checkFlickr: function( url, $selector, upload ) { |
| 34 | + if ( url.search(/http:\/\/(www.)?flickr.com\/photos\//) !== -1 ) { |
| 35 | + photoIdMatches = url.match(/photos\/[^\/]+\/([0-9]+)/); |
| 36 | + photoId = photoIdMatches[1]; |
| 37 | + $.getJSON(this.apiUrl+'&method=flickr.photos.getInfo&api_key='+this.apiKey+'&photo_id='+photoId+'&format=json&jsoncallback=?', |
| 38 | + function( data ) { |
| 39 | + if ( typeof data.photo != 'undefined' ) { |
| 40 | + // XXX do all the work here |
| 41 | + // The returned data.photo.license is just an ID that we use to look up the license name |
| 42 | + console.debug( mw.FlickrChecker.licenseList[data.photo.license] ); |
| 43 | + } |
| 44 | + } |
| 45 | + ); |
| 46 | + } |
| 47 | + }, |
| 48 | + |
| 49 | + /** |
| 50 | + * Retrieve the list of all current Flickr licenses and store it in an array (mw.FlickrChecker.licenses) |
| 51 | + */ |
| 52 | + getLicenses: function() { |
| 53 | + $.getJSON(this.apiUrl+'&method=flickr.photos.licenses.getInfo&api_key='+this.apiKey+'&format=json&jsoncallback=?', |
| 54 | + function( data ) { |
| 55 | + if ( typeof data.licenses != 'undefined' ) { |
| 56 | + $.each( data.licenses.license, function(index, value) { |
| 57 | + mw.FlickrChecker.licenseList[value.id] = value.name; |
| 58 | + } ); |
| 59 | + console.debug(mw.FlickrChecker.licenseList); |
| 60 | + console.debug(mw.FlickrChecker.licenseMaps); |
| 61 | + } |
| 62 | + } |
| 63 | + ); |
| 64 | + }, |
| 65 | + |
| 66 | +}; |
| 67 | + |
| 68 | +} )( window.mediaWiki ); |