Bug 20489 Configure illegal file characters https://bugzilla.wikimedia.org/show_bug...
[mediawiki.git] / js2 / mwEmbed / libAddMedia / searchLibs / baseRemoteSearch.js
blob687649f26b32e4dac9a89491554adb02312a87b9
1 //base remote search obj
3 loadGM({
4         "mwe-imported_from" : "$1 imported from [$2 $3]. See the original [$4 resource page] for more information."
5 })
6 // @key is name of rObj variable
7 // @value is where to find the value in the item xml
8 //
9 // *format:*
10 // . indicates multiple tags @ seperates the tag from attribute list
11 // {.}tag_name@{attribute1|attribute12}
12 // @@todo should probably switch this over to something like Xpath if we end up parsing a lot of rss formats
13 var rsd_default_rss_item_mapping = {
14         'poster'        : 'media:thumbnail@url',
15         'roe_url'       : 'media:roe_embed@url',
16         'person'        : 'media:person@label|url',
17         'parent_clip':'media:parent_clip@url',
18         'bill'          : 'media:bill@label|url',
19         'title'         : 'title',
20         'link'          : 'link',
21         'desc'          : 'description',
22         //multiple items
23         'category'  : '.media:category@label|url'
26 var baseRemoteSearch = function(iObj) {
27         return this.init(iObj);
29 baseRemoteSearch.prototype = {
31         completed_req:0,
32         num_req:0,
34         resultsObj:{},
36         //default search result values for paging:
37         offset                   :0,
38         limit                     :20,
39         more_results    :false,
40         num_results             :0,
42         //init the object:
43         init: function( iObj ){
44                 js_log('mvBaseRemoteSearch:init');
45                 for(var i in iObj){
46                         this[i] = iObj[i];
47                 }
48                 return this;
49         },
50         getSearchResults:function(){
51                 //empty out the current results before issuing a request
52                 this.resultsObj = {};
53                 //do global getSearchResults bindings
54                 this.last_query = $j('#rsd_q').val();
55                 this.last_offset = this.cp.offset;
56                 //@@todo its possible that video rss is the "default" format we could put that logic here:
57         },
58         /*
59         * Parses and adds video rss based input format
60         * @param $data XML data to parse
61         * @param provider_url    the source url (used to generate absolute links)
62         */
63         addRSSData:function( data , provider_url ){
64                 js_log('f:addRSSData');
65                 var _this = this;
66                 var http_host = '';
67                 var http_path = '';
68                 if(provider_url){
69                         pUrl =  parseUri( provider_url );
70                         http_host = pUrl.protocol +'://'+ pUrl.authority;
71                         http_path = pUrl.directory;
72                 }
73                 var items = data.getElementsByTagName('item');
74                 //js_log('found ' + items.length );
75                 $j.each(items, function(inx, item){
76                         var rObj = {};
77                         for(var i in rsd_default_rss_item_mapping){
78                                 var selector = rsd_default_rss_item_mapping[i].split('@');
80                                 var flag_multiple = (  selector[0].substr(0,1) == '.' ) ? true : false;
81                                 if( flag_multiple ){
82                                         rObj[i] = new Array();
83                                         var tag_name = selector[0].substr(1);
84                                 }else{
85                                         var tag_name = selector[0];
86                                 }
88                                 var attr_name = null;
89                                 if( typeof selector[1] != 'undefined'){
90                                         attr_name = selector[1];
91                                         if( attr_name.indexOf('|') != -1 )
92                                                 attr_name = attr_name.split('|');
93                                 }
95                                 $j.each( item.getElementsByTagName( tag_name ), function ( inx, node ){
96                                         var tag_val = '';
97                                         if( node!=null && attr_name == null ){
98                                                 if( node.childNodes[0] != null){
99                                                         //trim and strip html:
100                                                         tag_val = $j.trim( node.firstChild.nodeValue ).replace(/(<([^>]+)>)/ig,"");
101                                                 }
102                                         }
103                                         if( node!=null && attr_name != null){
104                                                 if( typeof attr_name == 'string' ){
105                                                         tag_val = $j.trim( $j( node ).attr( attr_name ) );
106                                                 }else{
107                                                         var attr_vals = {};
108                                                         for( var j in attr_name ){
109                                                                 if( $j(node).attr( attr_name[j] ).length != 0)
110                                                                         attr_vals[ attr_name[j] ] = $j.trim( $j(node).attr( attr_name[j]) ).replace(/(<([^>]+)>)/ig,"");
111                                                         }
112                                                         tag_val = attr_vals ;
113                                                 }
114                                         }
115                                         if(flag_multiple){
116                                                 rObj[i].push( tag_val)
117                                         }else{
118                                                 rObj[i] = tag_val;
119                                         }
120                                 });
123                         } // done with property loop
126                         //make relative urls absolute:
127                         var url_param = new Array('src', 'poster');
128                         for(var j=0; j < url_param.length; j++){
129                                 var p = url_param[j];
130                                 if(typeof rObj[p] != 'undefined'){
131                                         if( rObj[p].substr(0,1)=='/' ){
132                                                 rObj[p] = http_host + rObj[p];
133                                         }
134                                         if( parseUri( rObj[i] ).host ==  rObj[p]){
135                                                 rObj[p] = http_host + http_path + rObj[p];
136                                         }
137                                 }
138                         }
139                         //force a mime type for now.. in the future generalize for other RSS feeds and conversions
140                         rObj['mime'] = 'video/ogg';
141                         //add pointer to parent search obj:( this.cp.limit )? this.cp.limit : this.limit,
143                         rObj['pSobj'] = _this;
144                         //add the result to the result set:
145                         _this.resultsObj[ inx ] = rObj;
146                         _this.num_results++;
147                 });
148         },
149         //by default just return the existing image with callback
150         getImageObj:function( rObj, size, callback){
151                 callback( {'url':rObj.poster} );
152         },
153         //by default just return the rObj.desc
154         getInlineDescWiki:function( rObj ){
155                 //return striped html  & trim white space
156                 if(rObj.desc)
157                         return $j.trim( rObj.desc.replace(/(<([^>]+)>)/ig,"") );
158                 //no desc avaliable:
159                 return '';
160         },
161         //default license permission wiki text is cc based template mapping (does not confirm the templates actually exist)
162         getPermissionWikiTag: function( rObj ){
163                 if(!rObj.license)
164                         return '';//no license info
165                 //check that its a defined creative commons licnese key:
166                 if(  this.rsd.licenses.cc.licenses[ rObj.license.key ] != 'undefined' ){
167                         return '{{Cc-' + rObj.license.key + '}}';
168                 }else if( rObj.license.lurl ) {
169                         return '{{Template:External_License|' + rObj.license.lurl + '}}';
170                 }
172         },
173         getImportResourceDescWiki:function(rObj){
174                 return gM('mwe-imported_from', [rObj.title,  this.cp.homepage, this.cp.title, rObj.link]);
175         },
176         //for thigns like categories and the like
177         getExtraResourceDescWiki:function( rObj ){
178                 return '';
179         },
180         //by default just return the poster (clients can overide)
181         getImageTransform:function(rObj, opt){
182                 return rObj.poster;
183         },
184         getEmbedObjParsedInfo:function(rObj, eb_id){
185                 return rObj;
186         },
187         getEmbedTimeMeta:function(rObj, callback){
188                 callback();
189         },
190         getEmbedWikiCode:function(rObj){
191                 var layout = ( rObj.layout)? rObj.layout:"right"
192                 var o= '[[' + this.rsd.cFileNS + ':' + rObj.target_resource_title + '|thumb|'+layout;
194                 if(!rObj.target_width && rObj.width){
195                         rObj.target_width = (rObj.width < 640)? rObj.width: '640';
196                 }
198                 if(rObj.target_width)
199                         o+='|' + rObj.target_width + 'px';
201                 if( rObj.inlineDesc )
202                         o+='|' + rObj.inlineDesc;
204                 o+=']]';
205                 return o;
206         },
207         updateTargetResourceTitle:function(rObj){
208                 rObj.target_resource_title = rObj.titleKey.replace(/File:|Image:/,'');
209                 rObj.target_resource_title = this.cp.resource_prefix + rObj.target_resource_title;
210         },
211         updateDataForImport:function( rObj ){
212                 return rObj;
213         }