here it is ... the upload-api, script-server, js2 (javascript phase2) branch merge...
[lhc/web/wiklou.git] / js2 / mwEmbed / libAddMedia / searchLibs / mediaWikiSearch.js
1 var mediaWikiSearch = function( iObj ) {
2 return this.init( iObj );
3 };
4 mediaWikiSearch.prototype = {
5 init:function( iObj ){
6 //init base class and inherit:
7 var baseSearch = new baseRemoteSearch( iObj );
8 for(var i in baseSearch){
9 if(typeof this[i] =='undefined'){
10 this[i] = baseSearch[i];
11 }else{
12 this['parent_'+i] = baseSearch[i];
13 }
14 }
15 //inherit the cp settings for
16 },
17 getSearchResults:function(){
18 //call parent:
19 this.parent_getSearchResults();
20
21 var _this = this;
22 this.loading = true;
23 js_log('f:getSearchResults for:' + $j('#rsd_q').val() );
24 //do two queries against the Image / File / MVD namespace:
25
26 //build the image request object:
27 var reqObj = {
28 'action':'query',
29 'generator':'search',
30 'gsrsearch': $j('#rsd_q').val(),
31 'gsrnamespace':6, //(only search the "file" namespace (audio, video, images)
32 'gsrwhat':'title',
33 'gsrlimit': this.cp.limit,
34 'gsroffset': this.cp.offset,
35 'prop':'imageinfo|revisions|categories',
36 'iiprop':'url|mime|size',
37 'iiurlwidth': parseInt( this.rsd.thumb_width ),
38 'rvprop':'content'
39 };
40 //set up the number of request:
41 this.completed_req=0;
42 this.num_req=1;
43 //setup the number of requests result flag:
44 //also do a request for page titles (would be nice if api could query both at the same time)
45 reqObj['gsrwhat']='text';
46 do_api_req( {
47 'data':reqObj,
48 'url':this.cp.api_url
49 }, function(data){
50 js_log('mediaWikiSearch: got data response');
51 //parse the return data
52 _this.addResults( data);
53 //_this.checkRequestDone(); //only need if we do two queries one for title one for text
54 _this.loading = false;
55 });
56 },
57 addResults:function( data ){
58 js_log("f:addResults");
59 var _this = this
60 //check if we have
61 if( typeof data['query-continue'] != 'undefined'){
62 if( typeof data['query-continue'].search != 'undefined')
63 this.more_results = true;
64 }
65 //make sure we have pages to iderate:
66
67 if(data.query && data.query.pages){
68 for(var page_id in data.query.pages){
69 var page = data.query.pages[ page_id ];
70
71 //make sure the reop is shared (don't show for now it confusing things)
72 //@@todo support remote repository better
73 if( page.imagerepository == 'shared'){
74 continue;
75 }
76
77 //make sure the page is not a redirect
78 if(page.revisions[0]['*'].indexOf('#REDIRECT')===0){
79 //skip page is redirect
80 continue;
81 }
82 //skip if its an empty or missing imageinfo:
83 if( !page.imageinfo )
84 continue;
85
86 this.resultsObj[page_id]={
87 'titleKey' : page.title,
88 'link' : page.imageinfo[0].descriptionurl,
89 'title' : page.title.replace(/File:|.jpg|.png|.svg|.ogg|.ogv|.oga/ig, ''),
90 'poster' : page.imageinfo[0].thumburl,
91 'thumbwidth' : page.imageinfo[0].thumbwidth,
92 'thumbheight': page.imageinfo[0].thumbheight,
93 'orgwidth' : page.imageinfo[0].width,
94 'orgheight' : page.imageinfo[0].height,
95 'mime' : page.imageinfo[0].mime,
96 'src' : page.imageinfo[0].url,
97 'desc' : page.revisions[0]['*'],
98 //add pointer to parent search obj:
99 'pSobj' :_this,
100 'meta':{
101 'categories':page.categories
102 }
103 }
104
105 //likely a audio clip if no poster and type application/ogg
106 //@@todo we should return audio/ogg for the mime type or some other way to specify its "audio"
107 if( ! this.resultsObj[page_id].poster && this.resultsObj[page_id].mime == 'application/ogg' ){
108 this.resultsObj[page_id].mime = 'audio/ogg';
109 }
110
111 this.num_results++;
112 //for(var i in this.resultsObj[page_id]){
113 // js_log('added: '+ i +' '+ this.resultsObj[page_id][i]);
114 //}
115 }
116 }else{
117 js_log('no results:' + data);
118 }
119 },
120 //check request done used for when we have multiple requests to check before formating results.
121 checkRequestDone:function(){
122 //display output if done:
123 this.completed_req++;
124 if(this.completed_req == this.num_req){
125 this.loading = 0;
126 }
127 },
128 getImageObj:function( rObj, size, callback ){
129 if( rObj.mime=='application/ogg' )
130 return callback( {'url':rObj.src, 'poster' : rObj.url } );
131
132 //we can just use direct request urls
133 //@@todo thumb.php has some issues (cant serve the full image size, has poor erro handling etc)
134 /*var baseImgUrl = this.cp.api_url.replace('api.php', 'thumb.php');
135 if ( rObj.mime=='image/jpeg' || rObj.mime=='image/png' ){
136 //if requested size is greater than org size return reduced size obj:
137 if( size.width > rObj.orgwidth){
138 callback({
139 'url' : baseImgUrl + '?f=' + rObj.titleKey.replace(/\s/g, '_') + '&w='+ rObj.orgwidth,
140 'width' : rObj.orgwidth,
141 'height': rObj.orgheight
142 });
143 return false;
144 }
145 }
146 //assuming svg or size is in range: give them requeted size
147 callback({
148 'url' : baseImgUrl + '?f=' + rObj.titleKey + '&w='+ size.width,
149 'width' : size.width,
150 'height': Math.round( ( rObj.orgheight / rObj.orgwidth)*size.width )
151 });
152 return false;
153 */
154
155 //his could be depreciated if thumb.php improves
156 var reqObj = {
157 'action':'query',
158 'format':'json',
159 'titles':rObj.titleKey,
160 'prop':'imageinfo',
161 'iiprop':'url|size|mime'
162 }
163 //set the width:
164 if(size.width)
165 reqObj['iiurlwidth']= size.width;
166 js_log('going to do req: ' + this.cp.api_url + ' ' + reqObj );
167 do_api_req( {
168 'data':reqObj,
169 'url' : this.cp.api_url
170 }, function(data){
171 var imObj = {};
172 for(var page_id in data.query.pages){
173 var iminfo = data.query.pages[ page_id ].imageinfo[0];
174 //store the orginal width:
175 imObj['org_width']=iminfo.width;
176 //check if thumb size > than image size and is jpeg or png (it will not scale well above its max res)
177 if( ( iminfo.mime=='image/jpeg' || iminfo=='image/png' ) &&
178 iminfo.thumbwidth > iminfo.width ){
179 imObj['url'] = iminfo.url;
180 imObj['width'] = iminfo.width;
181 imObj['height'] = iminfo.height;
182 }else{
183 imObj['url'] = iminfo.thumburl;
184 imObj['width'] = iminfo.thumbwidth;
185 imObj['height'] = iminfo.thumbheight;
186 }
187 }
188 js_log('getImageObj: get: ' + size.width + ' got url:' + imObj.url);
189 callback( imObj );
190 });
191 },
192 //the insert image function
193 insertImage:function( cEdit ){
194 if(!cEdit)
195 var cEdit = _this.cEdit;
196 },
197 getEmbedHTML: function( rObj , options) {
198 if(!options)
199 options = {};
200 //set up the output var with the default values:
201 var outOpt = { 'width': rObj.width, 'height': rObj.height};
202 if( options['max_height'] ){
203 outOpt.height = (options.max_height > rObj.height) ? rObj.height : options.max_height;
204 outOpt.width = (rObj.width / rObj.height) *outOpt.height;
205 }
206 var style_attr = 'style="width:' + outOpt.width + 'px;height:' + outOpt.height +'px"';
207 var id_attr = (options['id'])?' id = "' + options['id'] +'" ': '';
208 var cat = rObj;
209 //return the html type:
210 if(rObj.mime.indexOf('image')!=-1){
211 return '<img ' + id_attr + ' src="' + rObj.edit_url + '"' + style_attr + ' >';
212 }
213 var ahtml='';
214 if(rObj.mime == 'application/ogg' || rObj.mime == 'audio/ogg'){
215 ahtml = id_attr +
216 ' src="' + rObj.src + '" ' +
217 style_attr +
218 ' poster="'+ rObj.poster + '" '
219 if(rObj.mime.indexOf('application/ogg')!=-1){
220 return '<video ' + ahtml + '></video>';
221 }
222
223 if(rObj.mime.indexOf('audio/ogg')!=-1){
224 return '<audio ' + ahtml + '></audio>';
225 }
226 }
227 js_log('ERROR:unsupored mime type: ' + rObj.mime);
228 },
229 getInlineDescWiki:function( rObj ){
230 var desc = this.parent_getInlineDescWiki( rObj );
231 //just grab the description tag for inline desc:
232 var descMatch = new RegExp(/Description=(\{\{en\|)?([^|]*|)/);
233 var dparts = desc.match(descMatch);
234
235 if( dparts && dparts.length > 1){
236 desc = (dparts.length == 2) ? dparts[1] : dparts[2].replace('}}','');
237 desc = (desc.substr(0,2) == '1=') ?desc.substr(2): desc;
238 return desc;
239 }
240 //else return the title since we could not find the desc:
241 js_log('we could not find the Description tag in :' + desc );
242 return rObj.title;
243 },
244 parseWikiTemplate: function( text ){
245 //@@todo parse wiki Template return object with properties and values
246 },
247 //returns the inline wikitext for insertion (template based crops for now)
248 getEmbedWikiCode: function( rObj ){
249 //set default layout to right justified
250 var layout = ( rObj.layout)? rObj.layout:"right"
251 //if crop is null do base output:
252 if( rObj.crop == null)
253 return this.parent_getEmbedWikiCode( rObj );
254 //using the preview crop template: http://en.wikipedia.org/wiki/Template:Preview_Crop
255 //@@todo should be replaced with server side cropping
256 return '{{Preview Crop ' + "\n" +
257 '|Image = ' + rObj.target_resource_title + "\n" +
258 '|bSize = ' + rObj.width + "\n" +
259 '|cWidth = ' + rObj.crop.w + "\n" +
260 '|cHeight = ' + rObj.crop.h + "\n" +
261 '|oTop = ' + rObj.crop.y + "\n" +
262 '|oLeft = ' + rObj.crop.x + "\n" +
263 '|Location =' + layout + "\n" +
264 '|Description =' + rObj.inlineDesc + "\n" +
265 '}}';
266 }
267 }