1 var mediaWikiSearch = function( iObj
) {
2 return this.init( iObj
);
4 mediaWikiSearch
.prototype = {
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
];
12 this['parent_'+i
] = baseSearch
[i
];
15 //inherit the cp settings for
17 getSearchResults:function(){
19 this.parent_getSearchResults();
23 js_log('f:getSearchResults for:' + $j('#rsd_q').val() );
24 //do two queries against the Image / File / MVD namespace:
26 //build the image request object:
30 'gsrsearch': $j('#rsd_q').val(),
31 'gsrnamespace':6, //(only search the "file" namespace (audio, video, images)
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
),
40 //set up the number of request:
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';
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;
57 addResults:function( data
){
58 js_log("f:addResults");
61 if( typeof data
['query-continue'] != 'undefined'){
62 if( typeof data
['query-continue'].search
!= 'undefined')
63 this.more_results
= true;
65 //make sure we have pages to iderate:
67 if(data
.query
&& data
.query
.pages
){
68 for(var page_id
in data
.query
.pages
){
69 var page
= data
.query
.pages
[ page_id
];
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'){
77 //make sure the page is not a redirect
78 if(page
.revisions
[0]['*'].indexOf('#REDIRECT')===0){
79 //skip page is redirect
82 //skip if its an empty or missing imageinfo:
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:
101 'categories':page
.categories
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';
112 //for(var i in this.resultsObj[page_id]){
113 // js_log('added: '+ i +' '+ this.resultsObj[page_id][i]);
117 js_log('no results:' + data
);
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
){
128 getImageObj:function( rObj
, size
, callback
){
129 if( rObj
.mime
=='application/ogg' )
130 return callback( {'url':rObj
.src
, 'poster' : rObj
.url
} );
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){
139 'url' : baseImgUrl + '?f=' + rObj.titleKey.replace(/\s/g, '_') + '&w='+ rObj.orgwidth,
140 'width' : rObj.orgwidth,
141 'height': rObj.orgheight
146 //assuming svg or size is in range: give them requeted size
148 'url' : baseImgUrl + '?f=' + rObj.titleKey + '&w='+ size.width,
149 'width' : size.width,
150 'height': Math.round( ( rObj.orgheight / rObj.orgwidth)*size.width )
155 //his could be depreciated if thumb.php improves
159 'titles':rObj
.titleKey
,
161 'iiprop':'url|size|mime'
165 reqObj
['iiurlwidth']= size
.width
;
166 js_log('going to do req: ' + this.cp
.api_url
+ ' ' + reqObj
);
169 'url' : this.cp
.api_url
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
;
183 imObj
['url'] = iminfo
.thumburl
;
184 imObj
['width'] = iminfo
.thumbwidth
;
185 imObj
['height'] = iminfo
.thumbheight
;
188 js_log('getImageObj: get: ' + size
.width
+ ' got url:' + imObj
.url
);
192 //the insert image function
193 insertImage:function( cEdit
){
195 var cEdit
= _this
.cEdit
;
197 getEmbedHTML: function( rObj
, 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
;
206 var style_attr
= 'style="width:' + outOpt
.width
+ 'px;height:' + outOpt
.height
+'px"';
207 var id_attr
= (options
['id'])?' id = "' + options
['id'] +'" ': '';
209 //return the html type:
210 if(rObj
.mime
.indexOf('image')!=-1){
211 return '<img ' + id_attr
+ ' src="' + rObj
.edit_url
+ '"' + style_attr
+ ' >';
214 if(rObj
.mime
== 'application/ogg' || rObj
.mime
== 'audio/ogg'){
216 ' src="' + rObj
.src
+ '" ' +
218 ' poster="'+ rObj
.poster
+ '" '
219 if(rObj
.mime
.indexOf('application/ogg')!=-1){
220 return '<video ' + ahtml
+ '></video>';
223 if(rObj
.mime
.indexOf('audio/ogg')!=-1){
224 return '<audio ' + ahtml
+ '></audio>';
227 js_log('ERROR:unsupored mime type: ' + rObj
.mime
);
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
);
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
;
240 //else return the title since we could not find the desc:
241 js_log('we could not find the Description tag in :' + desc
);
244 parseWikiTemplate: function( text
){
245 //@@todo parse wiki Template return object with properties and values
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" +