function ruleInputsInfer($s3ql, $db, $user_id) { $s3ql['where']['subject'] = $s3ql['where']['subject'] != '' ? $s3ql['where']['subject'] : ($s3ql['where']['subject_id'] != '' ? subject4subjectID(array('key' => $s3ql['key'], 'subject_id' => $s3ql['where']['subject_id'], 'db' => $db)) : ''); if ($s3ql['where']['verb_id'] == '') { #turn a literal verb into an instance of a class #class exists in project? no? create it;else find it's $VerbClass = projectVerbClass(array('project_id' => $s3ql['where']['project_id'], 'db' => $db, 'user_id' => $user_id)); if (!$VerbClass) { $to_create = array('project_id' => $s3ql['where']['project_id'], 'entity' => 's3dbVerb', 'notes' => 'Collection created by S3DB for holding Verbs'); $inputs = gatherInputs(array('element' => 'collection', 'to_create' => $to_create, 'db' => $db, 'user_id' => $user_id)); $inserted = insert_s3db(array('element' => 'collection', 'inputs' => $inputs, 'user_id' => $user_id, 'db' => $db)); #try again; $VerbClass = projectVerbClass(array('project_id' => $s3ql['where']['project_id'], 'db' => $db, 'user_id' => $user_id)); } #now create the instanceVerb $inputs = array('resource_class_id' => $VerbClass['resource_id'], 'project_id' => $s3ql['where']['project_id'], 'notes' => $s3ql['where']['verb'], 'created_by' => $user_id, 'entity' => $VerbClass['entity'], 'status' => 'A'); #echo '<pre>';print_r($inputs);exit; $inserted = insert_s3db(array('element' => 'item', 'inputs' => $inputs, 'user_id' => $user_id, 'db' => $db)); #echo '<pre>';print_r($inserted);exit; $s3ql['where']['verb_id'] = $inserted['item_id']; #echo '<pre>';print_r($s3ql);exit; } $s3ql['where']['verb'] = verb4instanceID(array('key' => $s3ql['key'], 'instance_id' => $s3ql['where']['verb_id'], 'db' => $db)); $s3ql['where']['object'] = $s3ql['where']['object'] != '' ? $s3ql['where']['object'] : ($s3ql['where']['object_id'] != '' ? object4objectID(array('key' => $s3ql['key'], 'object_id' => $s3ql['where']['object_id'], 'db' => $db)) : $s3ql['where']['object_id']); #echo '<pre>';print_r($s3ql);exit; return $s3ql; }
function getSubClassStats($uid, $subClass_info, $inData, $user_id, $db, $N3coreNames, $inputs) { global $verbs; $uid_info = uid($uid); $letter = substr($uid, 0, 1); $a_class_type = $GLOBALS['s3codes'][substr($uid, 0, 1)]; $a_class_id = $GLOBALS['COREids'][$a_class_type]; $CORElabel = $GLOBALS['CORElabel']; $N3coreNames = $N3coreNames != '' ? $N3coreNames : $GLOBALS['N3coreNames']; if (!$inputs['nocore']) { $n3 .= sprintf(n3UID($uid_info['uid']) . ' a ' . $N3coreNames[$a_class_type] . ' .') . chr(10) . chr(10); } switch ($a_class_type) { case 'user': if ($uid_info['Did'] == $GLOBALS['s3db_info']['deployment']['Did']) { $objectPredicates = array('rdfs:subClassOf' => n3UID($uid_info['Did']), 'rdfs:label' => '"' . $subClass_info[$CORElabel[$a_class_id]] . '"', 'foaf:mbox' => '"' . $subClass_info['account_email'] . '"', 'foaf:name' => '"' . $subClass_info['account_uname'] . '"', 'foaf:password' => $user_id == 1 ? '"' . findPassword(ereg_replace('^' . $letter, '', $uid_info['uid']), $db) . '"' : '', 'dcterms:creator' => n3UID('U' . $subClass_info['created_by']), 'dcterms:created' => '"' . $subClass_info['created_on'] . '"'); } else { $objectPredicates = array('rdfs:subClassOf' => n3UID($GLOBALS['s3db_info']['deployment']['Did'])); } break; case 'group': $objectPredicates = array('rdfs:subClassOf' => ':' . $uid_info['Did'], 'rdfs:label' => '"' . $subClass_info[$CORElabel[$a_class_id]] . '"', 'dcterms:creator' => n3UID('U' . $subClass_info['created_by']), 'dcterms:created' => '"' . $subClass_info['created_on'] . '"'); $addStat .= addGroupUsers($user_id, $db, $subClass_info[$a_class_id]); break; case 'project': $objectPredicates = array('rdfs:subClassOf' => n3UID($uid_info['Did']), 'rdfs:label' => '"' . $subClass_info[$CORElabel[$a_class_id]] . '"', 'dcterms:creator' => n3UID('U' . $subClass_info['created_by']), 'dcterms:created' => '"' . $subClass_info['created_on'] . '"'); $addStat .= addProjectRulesAndCollections($user_id, $db, $url, $subClass_info[$a_class_id], $inData); break; case 'collection': $objectPredicates = array('rdfs:subClassOf' => n3UID('P' . $subClass_info['project_id']), 'rdfs:label' => '"' . $subClass_info['entity'] . '"', 'rdfs:comment' => '"' . $subClass_info['notes'] . '"'); if (!$inputs['nometa']) { $objectPredicates['dcterms:creator'] = n3UID('U' . $subClass_info['created_by']); $objectPredicates['dcterms:created'] = '"' . $subClass_info['created_on'] . '"'; } if (in_array('all', array_keys($inputs))) { $addStat .= addCollectionItemStats($user_id, $db, $subClass_info[$a_class_id], $inputs); } #trying to see if the collection declarations are meesing up the query a lot if ($inputs['nocore']) { $n3 .= sprintf(n3UID($uid_info['uid']) . ' a ' . $N3coreNames[$a_class_type] . ' .') . chr(10) . chr(10); } break; case 'rule': $subject_id = n3UID('C' . $subClass_info['subject_id']); $verb_id = $subClass_info['verb_id'] == "" ? ":I" . random_string(5) : n3UID("I" . $subClass_info['verb_id']); #predicates can't be literals, so create a resource for literal verbs. #when the verb is not an ID, a random string is generated that will simulate the ID of an instance. if (!in_array($subClass_info['verb'], array_keys($verbs))) { $addStat .= sprintf($verb_id); $addStat .= sprintf(' rdfs:label "' . $subClass_info['verb'] . '" ;') . chr(10); if (!$inputs['nocore']) { $addStat .= chr(9) . sprintf(' a s3db:s3dbItem ;') . chr(10); } ##Find collection of this item and output this information $item_info = s3info('item', $subClass_info['verb_id'], $db); if (is_array($item_info)) { $addStat .= chr(9) . sprintf(' a ' . n3UID("C" . $item_info['resource_class_id']) . ' .') . chr(10) . chr(10); } else { #find a collection for the verbs $verbCollection = projectVerbClass(array('project_id' => $subClass_info['project_id'], 'db' => $db, 'user_id' => $user_id)); $addStat .= chr(9) . sprintf(' a ' . n3UID("C" . $verbCollection['resource_id']) . ' .') . chr(10) . chr(10); } $verbs[$subClass_info['verb']] = $verb_id; } else { $verb_id = $verbs[$subClass_info['verb']]; } $object_id = $subClass_info['object_id'] == "" ? '"' . $subClass_info['object'] . '"' : n3UID("C" . $subClass_info['object_id']); $objectPredicates = array('rdfs:label' => '"' . $subClass_info['subject'] . ' ' . $subClass_info['verb'] . ' ' . $subClass_info['object'] . '"', 'rdfs:subClassOf' => n3UID('P' . $subClass_info['project_id']), 'rdf:subject' => $subject_id, 'rdf:predicate' => $verb_id, 'rdf:object' => $object_id); if (!$inputs['nometa']) { $objectPredicates['dcterms:creator'] = n3UID('U' . $subClass_info['created_by']); $objectPredicates['dcterms:created'] = '"' . $subClass_info['created_on'] . '"'; } ##When no core is needed (when the document is not meant to be reloaded, there is no need for $addStat .= sprintf($subject_id . ' ' . $verb_id . ' ' . $object_id . ' .') . chr(10); if (in_array('all', array_keys($inputs))) { $addStat .= addRuleStats($user_id, $db, $url, $subClass_info[$a_class_id], $inputs); } #trying to see if the collection declarations are meesing up the query a lot if ($inputs['nocore']) { $n3 .= sprintf(n3UID($uid_info['uid']) . ' a ' . $N3coreNames[$a_class_type] . ' .') . chr(10) . chr(10); } break; case 'item': $objectPredicates = array('rdfs:label' => '"' . $subClass_info['notes'] . '"', 'a' => n3UID('C' . $subClass_info['class_id'])); if (!$inputs['nometa']) { $objectPredicates['dcterms:creator'] = ':U' . $subClass_info['created_by']; $objectPredicates['dcterms:created'] = '"' . $subClass_info['created_on'] . '"'; } #trying to see if the collection declarations are meesing up the query a lot if ($inputs['nocore']) { $n3 .= sprintf(n3UID($uid_info['uid']) . ' a ' . $N3coreNames[$a_class_type] . ' .') . chr(10) . chr(10); } break; case 'statement': $subject_name = 'instance_id'; $predicate_name = 'rule_id'; $object_name = 'instance_id'; if ($subClass_info['file_name'] != '') { #find the file. Write it as base64encoded #echo '<pre>';print_r($subClass_info); $fileLocation = fileLocation($subClass_info, $db); if ($fileLocation != '') { if (!$inputs['files']) { $content = @fread(@fopen($fileLocation, 'r'), @filesize($fileLocation)); $content = base64_encode($content); $object = '"s3dbFile_' . $subClass_info['file_name'] . '_' . $content . '"'; } else { $object = '"s3dbLink_' . $subClass_info['file_name'] . '_' . S3DB_URI_BASE . '/download.php?key=' . $inputs['key'] . '&statement_id=' . $subClass_info['statement_id'] . '"'; } #echo $object;exit; #echo $subClass_info['file_name'].chr(13).chr(10); } #echo $object;exit; #ereg('<a href(.*)download.php(.*)>(.*)</a>', $stat_info['value'], $linkdata); #$statfilelink='<'.$url.'download.php'.str_replace('"', '', $linkdata[2]).'>'; #$object=$statfilelink; } elseif ($subClass_info['object_id'] != '') { $object = n3UID($COREletter[$object_name] . $subClass_info['value']); } else { ereg('<a href=(.*)>(.*)</a>', $subClass_info['value'], $links); if (!empty($links)) { $object = '"' . str_replace(array('"', '\''), array('', ''), $links[1]) . '"'; } else { $object = '"' . $subClass_info['value'] . '"'; } } $objectPredicates = array('rdf:subject' => n3UID('I' . $subClass_info[$subject_name]), 'rdf:predicate' => n3UID('R' . $subClass_info[$predicate_name]), 'rdf:object' => $object, 'dcterms:creator' => n3UID('U' . $subClass_info['created_by']), 'dcterms:created' => '"' . $subClass_info['created_on'] . '"'); #$objectPredicates['rdfs:label'] ='"'.$subClass_info['subject'].' '.$subClass_info['instance_notes'].' (I'.$subClass_info['instance_id'].') '.$subClass_info['verb'].' '.$subClass_info['object'].' '.(($subClass_info['object_id']!='')?($subClass_info['object_notes'].' (I'.$subClass_info['value'].')'):(($subClass_info['file_name']=='')?str_replace('"', '', $objectPredicates['rdf:object']):$subClass_info['file_name'])).'"'; $objectPredicates['rdfs:label'] = '"' . $subClass_info['subject'] . ' ' . $subClass_info['instance_notes'] . ' I' . $subClass_info['instance_id'] . ' ' . $subClass_info['verb'] . ' ' . $subClass_info['object'] . ' ' . ($subClass_info['object_id'] != '' ? $subClass_info['object_notes'] . ' I' . $subClass_info['value'] . '' : ($subClass_info['file_name'] == '' ? str_replace('"', '', $objectPredicates['rdf:object']) : $subClass_info['file_name'])) . '"'; #when no core is requested, return just the reified stat if ($inputs['nocore']) { $objectPredicates = array(); } #unreified statement $addStat .= sprintf("%s", $objectPredicates['rdf:subject']); $addStat .= chr(9) . sprintf("%s", $objectPredicates['rdf:predicate'] . ' ' . ($subClass_info['object_id'] != '' ? $objectPredicates['rdf:object'] : $objectPredicates['rdf:object']) . ' .') . chr(10); break; } #export user permissions in rdf if (in_array('p', array_keys($inData))) { $s3ql = compact('user_id', 'db'); $s3ql['from'] = 'users'; $s3ql['where'][$GLOBALS['COREids'][$a_class_type]] = $subClass_info[$GLOBALS['COREids'][$a_class_type]]; $users = S3QLaction($s3ql); if (is_array($users)) { $permissions = array_map('grab_permission', $users); $users = grab_id('user', $users); $specified_id_info['permissions'] = array_combine($users, $permissions); } if (is_array($specified_id_info['permissions'])) { $n3permissions .= chr(10); foreach ($specified_id_info['permissions'] as $user_code => $pcode) { $n3permissions .= sprintf(n3UID('U' . $user_code) . ' s3dbpc:VCU' . $pcode . ' ' . n3UID($uid_info['uid']) . ' .') . chr(10); } } } $objectPredicates = array_filter($objectPredicates); if (is_array($objectPredicates) && !empty($objectPredicates)) { #Global "this is a resource" statement $n3 .= sprintf(n3UID($uid_info['uid'])); foreach ($objectPredicates as $predicate => $object) { if ($object != '""') { $n3 .= sprintf("%s", ' ' . $predicate . ' ' . $object . ($predicate == end(array_keys($objectPredicates)) ? ' .' . chr(10) . chr(10) : ' ;' . chr(10) . chr(9))); } } } if (is_array($user2declare)) { foreach ($user2declare as $user => $toDeclare) { $uid_info = uid($user); $addStat .= sprintf(n3UID('U' . $user), 'rdfs:label "' . getUserName($user, $db) . '"') . chr(10); } } $n3 .= $addStat . $n3permissions; #echo $n3;exit; return $n3; }