r87002 MediaWiki - Code Review archive

Repository:MediaWiki
Revision:r87001‎ | r87002 | r87003 >
Date:07:00, 27 April 2011
Author:kaldari
Status:resolved (Comments)
Tags:
Comment:
initial partially functioning version of FlickrChecker
Modified paths:
  • /trunk/extensions/UploadWizard/resources/mw.FlickrChecker.js (added) (history)

Diff [purge]

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 );

Follow-up revisions

RevisionCommit summaryAuthorDate
r87043fixing sloppy var scoping from r87002kaldari20:34, 27 April 2011
r87096follow-up to 87002, take into account Flickr adding new licenses, i.e. unknow...kaldari19:17, 28 April 2011
r87258FlickrChecker fixes, follow-up to r87002 and r87031kaldari17:24, 2 May 2011

Comments

#Comment by NeilK (talk | contribs)   20:12, 27 April 2011

- add var's to photoIdMatches & photoId - needs to have console messages removed, obviously - wondering, the UploadWizard license picker already maps license names to wikitext, can that be reused somehow? - I don't think there's a need to map names to 'invalid'; it should be that whatever doesn't have a defined wikitext mapping is invalid. You need to write it that way anyway, in case Flickr adds more licenses later

#Comment by Kaldari (talk | contribs)   20:25, 27 April 2011

The console messages have all been removed in subsequent revs. Will fix the sloppy var scoping. My thinking with the 'invalid' mapping is that if the license is definitely not supported on Commons, it's nice to let the user know that rather than just giving them a generic error, or worse letting them proceed with the upload only to have it deleted later. If the license doesn't successfully map to anything, we should probably take no action and let the user pick the license in the usual way.

#Comment by Catrope (talk | contribs)   11:22, 2 May 2011
+			$.getJSON(this.apiUrl+'&method=flickr.photos.getInfo&api_key='+this.apiKey+'&photo_id='+photoId+'&format=json&jsoncallback=?',

This doesn't escape anything whatsoever. It's safe now because the API key is hardcoded hex and the photo ID is all decimal, but it's nicer to do it the "right" way:

$.getJSON( this.apiUrl + '?jsoncallback=?', { 'method': 'flickr.photos.getInfo', 'api_key': this.apiKey, 'photo_id': photoId, 'format': 'json' }, ...
#Comment by Kaldari (talk | contribs)   17:31, 2 May 2011

Fixed in r87258.

Status & tagging log