updating the JS script runner, and the worm to match
authormiker <miker@dcc99617-32d9-48b4-a31d-7c20da2025e4>
Sat, 1 Jul 2006 00:38:38 +0000 (00:38 +0000)
committermiker <miker@dcc99617-32d9-48b4-a31d-7c20da2025e4>
Sat, 1 Jul 2006 00:38:38 +0000 (00:38 +0000)
git-svn-id: svn://svn.open-ils.org/ILS/trunk@4876 dcc99617-32d9-48b4-a31d-7c20da2025e4

Open-ILS/examples/json-request-test.pl [new file with mode: 0755]
Open-ILS/examples/req-test.js [new file with mode: 0644]
Open-ILS/src/javascript/backend/catalog/biblio_fingerprint.js
Open-ILS/src/perlmods/OpenILS/Application/WoRM.pm
Open-ILS/src/perlmods/OpenILS/Utils/ScriptRunner.pm

diff --git a/Open-ILS/examples/json-request-test.pl b/Open-ILS/examples/json-request-test.pl
new file mode 100755 (executable)
index 0000000..4da4426
--- /dev/null
@@ -0,0 +1,35 @@
+#!/usr/bin/perl -w
+use strict;
+use lib '../src/perlmods/';
+use lib '../../OpenSRF/src/perlmods/';
+use lib '../src/perlmods/OpenILS/Utils/';
+
+use JSON;
+use OpenSRF::System;
+use OpenILS::Utils::ScriptRunner;
+use OpenSRF::Utils::Logger;
+use OpenSRF::Utils::SettingsClient;
+use OpenSRF::EX qw(:try);
+use Fieldmapper (IDL => 'fm_IDL.xml');
+
+unless (@ARGV > 1) {
+       print <<USAGE;
+Usage: $0 /openils-root-dir script.js
+USAGE
+}
+
+my $root = shift(@ARGV);
+
+OpenSRF::System->bootstrap_client( config_file => $root.'/conf/bootstrap.conf');
+
+try {
+        OpenILS::Utils::ScriptRunner->add_path($root.'/var/web/opac/common/js/');
+        OpenILS::Utils::ScriptRunner->add_path('../src/javascript/backend/libs/');
+        OpenILS::Utils::ScriptRunner->add_path('./');
+
+       print JSON->perl2JSON( OpenILS::Utils::ScriptRunner->new( file => shift(@ARGV) )->run );
+
+} otherwise {
+        warn 'crap:'.shift();
+};
+
diff --git a/Open-ILS/examples/req-test.js b/Open-ILS/examples/req-test.js
new file mode 100644 (file)
index 0000000..b34d6b8
--- /dev/null
@@ -0,0 +1,25 @@
+load_lib('fmall.js');
+load_lib('fmgen.js');
+load_lib('jsonopensrfrequest.js');
+
+
+var bc;
+environment.result = [];
+
+var req = new JSONOpenSRFRequest()
+
+req.connect('open-ils.cstore');
+req.call('open-ils.cstore.direct.actor.user.search.atomic');
+
+req.send({ family_name : "arl-dan" },{flesh:1,flesh_fields:{au:['cards']}});
+user = req.responseJSON[0].cards()[0].barcode();
+log_debug(user);
+environment.result.push(req.responseJSON);
+
+req.send({ family_name : "arl-east" },{flesh:1,flesh_fields:{au:['cards']}});
+user = req.responseJSON[0].cards()[0].barcode();
+log_debug(user);
+environment.result.push(req.responseJSON);
+
+req.finish();
+
index cb80059..6d83dba 100644 (file)
@@ -1,6 +1,8 @@
 // so we can tell if it's a book or other type
 load_lib('record_type.js');
 
+environment.result = {};
+
 var marcdoc = new XML(environment.marc);
 var marc_ns = new Namespace('http://www.loc.gov/MARC21/slim');
 
