public function callback($headers, $requestBody)
 {
     $jobID = $headers["Proxy-Transaction-Id"];
     $contentItemURI = $headers["Content-Item-Id"];
     $this->logger->trace("A message has been received" . " [ jobId :: {$jobID} ][ contentItemURI :: {$contentItemURI} ]" . " [ requestBody :: {$requestBody} ].");
     // get the posts for the specified job ID.
     $posts = $this->jobService->getPostByJobID($jobID);
     // exit if the job ID does NOT exist.
     if (0 === count($posts)) {
         $this->logger->error("No job found for id [ jobID :: {$jobID} ]" . "[ posts :: " . var_export($posts, true) . " ].");
         echo "No job found for id [ jobID :: {$jobID} ]" . "[ posts :: " . var_export($posts, true) . " ].";
         return;
     }
     // get the post ID.
     $postID = $posts[0]->ID;
     $this->logger->trace("A post was found [ postID :: {$postID} ]" . "[ jobID :: {$jobID} ].");
     $this->logger->trace("Removing existing enhancements [ postID :: {$postID} ].");
     $this->tripleStoreService->query("DELETE { ?s ?p ?o }\n                WHERE {\n                    ?s a fise:Enhancement .\n                    ?s wordlift:postID \"{$postID}\" .\n                    ?s ?p ?o .\n                }");
     $index = $this->triplesUtils->getIndexFromData($requestBody);
     $newIndex = $this->triplesUtils->bNodesToMD5($index);
     $this->changeSetService->applyChanges($newIndex, $this->changeCreator, false, "analysis");
     if (!empty($contentItemURI)) {
         $this->logger->trace("Setting the postID on the enhancements" . " [ postID :: {$postID} ].");
         $this->tripleStoreService->query("INSERT INTO <> { ?subject wordlift:postID \"{$postID}\" }\n                    WHERE { ?subject a fise:Enhancement .\n                            ?subject fise:extracted-from {$contentItemURI} }");
     }
     $this->logger->trace("Setting the job to completed" . " [ postID :: {$postID} ][ jobID :: {$jobID} ].");
     $this->jobService->setJob($postID, $jobID, WordLift_JobService::COMPLETED);
     echo "Data load completed successfully.";
     return WordPress_AjaxProxy::CALLBACK_RETURN_NULL;
 }
 public function clear($entity, $requestBody)
 {
     $json = json_decode($requestBody);
     $clear =& $json->clear;
     $postID = $json->postID;
     $this->logger->trace("[ entity :: {$entity} ][ clear :: " . var_export($clear, true) . " ].");
     while (0 < count($clear)) {
         $textAnnotation = array_shift($clear);
         $query = "DELETE {\n                        ?entityAnnotation wordlift:selected true ; \n                                          dcterms:references <urn:wordpress:{$postID}> . \n                    }\n                    WHERE {\n                        ?entityAnnotation a fise:EntityAnnotation;\n                            dcterms:relation <{$textAnnotation->about}>\n                    }";
         $this->tripleStoreService->query($query);
     }
 }
 private function getEntitiesAndTextAnnotations($postID)
 {
     $query = "SELECT ?textAnnotation ?confidence ?selectionHead ?selectionTail ?selectedText ?entity ?name ?type ?image ?url ?selected\n                  WHERE {\n                    ?entity a ?type;\n                        schema:name ?name .\n                    ?textAnnotation a fise:TextAnnotation;\n                        wordlift:postID \"{$postID}\";\n                        fise:selection-head ?selectionHead;\n                        fise:selection-tail ?selectionTail;\n                        fise:selected-text ?selectedText .\n                    ?entityAnnotation a fise:EntityAnnotation;\n                        dcterms:relation ?textAnnotation;\n                        fise:entity-reference ?entity;\n                        fise:confidence ?confidence .\n                    OPTIONAL { ?entity schema:image ?image } .\n                    OPTIONAL { ?entity schema:url ?url } .\n                    OPTIONAL { ?entityAnnotation wordlift:selected ?selected } .\n                    FILTER ( lang(?name) = \"EN\" )\n                  } ORDER BY DESC( ?confidence )";
     $result = $this->queryService->query($query);
     $rows =& $result["result"]["rows"];
     $this->logger->trace("Found " . count($rows) . " row(s).");
     $textAnnotations = array();
     $entities = array();
     foreach ($rows as $row) {
         $textAnnotation = $row["textAnnotation"];
         $entity = $row["entity"];
         $confidence = (double) $row["confidence"];
         $name = $row["name"];
         $type = $row["type"];
         $image = $row["image"];
         $url = $row["url"];
         $selected = "true" === $row["selected"] ? true : false;
         if (!array_key_exists($textAnnotation, $textAnnotations)) {
             $textAnnotations[$textAnnotation] = array("entities" => array(), "selectionHead" => $row["selectionHead"], "selectionTail" => $row["selectionTail"], "selectedText" => $row["selectedText"]);
         }
         if (!array_key_exists($entity, $textAnnotations[$textAnnotation]["entities"])) {
             $textAnnotations[$textAnnotation]["entities"][$entity] = array("confidence" => $confidence, "selected" => $selected);
         }
         //            if ( $confidence > $textAnnotations[ $textAnnotation ][ "entities" ][ $entity ][ "highestConfidence" ] )
         //                $textAnnotations[ $textAnnotation ][ "entities" ][ $entity ][ "highestConfidence" ] = $confidence;
         //            if ( $confidence < $textAnnotations[ $textAnnotation ][ "entities" ][ $entity ][ "lowestConfidence" ] )
         //                $textAnnotations[ $textAnnotation ][ "entities" ][ $entity ][ "lowestConfidence" ] = $confidence;
         //            $textAnnotations[ $textAnnotation ][ "entities" ][ $entity ][ "selected" ] =
         //                (boolean) $textAnnotations[ $textAnnotation ][ "entities" ][ $entity ][ "selected" ] && $selected;
         if (!array_key_exists($entity, $entities)) {
             $entities[$entity] = array("textAnnotations" => array(), "about" => $entity, "name" => $name, "type" => $type, "image" => array(), "url" => array());
         }
         if (NULL !== $image && !in_array($image, $entities[$entity]["image"])) {
             $entities[$entity]["image"][] = $image;
         }
         if (NULL !== $url && !in_array($url, $entities[$entity]["url"])) {
             $entities[$entity]["url"][] = $url;
         }
         if (!in_array($textAnnotation, $entities[$entity]["textAnnotations"])) {
             $entities[$entity]["textAnnotations"][] = $textAnnotation;
         }
     }
     return array("entities" => $entities, "textAnnotations" => $textAnnotations);
 }
    public function findRelated($postID)
    {
        $query = <<<EOF
            SELECT DISTINCT ?postID ?entity ?type ?name ?image
                WHERE {
                    ?entity a ?type;
                        schema:name ?name .
                    ?textAnnotation a fise:TextAnnotation;
                        wordlift:postID "{$postID}" .
                    ?entityAnnotation a fise:EntityAnnotation;
                        dcterms:relation ?textAnnotation;
                        fise:entity-reference ?entity;
                        wordlift:selected true .
                    ?entityAnnotations fise:entity-reference ?entity;
                        dcterms:relation ?textAnnotations;
                        wordlift:selected true .
                    ?textAnnotations wordlift:postID ?postID .
                OPTIONAL { ?entity schema:image ?image }
                FILTER( ?postID != "{$postID}" )
            }
            ORDER BY DESC( ?postID )
EOF;
        $result = $this->tripleStoreService->query($query);
        $rows =& $result["result"]["rows"];
        $related = array("entities" => array(), "posts" => array());
        $entities =& $related["entities"];
        $posts =& $related["posts"];
        foreach ($rows as $row) {
            if (!array_key_exists($row["postID"], $posts)) {
                $posts[$row["postID"]] = array("entities" => array());
            }
            $post =& $posts[$row["postID"]];
            if (!array_key_exists($row["entity"], $entities)) {
                $entities[$row["entity"]] = array("images" => array(), "names" => array(), "types" => array(), "posts" => array());
            }
            $entity =& $entities[$row["entity"]];
            if (!in_array($row["postID"], $entity["posts"])) {
                $entity["posts"][] = $row["postID"];
            }
            if (!in_array($row["entity"], $post["entities"])) {
                $post["entities"][] = $row["entity"];
            }
            if (!empty($row["image"]) && !in_array($row["image"], $entity["images"])) {
                $entity["images"][] = $row["image"];
            }
            if (!empty($row["name"]) && !in_array($row["name"], $entity["names"])) {
                $entity["names"][] = $row["name"];
            }
            if (!empty($row["type"]) && !in_array($row["type"], $entity["types"])) {
                $entity["types"][] = $row["type"];
            }
        }
        return $related;
    }