private function getClassOntology($class)
 {
     if (isset($this->classOntologyCache[$class])) {
         return $class;
     } else {
         $crudRead = new CrudReadQuery($this->network);
         $classes = array();
         foreach ($this->checkUsingOntologies as $ontology) {
             $classes[] = $class;
         }
         $crudRead->uri($classes)->dataset($this->checkUsingOntologies)->mime('resultset')->send();
         if ($crudRead->isSuccessful()) {
             $resultset = $crudRead->getResultset()->getResultset();
             $this->classOntologyCache[key($resultset)] = key($resultset);
             return $this->classOntologyCache[key($resultset)];
         } else {
             return FALSE;
         }
     }
 }
 private function getTypeOntology($type)
 {
     if (isset($this->typeOntologyCache[$type])) {
         return $type;
     } else {
         $crudRead = new CrudReadQuery($this->network);
         $crudRead->uri($type)->mime('resultset')->send();
         if ($crudRead->isSuccessful()) {
             $resultset = $crudRead->getResultset()->getResultset();
             return key($resultset);
         } else {
             return FALSE;
         }
     }
 }
 private function fixURIReference($unexistingURI, $affectedURI, $dataset)
 {
     $crudRead = new CrudReadQuery($this->network);
     $crudRead->dataset($dataset)->uri($affectedURI)->excludeLinksback()->includeReification()->mime('resultset')->send();
     if ($crudRead->isSuccessful()) {
         $resultset = $crudRead->getResultset()->getResultset();
         // Remove that triple from the record's description
         foreach ($resultset[$dataset][$affectedURI] as $property => $values) {
             if (is_array($values) && $property != 'http://www.w3.org/1999/02/22-rdf-syntax-ns#type') {
                 foreach ($values as $key => $value) {
                     if (isset($value['uri']) && $value['uri'] == $unexistingURI) {
                         unset($resultset[$dataset][$affectedURI][$property][$key]);
                         $rset = new Resultset($this->network);
                         $rset->setResultset($resultset);
                         // Use the CRUD: Update endpoint to do the modifications. That way we will revision all the changes
                         // performed by this fix procedure.
                         $crudUpdate = new CrudUpdateQuery($this->network);
                         $crudUpdate->dataset($dataset)->createRevision()->isPublished()->document($rset->getResultsetRDFN3())->documentMimeIsRdfN3()->send();
                         if ($crudUpdate->isSuccessful()) {
                             cecho('  -> <' . $dataset . '> <' . $affectedURI . '> <' . $property . '> <' . $unexistingURI . "> (fixed)\n", 'LIGHT_BLUE');
                             if (!isset($this->deletedNTriples[$dataset])) {
                                 $this->deletedNTriples[$dataset] = array();
                             }
                             if (!isset($this->deletedNTriples[$dataset][$affectedURI])) {
                                 $this->deletedNTriples[$dataset][$affectedURI] = array();
                             }
                             if (!isset($this->deletedNTriples[$dataset][$affectedURI][$property])) {
                                 $this->deletedNTriples[$dataset][$affectedURI][$property] = array();
                             }
                             $this->deletedNTriples[$dataset][$affectedURI][$property][] = $unexistingURI;
                         } else {
                             cecho("We couldn't update the description of an affected record from the structWSF instance\n", 'YELLOW');
                             $this->errors[] = array('id' => 'URI-EXISTENCE-53', 'type' => 'warning', '');
                         }
                     }
                 }
             }
         }
     } else {
         cecho("We couldn't read the description of an affected record from the structWSF instance\n", 'YELLOW');
         $this->errors[] = array('id' => 'URI-EXISTENCE-52', 'type' => 'warning');
     }
 }
/**
 * Export a Dataset from a structWSF instance
 * 
 * @param mixed $uri URI of the dataset to export
 * @param mixed $structwsf URL of the structWSF network
 * @param mixed $file File where to export the dataset
 * @param mixed $mime Mime to use for the exported dataset file
 * 
 * @return Return FALSE if the dataset couldn't be exported. Return TRUE otherwise.
 */
