/** * Format the output to JSON-LD Specifically formatted to the CATALYST INTERCHANGE FORMAT * * @param Object $object - the data to format * @return string */ function format($object) { global $CFG, $HUB_FLM; $this->serviceurl = $CFG->homeAddress . 'api/'; $api_class = get_class($object); // If the passed object has a cipher use it, else create a new one. if (isset($object->cipher)) { $this->cipher = $object->cipher; } else { $salt = openssl_random_pseudo_bytes(32); $this->cipher = new Cipher($salt); } $this->mainStr = '{'; $this->mainStr .= '"@context":['; $this->mainStr .= '"http://purl.org/catalyst/jsonld",'; $this->namespaces .= '{'; $this->namespaces .= '"' . $this->site . '": "' . $this->serviceurl . '",'; $this->outerGraphStr .= '"@graph": ['; switch ($api_class) { case "UnobfuscatedUserSet": $users = $object->users; if (isset($object->cipher)) { foreach ($users as $user) { if (in_array($user->userid, $this->checkUsers) === FALSE) { $this->addFullUserProfile($user); array_push($this->checkUsers, $user->userid); } } } break; case "GroupSet": // a set of conversations ? $this->addGroups($object); break; case "Group": // a conversation $this->addGroup($object); break; case "View": // one issue tree $this->addView($object); break; case "ViewSet": // one issue tree $this->addViews($object); break; case "ConnectionSet": $this->addConnections($object); break; case "NodeSet": $this->conversationBlockStr .= '{'; $this->conversationBlockStr .= '"@type":["catalyst:Conversation","Graph"],'; $this->conversationBlockStr .= '"@id": "' . $this->site . ':nodes",'; $this->conversationBlockStr .= '},'; $this->addNodes($object); break; case "CNode": $this->conversationBlockStr .= '{'; $this->conversationBlockStr .= '"@type": "catalyst:Conversation",'; $this->conversationBlockStr .= '"@id": "' . $this->site . ':nodes/' . $id . '",'; $this->conversationBlockStr .= '"data_graph": "' . $this->site . ':nodes"'; $this->conversationBlockStr .= '},'; $this->addNode($object); break; /* case "URL": $this->conversationBlockStr .= '"data_graph": "'.$this->site.':urls",'; $this->addURL($object); $this->addURL($object); break; case "URLSet": $this->conversationBlockStr .= '"data_graph": "'.$this->site.':urls",'; $nodeUrls = $object->urls; $this->addURLs($nodeUrls); break; */ /* case "Connection": $this->addConnection($object); break; case "User": //$this->conversationBlockStr .= '"pseudonyms_graph": "'.$this->site.':pseudonyms",'; $this->addUser($object); break; case "UserSet": //$this->conversationBlockStr .= '"pseudonyms_graph": "'.$this->site.':pseudonyms",'; $users = $object->users; foreach ($users as $user){ if (in_array($user->userid,$this->checkUsers) === FALSE) { $this->addUser($user); array_push($this->checkUsers, $user->userid); } } break; */ /* case "URL": $this->conversationBlockStr .= '"data_graph": "'.$this->site.':urls",'; $this->addURL($object); $this->addURL($object); break; case "URLSet": $this->conversationBlockStr .= '"data_graph": "'.$this->site.':urls",'; $nodeUrls = $object->urls; $this->addURLs($nodeUrls); break; */ /* case "Connection": $this->addConnection($object); break; case "User": //$this->conversationBlockStr .= '"pseudonyms_graph": "'.$this->site.':pseudonyms",'; $this->addUser($object); break; case "UserSet": //$this->conversationBlockStr .= '"pseudonyms_graph": "'.$this->site.':pseudonyms",'; $users = $object->users; foreach ($users as $user){ if (in_array($user->userid,$this->checkUsers) === FALSE) { $this->addUser($user); array_push($this->checkUsers, $user->userid); } } break; */ case "error": $doc .= "<error><message>" . $object->message . "</message><code>" . $object->code . "</code></error>"; return $doc; break; default: //error as method not defined. global $ERROR; $ERROR = new error(); $ERROR->createInvalidMethodError(); include $HUB_FLM->getCodeDirPath("core/formaterror.php"); die; } $this->namespaces = $this->trimFinalComma($this->namespaces); $this->namespaces .= '}'; $this->mainStr .= $this->namespaces; $this->mainStr .= '],'; if ($this->conversationBlockStr != "") { $this->outerGraphStr .= $this->conversationBlockStr; } // ADD ANNOTAION OUTER GRAPHS if ($this->annotationGraphListStr != "") { $this->outerGraphStr .= $this->annotationGraphListStr; } // ADD DATA GRAPH if ($this->dataGraphStr != "") { $this->outerGraphStr .= $this->dataGraphStr; } // ADD HISTORY GRAPH if ($this->historyGraphStr != "") { $this->outerGraphStr .= $this->historyGraphStr; } $this->outerGraphStr = $this->trimFinalComma($this->outerGraphStr); $this->outerGraphStr .= ']'; $this->mainStr .= $this->outerGraphStr; $this->mainStr .= '}'; if (isset($object->unobfuscationid) && $object->unobfuscationid != "" && $api_class != "UnobfuscatedUserSet") { $count = count($this->checkUsers); $users = ""; for ($i = 0; $i < $count; $i++) { $userid = $this->checkUsers[$i]; if ($userid != "") { $users .= $userid . ","; } } $users = trimFinalComma($users); setObfuscationUsers($object->unobfuscationid, $users); } return $this->mainStr; }
} $response = $group; } else { global $ERROR; $ERROR = new error(); $ERROR->createAccessDeniedError(); include $HUB_FLM->getCodeDirPath("core/formaterror.php"); die; } } break; default: //error as method not defined. global $ERROR; $ERROR = new error(); $ERROR->createInvalidMethodError(); include $HUB_FLM->getCodeDirPath("core/formaterror.php"); die; } //error_log("ENDING Initial data gathering"); // finally format the output $jsonld = format_output_rest($response); if (isset($HUB_CACHE)) { $HUB_CACHE->setStringData($request, $jsonld, $CFG->CACHE_DEFAULT_TIMEOUT); } } else { // write to file; //$file = 'cachedjsonld.txt'; //$current .= $jsonld; //file_put_contents($file, $current); //error_log("JSONLD FOUND: resapi for ".$request);