Commit RELEASE-NOTES line for the wgCategories js variable I added some time ago.
[lhc/web/wiklou.git] / js2 / mwEmbed / libAddMedia / searchLibs / flickrSearch.js
1 /*
2 * Basic flickr search uses flickr jsonp api
3 * http://www.flickr.com/services/api/
4 *
5 *
6 * we look for licenses from method=flickr.photos.licenses.getInfo
7 * per http://commons.wikimedia.org/wiki/Special:Upload?uselang=fromflickr
8 * we are interested in:
9 * (4) Attribution License
10 * (5) Attribution-ShareAlike License,
11 * (7) No known copyright restrictions,
12 * (8) United States Government Work
13 */
14
15 var flickrSearch = function ( iObj ) {
16 return this.init( iObj );
17 }
18 flickrSearch.prototype = {
19 dtUrl : 'http://www.flickr.com/photos/',
20 // @@todo probably would be good to read the api-key from configuration
21 apikey : '2867787a545cc66c0bce6f2e57aca1d1',
22 // What license we are interested in
23 _license_keys: '4,5,7,8',
24 _srctypes: ['t', 'sq', 's', 'm', 'o'],
25 licenseMap: {
26 '4' : 'http://creativecommons.org/licenses/by/3.0/',
27 '5' : 'http://creativecommons.org/licenses/by-sa/3.0/',
28 '7' : 'http://www.flickr.com/commons/usage/',
29 '8' : 'http://www.usa.gov/copyright.shtml'
30 },
31 /**
32 * Initialize the flickr Search with provided options
33 */
34 init:function( options ) {
35 var baseSearch = new baseRemoteSearch( options );
36 for ( var i in baseSearch ) {
37 if ( typeof this[i] == 'undefined' ) {
38 this[i] = baseSearch[i];
39 } else {
40 this['parent_' + i] = baseSearch[i];
41 }
42 }
43 },
44 /**
45 * Gets the Search results setting _loading flag to false once results have been added
46 */
47 getSearchResults:function() {
48 var _this = this;
49 js_log( "flickr::getSearchResults" );
50 // call parent (sets loading sate and other setup stuff)
51 this.parent_getSearchResults();
52 // setup the flickr request:
53 var reqObj = {
54 'method':'flickr.photos.search',
55 'format':'json',
56 'license':this._license_keys,
57 'api_key':this.apikey,
58 'per_page': this.cp.limit,
59 'page' : this.cp.offset,
60 'text': $j( '#rsd_q' ).val(),
61 'extras' : 'license, date_upload, date_taken, owner_name, icon_server, original_format, last_update, geo, tags, machine_tags, o_dims, views, media, path_alias, url_sq, url_t, url_s, url_m, url_o'
62 }
63 do_api_req( {
64 'data': reqObj,
65 'url':this.cp.api_url,
66 'jsonCB':'jsoncallback',
67 }, function( data ) {
68 _this.addResults( data );
69 _this.loading = false;
70 } );
71 },
72 /**
73 * Adds Results for a given data response from api query
74 */
75 addResults:function( data ) {
76 var _this = this;
77 if ( data.photos && data.photos.photo ) {
78 // set result info:
79 this.num_results = data.photos.total;
80 if ( this.num_results > this.cp.offset + this.cp.limit ) {
81 this.more_results = true;
82 }
83 for ( var resource_id in data.photos.photo ) {
84 var sourceResource = data.photos.photo[ resource_id ];
85 var rObj = _this.getResourceObject( sourceResource );
86 _this.resultsObj[ resource_id ] = rObj;
87 }
88 }
89 },
90 /**
91 * Gets an individual resource object from a given source Resource
92 */
93 getResourceObject: function( resource ){
94 var _this = this;
95 var rObj = {
96 'titleKey' : resource.title + '.jpg',
97 'resourceKey': resource.id,
98 'link' : _this.dtUrl + resource.pathalias + '/' + resource.id,
99 'title' : resource.title,
100 'thumbwidth' : resource.width_t,
101 'thumbheight': resource.height_t,
102 'desc' : resource.title,
103 // Set the license
104 'license' : this.rsd.getLicenceFromUrl( _this.licenseMap[ resource.license ] ),
105 'pSobj' : _this,
106 // Assume image/jpeg for flickr response
107 'mime' : 'image/jpeg'
108 };
109 // Add all the provided src types that are avaliable
110 rObj['srcSet'] = { };
111 for ( var i in _this._srctypes ) {
112 var st = _this._srctypes[i];
113 // if resource has a url add it to the srcSet:
114 if ( resource['url_' + st] ) {
115 rObj['srcSet'][st] = {
116 'h': resource['height_' + st],
117 'w': resource['width_' + st],
118 'src': resource['url_' + st]
119 }
120 // Set src to the largest
121 rObj['src'] = resource['url_' + st];
122 }
123 }
124 return rObj;
125 },
126 /**
127 * return image transform via callback
128 */
129 getImageObj:function( rObj, size, callback ) {
130 if ( size.width ) {
131 var skey = this.getSrcTypeKey( rObj, size.width )
132 callback ( {
133 'url' : rObj.srcSet[ skey ].src,
134 'width' : rObj.srcSet[ skey ].w,
135 'height' : rObj.srcSet[ skey ].h
136 } );
137 }
138 },
139 /**
140 * Gets an image transformation based a SrcTypeKey gennerated by the requested options
141 */
142 getImageTransform:function( rObj, options ) {
143 if ( options.width ) {
144 return rObj.srcSet[ this.getSrcTypeKey( rObj, options.width ) ].src;
145 }
146 return rObj.srcSet[ _srctypes[_srctypes.length-1] ];
147 },
148 getSrcTypeKey:function( rObj, width ) {
149 if ( width <= 75 ) {
150 if ( rObj.srcSet['sq'] )
151 return 'sq';
152 } else if ( width <= 100 ) {
153 if ( rObj.srcSet['t'] )
154 return 't';
155 } else if ( width <= 240 ) {
156 if ( rObj.srcSet['s'] )
157 return 's';
158 } else if ( width <= 500 ) {
159 if ( rObj.srcSet['m'] )
160 return 'm';
161 } else {
162 if ( rObj.srcSet['o'] )
163 return 'o';
164 }
165 // original was missing return medium or small
166 if ( rObj.srcSet['m'] )
167 return 'm';
168 if ( rObj.srcSet['s'] )
169 return 's';
170
171 }
172 }