source: main/trunk/greenstone3/web/interfaces/default/js/gsajaxapi.js@ 32127

Last change on this file since 32127 was 31542, checked in by ak19, 7 years ago
  1. Rewriting callMetadataServer to do the ajax requests as POST operations rather than the default GET. This rewrite presently uses gsajaxapi.js functions now, particularly ones recently ported from the GS2 version of the file. This was the quickest way, with a minimum number of changes, to get the existing code to POST the requests. In a subsequent commit will do a jQuery ajax call to POST, which requires more code changes. 2. The gsajaxapi.js file's own modifications in this commit are merely indentation based. 3. usercomments.xsl modified to additionally import gsajaxapi.js script.
File size: 16.9 KB
Line 
1
2function GSAjaxAPI(gwcgi,collect,un,ky)
3{
4 var gwcgi_ = gwcgi;
5 var collect_ = collect;
6 var un_ = un;
7 var ky_ = ky;
8
9
10 this.fullDomainURL = function(localURL)
11 {
12 return window.location.protocol+'//'+window.location.host+localURL;
13 }
14
15 this.apiURL = function(apiProg)
16 {
17 //get the location of the cgi program
18 splitpos = gwcgi_.lastIndexOf("/");
19
20 var mdserver;
21 if (splitpos >= 0) {
22 mdserver = gwcgi.substring(0,(splitpos+1)) + apiProg;
23 }
24 else {
25 mdserver = apiProg;
26 }
27
28 return mdserver;
29 }
30
31 this.metadataserverURL = function()
32 {
33 return this.apiURL("metadata-server.pl");
34 }
35
36 this.indexserverURL = function()
37 {
38 return this.apiURL("index-server.pl");
39 }
40
41 this.buildserverURL = function()
42 {
43 return this.apiURL("build-server.pl");
44 }
45
46 this.explodeserverURL = function()
47 {
48 return this.apiURL("explode-server.pl");
49 }
50
51 this.myspaceserverURL = function()
52 {
53 return this.apiURL("myspace-server.pl");
54 }
55
56
57 this.urlGetAsync = function(url,callback)
58 {
59 var xmlHttp;
60 try {
61 // Firefox, Opera 8.0+, Safari
62 xmlHttp=new XMLHttpRequest();
63 }
64 catch (e) {
65 // Internet Explorer
66 try {
67 xmlHttp=new ActiveXObject("Msxml2.XMLHTTP");
68 }
69 catch (e) {
70 try {
71 xmlHttp=new ActiveXObject("Microsoft.XMLHTTP");
72 }
73 catch (e) {
74 alert("Your browser does not support AJAX!");
75 return false;
76 }
77 }
78 }
79
80 var typeof_callback = typeof(callback);
81 if ((typeof_callback == "string") || (typeof_callback == "number") || (typeof_callback == "boolean")) {
82 var locid = callback;
83
84 xmlHttp.onreadystatechange=function() {
85 if(xmlHttp.readyState==4) {
86 if (locelem != null) {
87 var locelem = document.getElementById(locid);
88
89 locelem.innerHTML = xmlHttp.responseText;
90 }
91 }
92 }
93 }
94 else if (typeof_callback == "function") {
95 xmlHttp.onreadystatechange=function() {
96 if(xmlHttp.readyState==4) {
97 callback(xmlHttp);
98 }
99 }
100 }
101 else {
102 alert("Unrecognized type of callback value: " + typeof_callback);
103 }
104
105 if(un_ != null) {
106 url += "&un=" + un_;
107 }
108 if(ky_ != null) {
109 url += "&ky=" + ky_;
110 }
111
112 xmlHttp.open("GET",url,true);
113 xmlHttp.send(null);
114 }
115
116
117 this.urlGetSync = function(url)
118 {
119 // alert("url = " + url);
120
121 var xmlHttp;
122 try {
123 // Firefox, Opera 8.0+, Safari
124 xmlHttp=new XMLHttpRequest();
125 }
126 catch (e) {
127 // Internet Explorer
128 try {
129 xmlHttp=new ActiveXObject("Msxml2.XMLHTTP");
130 }
131 catch (e) {
132 try {
133 xmlHttp=new ActiveXObject("Microsoft.XMLHTTP");
134 }
135 catch (e) {
136 alert("Your browser does not support AJAX!");
137 return false;
138 }
139 }
140 }
141
142 if(un_ != null) {
143 url += "&un=" + un_;
144 }
145 if(ky_ != null) {
146 url += "&ky=" + ky_;
147 }
148
149 xmlHttp.open("GET",url,false);
150 xmlHttp.send(null);
151
152 // alert("response = '" + xmlHttp.responseText + "'");
153
154 return xmlHttp.responseText;
155 }
156
157//*********ADDITIONS TO BRING GS3 VERSION OF THIS FILE UP TO SPEED WITH GS2 VERSION********************//
158//*********BUT NOT USED BY GS3. SEE GS3's javascript-global-functions.js INSTEAD (UPCOMING CHANGES)
159//*********FOR THE PORTED VERSIONS OF THOSE FUNCTIONS AMONG THESE ADDITIONS THAT ARE NECESSARY FOR GS3.
160
161// New, an Ajax Synchronous Post method.
162// http://www.degraeve.com/reference/simple-ajax-example.php
163// Async vs Sync: http://www.w3schools.com/ajax/ajax_xmlhttprequest_send.asp
164// Also:
165// http://stackoverflow.com/questions/6312447/in-an-ajax-post-do-i-need-to-urlencode-parameters-before-sending
166// http://api.jquery.com/jQuery.post/
167// http://www.w3schools.com/ajax/ajax_xmlhttprequest_send.asp
168 this.urlPostSync = function(scriptURL, params) {
169 var xmlHttp=false;
170 try {
171 // Firefox, Opera 8.0+, Safari
172 xmlHttp=new XMLHttpRequest();
173 }
174 catch (e) {
175 // Internet Explorer
176 try {
177 xmlHttp=new ActiveXObject("Msxml2.XMLHTTP");
178 }
179 catch (e) {
180 try {
181 xmlHttp=new ActiveXObject("Microsoft.XMLHTTP");
182 }
183 catch (e) {
184 alert("Your browser does not support AJAX!");
185 return false;
186 }
187 }
188 }
189
190 // e.g. scriptURL: /greenstone/cgi-bin/metadata-server.pl
191 xmlHttp.open('POST', scriptURL, false); // false means synchronous
192 xmlHttp.setRequestHeader('Content-Type', 'application/x-www-form-urlencoded');
193
194 if(un_ != null) {
195 params += "&un=" + un_;
196 }
197 if(ky_ != null) {
198 params += "&ky=" + ky_;
199 }
200
201 xmlHttp.send(params); // needs to be escaped/encoded
202
203 //alert(scriptURL + "?" + params);
204 //alert(xmlHttp.responseText); // if synchronous, process xmlHttp.responseText AFTER send() call
205 return xmlHttp.responseText;
206 }
207
208 // New, an Ajax Asynchronous Post method.
209 // For helpful links, see the urlPostSync() method above
210 this.urlPostAsync = function(scriptURL, params, callback) {
211 var xmlHttp=false;
212 try {
213 // Firefox, Opera 8.0+, Safari
214 xmlHttp=new XMLHttpRequest();
215 }
216 catch (e) {
217 // Internet Explorer
218 try {
219 xmlHttp=new ActiveXObject("Msxml2.XMLHTTP");
220 }
221 catch (e) {
222 try {
223 xmlHttp=new ActiveXObject("Microsoft.XMLHTTP");
224 }
225 catch (e) {
226 alert("Your browser does not support AJAX!");
227 return false;
228 }
229 }
230 }
231
232
233
234 // e.g. scriptURL: /greenstone/cgi-bin/metadata-server.pl
235 xmlHttp.open('POST', scriptURL, true); // true means asynchronous
236 xmlHttp.setRequestHeader('Content-Type', 'application/x-www-form-urlencoded');
237
238
239 // If asynchronous:
240 // If the callback param is a function, we will set it up to get called when
241 // the async post has finished (is ready)
242 // if the callback parameter isn't a function, the param represents a field
243 // that we want to dynamically update when the async post process has finished
244
245 var typeof_callback = typeof(callback);
246 if ((typeof_callback == "string") || (typeof_callback == "number") || (typeof_callback == "boolean")) {
247 var locid = callback;
248
249 xmlHttp.onreadystatechange=function() {
250 if(xmlHttp.readyState==4) {
251 if (locelem != null) {
252 var locelem = document.getElementById(locid);
253
254 locelem.innerHTML = xmlHttp.responseText;
255 }
256 }
257 }
258 }
259 else if (typeof_callback == "function") {
260 xmlHttp.onreadystatechange=function() {
261 if(xmlHttp.readyState==4) {
262 callback(xmlHttp); // e.g. this might do: updatepage(xmlHttp.responseText);
263 }
264 }
265 }
266 else {
267 alert("Unrecognized type of callback value: " + typeof_callback);
268 }
269
270 if(un_ != null) {
271 params += "&un=" + un_;
272 }
273 if(ky_ != null) {
274 params += "&ky=" + ky_;
275 }
276 //alert("Posting Async: " + scriptURL + "?" + params);
277
278 xmlHttp.send(params); // needs to be escaped/encoded
279 // if synchronous, would process xmlHttp AFTER send() call, such as by
280 // accessing xmlHttp.responseText to return that to the caller at this point.
281 }
282
283 // New
284 // The where parameter can be specified as one or more of: import, archives, index, live
285 // separated by |. If null, it is assumed to be index which is the original default
286 // behaviour of calling set-metadata. E.g. where=import|archives|index
287 this.setMetadata = function(docid,metaname,metapos,metavalue,metamode,where)
288 {
289 var mdserver = this.metadataserverURL();
290
291 var params = "a=set-metadata";
292 if(where != null) {
293 params += "&where=" + where; // if where not specified, meta-server will default to setting index meta
294 //} else {
295 // params += "&where=import|archives|index";
296 }
297 params += "&c="+collect_;
298 params += "&d="+docid;
299 params += "&metaname=" + metaname;
300 if (metapos!=null) {
301 params += "&metapos=" + metapos;
302 }
303 params += "&metavalue=" + metavalue;
304 if (metamode!=null) {
305 params += "&metamode=" + metamode;
306 }
307
308 //this.urlGetSync(mdserver + "?" + params);
309 this.urlPostSync(mdserver,params);
310 }
311
312 // New
313 // The where parameter can be specified as one or more of: import, archives, index, live
314 // separated by |. If null, it is assumed to be index which is the original default
315 // behaviour of calling set-metadata-array). E.g. where=import|archives|index
316 this.setMetadataArray = function(docArray,metamode,where)
317 {
318 docArrayJSON = JSON.stringify(docArray);
319
320 var mdserver = this.metadataserverURL();
321
322 var params = "a=" + escape("set-metadata-array"); //"a=set-metadata-array";
323 if(where != null) {
324 params += "&where=" + escape(where); // if where not specified, meta-server will default to setting index meta
325 //} else {
326 // params += "&where=import|archives|index";
327 }
328 params += "&c="+escape(collect_);
329 params += "&json="+escape(docArrayJSON);
330
331 if (metamode!=null) {
332 params += "&metamode=" + escape(metamode);
333 }
334
335 //this.urlGetSync(mdserver + "?" + params);
336 return this.urlPostSync(mdserver,params);
337 }
338
339 // New
340 this.getArchivesMetadata = function(docoid,metaname,metapos)
341 {
342 var mdserver = this.metadataserverURL();
343
344 var url = mdserver + "?a=get-archives-metadata";
345 url += "&c="+collect_;
346 url += "&d="+docoid;
347 url += "&metaname=" + metaname;
348 if (metapos!=null) {
349 url += "&metapos=" + metapos;
350 }
351
352 //alert("In getArchivesMeta. URL: " + url)
353 return this.urlGetSync(url); //Once this works, make it POST
354 }
355
356 this.getMetadataArray = function(docArray,where)
357 {
358 docArrayJSON = JSON.stringify(docArray);
359
360 var mdserver = this.metadataserverURL();
361
362 var params = "a=" + escape("get-metadata-array"); //"a=set-metadata-array";
363 if(where != null) {
364 params += "&where=" + escape(where); // if where not specified, meta-server will default to setting index meta
365 //} else {
366 // params += "&where=import|archives|index";
367 }
368 params += "&c="+escape(collect_);
369 params += "&json="+escape(docArrayJSON);
370
371 //this.urlGetSync(mdserver + "?" + params);
372 return this.urlPostSync(mdserver,params);
373 }
374//*******END OF ADDITIONS TO BRING GS3 VERSION OF THIS FILE UP TO SPEED WITH GS2 VERSION********//
375
376 this.setLiveMetadata = function(id,metaname,metavalue)
377 {
378 var mdserver = this.metadataserverURL();
379
380 var url = mdserver + "?a=set-live-metadata";
381 url += "&c="+collect_;
382 url += "&d="+id;
383 url += "&metaname=" + metaname;
384 url += "&metavalue=" + metavalue;
385
386 this.urlGetSync(url);
387 }
388
389 this._setMetadata = function(mode,docid,metaname,metapos,metavalue,metamode)
390 {
391 var mdserver = this.metadataserverURL();
392
393 var params = "a=set" + mode + "-metadata";
394 params += "&c="+collect_;
395 params += "&d="+docid;
396 params += "&metaname=" + metaname;
397 if (metapos!=null) {
398 params += "&metapos=" + metapos;
399 }
400 params += "&metavalue=" + metavalue;
401 if (metamode!=null) {
402 params += "&metamode=" + metamode;
403 }
404
405 this.urlGetSync(mdserver + "?" + params);
406 //this.urlPostSync(mdserver,params);
407 }
408
409
410 this._setDocumentArrayMetadata = function(mode,docArray,metamode)
411 {
412 docArrayJSON = JSON.stringify(docArray);
413
414 var mdserver = this.metadataserverURL();
415
416 var params = "a=set" + mode + "-metadata-array";
417 params += "&c="+collect_;
418 params += "&json="+docArrayJSON;
419
420 if (metamode!=null) {
421 params += "&metamode=" + metamode;
422 }
423
424 this.urlGetSync(mdserver + "?" + params);
425
426 }
427
428
429 this.setDocumentMetadata = function(docid,metaname,metapos,metavalue)
430 {
431 // Allow for three param call to function, where metapos is missed out
432 if (metavalue==null) {
433 // 4 param case
434 metavalue = metapos;
435 metapos = null;
436 }
437
438 this._setMetadata("",docid,metaname,metapos,metavalue);
439 this._setMetadata("-archives",docid,metaname,metapos,metavalue,"override");
440
441 }
442
443 this.setDocumentArrayMetadata = function(docArray,metamode)
444 {
445 //showDialog('Greenstone Javascript API','This sequence of changes has been commited into the system.','success', 2);
446
447 this._setDocumentArrayMetadata("",docArray,metamode);
448 this._setDocumentArrayMetadata("-archives",docArray,metamode);
449 }
450
451 this.setNewDocumentMetadata = function(docid,metaname,metavalue)
452 {
453 this._setMetadata("",docid,metaname,null,metavalue);
454 this._setMetadata("-archives",docid,metaname,null,metavalue,"accumulate");
455 }
456
457 this.setImportMetadata = function(docid,metaname,metapos,metavalue)
458 {
459 this._setMetadata("-import",docid,metaname,metapos,metavalue,"override");
460 }
461
462
463 this.explodeDocument = function(docid)
464 {
465 var exserver = this.explodeserverURL();
466
467 var url = exserver + "?a=explode-document";
468 url += "&c="+collect_;
469 url += "&d="+docid;
470
471 this.urlGetSync(url);
472 }
473
474 this.deleteDocument = function(docid,onlyAdd)
475 {
476 var exserver = this.explodeserverURL();
477
478 var url = exserver + "?a=delete-document";
479 url += "&c="+collect_;
480 params += "&onlyadd="+onlyAdd;
481 url += "&d="+docid;
482
483 this.urlGetSync(url);
484 }
485
486 this.deleteDocumentArray = function(docArray,onlyAdd)
487 {
488 docArrayJSON = JSON.stringify(docArray);
489
490 var exserver = this.explodeserverURL();
491
492 var params = "a=delete-document-array";
493 params += "&c="+collect_;
494 params += "&onlyadd="+onlyAdd;
495 params += "&json="+docArrayJSON;
496
497 this.urlGetSync(exserver + "?" + params);
498
499 }
500
501
502 this.cloneDocument = function(docid,toCollect)
503 {
504 var msserver = this.myspaceserverURL();
505
506 var url = msserver + "?a=clone";
507 url += "&c="+collect_;
508 url += "&d="+docid;
509 url += "&toCollect="+toCollect;
510
511 this.urlGetSync(url);
512 }
513
514 // consider name change to reindexDocument
515 this.documentReindex = function(docid)
516 {
517 var mdserver = this.metadataserverURL();
518
519 var url = mdserver + "?a=reindex-document";
520 url += "&c="+collect_;
521 url += "&d="+docid;
522
523 this.urlGetSync(url);
524 }
525
526
527 this.reindexCollection = function(mode,callback)
528 {
529 if (mode==null) {
530 mode = "incremental";
531 }
532
533 var idserver = this.indexserverURL();
534
535 var url = idserver + "?a=" + mode + "-rebuild";
536 url += "&c="+collect_;
537
538 this.urlGetAsync(url,callback);
539 }
540
541
542 this.buildByManifestGeneral = function(hashargs)
543 {
544 var idserver = this.buildserverURL();
545
546 var url = idserver + "?a=build-by-manifest";
547 url += "&c="+collect_;
548
549 if (hashargs["index-files"] != undefined) {
550 url += "&index-files=" + JSON.stringify(hashargs["index-files"]);
551 }
552
553 if (hashargs["reindex-files"] != undefined) {
554 url += "&reindex-files=" + JSON.stringify(hashargs["reindex-files"]);
555 }
556 if (hashargs["delete-OIDs"] != undefined) {
557 url += "&delete-OIDs=" + JSON.stringify(hashargs["delete-OIDs"]);
558 }
559
560 this.urlGetSync(url);
561 }
562
563 this.indexByManifest = function(docidArray)
564 {
565 var hashargs = {};
566 hashargs["index-files"] = docidArray;
567 this.buildByManifestGeneral(hashargs);
568 }
569
570 this.reindexByManifest = function(docidArray)
571 {
572 var hashargs = {};
573 hashargs["reindex-files"] = docidArray;
574 this.buildByManifestGeneral(hashargs);
575 }
576 this.deleteByManifest = function(docidArray)
577 {
578 var hashargs = {};
579 hashargs["delete-OIDs"] = docidArray;
580 this.buildByManifestGeneral(hashargs);
581 }
582
583 this.getLiveMetadata = function(id,metaname)
584 {
585 var mdserver = this.metadataserverURL();
586
587 var url = mdserver + "?a=get-live-metadata";
588 url += "&c="+collect_;
589 url += "&d="+id;
590 url += "&metaname=" + metaname;
591
592 var metavalue = this.urlGetSync(url);
593
594 return metavalue;
595 }
596
597 this.getDocumentMetadata = function(docoid,metaname,metapos)
598 {
599 var mdserver = this.metadataserverURL();
600
601 var url = mdserver + "?a=get-metadata";
602 url += "&c="+collect_;
603 url += "&d="+docoid;
604 url += "&metaname=" + metaname;
605 if (metapos!=null) {
606 url += "&metapos=" + metapos;
607 }
608
609 return this.urlGetSync(url);
610 }
611
612 this.removeLiveMetadata = function(id,metaname)
613 {
614 var mdserver = this.metadataserverURL();
615
616 var url = mdserver + "?a=remove-live-metadata";
617 url += "&c="+collect_;
618 url += "&d="+id;
619 url += "&metaname=" + metaname;
620
621 this.urlGetSync(url);
622 }
623
624 this.removeDocumentMetadata = function(docid,metaname,metapos)
625 {
626 var mdserver = this.metadataserverURL();
627
628 var url = mdserver + "?a=remove-metadata";
629 url += "&c="+collect_;
630 url += "&d="+docid;
631 url += "&metaname=" + metaname;
632 if (metapos!=null) {
633 url += "&metapos=" + metapos;
634 }
635
636 this.urlGetSync(url);
637 }
638
639 return true;
640
641}
Note: See TracBrowser for help on using the repository browser.