import "/std/ns.rql"; import "common.rql"; Session.namespaces["cgi"] = "http://www.intellidimension.com/dsp/cgi#"; var LOGFILE = null; var Locator = new ActiveXObject("WbemScripting.SWbemLocator"); var Service = Locator.ConnectServer(); Service.Security_.ImpersonationLevel=3; try { doQuery(); } catch(e) { } function doQuery() { var resUnbound = Resource(Request.serverVariables["HTTP_ITD_RQI_UNBOUND_URI"]); logEvent(LOGFILE, "DoQuery"); var dsOut = new datasource(); var ds = new datasource("inet?parsetype=ntriples&src=" + server.urlEncode(request.binaryRead())); var rs = (select ?p ?s ?o using #ds where {?p ?s ?o}); for (;!rs.EOF; rs.MoveNext()) { var kind = (rs[0] != resUnbound?1:0) + (rs[1] != resUnbound?2:0) + (rs[2] != resUnbound?4:0); logEvent(LOGFILE, "kind:" + kind + " " + rs[0] + " " + rs[1] + " " + rs[2]); try { switch (kind) { case 0: break; case 1: doP(rs[0], dsOut); break; case 2: doS(rs[1], dsOut); break; case 3: doPS(rs[0], rs[1], dsOut); break; case 4: doO(rs[2], dsOut); break; case 5: doPO(rs[0], rs[2], dsOut); break; case 6: doSO(rs[1], rs[2], dsOut); break; case 7: doPSO(rs[0], rs[1], rs[2], dsOut); break; default: break; } } catch (e) { logEvent(LOGFILE, e); } } response.write(dsOut.format("application/ntriples")); } function doP(p, dsOut) { //look in the schema to see which class(es) support the predicate var rs = getClassesForPred(p); for (;!rs.EOF; rs.MoveNext()) { var strClass = getClassFromUri(rs[0]); if (strClass != null) { var Class = Service.Get(strClass); if (Class != null) { for (var Iter = new Enumerator(Class.Instances_());!Iter.atEnd(); Iter.moveNext()) dumpObject(Iter.item(), makeObjectUri(Iter.item()), dsOut, p); } } } } function doS(s, dsOut) { var strClass = getClassFromUri(s); if (strClass != null) { var strId = getIdFromUri(s); if (strId != null) { var Obj = Service.Get(strClass + strId); if (Obj != null) dumpObject(Obj, makeObjectUri(Obj), dsOut); } } } function doPS(p, s, dsOut) { var strClass = getClassFromUri(s); if (strClass != null) { var strId = getIdFromUri(s); if (strId != null) { var Obj = Service.Get(strClass + strId); if (Obj != null) dumpObject(Obj, makeObjectUri(Obj), dsOut, p); } } } function doO(o, dsOut) { //only do this if o is a uri if (typeof(o) != "resource") return; var strClass = getClassFromUri(s); if (strClass != null) { var strId = getIdFromUri(s); if (strId != null) { var Obj = Service.Get(strClass + strId); for (var Iter = new Enumerator(Obj.References_());!Iter.atEnd(); Iter.moveNext()) dumpObject(Iter.item(), makeObjectUri(Iter.item()), dsOut, null, o); } } } function doPO(p, o, dsOut) { if (p == resource("rdf:type")) { var strClass = getClassFromUri(o); if (strClass != null) { var Class = Service.Get(strClass); if (Class != null) { for (var Iter = new Enumerator(Class.Instances_());!Iter.atEnd(); Iter.moveNext()) { var resObj = makeObjectUri(Iter.Item); (insert {[rdf:type] #resObj #o} into #dsOut); } } } } else { var rs = getClassesForPred(p); for (;!rs.EOF; rs.MoveNext()) { var strClass = getClassFromUri(rs[0]); if (strClass != null) { if (typeof(o) == "string") { var strPred = string(p).substring(string(BASE + strClass).length + 1); var cmd = "SELECT * FROM " + strClass + " WHERE " + strPred + "=" + LiteralToWMIQueryValue(o); for (var Iter = new Enumerator(Service.ExecQuery(cmd));!Iter.atEnd(); Iter.moveNext()) dumpObject(Iter.item(), makeObjectUri(Iter.item()), dsOut, p, o); } else { var Class = Service.Get(strClass, 0, NVSet); if (Class != null) { for (var Iter = new Enumerator(Class.Instances_());!Iter.atEnd(); Iter.moveNext()) dumpObject(Iter.item(), makeObjectUri(Iter.item()), dsOut, p, o); } } } } } } function LiteralToWMIQueryValue(l) { switch (l.datatype) { case resource("xsd:unsignedInt"): case resource("xsd:unsignedByte"): case resource("xsd:byte"): case resource("xsd:unsignedShort"): case resource("xsd:unsignedLong"): case resource("xsd:float"): case resource("xsd:short"): case resource("xsd:double"): case resource("xsd:integer"): case resource("xsd:long"): case resource("xsd:boolean"): return l; default: return "'" + l + "'"; } } function doSO(s, o, dsOut) { var strClass = getClassFromUri(s); if (strClass != null) { var strId = getIdFromUri(s); if (strId != null) { var Obj = Service.Get(strClass + strId); if (Obj != null) dumpObject(Obj, makeObjectUri(Obj), dsOut, null, o); } } } function doPSO(p, s, o, dsOut) { var strClass = getClassFromUri(s); if (strClass != null) { var strId = getIdFromUri(s); if (strId != null) { var Obj = Service.Get(strClass + strId); if (Obj != null) dumpObject(Obj, makeObjectUri(Obj), dsOut, p, o); } } } function getClassesForPred(pred) { var rs = (select ?c using #SCHEMA where {[rdfs:domain] #pred ?c} and {[cim:visibility] ?c #VIS_DYNAMIC}); return rs; } function getClassFromUri(_uri) { var res = (new regexp(".*/([^/\\?]*)")).exec(_uri); if (res != null) { var resClass = resource((new regexp("([^?]*)")).exec(_uri)[1]); var rs = (select ?c using #SCHEMA where {[cim:visibility] #resClass #VIS_DYNAMIC}); if (rs.EOF) return null; return res[1]; } return null; } function getClassFromObject(Obj) { return resource("cim:" + Obj.Path_.Class); } function getIdFromUri(_uri) { var res = (new regexp(".*\\?id=(.*)")).exec(_uri); if (res != null) return urldecode(res[1]); return null; } function makeObjectUri(o) { var strClass = o.Path_.Class; return resource(BASE + strClass + "?id=" + server.urlEncode(string(o.Path_.Relpath).substring(strClass.length))); } function dumpObject(Obj, resObj, ds, p, o) { var resCls = getClassFromObject(Obj); if (p == null || p == resource("rdf:type")) { if (o == null || o == resCls) (insert {[rdf:type] #resObj #resCls} into #ds); } if (p == null || p == resource("rdfs:label")) { if (o == null || o == Obj.Path_.RelPath) (insert {[rdfs:label] #resObj #(Obj.Path_.RelPath)} into #ds); } if (p != null) { var strProp = string(p).substring(string(resCls).length + 1); var Prop = Obj.Properties_.item(strProp); if (Prop != null) dumpPropVal(p, resObj, Prop, ds, o); } else { for (var Iter = new Enumerator(Obj.Properties_); !Iter.atEnd(); Iter.moveNext()) { var resProp = Resource(resCls + "_" + Iter.item().Name); if (p != null && resProp != p) continue; dumpPropVal(resProp, resObj, Iter.item(), ds, o); } } } function dumpPropVal(resProp, resObj, Prop, ds, o) { try { if (Prop.Value != null) { var cimtype = Prop.CIMType; var val; if (cimtype == 102) { val = makeObjectUri(Service.Get(Prop.Value)); } else { val = string(Prop.Value); val.datatype = getXSDType(Prop.CIMType); } if (o == null || o == val) (insert {#resProp #resObj #val} into #ds); } } catch(e) { } } function getXSDType(cimtype) { switch(cimtype) { case 8: return resource("xsd:string"); case 101: return resource("xsd:dateTime"); case 19: return resource("xsd:unsignedInt"); case 17: return resource("xsd:unsignedByte"); case 16: return resource("xsd:byte"); case 18: return resource("xsd:unsignedShort"); case 11: return resource("xsd:boolean"); case 21: return resource("xsd:unsignedLong"); case 4: return resource("xsd:float"); case 2: return resource("xsd:short"); case 5: return resource("xsd:double"); case 3: return resource("xsd:integer"); case 20: return resource("xsd:long"); } } function logEvent(file, msg) { if (file == null) return; var sCmdLine = Server.urlEncode("cmd.exe"); var sDir = Server.urlEncode("c:/") var cgi = new DataSource("cgi?cmdline=" + sCmdLine + "&dir=" + sDir); // Define an environment variable INSERT {[rdf:type] ?id [cgi:EnvironmentVariable]} {[rdfs:label] ?id 'message'} {[rdf:value] ?id #msg} INTO #cgi WHERE ?id=#(new Resource()); var strCmd = "echo %message% >> " + file + "\r\n"; INSERT {[rdf:type] ?id [cgi:Process]} {[cgi:stdin] ?id #strCmd} INTO #cgi WHERE ?id=#(new Resource()); SELECT ?o USING #cgi WHERE {[cgi:stdout] ?s ?o}; }