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

Last change on this file was 38238, checked in by anupama, 7 months ago

Moving the handling of the silent gsdl_cgi errors (that result in perl die statements, but yet do not cause ajax error functions to be called, and the ajax success callback function is still called instead) to be processed at toplevel when first encountered: in javascript_global_functions.js::_callMetadataServer() and gsajaxapi.js::urlPostAsync and gsajaxapi.js::urlPostSync(). The latter two are untested, but I think (something like) this is needed there as well. Basically, if no errorcallback is registered, we see an alert message popup with the otherwise silent error message displayed. If an error callback is registered, then the silent error message is at least printed to console, and the errorcallback can handle the rest as the coder chooses.

File size: 18.8 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 if(xmlHttp.status != 200
206 || xmlHttp.responseText.indexOf("ERROR: ") !== -1
207 || xmlHttp.responseText.indexOf("<Error>") !== -1)
208 {
209 var errorMsg = "callingFunction Failed with";
210 if(xmlHttp.status == 200) {
211 errorMsg += " gsdlCGI error";
212 }
213 errorMsg += ":\n" + xmlHttp.responseText;
214
215 console.log(errorMsg);
216 }
217
218 return xmlHttp.responseText;
219 }
220
221 // New, an Ajax Asynchronous Post method.
222 // For helpful links, see the urlPostSync() method above
223 this.urlPostAsync = function(scriptURL, params, callback, errorCallback) {
224 var xmlHttp=false;
225 try {
226 // Firefox, Opera 8.0+, Safari
227 xmlHttp=new XMLHttpRequest();
228 }
229 catch (e) {
230 // Internet Explorer
231 try {
232 xmlHttp=new ActiveXObject("Msxml2.XMLHTTP");
233 }
234 catch (e) {
235 try {
236 xmlHttp=new ActiveXObject("Microsoft.XMLHTTP");
237 }
238 catch (e) {
239 alert("Your browser does not support AJAX!");
240 return false;
241 }
242 }
243 }
244
245
246
247 // e.g. scriptURL: /greenstone/cgi-bin/metadata-server.pl
248 xmlHttp.open('POST', scriptURL, true); // true means asynchronous
249 xmlHttp.setRequestHeader('Content-Type', 'application/x-www-form-urlencoded');
250
251
252 // If asynchronous:
253 // If the callback param is a function, we will set it up to get called when
254 // the async post has finished (is ready)
255 // if the callback parameter isn't a function, the param represents a field
256 // that we want to dynamically update when the async post process has finished
257
258 // NOTE: if passing in an error callback function *separate* from success callback (i.e. if current callback param won't check xmlHttp.status == 200)
259 // then see https://stackoverflow.com/questions/1442425/detect-xhr-error-is-really-due-to-browser-stop-or-click-to-new-page
260 // and https://stackoverflow.com/questions/8866761/xmlhttprequest-ajax-error
261
262 var typeof_callback = typeof(callback);
263 if ((typeof_callback == "string") || (typeof_callback == "number") || (typeof_callback == "boolean")) {
264 var locid = callback;
265
266 xmlHttp.onreadystatechange=function() {
267 if(xmlHttp.readyState==4) {
268 if (locelem != null) {
269 var locelem = document.getElementById(locid);
270
271 locelem.innerHTML = xmlHttp.responseText;
272 }
273 }
274 }
275 }
276 else if (typeof_callback == "function") {
277 xmlHttp.onreadystatechange=function() {
278 if(xmlHttp.readyState==4) {
279 // Original behaviour of this block was just this single line:
280 //callback(xmlHttp); // e.g. this might do: updatepage(xmlHttp.responseText)
281
282 var errorMsg = "callingFunction Failed with";
283
284 // if a proper ajax error is detected, then call any errorCallback
285 if(xmlHttp.status != 200 && errorCallback != null) {
286 errorMsg += ":\n" + xmlHttp.responseText;
287 alert(errorMsg);
288 console.log(errorMsgPrefix);
289
290 errorCallback(xmlHttp);
291 }
292
293 // Check for silent errors (gsdl_cgi->generate_error causes a silent die)
294 // and alert user then call any errorCallback
295 else if(xmlHttp.responseText.indexOf("ERROR: ") !== -1
296 || xmlHttp.responseText.indexOf("<Error>") !== -1)
297 {
298 errorMsg += " gsdlCGI error:\n" + xmlHttp.responseText;
299 alert(errorMsg);
300 console.log(errorMsg);
301
302 if(errorCallback != null) {
303 errorCallback(xmlHttp);
304 } else { // if silent error and no errorCallback, resort to sole callback
305 callback(xmlHttp);
306 }
307 }
308
309 else {
310 // if either success, Or error status but there's no error callback,
311 // or if no silent die on error, then call default callback
312 callback(xmlHttp); // e.g. this might do: updatepage(xmlHttp.responseText);
313 }
314 }
315 }
316 }
317 else {
318 alert("Unrecognized type of callback value: " + typeof_callback);
319 }
320
321 if(un_ != null) {
322 params += "&un=" + un_;
323 }
324 if(ky_ != null) {
325 params += "&ky=" + ky_;
326 }
327 //alert("Posting Async: " + scriptURL + "?" + params);
328
329 xmlHttp.send(params); // needs to be escaped/encoded
330 // if synchronous, would process xmlHttp AFTER send() call, such as by
331 // accessing xmlHttp.responseText to return that to the caller at this point.
332 }
333
334 // New
335 // The where parameter can be specified as one or more of: import, archives, index, live
336 // separated by |. If null, it is assumed to be index which is the original default
337 // behaviour of calling set-metadata. E.g. where=import|archives|index
338 this.setMetadata = function(docid,metaname,metapos,metavalue,metamode,where)
339 {
340 var mdserver = this.metadataserverURL();
341
342 var params = "a=set-metadata";
343 if(where != null) {
344 params += "&where=" + where; // if where not specified, meta-server will default to setting index meta
345 //} else {
346 // params += "&where=import|archives|index";
347 }
348 params += "&c="+collect_;
349 params += "&d="+docid;
350 params += "&metaname=" + metaname;
351 if (metapos!=null) {
352 params += "&metapos=" + metapos;
353 }
354 params += "&metavalue=" + metavalue;
355 if (metamode!=null) {
356 params += "&metamode=" + metamode;
357 }
358
359 //this.urlGetSync(mdserver + "?" + params);
360 this.urlPostSync(mdserver,params);
361 }
362
363 // New
364 // The where parameter can be specified as one or more of: import, archives, index, live
365 // separated by |. If null, it is assumed to be index which is the original default
366 // behaviour of calling set-metadata-array). E.g. where=import|archives|index
367 this.setMetadataArray = function(docArray,metamode,where)
368 {
369 docArrayJSON = JSON.stringify(docArray);
370
371 var mdserver = this.metadataserverURL();
372
373 var params = "a=" + escape("set-metadata-array"); //"a=set-metadata-array";
374 if(where != null) {
375 params += "&where=" + escape(where); // if where not specified, meta-server will default to setting index meta
376 //} else {
377 // params += "&where=import|archives|index";
378 }
379 params += "&c="+escape(collect_);
380 params += "&json="+escape(docArrayJSON);
381
382 if (metamode!=null) {
383 params += "&metamode=" + escape(metamode);
384 }
385
386 //this.urlGetSync(mdserver + "?" + params);
387 return this.urlPostSync(mdserver,params);
388 }
389
390 // New
391 this.getArchivesMetadata = function(docoid,metaname,metapos)
392 {
393 var mdserver = this.metadataserverURL();
394
395 var url = mdserver + "?a=get-archives-metadata";
396 url += "&c="+collect_;
397 url += "&d="+docoid;
398 url += "&metaname=" + metaname;
399 if (metapos!=null) {
400 url += "&metapos=" + metapos;
401 }
402
403 //alert("In getArchivesMeta. URL: " + url)
404 return this.urlGetSync(url); //Once this works, make it POST
405 }
406
407 this.getMetadataArray = function(docArray,where)
408 {
409 docArrayJSON = JSON.stringify(docArray);
410
411 var mdserver = this.metadataserverURL();
412
413 var params = "a=" + escape("get-metadata-array"); //"a=set-metadata-array";
414 if(where != null) {
415 params += "&where=" + escape(where); // if where not specified, meta-server will default to setting index meta
416 //} else {
417 // params += "&where=import|archives|index";
418 }
419 params += "&c="+escape(collect_);
420 params += "&json="+escape(docArrayJSON);
421
422 //this.urlGetSync(mdserver + "?" + params);
423 return this.urlPostSync(mdserver,params);
424 }
425//*******END OF ADDITIONS TO BRING GS3 VERSION OF THIS FILE UP TO SPEED WITH GS2 VERSION********//
426
427 this.setLiveMetadata = function(id,metaname,metavalue)
428 {
429 var mdserver = this.metadataserverURL();
430
431 var url = mdserver + "?a=set-live-metadata";
432 url += "&c="+collect_;
433 url += "&d="+id;
434 url += "&metaname=" + metaname;
435 url += "&metavalue=" + metavalue;
436
437 this.urlGetSync(url);
438 }
439
440 this._setMetadata = function(mode,docid,metaname,metapos,metavalue,metamode)
441 {
442 var mdserver = this.metadataserverURL();
443
444 var params = "a=set" + mode + "-metadata";
445 params += "&c="+collect_;
446 params += "&d="+docid;
447 params += "&metaname=" + metaname;
448 if (metapos!=null) {
449 params += "&metapos=" + metapos;
450 }
451 params += "&metavalue=" + metavalue;
452 if (metamode!=null) {
453 params += "&metamode=" + metamode;
454 }
455
456 this.urlGetSync(mdserver + "?" + params);
457 //this.urlPostSync(mdserver,params);
458 }
459
460
461 this._setDocumentArrayMetadata = function(mode,docArray,metamode)
462 {
463 docArrayJSON = JSON.stringify(docArray);
464
465 var mdserver = this.metadataserverURL();
466
467 var params = "a=set" + mode + "-metadata-array";
468 params += "&c="+collect_;
469 params += "&json="+docArrayJSON;
470
471 if (metamode!=null) {
472 params += "&metamode=" + metamode;
473 }
474
475 this.urlGetSync(mdserver + "?" + params);
476
477 }
478
479
480 this.setDocumentMetadata = function(docid,metaname,metapos,metavalue)
481 {
482 // Allow for three param call to function, where metapos is missed out
483 if (metavalue==null) {
484 // 4 param case
485 metavalue = metapos;
486 metapos = null;
487 }
488
489 this._setMetadata("",docid,metaname,metapos,metavalue);
490 this._setMetadata("-archives",docid,metaname,metapos,metavalue,"override");
491
492 }
493
494 this.setDocumentArrayMetadata = function(docArray,metamode)
495 {
496 //showDialog('Greenstone Javascript API','This sequence of changes has been commited into the system.','success', 2);
497
498 this._setDocumentArrayMetadata("",docArray,metamode);
499 this._setDocumentArrayMetadata("-archives",docArray,metamode);
500 }
501
502 this.setNewDocumentMetadata = function(docid,metaname,metavalue)
503 {
504 this._setMetadata("",docid,metaname,null,metavalue);
505 this._setMetadata("-archives",docid,metaname,null,metavalue,"accumulate");
506 }
507
508 this.setImportMetadata = function(docid,metaname,metapos,metavalue)
509 {
510 this._setMetadata("-import",docid,metaname,metapos,metavalue,"override");
511 }
512
513
514 this.explodeDocument = function(docid)
515 {
516 var exserver = this.explodeserverURL();
517
518 var url = exserver + "?a=explode-document";
519 url += "&c="+collect_;
520 url += "&d="+docid;
521
522 this.urlGetSync(url);
523 }
524
525 this.deleteDocument = function(docid,onlyAdd)
526 {
527 var exserver = this.explodeserverURL();
528
529 var url = exserver + "?a=delete-document";
530 url += "&c="+collect_;
531 params += "&onlyadd="+onlyAdd;
532 url += "&d="+docid;
533
534 this.urlGetSync(url);
535 }
536
537 this.deleteDocumentArray = function(docArray,onlyAdd)
538 {
539 docArrayJSON = JSON.stringify(docArray);
540
541 var exserver = this.explodeserverURL();
542
543 var params = "a=delete-document-array";
544 params += "&c="+collect_;
545 params += "&onlyadd="+onlyAdd;
546 params += "&json="+docArrayJSON;
547
548 this.urlGetSync(exserver + "?" + params);
549
550 }
551
552
553 this.cloneDocument = function(docid,toCollect)
554 {
555 var msserver = this.myspaceserverURL();
556
557 var url = msserver + "?a=clone";
558 url += "&c="+collect_;
559 url += "&d="+docid;
560 url += "&toCollect="+toCollect;
561
562 this.urlGetSync(url);
563 }
564
565 // consider name change to reindexDocument
566 this.documentReindex = function(docid)
567 {
568 var mdserver = this.metadataserverURL();
569
570 var url = mdserver + "?a=reindex-document";
571 url += "&c="+collect_;
572 url += "&d="+docid;
573
574 this.urlGetSync(url);
575 }
576
577
578 this.reindexCollection = function(mode,callback)
579 {
580 if (mode==null) {
581 mode = "incremental";
582 }
583
584 var idserver = this.indexserverURL();
585
586 var url = idserver + "?a=" + mode + "-rebuild";
587 url += "&c="+collect_;
588
589 this.urlGetAsync(url,callback);
590 }
591
592
593 this.buildByManifestGeneral = function(hashargs)
594 {
595 var idserver = this.buildserverURL();
596
597 var url = idserver + "?a=build-by-manifest";
598 url += "&c="+collect_;
599
600 if (hashargs["index-files"] != undefined) {
601 url += "&index-files=" + JSON.stringify(hashargs["index-files"]);
602 }
603
604 if (hashargs["reindex-files"] != undefined) {
605 url += "&reindex-files=" + JSON.stringify(hashargs["reindex-files"]);
606 }
607 if (hashargs["delete-OIDs"] != undefined) {
608 url += "&delete-OIDs=" + JSON.stringify(hashargs["delete-OIDs"]);
609 }
610
611 this.urlGetSync(url);
612 }
613
614 this.indexByManifest = function(docidArray)
615 {
616 var hashargs = {};
617 hashargs["index-files"] = docidArray;
618 this.buildByManifestGeneral(hashargs);
619 }
620
621 this.reindexByManifest = function(docidArray)
622 {
623 var hashargs = {};
624 hashargs["reindex-files"] = docidArray;
625 this.buildByManifestGeneral(hashargs);
626 }
627 this.deleteByManifest = function(docidArray)
628 {
629 var hashargs = {};
630 hashargs["delete-OIDs"] = docidArray;
631 this.buildByManifestGeneral(hashargs);
632 }
633
634 this.getLiveMetadata = function(id,metaname)
635 {
636 var mdserver = this.metadataserverURL();
637
638 var url = mdserver + "?a=get-live-metadata";
639 url += "&c="+collect_;
640 url += "&d="+id;
641 url += "&metaname=" + metaname;
642
643 var metavalue = this.urlGetSync(url);
644
645 return metavalue;
646 }
647
648 this.getDocumentMetadata = function(docoid,metaname,metapos)
649 {
650 var mdserver = this.metadataserverURL();
651
652 var url = mdserver + "?a=get-metadata";
653 url += "&c="+collect_;
654 url += "&d="+docoid;
655 url += "&metaname=" + metaname;
656 if (metapos!=null) {
657 url += "&metapos=" + metapos;
658 }
659
660 return this.urlGetSync(url);
661 }
662
663 this.removeLiveMetadata = function(id,metaname)
664 {
665 var mdserver = this.metadataserverURL();
666
667 var url = mdserver + "?a=remove-live-metadata";
668 url += "&c="+collect_;
669 url += "&d="+id;
670 url += "&metaname=" + metaname;
671
672 this.urlGetSync(url);
673 }
674
675 this.removeDocumentMetadata = function(docid,metaname,metapos)
676 {
677 var mdserver = this.metadataserverURL();
678
679 var url = mdserver + "?a=remove-metadata";
680 url += "&c="+collect_;
681 url += "&d="+docid;
682 url += "&metaname=" + metaname;
683 if (metapos!=null) {
684 url += "&metapos=" + metapos;
685 }
686
687 this.urlGetSync(url);
688 }
689
690 return true;
691
692}
Note: See TracBrowser for help on using the repository browser.