function exportDataset($uri, $structwsf, $file, $mime)
{
    // Get the number of records in that dataset
    $search = new SearchQuery($structwsf);
    $search->includeAggregates()->items(0)->datasetFilter($uri)->send();
    if ($search->isSuccessful()) {
        $resultset = $search->getResultset()->getResultset();
        $nbResults = 0;
        $slice = 25;
        foreach ($resultset['unspecified'] as $aggr) {
            if ($aggr['type'][0] == Namespaces::$aggr . 'Aggregate' && $aggr[Namespaces::$aggr . 'property'][0]['uri'] == Namespaces::$void . 'Dataset' && $aggr[Namespaces::$aggr . 'object'][0]['uri'] == $uri) {
                $nbResults = $aggr[Namespaces::$aggr . 'count'][0]['value'];
                $prefixes = array();
                @unlink($file);
                for ($i = 0; $i < $nbResults; $i += $slice) {
                    cecho('Exporting records ' . $i . ' to ' . ($i + $slice) . "\n", 'CYAN');
                    $searchExport = new SearchQuery($structwsf);
                    $searchExport->excludeAggregates()->includeAttribute('uri')->items($slice)->page($i)->datasetFilter($uri)->send();
                    $uris = array();
                    $datasets = array();
                    if ($searchExport->isSuccessful()) {
                        $resultsetExport = $searchExport->getResultset();
                        foreach ($resultsetExport->getSubjects() as $subject) {
                            $uris[] = $subject->getUri();
                            $d = $subject->getObjectPropertyValues(Namespaces::$dcterms . 'isPartOf');
                            $datasets[] = $d[0]['uri'];
                        }
                        // Get the full description of the records from the CRUD: Read endpoint
                        $crudRead = new CrudReadQuery($structwsf);
                        $crudRead->dataset($datasets)->uri($uris)->excludeLinksback()->mime($mime)->send();
                        if ($crudRead->isSuccessful()) {
                            $rdf = $crudRead->getResultset();
                            switch ($mime) {
                                case 'application/rdf+n3':
                                    $prefixes = array_merge(getN3Prefixes($rdf), $prefixes);
                                    $rdf = n3RemovePrefixes($rdf);
                                    $rdf = n3RemoveIsPartOf($rdf);
                                    file_put_contents($file, $rdf, FILE_APPEND);
                                    break;
                                case 'application/rdf+xml':
                                    $prefixes = array_merge(getXMLPrefixes($rdf), $prefixes);
                                    $rdf = xmlRemovePrefixes($rdf);
                                    $rdf = xmlRemoveIsPartOf($rdf);
                                    file_put_contents($file, $rdf, FILE_APPEND);
                                    break;
                            }
                        } else {
                            cecho("Error exporting this dataset slice.\n", 'RED');
                        }
                    } else {
                        cecho("Error exporting this dataset slice.\n", 'RED');
                    }
                }
                switch ($mime) {
                    case 'application/rdf+n3':
                        // Prepend the prefixes to the dataset file.
                        $tmpFile = md5(microtime());
                        $first = TRUE;
                        foreach ($prefixes as $prefix => $uri) {
                            if ($first) {
                                exec("echo '@prefix {$prefix}: <{$uri}> .' > /tmp/{$tmpFile}");
                                $first = FALSE;
                            } else {
                                exec("echo '@prefix {$prefix}: <{$uri}> .' >> /tmp/{$tmpFile}");
                            }
                        }
                        exec("echo '\n\n\n' >> /tmp/{$tmpFile}");
                        exec("cat {$file} >> /tmp/{$tmpFile}");
                        exec("cp /tmp/{$tmpFile} {$file}");
                        unlink("/tmp/{$tmpFile}");
                        break;
                    case 'application/rdf+xml':
                        // Prepend the prefixes to the dataset file.
                        $tmpFile = md5(microtime());
                        exec("echo '<?xml version=\"1.0\"?>' > /tmp/{$tmpFile}");
                        exec("echo '<rdf:RDF ' >> /tmp/{$tmpFile}");
                        foreach ($prefixes as $prefix => $uri) {
                            exec("echo 'xmlns:{$prefix}=\"{$uri}\"' >> /tmp/{$tmpFile}");
                        }
                        exec("echo '>' >> /tmp/{$tmpFile}");
                        exec("echo '\n\n\n' >> /tmp/{$tmpFile}");
                        exec("cat {$file} >> /tmp/{$tmpFile}");
                        exec("cp /tmp/{$tmpFile} {$file}");
                        unlink("/tmp/{$tmpFile}");
                        break;
                }
            }
        }
    } else {
        $debugFile = md5(microtime()) . '.error';
        file_put_contents('/tmp/' . $debugFile, var_export($search, TRUE));
        @cecho('Can\'t export dataset ' . $uri . '. ' . $search->getStatusMessage() . $search->getStatusMessageDescription() . "\nDebug file: /tmp/{$debugFile}\n", 'RED');
        return FALSE;
    }
    return TRUE;
}