@@ -105,7 +107,7 @@ if (!author) {
 
 author = author.toLowerCase().replace(/^\s*(\w+).*?$/,"$1");
 
-result.fingerprint = title + author;
+environment.result.fingerprint = title + author;
 
 if (marcdoc.datafield.(@tag == '040').subfield.(@code == 'a').toString().match(/DLC/)) {
        quality += 5;
@@ -131,6 +133,5 @@ if (extractFixedField(marcdoc, 'Lang') == 'eng') {
 }
 
 
-// XXX this has to be a string ... for now. JS::SM limitation
-result.quality = '' + parseInt( '' + quality );
+environment.result.quality = quality;
 
index b698bc5..1402b3e 100644 (file)
@@ -1423,14 +1423,14 @@ sub biblio_fingerprint {
        $log->debug("Applying environment for biblio fingerprinting...");
 
        my $env = {marc => $marc, mods => $mods};
-       my $res = {fingerprint => '', quality => '0'};
+       #my $res = {fingerprint => '', quality => '0'};
 
        $fp_script->insert('environment' => $env);
-       $fp_script->insert('result' => $res);
+       #$fp_script->insert('result' => $res);
 
        $log->debug("Running script for biblio fingerprinting...");
 
-       $fp_script->run || ($log->error( "Fingerprint script died!  $@" ) && return 0);
+       my $res = $fp_script->run || ($log->error( "Fingerprint script died!  $@" ) && return 0);
 
        $log->debug("Script for biblio fingerprinting completed successfully...");
 
index 36b7e20..c029df8 100644 (file)
@@ -7,8 +7,9 @@ use JavaScript::SpiderMonkey;
 use LWP::UserAgent;
 use XML::LibXML;
 use Time::HiRes qw/time/;
-use vars qw/%_paths/;
+use vars qw/%_paths $__json_js/;
 
+{ local $/ = undef; $__json_js = <DATA>; }
 
 sub new {
        my $class = shift;
@@ -111,7 +112,14 @@ sub reset_count {
 
 sub run {
        my $self = shift;
-       my $file = shift() || $self->{file};
+       my $file = shift();
+
+       my $_real = 0;
+       if(!$file) {
+               $_real = 1;
+               $file = $self->{file};
+       }
+
        my $js = $self->context;
 
 
@@ -129,18 +137,31 @@ sub run {
                return 0;
        }
 
+       my $res = '';
        {       local $/ = undef;
+
+               $self->insert('environment.result' => {});
+
                my $content = <F>;
+               print ( "full script is [$content]" );
+
                my $s = time();
                if( !$js || !$content || !$js->eval($content) ) {
                        $logger->error("$file Eval failed: $@");  
                        return 0;
                }
                $logger->debug("eval of $file took ". sprintf('%0.3f', time - $s) . " seconds");
+
+               if ($_real) {
+                       $self->insert('__' => {'OILS_RESULT' => ''});
+                       $js->eval($__json_js."__.OILS_RESULT = js2JSON(environment.result);");
+                       $res = $self->retrieve('__.OILS_RESULT');
+               }
        }
 
        close(F);
-       return 1;
+       $logger->debug( "script result is [$res]" );
+       return JSON->JSON2perl( $res );
 }
 
 sub remove_path { 
@@ -552,3 +573,142 @@ sub _JS_DOM {
 
 
 1;
+
+__DATA__
+// in case we run on an implimentation that doesn't have "undefined";
+var undefined;
+
+function Cast (obj, class_constructor) {
+       try {
+               if (eval(class_constructor + '["_isfieldmapper"]')) {
+                       obj = eval("new " + class_constructor + "(obj)");
+               }
+       } catch( E ) {
+               alert( E + "\n");
+       } finally {
+               return obj;
+       }
+}
+
+function JSON2js (json) {
+
+       json = String(json).replace( /\/\*--\s*S\w*?\s*?\s+\w+\s*--\*\//g, 'Cast(');
+       json = String(json).replace( /\/\*--\s*E\w*?\s*?\s+(\w+)\s*--\*\//g, ', "$1")');
+
+       var obj;
+       if (json != '') {
+               try {
+                       eval( 'obj = ' + json );
+               } catch(E) {
+                       debug("Error building JSON object with string " + E + "\nString:\n" + json );
+                       return null;
+               }
+       }
+       return obj;
+}
+
+
+function object2Array(obj) {
+       if( obj == null ) return null;
+
+       var arr = new Array();
+       for( var i  = 0; i < obj.length; i++ ) {
+               arr[i] = obj[i];
+       }
+       return arr;
+}
+
+
+function js2JSON(arg) {
+       return _js2JSON(arg);
+}
+
+function _js2JSON(arg) {
+       var i, o, u, v;
+
+               switch (typeof arg) {
+                       case 'object':
+       
+                               if(arg) {
+       
+                                       if (arg._isfieldmapper) { /* magi-c-ast for fieldmapper objects */
+       
+                                               if( arg.a.constructor != Array ) {
+                                                       var arr = new Array();
+                                                       for( var i  = 0; i < arg.a.length; i++ ) {
+                                                               if( arg.a[i] == null ) {
+                                                                       arr[i] = null; continue;
+                                                               }
+       
+                                                               if( typeof arg.a[i] != 'object' ) { 
+                                                                       arr[i] = arg.a[i];
+       
+                                                               } else if( typeof arg.a[i] == 'object' 
+                                                                                       && arg.a[i]._isfieldmapper) {
+       
+                                                                       arr[i] = arg.a[i];
+       
+                                                               } else {
+                                                                       arr[i] = object2Array(arg.a[i]);                
+                                                               }
+                                                       }
+                                                       arg.a = arr;
+                                               }
+       
+                                               return "/*--S " + arg.classname + " --*/" + js2JSON(arg.a) + "/*--E " + arg.classname + " --*/";
+       
+                                       } else {
+       
+                                               if (arg.constructor == Array) {
+                                                       o = '';
+                                                       for (i = 0; i < arg.length; ++i) {
+                                                               v = js2JSON(arg[i]);
+                                                               if (o) {
+                                                                       o += ',';
+                                                               }
+                                                               if (v !== u) {
+                                                                       o += v;
+                                                               } else {
+                                                                       o += 'null';
+                                                               }
+                                                       }
+                                                       return '[' + o + ']';
+       
+                                               } else if (typeof arg.toString != 'undefined') {
+                                                       o = '';
+                                                       for (i in arg) {
+                                                               v = js2JSON(arg[i]);
+                                                               if (v !== u) {
+                                                                       if (o) {
+                                                                               o += ',';
+                                                                       }
+                                                                       o += js2JSON(i) + ':' + v;
+                                                               }
+                                                       }
+       
+                                                       o = '{' + o + '}';
+                                                       return o;
+       
+                                               } else {
+                                                       return;
+                                               }
+                                       }
+                               }
+                               return 'null';
+       
+                       case 'unknown':
+                       case 'number':
+                               if( isNaN(arg) ) throw "JSON.js encountered NaN in js2JSON()";
+                               return arg;
+       
+                       case 'undefined':
+                       case 'function':
+                               return u;
+       
+                       case 'string':
+                       default:
+                               return '"' + String(arg).replace(/(["\\])/g, '\\$1') + '"';
+               }
+
+}
+