removed output-disable in dbms-output fetching procedure
[mediawiki.git] / js2 / mwEmbed / libAddMedia / searchLibs / metavidSearch.js
blob135ca5512cb4d1d2f26fa51c01ee4b1a47a33f4c
1 /*
2 * API modes (implementations should call these objects which inherit the mvBaseRemoteSearch
3 */
4 loadGM( {
5         "mwe-stream_title" : "$1 $2 to $3"
6 } );
7 var metavidSearch = function( iObj ) {
8         return this.init( iObj );
9 };
10 metavidSearch.prototype = {
11         defaultReq: {  // set up the default request paramaters
12                 'order':'recent',
13                 'feed_format':'json_rss',
14                 'cb_inx': 1 // Not really used (we should update the metavid json retrun system) 
15         },
16         init:function( iObj ) {
17                 // init base class and inherit:
18                 var baseSearch = new baseRemoteSearch( iObj );
19                 for ( var i in baseSearch ) {
20                         if ( typeof this[i] == 'undefined' ) {
21                                 this[i] = baseSearch[i];
22                         } else {
23                                 this['parent_' + i] =  baseSearch[i];
24                         }
25                 }
26         },
27         getSearchResults:function() {
28                 // call parent:
29                 this.parent_getSearchResults();
30                 // set local ref:
31                 var _this = this;
32                 js_log( 'metavidSearch::getSearchResults()' );
33                 // Proccess all options
34                 var url = this.cp.api_url;
35                 var reqObj = $j.extend({}, this.defaultReq);
36                 reqObj[ 'f[0][t]' ] = 'match';
37                 reqObj[ 'f[0][v]' ] = $j( '#rsd_q' ).val();
38                 
39                 // add offset limit:
40                 reqObj[ 'limit' ] = this.cp.limit;
41                 reqObj[ 'offset' ] =  this.cp.offset;
43                 do_api_req({
44                         'url' : url,
45                         'jsonCB' : 'cb',                        
46                         'data' : reqObj
47                 }, function( data ) {   
48                         js_log( 'mvSearch: got data response::' );
49                         var xmldata = ( data && data['pay_load'] ) ? mw.parseXML( data['pay_load'] ) : false;
50                         if( !xmldata ){
51                                 // XML Error or No results: 
52                                 _this.resultsObj = {};
53                                 _this.loading = 0;
54                                 return ;                                
55                         }
56                                                 
57                         // Add the data xml payload with context url:
58                         _this.addRSSData( xmldata , url );
59                         
60                         // Do some metavid specific pos processing on the rObj data:
61                         for ( var i in _this.resultsObj ) {
62                                 var rObj = _this.resultsObj[i];
63                                 var proe = mw.parseUri( rObj['roe_url'] );
64                                 rObj['start_time'] = proe.queryKey['t'].split( '/' )[0];
65                                 rObj['end_time'] = proe.queryKey['t'].split( '/' )[1];
66                                 rObj['stream_name'] = proe.queryKey['stream_name'];
68                                 // All metavid content is public domain:
69                                 rObj['license'] = _this.rsd.getLicenceFromKey( 'pd' );
71                                 // Transform the title into a wiki_safe title:                          
72                                 rObj['titleKey'] =       _this.getTitleKey( rObj );
74                                 // Default width of metavid clips:
75                                 rObj['target_width'] = 400;
76                                                                 
77                                 rObj['author'] = 'US Government';
78                                 
79                                 // Add in the date as UTC "toDateString" : 
80                                 var d = _this.getDateFromLink( rObj.link );
81                                 rObj['date'] =   d.toDateString();
82                                 
83                                 // Set the license_template_tag ( all metavid content is PD-USGov )
84                                 rObj['license_template_tag'] = 'PD-USGov';
85                         }
86                         // done loading:
87                         _this.loading = 0;
88                 } );
89         },
90         /** 
91         * Get a Title key for the assset name inside the mediaWiki system
92         */
93         getTitleKey:function( rObj ) {
94                 return rObj['stream_name'] + '_part_' + rObj['start_time'].replace(/:/g, '.' ) + '_to_' + rObj['end_time'].replace(/:/g, '.' ) + '.ogv';
95         },
96         getTitle:function( rObj ) {
97                 var sn = rObj['stream_name'].replace( /_/g, ' ' );
98                 sn = sn.charAt( 0 ).toUpperCase() + sn.substr( 1 );
99                 return gM( 'mwe-stream_title', [ sn, rObj.start_time, rObj.end_time ] );
100         },
101         getExtraResourceDescWiki:function( rObj ) {
102                 var o = "\n";
103                 // check for person
104                 if (  rObj.person && rObj.person['label'] )
105                         o += '* featuring [[' + rObj.person['label'] + ']]' + "\n";
107                 if ( rObj.parent_clip )
108                         o += '* part of longer [' + rObj.parent_clip + ' video clip]' + "\n";
110                 if ( rObj.person && rObj.person['url'] && rObj.person['label'] )
111                         o += '* also see speeches by [' +  $j.trim( rObj.person.url ) + ' ' + rObj.person['label'] + ']' + "\n";
113                 // check for bill:
114                 if ( rObj.bill && rObj.bill['label'] && rObj.bill['url'] )
115                         o += '* related to bill: [[' + rObj.bill['label'] + ']] more bill [' + rObj.bill['url'] + ' video clips]' + "\n";
116                 return o;
117         },
118         // format is "quote" followed by [[name of person]]
119         getInlineDescWiki:function( rObj ) {
120                 var o = this.parent_getInlineDescWiki( rObj );
121                 // add in person if found
122                 if ( rObj.person &&  rObj.person['label'] ) {
123                         o = $j.trim(  o.replace( rObj.person['label'], '' ) );
124                         // trim leading :
125                         if ( o.substr( 0, 1 ) == ':' )
126                                 o =  o.substr( 1 );
127                         // add quotes and person at the end:
128                         var d = this.getDateFromLink( rObj.link );
129                         o = '"' + o + '" [[' + rObj.person['label'] + ']] on ' + d.toDateString();
130                 }
131                 // could do ref or direct link:
132                 o += ' \'\'[' + $j.trim( rObj.link ) + ' source clip]\'\' ';
134                 // var o= '"' + o + '" by [[' + rObj.person['label'] + ']] '+
135                 //              '<ref>[' + rObj.link + ' Metavid Source Page] for ' + rObj.title +'</ref>';
136                 return o;
137         },
138         // give an updated start and end time updates the title and url
139         applyVideoAdj: function( rObj ) {
140                 js_log( 'mv ApplyVideoAdj::' );
141                 // update the titleKey:
142                 rObj['titleKey'] =       this.getTitleKey( rObj );
144                 // update the title:
145                 rObj['title'] = this.getTitle( rObj );
147                 // update the interface:
148                 js_log( 'update title to: ' + rObj['title'] );
149                 $j( '#rsd_resource_title' ).html( gM( 'rsd_resource_edit', rObj['title'] ) );
151                 // if the video is "roe" based select the ogg stream
152                 if ( rObj.roe_url && rObj.pSobj.cp.stream_import_key ) {
153                         var source = $j( '#embed_vid' ).get( 0 ).media_element.getSourceById( rObj.pSobj.cp.stream_import_key );
154                         if ( !source ) {
155                                 js_error( 'Error::could not find source: ' +  rObj.pSobj.cp.stream_import_key );
156                         } else {
157                                 rObj['src'] = source.getURI();
158                                 js_log( "g src_key: " + rObj.pSobj.cp.stream_import_key + ' src:' + rObj['src'] ) ;
159                                 return true;
160                         }
161                 }
162         },
163         getEmbedHTML:function( rObj , options ) {
164             if ( !options )
165                      options = { };
166                 var id_attr = ( options['id'] ) ? ' id = "' + options['id'] + '" ': '';
167                 var style_attr = ( options['max_width'] ) ? ' style="width:' + options['max_width'] + 'px;"':'';
168                 // @@maybe check type here ?
169                 if ( options['only_poster'] ) {
170                         return '<img ' + id_attr + ' src="' + rObj['poster'] + '" ' + style_attr + '>';
171                 } else {
172                         return '<video ' + id_attr + ' roe="' + rObj['roe_url'] + '"></video>';
173                 }
174         },
175         getImageTransform:function( rObj, opt ) {
176                 if ( opt.width <= 80 ) {
177                         return getURLParamReplace( rObj.poster, { 'size' : "icon" } )
178                 } else if ( opt.width <= 160 ) {
179                         return getURLParamReplace( rObj.poster, { 'size' : "small" } )
180                 } else if ( opt.width <= 320 ) {
181                         return getURLParamReplace( rObj.poster, { 'size' : 'medium' } )
182                 } else if ( opt.width <= 512 ) {
183                         return getURLParamReplace( rObj.poster, { 'size' : 'large' } )
184                 } else {
185                         return getURLParamReplace( rObj.poster, { 'size' : 'full' } )
186                 }
187         },
188         addResourceInfoFromEmbedInstance : function( rObj, embed_id ) {
189                 var sources = $j( '#' + embed_id ).get( 0 ).media_element.getSources();
190                 rObj.other_versions = '*[' + rObj['roe_url'] + ' XML of all Video Formats and Timed Text]' + "\n";
191                 for ( var i in sources ) {
192                         var cur_source = sources[i];
193                         // rObj.other_versions += '*['+cur_source.getURI() +' ' + cur_source.title +']' + "\n";
194                         if ( cur_source.id ==  this.cp.target_source_id )
195                                 rObj['url'] = cur_source.getURI();
196                 }
197                 // js_log('set url to: ' + rObj['url']);
198                 return rObj;
199         },
200         getDateFromLink:function( link ) {
201                 var dateExp = new RegExp( /_([0-9]+)\-([0-9]+)\-([0-9]+)/ );
202                 var dParts = link.match ( dateExp );
203                 var d = new Date();
204                 var year_full = ( dParts[3].length == 2 ) ? '20' + dParts[3].toString():dParts[3];
205                 d.setFullYear( year_full, dParts[1] - 1, dParts[2] );
206                 return d;
207         }