Ejemplo n.º 1
0
 /**
  * Add a task to the menu
  * 
  * @param object $task Task name
  * 
  * @return void
  */
 function appendTask($task = null)
 {
     if ($task instanceof Task) {
         $this->task_list[] = $task;
     } else {
         cecho("Given parameter is not a Task object.", "red", "bold");
         echo "\n";
     }
 }
function showDatasets($datasets)
{
    $nb = 0;
    cecho("Datasets: \n", 'WHITE');
    foreach ($datasets as $dataset) {
        $nb++;
        cecho("  ({$nb}) " . $dataset['label'] . ' ' . cecho('(' . $dataset['uri'] . ')', 'CYAN', TRUE) . "\n");
    }
}
Ejemplo n.º 3
0
/**
 * The Procedure for the tuning-primer Shell script
 * 
 * @param Menu $backMenu The Menu for return
 * 
 * @return void
 */
function tuningPrimerProcedure(Menu $backMenu)
{
    $procedure = new Procedure();
    echo "Select a mode:\n\n";
    echo "[1] All (perform all checks) [default]\n";
    echo "[2] Prompt (prompt for login credintials and socket and execution mode)\n";
    echo "[3] Memory (run checks for tunable options which effect memory usage)\n";
    echo "[4] Disk, file (run checks for options which effect i/o performance or file handle limits)\n";
    echo "[5] InnoDB (run InnoDB checks)\n";
    echo "[6] Misc (run checks for that don't categorise" . " well Slow Queries, Binary logs, Used Connections and Worker Threads)\n";
    echo "[7] Banner (show banner info)\n";
    echo "-------------------------------------------------------------------------------\n";
    $choice = "0";
    $procedure->showReturnChoice($choice);
    $qt_mode = $procedure->createQuestion("\nSelected mode: ");
    $mode = $procedure->askQuestion($qt_mode);
    switch ($mode) {
        case "1":
            $mode = "all";
            break;
        case "2":
            $mode = "prompt";
            break;
        case "3":
            $mode = "memory";
            break;
        case "4":
            $mode = "file";
            break;
        case "5":
            $mode = "innodb";
            break;
        case "6":
            $mode = "misc";
            break;
        case "7":
            $mode = "banner";
            break;
        case "":
            $mode = "all";
            break;
        case $choice:
            $procedure->clearScreen();
            $procedure->showMenu($backMenu, true);
        default:
            $procedure->clearScreen();
            cecho("Incorrect input", "red");
            echo "\n";
            setupProcedure($backMenu);
    }
    echo "\n";
    echo shell_exec("sh " . dirname(__FILE__) . "/tuning-primer.sh " . $mode) . "\n";
}
/**
 * Delete a Dataset from a structWSF instance
 * 
 * @param mixed $uri URI of the dataset to delete
 * @param mixed $structwsf URL of the structWSF network
 * 
 * @return Return FALSE if the dataset couldn't be delete. Return TRUE otherwise.
 */
function deleteDataset($uri, $structwsf)
{
    $datasetDelete = new DatasetDeleteQuery($structwsf);
    $datasetDelete->uri($uri)->send();
    if ($datasetDelete->isSuccessful()) {
        cecho("Dataset successfully deleted: {$uri}\n", 'CYAN');
    } else {
        $debugFile = md5(microtime()) . '.error';
        file_put_contents('/tmp/' . $debugFile, var_export($datasetDelete, TRUE));
        @cecho('Can\'t delete dataset ' . $uri . '. ' . $datasetDelete->getStatusMessage() . $datasetDelete->getStatusMessageDescription() . "\nDebug file: /tmp/{$debugFile}\n", 'RED');
        return FALSE;
    }
    return TRUE;
}
/**
 * Save an ontology from a structWSF instance
 * 
 * @param mixed $uri URI of the ontology to save
 * @param mixed $structwsf URL of the structWSF network
 * 
 * @return Return FALSE if the ontology couldn't be saved. Return TRUE otherwise.
 */
function saveOntology($uri, $structwsf, $queryExtension = NULL)
{
    $ontologyUpdate = new OntologyUpdateQuery($structwsf);
    $ontologyUpdate->ontology($uri)->saveOntology()->send($queryExtension !== NULL ? $queryExtension : NULL);
    if ($ontologyUpdate->isSuccessful()) {
        cecho("Ontology successfully saved: {$uri}\n", 'CYAN');
    } else {
        $debugFile = md5(microtime()) . '.error';
        file_put_contents('/tmp/' . $debugFile, var_export($ontologyUpdate, TRUE));
        @cecho('Can\'t save ontology ' . $uri . '. ' . $ontologyUpdate->getStatusMessage() . $ontologyUpdate->getStatusMessageDescription() . "\nDebug file: /tmp/{$debugFile}\n", 'RED');
        return FALSE;
    }
    return TRUE;
}
/**
 * Delete an ontology from a structWSF instance
 * 
 * @param mixed $uri URI of the ontology to delete
 * @param mixed $structwsf URL of the structWSF network
 * 
 * @return Return FALSE if the ontology couldn't be delete. Return TRUE otherwise.
 */
function deleteOntology($uri, $structwsf, $queryExtension = NULL)
{
    $ontologyDelete = new OntologyDeleteQuery($structwsf);
    $ontologyDelete->ontology($uri)->deleteOntology()->send($queryExtension !== NULL ? $queryExtension : NULL);
    if ($ontologyDelete->isSuccessful()) {
        cecho("Ontology successfully deleted: {$uri}\n", 'CYAN');
    } else {
        if (strpos($ontologyDelete->getStatusMessageDescription(), 'WS-ONTOLOGY-DELETE-300') !== FALSE) {
            cecho("{$uri} not currently loaded; skip deletation\n", 'BLUE');
        } else {
            $debugFile = md5(microtime()) . '.error';
            file_put_contents('/tmp/' . $debugFile, var_export($ontologyDelete, TRUE));
            @cecho('Can\'t delete ontology ' . $uri . '. ' . $ontologyDelete->getStatusMessage() . $ontologyDelete->getStatusMessageDescription() . "\nDebug file: /tmp/{$debugFile}\n", 'RED');
            return FALSE;
        }
    }
    return TRUE;
}
Ejemplo n.º 7
0
 /**
  * Dispater for CLI mode.
  * @param array $argv The params of command line.
  * @return bool
  */
 public static function handleCLI($argv)
 {
     $cmd = isset($argv[1]) ? $argv[1] : 'help';
     require CORE_PATH . 'Command.php';
     // Check framework commands.
     if (Command::$cmd() !== false) {
         return true;
     }
     $sCmdClass = ucfirst($cmd);
     $sCmdFile = APP_PATH . 'cmd' . DS . $sCmdClass . '.php';
     if (!file_exists($sCmdFile)) {
         cecho('Invalid Command : ' . $cmd, 'error');
     } else {
         require $sCmdFile;
         $classname = "\\cmd\\" . $sCmdClass;
         $oCmd = new $classname();
         $oCmd->run();
     }
 }
function showLoadedOntologies($ontologies)
{
    $nb = 0;
    cecho("Local Ontologies: \n", 'WHITE');
    foreach ($ontologies['local'] as $ontology) {
        $nb++;
        cecho("  ({$nb}) " . $ontology['label'] . '  ' . cecho('(' . $ontology['uri'] . ')', 'CYAN', TRUE) . '  ' . ($ontology['modified'] ? '  ' . cecho('[modified; not saved]', 'YELLOW', TRUE) : '') . "\n", 'WHITE');
    }
    cecho("\nReference Ontologies: \n", 'WHITE');
    foreach ($ontologies['reference'] as $ontology) {
        $nb++;
        cecho("  ({$nb}) " . $ontology['label'] . '  ' . cecho('(' . $ontology['uri'] . ')', 'CYAN', TRUE) . '  ' . ($ontology['modified'] ? '  ' . cecho('[modified; not saved]', 'YELLOW', TRUE) : '') . "\n", 'WHITE');
    }
    cecho("\nAdministrative Ontologies: \n", 'WHITE');
    foreach ($ontologies['admin'] as $ontology) {
        $nb++;
        cecho("  ({$nb}) " . $ontology['label'] . '  ' . cecho('(' . $ontology['uri'] . ')', 'CYAN', TRUE) . '  ' . ($ontology['modified'] ? '  ' . cecho('[modified; not saved]', 'YELLOW', TRUE) : '') . "\n", 'WHITE');
    }
}
Ejemplo n.º 9
0
 /**
  * Create cmd.
  */
 public static function createCmd()
 {
     global $argv;
     if (!isset($argv[2])) {
         cecho('Pleate input your command name', 'error');
         die;
     }
     $sCmdName = ucfirst($argv[2]);
     $sTemplateFile = CORE_PATH . 'template' . DS . 'cmd.template';
     $sTemplateOrigin = file_get_contents($sTemplateFile);
     $sTemplate = str_replace('{$cmd}', $sCmdName, $sTemplateOrigin);
     $sCmdFile = APP_PATH . 'cmd' . DS . $sCmdName . '.php';
     if (file_exists($sCmdFile)) {
         cecho('The cmd has existed.', 'error');
         die;
     }
     $sCmdPath = APP_PATH . 'cmd';
     if (!is_dir($sCmdPath)) {
         mkdir($sCmdPath);
     }
     $bResult = file_put_contents($sCmdFile, $sTemplate);
     if ($bResult) {
         $sMsg = 'Success!';
         $sMsgTheme = 'notice';
     } else {
         $sMsg = 'Failed!';
         $sMsgTheme = 'error';
     }
     cecho($sMsg, $sMsgTheme);
 }
Ejemplo n.º 10
0
/**
 * The Procedure for the update function
 * 
 * @param Menu $backMenu The Menu for return
 * 
 * @return void
 */
function updateProcedure(Menu $backMenu)
{
    $procedure = new Procedure();
    echo "Action to perform:\n\n";
    echo "[1] Show the update log\n";
    echo "[2] Perform the actual update\n";
    echo "--------------------------------\n";
    $choice = "0";
    $procedure->showReturnChoice($choice);
    $qt_action = $procedure->createQuestion("\nSelected action: ");
    $action = $procedure->askQuestion($qt_action);
    switch ($action) {
        case "1":
            $action = "info";
            break;
        case "2":
            $action = "real";
            break;
        case $choice:
            $procedure->clearScreen();
            $procedure->showMenu($backMenu, true);
        default:
            $procedure->clearScreen();
            cecho("Incorrect input", "red");
            echo "\n";
            setupProcedure($backMenu);
    }
    $qt_revision = $procedure->createQuestion("\nRevision number [default HEAD]: ", "HEAD");
    $revision = $procedure->askQuestion($qt_revision);
    echo "\n";
    update($action, $revision);
}
 public function run()
 {
     cecho("\n\n");
     cecho("Data validation test: " . $this->description . "...\n\n", 'LIGHT_BLUE');
     $sparql = new SparqlQuery($this->network);
     $from = '';
     foreach ($this->checkOnDatasets as $dataset) {
         $from .= 'from named <' . $dataset . '> ';
     }
     foreach ($this->checkUsingOntologies as $ontology) {
         $from .= 'from <' . $ontology . '> ';
     }
     // Get the list of all the datatype properties used within the datasets
     $sparql->mime("application/sparql-results+json")->query('select distinct ?p ?range
                   ' . $from . '
                   where
                   {
                     graph ?g {
                       ?s ?p ?o .
                       filter(!isIRI(?o))                          
                     } 
                     
                     optional
                     {                                                          
                       ?p <http://www.w3.org/2000/01/rdf-schema#range> ?range .
                     }
                   }')->send();
     if ($sparql->isSuccessful()) {
         $results = json_decode($sparql->getResultset(), TRUE);
         if (isset($results['results']['bindings']) && count($results['results']['bindings']) > 0) {
             $datatypeProperties = array();
             $thereAreEmptyRanges = FALSE;
             foreach ($results['results']['bindings'] as $result) {
                 $datatypeProperty = $result['p']['value'];
                 $datatypeProperties[$datatypeProperty] = '';
                 if (isset($result['range'])) {
                     $datatypeProperties[$datatypeProperty] = $result['range']['value'];
                 } else {
                     $thereAreEmptyRanges = TRUE;
                 }
             }
             // Display warnings
             if ($thereAreEmptyRanges) {
                 cecho("The following datatype properties are used to describe records, but their datatype range is not specified in the ontologies. No further checks are performed against these datatype properties, but you may want to define it further and re-run this check:\n", 'YELLOW');
                 foreach ($datatypeProperties as $datatypeProperty => $range) {
                     if (empty($range)) {
                         cecho('  -> object property: ' . $datatypeProperty . "\n", 'YELLOW');
                         $this->errors[] = array('id' => 'DATATYPE-PROPERTIES-DATATYPE-50', 'type' => 'warning', 'datatypeProperty' => $datatypeProperty);
                     }
                 }
             }
             // Now, for each datatype properties that have a datatype range defined,
             // we:
             //
             //  (a) List all values used for a given property
             //  (b) For each of these values, make sure they comply with what is defined in the ontology as
             //      the range of the property
             //  (c) Then make sure that the value is valid according to the validators defined in this Check
             foreach ($datatypeProperties as $datatypeProperty => $range) {
                 // If the range is empty, don't validate anything further
                 // We consider the values of time rdfs:Literal
                 if (!empty($range) && $range != 'http://www.w3.org/2000/01/rdf-schema#Literal') {
                     $values = array();
                     $sparql = new SparqlQuery($this->network);
                     $from = '';
                     foreach ($this->checkOnDatasets as $dataset) {
                         $from .= 'from <' . $dataset . '> ';
                     }
                     $sparql->mime("application/sparql-results+json")->query('select distinct ?value datatype(?value) as ?value_type ?s
                           ' . $from . '
                           where
                           {
                             ?s <' . $datatypeProperty . '> ?value.
                             filter(!isIRI(?value))
                           }')->send();
                     if ($sparql->isSuccessful()) {
                         $results = json_decode($sparql->getResultset(), TRUE);
                         $values = array();
                         if (isset($results['results']['bindings']) && count($results['results']['bindings']) > 0) {
                             foreach ($results['results']['bindings'] as $result) {
                                 $value = $result['value']['value'];
                                 $type = '';
                                 if (isset($result['value_type'])) {
                                     $type = $result['value_type']['value'];
                                 }
                                 $s = '';
                                 if (isset($result['s'])) {
                                     $s = $result['s']['value'];
                                 }
                                 $values[] = array('value' => $value, 'type' => $type, 'affectedRecord' => $s);
                             }
                         }
                         // For each value/type(s), we do validate that the range is valid
                         foreach ($values as $value) {
                             // First, check if we have a type defined for the value. If not, then we infer it is rdfs:Literal
                             if (empty($value['type'])) {
                                 $value['type'] = array('http://www.w3.org/2000/01/rdf-schema#Literal');
                             }
                             // Then, check if the $range and the $value['type'] directly match
                             // Note: Here we check if xsd:string is defined, if it is, then we ignore.
                             //       This is required since if no datatype is specified in the RDF document
                             //       Virtuoso is considering it to be a xsd:string
                             if ($range != $value['type'] && $value['type'] != 'http://www.w3.org/2001/XMLSchema#string') {
                                 // Here we need to take a few exceptions into account.
                                 // Virtuoso does internally change a few defined datatype into xsd:int (or others) when equivalent.
                                 // We have to mute such "false positive" errors
                                 if (!($range == 'http://www.w3.org/2001/XMLSchema#boolean' && $value['type'] == 'http://www.w3.org/2001/XMLSchema#integer' || $range == 'http://www.w3.org/2001/XMLSchema#unsignedByte' && $value['type'] == 'http://www.w3.org/2001/XMLSchema#integer' || $range == 'http://www.w3.org/2001/XMLSchema#nonPositiveInteger' && $value['type'] == 'http://www.w3.org/2001/XMLSchema#integer' || $range == 'http://www.w3.org/2001/XMLSchema#positiveInteger' && $value['type'] == 'http://www.w3.org/2001/XMLSchema#integer' || $range == 'http://www.w3.org/2001/XMLSchema#negativeInteger' && $value['type'] == 'http://www.w3.org/2001/XMLSchema#integer' || $range == 'http://www.w3.org/2001/XMLSchema#unsignedLong' && $value['type'] == 'http://www.w3.org/2001/XMLSchema#integer' || $range == 'http://www.w3.org/2001/XMLSchema#nonNegativeInteger' && $value['type'] == 'http://www.w3.org/2001/XMLSchema#integer' || $range == 'http://www.w3.org/2001/XMLSchema#unsignedShort' && $value['type'] == 'http://www.w3.org/2001/XMLSchema#integer' || $range == 'http://www.w3.org/2001/XMLSchema#unsignedLong' && $value['type'] == 'http://www.w3.org/2001/XMLSchema#decimal')) {
                                     cecho('  -> Datatype property "' . $datatypeProperty . '" doesn\'t match datatype range "' . $range . '" for value \'' . $value['value'] . '\' with defined type \'' . $value['type'] . '\' ' . "\n", 'LIGHT_RED');
                                     // If it doesn't match, then we report an error directly
                                     $this->errors[] = array('id' => 'DATATYPE-PROPERTIES-DATATYPE-100', 'type' => 'error', 'datatypeProperty' => $datatypeProperty, 'expectedDatatype' => $range, 'valueDatatype' => $value['type'], 'value' => $value['value'], 'affectedRecord' => $value['affectedRecord']);
                                     continue;
                                 }
                             }
                             // If then match, then we make sure that the value is valid according to the
                             // internal Check datatype validation tests
                             $datatypeValidationError = FALSE;
                             switch ($range) {
                                 case "http://www.w3.org/2001/XMLSchema#anySimpleType":
                                     if (!$this->validateAnySimpleType($value['value'])) {
                                         $datatypeValidationError = TRUE;
                                     }
                                     break;
                                 case "http://www.w3.org/2001/XMLSchema#base64Binary":
                                     if (!$this->validateBase64Binary($value['value'])) {
                                         $datatypeValidationError = TRUE;
                                     }
                                     break;
                                 case "http://www.w3.org/2001/XMLSchema#boolean":
                                     if (!$this->validateBoolean($value['value'])) {
                                         $datatypeValidationError = TRUE;
                                     }
                                     break;
                                 case "http://www.w3.org/2001/XMLSchema#byte":
                                     if (!$this->validateByte($value['value'])) {
                                         $datatypeValidationError = TRUE;
                                     }
                                     break;
                                 case "http://www.w3.org/2001/XMLSchema#dateTimeStamp":
                                     if (!$this->validateDateTimeStampISO8601($value['value'])) {
                                         $datatypeValidationError = TRUE;
                                     }
                                     break;
                                 case "http://www.w3.org/2001/XMLSchema#dateTime":
                                     if (!$this->validateDateTimeISO8601($value['value'])) {
                                         $datatypeValidationError = TRUE;
                                     }
                                     break;
                                 case "http://www.w3.org/2001/XMLSchema#decimal":
                                     if (!$this->validateDecimal($value['value'])) {
                                         $datatypeValidationError = TRUE;
                                     }
                                     break;
                                 case "http://www.w3.org/2001/XMLSchema#double":
                                     if (!$this->validateDouble($value['value'])) {
                                         $datatypeValidationError = TRUE;
                                     }
                                     break;
                                 case "http://www.w3.org/2001/XMLSchema#float":
                                     if (!$this->validateFloat($value['value'])) {
                                         $datatypeValidationError = TRUE;
                                     }
                                     break;
                                 case "http://www.w3.org/2001/XMLSchema#hexBinary":
                                     if (!$this->validateHexBinary($value['value'])) {
                                         $datatypeValidationError = TRUE;
                                     }
                                     break;
                                 case "http://www.w3.org/2001/XMLSchema#int":
                                     if (!$this->validateInt($value['value'])) {
                                         $datatypeValidationError = TRUE;
                                     }
                                     break;
                                 case "http://www.w3.org/2001/XMLSchema#integer":
                                     if (!$this->validateInteger($value['value'])) {
                                         $datatypeValidationError = TRUE;
                                     }
                                     break;
                                 case "http://www.w3.org/2001/XMLSchema#language":
                                     if (!$this->validateLanguage($value['value'])) {
                                         $datatypeValidationError = TRUE;
                                     }
                                     break;
                                 case "http://www.w3.org/2001/XMLSchema#long":
                                     if (!$this->validateLong($value['value'])) {
                                         $datatypeValidationError = TRUE;
                                     }
                                     break;
                                 case "http://www.w3.org/2001/XMLSchema#Name":
                                     if (!$this->validateName($value['value'])) {
                                         $datatypeValidationError = TRUE;
                                     }
                                     break;
                                 case "http://www.w3.org/2001/XMLSchema#NCName":
                                     if (!$this->validateNCName($value['value'])) {
                                         $datatypeValidationError = TRUE;
                                     }
                                     break;
                                 case "http://www.w3.org/2001/XMLSchema#negativeInteger":
                                     if (!$this->validateNegativeInteger($value['value'])) {
                                         $datatypeValidationError = TRUE;
                                     }
                                     break;
                                 case "http://www.w3.org/2001/XMLSchema#NMTOKEN":
                                     if (!$this->validateNMTOKEN($value['value'])) {
                                         $datatypeValidationError = TRUE;
                                     }
                                     break;
                                 case "http://www.w3.org/2001/XMLSchema#nonNegativeInteger":
                                     if (!$this->validateNonNegativeInteger($value['value'])) {
                                         $datatypeValidationError = TRUE;
                                     }
                                     break;
                                 case "http://www.w3.org/2001/XMLSchema#nonPositiveInteger":
                                     if (!$this->validateNonPositiveInteger($value['value'])) {
                                         $datatypeValidationError = TRUE;
                                     }
                                     break;
                                 case "http://www.w3.org/2001/XMLSchema#normalizedString":
                                     if (!$this->validateNormalizedString($value['value'])) {
                                         $datatypeValidationError = TRUE;
                                     }
                                     break;
                                 case "http://www.w3.org/1999/02/22-rdf-syntax-ns#PlainLiteral":
                                     if (!$this->validatePlainLiteral($value['value'])) {
                                         $datatypeValidationError = TRUE;
                                     }
                                     break;
                                 case "http://www.w3.org/2001/XMLSchema#positiveInteger":
                                     if (!$this->validatePositiveInteger($value['value'])) {
                                         $datatypeValidationError = TRUE;
                                     }
                                     break;
                                 case "http://www.w3.org/2001/XMLSchema#short":
                                     if (!$this->validateShort($value['value'])) {
                                         $datatypeValidationError = TRUE;
                                     }
                                     break;
                                 case "http://www.w3.org/2001/XMLSchema#string":
                                     if (!$this->validateString($value['value'])) {
                                         $datatypeValidationError = TRUE;
                                     }
                                     break;
                                 case "http://www.w3.org/2001/XMLSchema#token":
                                     if (!$this->validateToken($value['value'])) {
                                         $datatypeValidationError = TRUE;
                                     }
                                     break;
                                 case "http://www.w3.org/2001/XMLSchema#unsignedByte":
                                     if (!$this->validateUnsignedByte($value['value'])) {
                                         $datatypeValidationError = TRUE;
                                     }
                                     break;
                                 case "http://www.w3.org/2001/XMLSchema#unsignedInt":
                                     if (!$this->validateUnsignedInt($value['value'])) {
                                         $datatypeValidationError = TRUE;
                                     }
                                     break;
                                 case "http://www.w3.org/2001/XMLSchema#unsignedLong":
                                     if (!$this->validateUnsignedLong($value['value'])) {
                                         $datatypeValidationError = TRUE;
                                     }
                                     break;
                                 case "http://www.w3.org/2001/XMLSchema#unsignedShort":
                                     if (!$this->validateUnsignedShort($value['value'])) {
                                         $datatypeValidationError = TRUE;
                                     }
                                     break;
                                 case "http://www.w3.org/1999/02/22-rdf-syntax-ns#XMLLiteral":
                                     if (!$this->validateXMLLiteral($value['value'])) {
                                         $datatypeValidationError = TRUE;
                                     }
                                     break;
                                 case "http://www.w3.org/2001/XMLSchema#anyURI":
                                     if (!$this->validateAnyURI($value['value'])) {
                                         $datatypeValidationError = TRUE;
                                     }
                                     break;
                                 default:
                                     // Custom type, try to validate it according to the
                                     // description of that custom datatype within the
                                     // ontology
                                     if (!$this->validateCustomDatatype($value['type'], $value['value'])) {
                                         $datatypeValidationError = TRUE;
                                     }
                                     break;
                             }
                             if ($datatypeValidationError) {
                                 cecho('  -> Datatype property "' . $datatypeProperty . '" does match datatype range "' . $range . '" for value \'' . $value['value'] . '\' but an invalid value as been specified ' . "\n", 'LIGHT_RED');
                                 // If it doesn't match, then we report an error directly
                                 $this->errors[] = array('id' => 'DATATYPE-PROPERTIES-DATATYPE-101', 'type' => 'error', 'datatypeProperty' => $datatypeProperty, 'expectedDatatype' => $range, 'valueDatatype' => $value['type'], 'invalidValue' => $value['value'], 'affectedRecord' => $value['affectedRecord']);
                             }
                         }
                     } else {
                         cecho("We couldn't get the list of values for the {$datatypePropety} property\n", 'YELLOW');
                         $this->errors[] = array('id' => 'DATATYPE-PROPERTIES-DATATYPE-52', 'type' => 'warning');
                     }
                 }
             }
         }
     } else {
         cecho("We couldn't get the list of datatype properties from the structWSF instance\n", 'YELLOW');
         $this->errors[] = array('id' => 'DATATYPE-PROPERTIES-DATATYPE-51', '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;
}
Ejemplo n.º 13
0
// Fin création des taches du menu
// Check if PHP file is called with parameters
$command = array_shift($argv);
$task = array_shift($argv);
$task = strtolower($task);
if ($task != "") {
    if ($task != "help") {
        foreach ($mainMenu->getTaskList() as $oneTask) {
            $task2 = substr($oneTask->procedure, 0, strpos($oneTask->procedure, "Procedure"));
            if (strtolower($task2) == $task) {
                if (is_callable($task2 . "Call")) {
                    call_user_func($task2 . "Call", $command, $argv);
                    exit;
                } else {
                    echo "\n";
                    cecho($task2 . "Call is not a callable function.", "red", "bold");
                    echo "\n\n";
                    exit;
                }
            }
        }
    } else {
        echo "\nUsage : {$command} <task> [<params>]\n\n<task>  : task to run\n";
        $task_list = $mainMenu->getTaskList();
        array_shift($task_list);
        foreach ($task_list as $oneTask) {
            if ($oneTask->procedure) {
                $task = strtolower(substr($oneTask->procedure, 0, strpos($oneTask->procedure, "Procedure")));
                echo "  {$task} : " . $oneTask->description . "\n";
            }
        }
Ejemplo n.º 14
0
/**
 * Print information about a script
 * 
 * @param string $info Text to print
 * 
 * @return void
 */
function info_script($info)
{
    cecho(">>info: " . $info, "", "bold");
    echo "\n";
}
function generateStructures($folder, $structwsf, $queryExtension = NULL)
{
    include_once 'getLoadedOntologies.php';
    cecho("Generating derivate ontological structures...\n", 'CYAN');
    $ontologiesClustered = getLoadedOntologies($structwsf);
    $ontologies = array();
    $ontologies = array_merge($ontologies, $ontologiesClustered['local'], $ontologiesClustered['reference'], $ontologiesClustered['admin']);
    // Generate the ironXML schemas
    foreach ($ontologies as $ontology) {
        cecho("Generating ironXML schema of the " . $ontology['label'] . " ontology...\n", 'CYAN');
        $ontologyRead = new OntologyReadQuery($structwsf);
        $ontologyRead->ontology($ontology['uri'])->getIronXMLSchema()->send($queryExtension !== NULL ? $queryExtension : NULL);
        if ($ontologyRead->isSuccessful()) {
            $resultset = $ontologyRead->getResultset()->getResultset();
            $ironXML = $resultset['unspecified'][$ontology['uri']]['http://purl.org/ontology/wsf#serializedIronXMLSchema']['0']['value'];
            cecho("Generated...\n", 'CYAN');
            $filename = substr($ontology['uri'], strripos($ontology['uri'], "/") + 1);
            $filename = substr($filename, 0, strripos($filename, "."));
            file_put_contents(rtrim($folder, '/') . '/' . $filename . '.xml', $ironXML);
            cecho("Saved to: " . rtrim($folder, '/') . '/' . $filename . '.xml' . "\n", 'CYAN');
        } else {
            $debugFile = md5(microtime()) . '.error';
            file_put_contents('/tmp/' . $debugFile, var_export($ontologyRead, TRUE));
            @cecho('Can\'t get the ironXML schema structure for this ontology: ' . $ontology . '. ' . $ontologyRead->getStatusMessage() . $ontologyRead->getStatusMessageDescription() . "\nDebug file: /tmp/{$debugFile}\n", 'RED');
            continue;
        }
    }
    // Generate the ironJSON schemas
    foreach ($ontologies as $ontology) {
        cecho("Generating ironJSON schema of the " . $ontology['label'] . " ontology...\n", 'CYAN');
        $ontologyRead = new OntologyReadQuery($structwsf);
        $ontologyRead->ontology($ontology['uri'])->getIronJsonSchema()->send($queryExtension !== NULL ? $queryExtension : NULL);
        if ($ontologyRead->isSuccessful()) {
            $resultset = $ontologyRead->getResultset()->getResultset();
            $ironJSON = $resultset['unspecified'][$ontology['uri']]['http://purl.org/ontology/wsf#serializedIronJSONSchema']['0']['value'];
            cecho("Generated...\n", 'CYAN');
            $filename = substr($ontology['uri'], strripos($ontology['uri'], "/") + 1);
            $filename = substr($filename, 0, strripos($filename, "."));
            file_put_contents(rtrim($folder, '/') . '/' . $filename . '.json', $ironJSON);
            cecho("Saved to: " . rtrim($folder, '/') . '/' . $filename . '.json' . "\n", 'CYAN');
            // Create the pre-existing JS script to load that schema in a Schema object.
            $ironJSONSchema = 'var ' . $filename . '_schema_srz = ' . $ironJSON . ";";
            $ironJSONSchema .= 'var ' . $filename . '_schema = new Schema(' . $filename . '_schema_srz);';
            // Special handling: make sure to convert all "%5C" characters into "\". This has to be done because
            // of the way the ProcessorXML.php (so, the DOMDocument API) works, and how the xml-encoding currently
            // works. Enventually, we should use the simplexml API in the processorXML script to properly
            // manage the encoding, and decoding of the XML data, *AND* of the HTML entities (it is the HTML
            // entities that are strangely manipulated in the DOMDocument API).
            $ironJSONSchema = str_replace("%5C", '\\', $ironJSONSchema);
            file_put_contents(rtrim($folder, "/") . "/" . $filename . ".js", $ironJSONSchema);
            cecho("Saved to: " . rtrim($folder, '/') . '/' . $filename . '.js' . "\n", 'CYAN');
        } else {
            $debugFile = md5(microtime()) . '.error';
            file_put_contents('/tmp/' . $debugFile, var_export($ontologyRead, TRUE));
            @cecho('Can\'t get the ironJSON schema structure for this ontology: ' . $ontology . '. ' . $ontologyRead->getStatusMessage() . $ontologyRead->getStatusMessageDescription() . "\nDebug file: /tmp/{$debugFile}\n", 'RED');
            continue;
        }
    }
    // Create the JS schema file.
    $schemaJS = ' /* List of attribute URIs that are generally used for labeling entities in different ontologies */
                var prefLabelAttributes = [
                  "http://www.w3.org/2004/02/skos/core#prefLabel",
                  "http://purl.org/ontology/iron#prefLabel",
                  "http://umbel.org/umbel#prefLabel",
                  "http://purl.org/dc/terms/title",
                  "http://purl.org/dc/elements/1.1/title",
                  "http://xmlns.com/foaf/0.1/name",
                  "http://xmlns.com/foaf/0.1/givenName",
                  "http://xmlns.com/foaf/0.1/family_name",
                  "http://www.geonames.org/ontology#name",
                  "http://www.w3.org/2000/01/rdf-schema#label"
                ];

                var altLabelAttributes = [
                  "http://www.w3.org/2004/02/skos/core#altLabel",
                  "http://purl.org/ontology/iron#altLabel",
                  "http://umbel.org/umbel#altLabel",
                ];

                var descriptionAttributes = [
                  "http://purl.org/ontology/iron#description",
                  "http://www.w3.org/2000/01/rdf-schema#comment",
                  "http://purl.org/dc/terms/description",
                  "http://purl.org/dc/elements/1.1/description",
                  "http://www.w3.org/2004/02/skos/core#definition"
                ];


                function Schema(sjson)
                {
                  // Define all prefixes of this resultset
                  this.prefixes = sjson.schema.prefixList;

                  // Unprefixize all URIs of this Schema
                  var resultsetJsonText = JSON.stringify(sjson);

                  for(var prefix in this.prefixes)
                  {
                    if(this.prefixes.hasOwnProperty(prefix))
                    {
                      var pattern = new RegExp(prefix+"_", "igm");
                      resultsetJsonText = resultsetJsonText.replace(pattern, this.prefixes[prefix]);
                    }
                  }

                  sjson = JSON.parse(resultsetJsonText);

                  this.attributes = sjson.schema.attributeList;

                  this.types = sjson.schema.typeList;

                  // Extend all attributes of this schema with additional functions
                  for(var i = 0; i < this.attributes.length; i++)
                  {
                    this.attributes[i].prefixes = this.prefixes;
                  }

                  // Extend all types of this schema with additional functions
                  for(var i = 0; i < this.types.length; i++)
                  {
                    this.types[i].prefixes = this.prefixes;
                  }
                }';
    file_put_contents(rtrim($folder, "/") . "/schema.js", $schemaJS);
    // Generate PHP serialized classes hierarchy
    cecho("Generating PHP serialized classes hierarchy structure file...\n", 'CYAN');
    $ontologyRead = new OntologyReadQuery($structwsf);
    $ontologyRead->getSerializedClassHierarchy()->send($queryExtension !== NULL ? $queryExtension : NULL);
    if ($ontologyRead->isSuccessful()) {
        $resultset = $ontologyRead->getResultset()->getResultset();
        $ironXML = $resultset['unspecified'][""]['http://purl.org/ontology/wsf#serializedClassHierarchy']['0']['value'];
        cecho("Generated...\n", 'CYAN');
        file_put_contents(rtrim($folder, '/') . '/classHierarchySerialized.srz', $ironXML);
        cecho("Saved to: " . rtrim($folder, '/') . '/classHierarchySerialized.srz' . "\n", 'CYAN');
    } else {
        $debugFile = md5(microtime()) . '.error';
        file_put_contents('/tmp/' . $debugFile, var_export($ontologyRead, TRUE));
        @cecho('Can\'t get the PHP serialized classes hierarchy structure file' . $ontologyRead->getStatusMessage() . $ontologyRead->getStatusMessageDescription() . "\nDebug file: /tmp/{$debugFile}\n", 'RED');
    }
    // Generate PHP serialized properties hierarchy
    cecho("Generating PHP serialized properties hierarchy structure file...\n", 'CYAN');
    $ontologyRead = new OntologyReadQuery($structwsf);
    $ontologyRead->getSerializedPropertyHierarchy()->send($queryExtension !== NULL ? $queryExtension : NULL);
    if ($ontologyRead->isSuccessful()) {
        $resultset = $ontologyRead->getResultset()->getResultset();
        $ironXML = $resultset['unspecified'][""]['http://purl.org/ontology/wsf#serializedPropertyHierarchy']['0']['value'];
        cecho("Generated...\n", 'CYAN');
        file_put_contents(rtrim($folder, '/') . '/propertyHierarchySerialized.srz', $ironXML);
        cecho("Saved to: " . rtrim($folder, '/') . '/propertyHierarchySerialized.srz' . "\n", 'CYAN');
    } else {
        $debugFile = md5(microtime()) . '.error';
        file_put_contents('/tmp/' . $debugFile, var_export($ontologyRead, TRUE));
        @cecho('Can\'t get the PHP serialized properties hierarchy structure file' . $ontologyRead->getStatusMessage() . $ontologyRead->getStatusMessageDescription() . "\nDebug file: /tmp/{$debugFile}\n", 'RED');
    }
    cecho("All structures generates!\n", 'CYAN');
}
Ejemplo n.º 16
0
/**
 * To replace a database
 * 
 * @param string $srcLocation Remote location, ie user@host. If localhost, symlink instead of scp
 * @param string $srcDir      Remote directory, ie /var/backup
 * @param string $srcDB       Source database name, ie mediboard
 * @param string $tgtDir      Temporary target directory, ie /tmp
 * @param string $tgtDB       Target database name, ie target_mediboard
 * @param string $restart     [optional] To restart the Mysql server (Warning), ie for InnoDB
 * @param string $safeCopy    [optional] To make a safe copy of existing target database first
 * @param string $mysqlDir    [optional] Directory where databases are stored
 * @param string $port        [optional] SSH port of the target remote location
 * @param string $localCopy   To do a local copy
 * 
 * @return void
 */
function replaceBase($srcLocation, $srcDir, $srcDB, $tgtDir, $tgtDB, $restart, $safeCopy, $mysqlDir, $port, $localCopy)
{
    $currentDir = dirname(__FILE__);
    $event = $currentDir . "/../tmp/monitevent.txt";
    announce_script("Mediboard replace base");
    $restart = false;
    if ($restart === "y") {
        $restart = true;
    }
    if ($port === "") {
        $port = "22";
    }
    $safeCopy = false;
    if ($safeCopy === "y") {
        $safeCopy = true;
    }
    $localCopy = true;
    if ($localCopy === "n") {
        $localCopy = false;
    }
    if ($mysqlDir === "") {
        $mysqlDir = "/var/lib/mysql";
    }
    if ($restart) {
        echo "\n";
        cecho("Warning !!!!!!!!!!!! This will restart the MySQL server", "white", "bold", "red");
        echo "\n\n";
    }
    // MySQL Path
    $path = "/etc/init.d/mysql";
    $mysql_path = "/etc/init.d/mysqld";
    if (file_exists($path)) {
        $mysql_path = $path;
    }
    // Retrieve archive
    $archive = "archive.tar.gz";
    @unlink($tgtDir . "/" . $archive);
    if ($localCopy) {
        $scp = shell_exec("scp " . $srcLocation . ":" . $srcDir . "/" . $srcDB . "-db/" . $srcDB . "-latest.tar.gz " . $tgtDir . "/" . $archive);
        echo $scp . "\n";
        if (!check_errs(file_exists($tgtDir . "/" . $archive), false, "Failed to retrieve remote archive", "Succesfully retrieved remote archive!")) {
            exit(0);
        }
    } else {
        unlink($tgtDir . "/" . $archive);
        $res = symlink($srcDir . "/" . $srcDB . "-db/" . $srcDB . "-latest.tar.gz", $tgtDir . "/" . $archive);
        if (!check_errs($res, false, "Failed to symlink local archive", "Successfully symlinked local archive!")) {
            exit(0);
        }
    }
    // Extract base
    chdir($tgtDir);
    exec("tar -xf " . $archive, $tar, $return_var);
    check_errs($return_var, true, "Failed to extract files", "Succesfully extracted files");
    // Stop MySQL
    if ($restart) {
        exec($mysql_path . " stop", $stop, $return_var);
        check_errs($return_var, true, "Failed to stop mysql", "Succesfully stopped mysql");
    }
    $dir_target = $mysqlDir . "/" . $tgtDB;
    if ($safeCopy) {
        // Copy database
        $DATETIME = date("Y_m_d\\TH_i_s");
        $res = rename($dir_target, $dir_target . "_" . $DATETIME);
        check_errs($res, false, "Failed to move MySQL target directory", "Successfully moved MySQL target directory");
        $res = mkdir($dir_target);
        check_errs($res, false, "Failed to create mysql target directory", "Succesfully created mysql target directory");
        $res = chown($dir_target, "mysql");
        check_errs($res, false, "Failed to change owner", "Succesfully changed owner");
        $res = chgrp($dir_target, "mysql");
        check_errs($res, false, "Failed to change group", "Succesfully changed group");
    } else {
        // Delete files in mediboard database
        $i = 0;
        $tab = array();
        $glob = glob($dir_target . "/*");
        if ($glob) {
            foreach ($glob as $one_file) {
                if ($one_file != "." && $one_file != "..") {
                    $tab[$i] = unlink($one_file);
                    $i++;
                }
            }
            $res = true;
        } else {
            $res = false;
        }
        for ($i = 0; $i < count($tab); $i++) {
            $res = $res && $tab[$i];
        }
        check_errs($res, false, "Failed to delete files", "Successfully deleted files");
    }
    // Move table files
    chdir($srcDB);
    $i = 0;
    $tab2 = array();
    $glob = glob("*");
    if ($glob) {
        foreach ($glob as $one_file) {
            if ($one_file != "." && $one_file != "..") {
                $tab2[$i] = rename($one_file, $dir_target . "/" . $one_file);
                $i++;
            }
        }
        $res = true;
    } else {
        $res = false;
    }
    for ($i = 0; $i < count($tab2); $i++) {
        $res = $res && $tab2[$i];
    }
    check_errs($res, false, "Failed to move files", "Successfully moved files");
    // Change owner and group
    chdir($dir_target);
    $i = 0;
    $tab3 = array();
    $tab4 = array();
    $glob = glob("*");
    if ($glob) {
        foreach ($glob as $one_file) {
            if ($one_file != "." && $one_file != "..") {
                $tab3[$i] = chgrp($one_file, "mysql");
                $tab4[$i] = chown($one_file, "mysql");
                $i++;
            }
        }
        $res = true;
    } else {
        $res = false;
    }
    for ($i = 0; $i < count($tab3); $i++) {
        $res = $res && $tab3[$i] && $tab4[$i];
    }
    check_errs($res, false, "Failed to change owner and group", "Succesfully changed owner and group");
    // Start MySQL
    if ($restart) {
        exec($mysql_path . " start", $start, $return_var);
        check_errs($return_var, true, "Failed to start mysql", "Succesfully started mysql");
    }
    // Cleanup temporary archive
    $res = rrmdir($tgtDir . "/" . $srcDB);
    $res2 = unlink($tgtDir . "/" . $archive);
    check_errs($res && $res2, false, "Failed to delete temporary archive", "Succesfully deleted temporary archive");
    // Write into event file
    if (file_exists($event)) {
        $fic = fopen($event, "a");
    } else {
        $fic = fopen($event, "w");
    }
    fwrite($fic, "\n#" . date('Y-m-d H:i:s'));
    fwrite($fic, "\nreplaceBase: <strong>" . $srcDB . "</strong> to <strong>" . $tgtDB . "</strong>");
    fclose($fic);
}
Ejemplo n.º 17
0
function oecho($lines)
{
    foreach ($lines as $line) {
        cecho($line . "\n", 'BLUE');
    }
}
Ejemplo n.º 18
0
/**
 * The Procedure for the basebackup function
 * 
 * @param Menu $backMenu The Menu for return
 * 
 * @return void
 */
function baseBackupProcedure(Menu $backMenu)
{
    $procedure = new Procedure();
    echo "Method:\n\n";
    echo "[1] Hotcopy\n";
    echo "[2] Dump\n";
    echo "--------------------\n";
    $choice = "0";
    $procedure->showReturnChoice($choice);
    $qt_method = $procedure->createQuestion("\nSelected method: ");
    $method = $procedure->askQuestion($qt_method);
    switch ($method) {
        case "1":
            $method = "hotcopy";
            break;
        case "2":
            $method = "dump";
            break;
        case $choice:
            $procedure->clearScreen();
            $procedure->showMenu($backMenu, true);
            break;
        default:
            $procedure->clearScreen();
            cecho("Incorrect input", "red");
            echo "\n";
            baseBackupProcedure($backMenu);
    }
    $qt_username = $procedure->createQuestion("Username (to access database): ");
    $username = $procedure->askQuestion($qt_username);
    $password = prompt_silent();
    $hostname = "";
    $port = "";
    $qt_DBBackup = $procedure->createQuestion("Database to backup (ie mediboard): ");
    $DBBackup = $procedure->askQuestion($qt_DBBackup);
    $qt_backupPath = $procedure->createQuestion("Backup path (ie /var/backup): ");
    $backupPath = $procedure->askQuestion($qt_backupPath);
    $qt_time = $procedure->createQuestion("Time (in days before removal of files) [default 7]: ", 7);
    $time = $procedure->askQuestion($qt_time);
    if ($method == "hotcopy") {
        $qt_binLog = $procedure->createQuestion("Create a binary log index [y or n, default n]? ", "n");
        $binLog = $procedure->askQuestion($qt_binLog);
    } else {
        $binLog = "";
    }
    $qt_mail = $procedure->createQuestion("Send a mail when diskfull is detected [y or n, default n]? ", "n");
    $mail = $procedure->askQuestion($qt_mail);
    if ($mail == "y") {
        $qt_usernameMail = $procedure->createQuestion("Username (to send a mail): ");
        $usernameMail = $procedure->askQuestion($qt_usernameMail);
        $passwordMail = prompt_silent();
    } else {
        $usernameMail = "";
        $passwordMail = "";
    }
    $qt_lock = $procedure->createQuestion("Lock file path (ie /tmp/lock.txt) (leave blank for no lock file): ", null);
    $lock = $procedure->askQuestion($qt_lock);
    echo "\n";
    baseBackup($method, $username, $password, $hostname, $port, $DBBackup, $backupPath, $time, $binLog, $usernameMail, $passwordMail, $lock);
}
Ejemplo n.º 19
0
 protected function testValidators()
 {
     // test xsd:unsignedInt validator
     if ($this->validateUnsignedInt(0) !== TRUE) {
         cecho("Validator testing issue: xsd:unsignedInt with 4294967296\n", 'RED');
     }
     if ($this->validateUnsignedInt(1) !== TRUE) {
         cecho("Validator testing issue: xsd:unsignedInt with 1\n", 'RED');
     }
     if ($this->validateUnsignedInt(4294967295) !== TRUE) {
         cecho("Validator testing issue: xsd:unsignedInt with 4294967295\n", 'RED');
     }
     if ($this->validateUnsignedInt(-1) !== FALSE) {
         cecho("Validator testing issue: xsd:unsignedInt with -1\n", 'RED');
     }
     if ($this->validateUnsignedInt(4294967296) !== FALSE) {
         cecho("Validator testing issue: xsd:unsignedInt with 4294967296\n", 'RED');
     }
     // test xsd:base64Binary
     if ($this->validateBase64Binary('dGhpcyBpcyBhIHRlc3Q=') !== TRUE) {
         cecho("Validator testing issue: xsd:base64Binary with 'dGhpcyBpcyBhIHRlc3Q='\n", 'RED');
     }
     if ($this->validateBase64Binary('dGhpcyBpcyBhIHRlc3Q-') !== FALSE) {
         cecho("Validator testing issue: xsd:base64Binary with 'dGhpcyBpcyBhIHRlc3Q-'\n", 'RED');
     }
     // test xsd:dateTime
     if ($this->validateDateTimeISO8601('1997') !== TRUE) {
         cecho("Validator testing issue: xsd:dateTime with '1997'\n", 'RED');
     }
     if ($this->validateDateTimeISO8601('1997-07') !== TRUE) {
         cecho("Validator testing issue: xsd:dateTime with '1997-07'\n", 'RED');
     }
     if ($this->validateDateTimeISO8601('1997-07-16') !== TRUE) {
         cecho("Validator testing issue: xsd:dateTime with '1997-07-16'\n", 'RED');
     }
     if ($this->validateDateTimeISO8601('1997-07-16T19:20+01:00') !== TRUE) {
         cecho("Validator testing issue: xsd:dateTime with '1997-07-16T19:20+01:00'\n", 'RED');
     }
     if ($this->validateDateTimeISO8601('1997-07-16T19:20:30+01:00') !== TRUE) {
         cecho("Validator testing issue: xsd:dateTime with '1997-07-16T19:20:30+01:00'\n", 'RED');
     }
     if ($this->validateDateTimeISO8601('1997-07-16T19:20:30.45+01:00') !== TRUE) {
         cecho("Validator testing issue: xsd:dateTime with '1997-07-16T19:20:30.45+01:00'\n", 'RED');
     }
     if ($this->validateDateTimeISO8601('1997-07-') !== FALSE) {
         cecho("Validator testing issue: xsd:dateTime with '1997-07-'\n", 'RED');
     }
     if ($this->validateDateTimeISO8601('19') !== FALSE) {
         cecho("Validator testing issue: xsd:dateTime with '19'\n", 'RED');
     }
     if ($this->validateDateTimeISO8601('1997 06 24') !== FALSE) {
         cecho("Validator testing issue: xsd:dateTime with '1997 06 24'\n", 'RED');
     }
     if ($this->validateDateTimeISO8601('') !== FALSE) {
         cecho("Validator testing issue: xsd:dateTime with ''\n", 'RED');
     }
     // test xsd:dateTimeStamp
     if ($this->validateDateTimeStampISO8601('2004-04-12T13:20:00-05:00') !== TRUE) {
         cecho("Validator testing issue: xsd:dateTimeStamp with '2004-04-12T13:20:00-05:00'\n", 'RED');
     }
     if ($this->validateDateTimeStampISO8601('2004-04-12T13:20:00Z') !== TRUE) {
         cecho("Validator testing issue: xsd:dateTimeStamp with '2004-04-12T13:20:00Z'\n", 'RED');
     }
     if ($this->validateDateTimeStampISO8601('2004-04-12T13:20:00') !== FALSE) {
         cecho("Validator testing issue: xsd:dateTimeStamp with '2004-04-12T13:20:00'\n", 'RED');
     }
     if ($this->validateDateTimeStampISO8601('2004-04-12T13:00Z') !== FALSE) {
         cecho("Validator testing issue: xsd:dateTimeStamp with '2004-04-12T13:00Z'\n", 'RED');
     }
     if ($this->validateDateTimeStampISO8601('2004-04-12Z') !== FALSE) {
         cecho("Validator testing issue: xsd:dateTimeStamp with '2004-04-12Z'\n", 'RED');
     }
     if ($this->validateDateTimeStampISO8601('1997-07-') !== FALSE) {
         cecho("Validator testing issue: xsd:dateTimeStamp with '1997-07-'\n", 'RED');
     }
     if ($this->validateDateTimeStampISO8601('19') !== FALSE) {
         cecho("Validator testing issue: xsd:dateTimeStamp with '19'\n", 'RED');
     }
     if ($this->validateDateTimeStampISO8601('1997 06 24') !== FALSE) {
         cecho("Validator testing issue: xsd:dateTimeStamp with '1997 06 24'\n", 'RED');
     }
     if ($this->validateDateTimeStampISO8601('') !== FALSE) {
         cecho("Validator testing issue: xsd:dateTimeStamp with ''\n", 'RED');
     }
     // test xsd:anyURI
     if ($this->validateAnyURI('http://datypic.com') !== TRUE) {
         cecho("Validator testing issue: xsd:anyURI with 'http://datypic.com'\n", 'RED');
     }
     if ($this->validateAnyURI('mailto:info@datypic.com') !== TRUE) {
         cecho("Validator testing issue: xsd:anyURI with 'mailto:info@datypic.com'\n", 'RED');
     }
     if ($this->validateAnyURI('http://datypic.com/prod.html#shirt') !== TRUE) {
         cecho("Validator testing issue: xsd:anyURI with 'http://datypic.com/prod.html#shirt'\n", 'RED');
     }
     if ($this->validateAnyURI('urn:example:org') !== TRUE) {
         cecho("Validator testing issue: xsd:anyURI with 'urn:example:org'\n", 'RED');
     }
     if ($this->validateAnyURI('http://datypic.com#frag1#frag2') !== FALSE) {
         cecho("Validator testing issue: xsd:anyURI with 'http://datypic.com#frag1#frag2'\n", 'RED');
     }
     if ($this->validateAnyURI('http://datypic.com#f% rag') !== FALSE) {
         cecho("Validator testing issue: xsd:anyURI with 'http://datypic.com#f% rag'\n", 'RED');
     }
     if ($this->validateAnyURI('') !== FALSE) {
         cecho("Validator testing issue: xsd:anyURI with ''\n", 'RED');
     }
     // test xsd:boolean
     if ($this->validateBoolean('true') !== TRUE) {
         cecho("Validator testing issue: xsd:boolean with 'true'\n", 'RED');
     }
     if ($this->validateBoolean('false') !== TRUE) {
         cecho("Validator testing issue: xsd:boolean with 'false'\n", 'RED');
     }
     if ($this->validateBoolean('0') !== TRUE) {
         cecho("Validator testing issue: xsd:boolean with '0'\n", 'RED');
     }
     if ($this->validateBoolean('1') !== TRUE) {
         cecho("Validator testing issue: xsd:boolean with '1'\n", 'RED');
     }
     if ($this->validateBoolean('TRUE') !== FALSE) {
         cecho("Validator testing issue: xsd:boolean with 'TRUE'\n", 'RED');
     }
     if ($this->validateBoolean('T') !== FALSE) {
         cecho("Validator testing issue: xsd:boolean with 'T'\n", 'RED');
     }
     if ($this->validateBoolean('') !== FALSE) {
         cecho("Validator testing issue: xsd:boolean with ''\n", 'RED');
     }
     // test xsd:byte
     if ($this->validateByte('+3') !== TRUE) {
         cecho("Validator testing issue: xsd:byte with '+3'\n", 'RED');
     }
     if ($this->validateByte('122') !== TRUE) {
         cecho("Validator testing issue: xsd:byte with '122'\n", 'RED');
     }
     if ($this->validateByte('0') !== TRUE) {
         cecho("Validator testing issue: xsd:byte with '0'\n", 'RED');
     }
     if ($this->validateByte('-123') !== TRUE) {
         cecho("Validator testing issue: xsd:byte with '-123'\n", 'RED');
     }
     if ($this->validateByte('130') !== FALSE) {
         cecho("Validator testing issue: xsd:byte with '130'\n", 'RED');
     }
     if ($this->validateByte('3.0') !== FALSE) {
         cecho("Validator testing issue: xsd:byte with '3.0'\n", 'RED');
     }
     // test xsd:unsignedByte
     if ($this->validateUnsignedByte('+3') !== TRUE) {
         cecho("Validator testing issue: xsd:unsignedByte with '+3'\n", 'RED');
     }
     if ($this->validateUnsignedByte('122') !== TRUE) {
         cecho("Validator testing issue: xsd:unsignedByte with '122'\n", 'RED');
     }
     if ($this->validateUnsignedByte('0') !== TRUE) {
         cecho("Validator testing issue: xsd:unsignedByte with '0'\n", 'RED');
     }
     if ($this->validateUnsignedByte('-123') !== FALSE) {
         cecho("Validator testing issue: xsd:unsignedByte with '-123'\n", 'RED');
     }
     if ($this->validateUnsignedByte('256') !== FALSE) {
         cecho("Validator testing issue: xsd:unsignedByte with '256'\n", 'RED');
     }
     if ($this->validateUnsignedByte('3.0') !== FALSE) {
         cecho("Validator testing issue: xsd:unsignedByte with '3.0'\n", 'RED');
     }
     // test xsd:decimal
     if ($this->validateDecimal('3.0') !== TRUE) {
         cecho("Validator testing issue: xsd:decimal with '3.0'\n", 'RED');
     }
     if ($this->validateDecimal('-3.0') !== TRUE) {
         cecho("Validator testing issue: xsd:decimal with '-3.0'\n", 'RED');
     }
     if ($this->validateDecimal('+3.5') !== TRUE) {
         cecho("Validator testing issue: xsd:decimal with '+3.5'\n", 'RED');
     }
     if ($this->validateDecimal('.3') !== TRUE) {
         cecho("Validator testing issue: xsd:decimal with '.3'\n", 'RED');
     }
     if ($this->validateDecimal('-.3') !== TRUE) {
         cecho("Validator testing issue: xsd:decimal with '-.3'\n", 'RED');
     }
     if ($this->validateDecimal('0003.0') !== TRUE) {
         cecho("Validator testing issue: xsd:decimal with '0003.0'\n", 'RED');
     }
     if ($this->validateDecimal('3.000') !== TRUE) {
         cecho("Validator testing issue: xsd:decimal with '3.000'\n", 'RED');
     }
     if ($this->validateDecimal('3,5') !== FALSE) {
         cecho("Validator testing issue: xsd:decimal with '3,5'\n", 'RED');
     }
     // test xsd:double
     if ($this->validateDouble('-3E2') !== TRUE) {
         cecho("Validator testing issue: xsd:double with '-3E2'\n", 'RED');
     }
     if ($this->validateDouble('4268.22752E11') !== TRUE) {
         cecho("Validator testing issue: xsd:double with '4268.22752E11'\n", 'RED');
     }
     if ($this->validateDouble('+24.3e-3') !== TRUE) {
         cecho("Validator testing issue: xsd:double with '+24.3e-3'\n", 'RED');
     }
     if ($this->validateDouble('12') !== TRUE) {
         cecho("Validator testing issue: xsd:double with '12'\n", 'RED');
     }
     if ($this->validateDouble('+3.5') !== TRUE) {
         cecho("Validator testing issue: xsd:double with '+3.5'\n", 'RED');
     }
     if ($this->validateDouble('-INF') !== TRUE) {
         cecho("Validator testing issue: xsd:double with '-INF'\n", 'RED');
     }
     if ($this->validateDouble('-0') !== TRUE) {
         cecho("Validator testing issue: xsd:double with '-0'\n", 'RED');
     }
     if ($this->validateDouble('NaN') !== TRUE) {
         cecho("Validator testing issue: xsd:double with 'NaN'\n", 'RED');
     }
     if ($this->validateDouble('-3E2.4') !== FALSE) {
         cecho("Validator testing issue: xsd:double with '-3E2.4'\n", 'RED');
     }
     if ($this->validateDouble('12E') !== FALSE) {
         cecho("Validator testing issue: xsd:double with '12E'\n", 'RED');
     }
     if ($this->validateDouble('NAN') !== FALSE) {
         cecho("Validator testing issue: xsd:double with 'NAN'\n", 'RED');
     }
     // test xsd:float
     if ($this->validateFloat('-3E2') !== TRUE) {
         cecho("Validator testing issue: xsd:float with '-3E2'\n", 'RED');
     }
     if ($this->validateFloat('4268.22752E11') !== TRUE) {
         cecho("Validator testing issue: xsd:float with '4268.22752E11'\n", 'RED');
     }
     if ($this->validateFloat('+24.3e-3') !== TRUE) {
         cecho("Validator testing issue: xsd:float with '+24.3e-3'\n", 'RED');
     }
     if ($this->validateFloat('12') !== TRUE) {
         cecho("Validator testing issue: xsd:float with '12'\n", 'RED');
     }
     if ($this->validateFloat('+3.5') !== TRUE) {
         cecho("Validator testing issue: xsd:float with '+3.5'\n", 'RED');
     }
     if ($this->validateFloat('-INF') !== TRUE) {
         cecho("Validator testing issue: xsd:float with '-INF'\n", 'RED');
     }
     if ($this->validateFloat('-0') !== TRUE) {
         cecho("Validator testing issue: xsd:float with '-0'\n", 'RED');
     }
     if ($this->validateFloat('NaN') !== TRUE) {
         cecho("Validator testing issue: xsd:float with 'NaN'\n", 'RED');
     }
     if ($this->validateFloat('-3E2.4') !== FALSE) {
         cecho("Validator testing issue: xsd:float with '-3E2.4'\n", 'RED');
     }
     if ($this->validateFloat('12E') !== FALSE) {
         cecho("Validator testing issue: xsd:float with '12E'\n", 'RED');
     }
     if ($this->validateFloat('NAN') !== FALSE) {
         cecho("Validator testing issue: xsd:float with 'NAN'\n", 'RED');
     }
     // test xsd:int
     if ($this->validateInt('+3') !== TRUE) {
         cecho("Validator testing issue: xsd:int with '+3'\n", 'RED');
     }
     if ($this->validateInt('122') !== TRUE) {
         cecho("Validator testing issue: xsd:int with '122'\n", 'RED');
     }
     if ($this->validateInt('0') !== TRUE) {
         cecho("Validator testing issue: xsd:int with '0'\n", 'RED');
     }
     if ($this->validateInt('-12312') !== TRUE) {
         cecho("Validator testing issue: xsd:int with '-12312'\n", 'RED');
     }
     if ($this->validateInt('2147483650') !== FALSE) {
         cecho("Validator testing issue: xsd:int with '2147483650'\n", 'RED');
     }
     if ($this->validateInt('-2147483650') !== FALSE) {
         cecho("Validator testing issue: xsd:int with '-2147483650'\n", 'RED');
     }
     if ($this->validateInt('3.0') !== FALSE) {
         cecho("Validator testing issue: xsd:int with '3.0'\n", 'RED');
     }
     // test xsd:integer
     if ($this->validateInteger('+3') !== TRUE) {
         cecho("Validator testing issue: xsd:integer with '+3'\n", 'RED');
     }
     if ($this->validateInteger('122') !== TRUE) {
         cecho("Validator testing issue: xsd:integer with '122'\n", 'RED');
     }
     if ($this->validateInteger('0') !== TRUE) {
         cecho("Validator testing issue: xsd:integer with '0'\n", 'RED');
     }
     if ($this->validateInteger('-12312') !== TRUE) {
         cecho("Validator testing issue: xsd:integer with '-12312'\n", 'RED');
     }
     if ($this->validateInteger('2147483650') !== TRUE) {
         cecho("Validator testing issue: xsd:integer with '2147483650'\n", 'RED');
     }
     if ($this->validateInteger('-2147483650') !== TRUE) {
         cecho("Validator testing issue: xsd:integer with '-2147483650'\n", 'RED');
     }
     if ($this->validateInteger('3.0') !== FALSE) {
         cecho("Validator testing issue: xsd:integer with '3.0'\n", 'RED');
     }
     // test xsd:nonNegativeInteger
     if ($this->validateNonNegativeInteger('+3') !== TRUE) {
         cecho("Validator testing issue: xsd:nonNegativeInteger with '+3'\n", 'RED');
     }
     if ($this->validateNonNegativeInteger('122') !== TRUE) {
         cecho("Validator testing issue: xsd:nonNegativeInteger with '122'\n", 'RED');
     }
     if ($this->validateNonNegativeInteger('0') !== TRUE) {
         cecho("Validator testing issue: xsd:nonNegativeInteger with '0'\n", 'RED');
     }
     if ($this->validateNonNegativeInteger('-3') !== FALSE) {
         cecho("Validator testing issue: xsd:nonNegativeInteger with '-3'\n", 'RED');
     }
     if ($this->validateNonNegativeInteger('3.0') !== FALSE) {
         cecho("Validator testing issue: xsd:nonNegativeInteger with '3.0'\n", 'RED');
     }
     // test xsd:nonPositiveInteger
     if ($this->validateNonPositiveInteger('-3') !== TRUE) {
         cecho("Validator testing issue: xsd:nonPositiveInteger with '-3'\n", 'RED');
     }
     if ($this->validateNonPositiveInteger('0') !== TRUE) {
         cecho("Validator testing issue: xsd:nonPositiveInteger with '0'\n", 'RED');
     }
     if ($this->validateNonPositiveInteger('3') !== FALSE) {
         cecho("Validator testing issue: xsd:nonPositiveInteger with '3'\n", 'RED');
     }
     if ($this->validateNonPositiveInteger('3.0') !== FALSE) {
         cecho("Validator testing issue: xsd:nonPositiveInteger with '3.0'\n", 'RED');
     }
     // test xsd:positiveInteger
     if ($this->validatePositiveInteger('+3') !== TRUE) {
         cecho("Validator testing issue: xsd:positiveInteger with '+3'\n", 'RED');
     }
     if ($this->validatePositiveInteger('122') !== TRUE) {
         cecho("Validator testing issue: xsd:positiveInteger with '122'\n", 'RED');
     }
     if ($this->validatePositiveInteger('1') !== TRUE) {
         cecho("Validator testing issue: xsd:positiveInteger with '1'\n", 'RED');
     }
     if ($this->validatePositiveInteger('0') !== FALSE) {
         cecho("Validator testing issue: xsd:positiveInteger with '0'\n", 'RED');
     }
     if ($this->validatePositiveInteger('-3') !== FALSE) {
         cecho("Validator testing issue: xsd:positiveInteger with '-3'\n", 'RED');
     }
     if ($this->validatePositiveInteger('3.0') !== FALSE) {
         cecho("Validator testing issue: xsd:positiveInteger with '3.0'\n", 'RED');
     }
     // test xsd:negativeInteger
     if ($this->validateNegativeInteger('-3') !== TRUE) {
         cecho("Validator testing issue: xsd:negativeInteger with '-3'\n", 'RED');
     }
     if ($this->validateNegativeInteger('-1') !== TRUE) {
         cecho("Validator testing issue: xsd:negativeInteger with '-1'\n", 'RED');
     }
     if ($this->validateNegativeInteger('0') !== FALSE) {
         cecho("Validator testing issue: xsd:negativeInteger with '0'\n", 'RED');
     }
     if ($this->validateNegativeInteger('3') !== FALSE) {
         cecho("Validator testing issue: xsd:negativeInteger with '3'\n", 'RED');
     }
     if ($this->validateNegativeInteger('3.0') !== FALSE) {
         cecho("Validator testing issue: xsd:negativeInteger with '3.0'\n", 'RED');
     }
     // test xsd:short
     if ($this->validateShort('+3') !== TRUE) {
         cecho("Validator testing issue: xsd:short with '+3'\n", 'RED');
     }
     if ($this->validateShort('122') !== TRUE) {
         cecho("Validator testing issue: xsd:short with '122'\n", 'RED');
     }
     if ($this->validateShort('0') !== TRUE) {
         cecho("Validator testing issue: xsd:short with '0'\n", 'RED');
     }
     if ($this->validateShort('-1213') !== TRUE) {
         cecho("Validator testing issue: xsd:short with '-1213'\n", 'RED');
     }
     if ($this->validateShort('32770') !== FALSE) {
         cecho("Validator testing issue: xsd:short with '32770'\n", 'RED');
     }
     if ($this->validateShort('-32770') !== FALSE) {
         cecho("Validator testing issue: xsd:short with '-32770'\n", 'RED');
     }
     if ($this->validateShort('3.0') !== FALSE) {
         cecho("Validator testing issue: xsd:short with '3.0'\n", 'RED');
     }
     // test xsd:unsignedShort
     if ($this->validateUnsignedShort('+3') !== TRUE) {
         cecho("Validator testing issue: xsd:unsignedShort with '+3'\n", 'RED');
     }
     if ($this->validateUnsignedShort('122') !== TRUE) {
         cecho("Validator testing issue: xsd:unsignedShort with '122'\n", 'RED');
     }
     if ($this->validateUnsignedShort('0') !== TRUE) {
         cecho("Validator testing issue: xsd:unsignedShort with '0'\n", 'RED');
     }
     if ($this->validateUnsignedShort('-121') !== FALSE) {
         cecho("Validator testing issue: xsd:unsignedShort with '-121'\n", 'RED');
     }
     if ($this->validateUnsignedShort('65540') !== FALSE) {
         cecho("Validator testing issue: xsd:unsignedShort with '65540'\n", 'RED');
     }
     if ($this->validateUnsignedShort('3.0') !== FALSE) {
         cecho("Validator testing issue: xsd:unsignedShort with '3.0'\n", 'RED');
     }
     // test xsd:long
     if ($this->validateLong('+3') !== TRUE) {
         cecho("Validator testing issue: xsd:long with '+3'\n", 'RED');
     }
     if ($this->validateLong('122') !== TRUE) {
         cecho("Validator testing issue: xsd:long with '122'\n", 'RED');
     }
     if ($this->validateLong('0') !== TRUE) {
         cecho("Validator testing issue: xsd:long with '0'\n", 'RED');
     }
     if ($this->validateLong('-1231235555') !== TRUE) {
         cecho("Validator testing issue: xsd:long with '-1231235555'\n", 'RED');
     }
     if ($this->validateLong('9223372036854775810') !== FALSE) {
         cecho("Validator testing issue: xsd:long with '9223372036854775810'\n", 'RED');
     }
     if ($this->validateLong('-9223372036854775810') !== FALSE) {
         cecho("Validator testing issue: xsd:long with '-9223372036854775810'\n", 'RED');
     }
     if ($this->validateLong('3.0') !== FALSE) {
         cecho("Validator testing issue: xsd:long with '3.0'\n", 'RED');
     }
     // test xsd:unsignedLong
     if ($this->validateUnsignedLong('+3') !== TRUE) {
         cecho("Validator testing issue: xsd:unsignedLong with '+3'\n", 'RED');
     }
     if ($this->validateUnsignedLong('122') !== TRUE) {
         cecho("Validator testing issue: xsd:unsignedLong with '122'\n", 'RED');
     }
     if ($this->validateUnsignedLong('0') !== TRUE) {
         cecho("Validator testing issue: xsd:unsignedLong with '0'\n", 'RED');
     }
     if ($this->validateUnsignedLong('-123') !== FALSE) {
         cecho("Validator testing issue: xsd:unsignedLong with '-123'\n", 'RED');
     }
     if ($this->validateUnsignedLong('18446744073709551620') !== FALSE) {
         cecho("Validator testing issue: xsd:unsignedLong with '18446744073709551620'\n", 'RED');
     }
     if ($this->validateUnsignedLong('3.0') !== FALSE) {
         cecho("Validator testing issue: xsd:unsignedLong with '3.0'\n", 'RED');
     }
     // test xsd:hexBinary
     if ($this->validateHexBinary('0FB8') !== TRUE) {
         cecho("Validator testing issue: xsd:hexBinary with '0FB8'\n", 'RED');
     }
     if ($this->validateHexBinary('0fb8') !== TRUE) {
         cecho("Validator testing issue: xsd:hexBinary with '0fb8'\n", 'RED');
     }
     if ($this->validateHexBinary('FB8') !== FALSE) {
         cecho("Validator testing issue: xsd:hexBinary with 'FB8'\n", 'RED');
     }
     if ($this->validateHexBinary('0G') !== FALSE) {
         cecho("Validator testing issue: xsd:hexBinary with '0G'\n", 'RED');
     }
     // test xsd:language
     if ($this->validateLanguage('en') !== TRUE) {
         cecho("Validator testing issue: xsd:language with 'en'\n", 'RED');
     }
     if ($this->validateLanguage('en-GB') !== TRUE) {
         cecho("Validator testing issue: xsd:language with 'en-GB'\n", 'RED');
     }
     if ($this->validateLanguage('fr') !== TRUE) {
         cecho("Validator testing issue: xsd:language with 'fr'\n", 'RED');
     }
     if ($this->validateLanguage('de') !== TRUE) {
         cecho("Validator testing issue: xsd:language with 'de'\n", 'RED');
     }
     if ($this->validateLanguage('i-navajo') !== TRUE) {
         cecho("Validator testing issue: xsd:language with 'i-navajo'\n", 'RED');
     }
     if ($this->validateLanguage('x-Newspeak') !== TRUE) {
         cecho("Validator testing issue: xsd:language with 'x-Newspeak'\n", 'RED');
     }
     if ($this->validateLanguage('longerThan8') !== FALSE) {
         cecho("Validator testing issue: xsd:language with 'longerThan8'\n", 'RED');
     }
     // test xsd:Name
     if ($this->validateName('myElement') !== TRUE) {
         cecho("Validator testing issue: xsd:Name with 'myElement'\n", 'RED');
     }
     if ($this->validateName('_my.Element') !== TRUE) {
         cecho("Validator testing issue: xsd:Name with '_my.Element'\n", 'RED');
     }
     if ($this->validateName('my-element') !== TRUE) {
         cecho("Validator testing issue: xsd:Name with 'my-element'\n", 'RED');
     }
     if ($this->validateName('pre:myelement3') !== TRUE) {
         cecho("Validator testing issue: xsd:Name with 'pre:myelement3'\n", 'RED');
     }
     if ($this->validateName('-myelement') !== FALSE) {
         cecho("Validator testing issue: xsd:Name with '-myelement'\n", 'RED');
     }
     if ($this->validateName('3rdElement') !== FALSE) {
         cecho("Validator testing issue: xsd:Name with '3rdElement'\n", 'RED');
     }
     // test xsd:NCName
     if ($this->validateNCName('myElement') !== TRUE) {
         cecho("Validator testing issue: xsd:NCName with 'myElement'\n", 'RED');
     }
     if ($this->validateNCName('_my.Element') !== TRUE) {
         cecho("Validator testing issue: xsd:NCName with '_my.Element'\n", 'RED');
     }
     if ($this->validateNCName('my-element') !== TRUE) {
         cecho("Validator testing issue: xsd:NCName with 'my-element'\n", 'RED');
     }
     if ($this->validateNCName('pre:myelement3') !== FALSE) {
         cecho("Validator testing issue: xsd:NCName with 'pre:myelement3'\n", 'RED');
     }
     if ($this->validateNCName('-myelement') !== FALSE) {
         cecho("Validator testing issue: xsd:NCName with '-myelement'\n", 'RED');
     }
     if ($this->validateNCName('3rdElement') !== FALSE) {
         cecho("Validator testing issue: xsd:NCName with '3rdElement'\n", 'RED');
     }
     // test xsd:NMTOKEN
     if ($this->validateNMTOKEN('ABCD') !== TRUE) {
         cecho("Validator testing issue: xsd:NMTOKEN with 'ABCD'\n", 'RED');
     }
     if ($this->validateNMTOKEN('123_456') !== TRUE) {
         cecho("Validator testing issue: xsd:NMTOKEN with '123_456'\n", 'RED');
     }
     if ($this->validateNMTOKEN('  starts_with_a_space') !== TRUE) {
         cecho("Validator testing issue: xsd:NMTOKEN with '  starts_with_a_space'\n", 'RED');
     }
     if ($this->validateNMTOKEN('contains a space') !== FALSE) {
         cecho("Validator testing issue: xsd:NMTOKEN with 'contains a space'\n", 'RED');
     }
     if ($this->validateNMTOKEN('') !== FALSE) {
         cecho("Validator testing issue: xsd:NMTOKEN with ''\n", 'RED');
     }
     // test xsd:string
     if ($this->validateString('This is a string!') !== TRUE) {
         cecho("Validator testing issue: xsd:string with 'This is a string!'\n", 'RED');
     }
     if ($this->validateString('12.5') !== TRUE) {
         cecho("Validator testing issue: xsd:string with '12.5'\n", 'RED');
     }
     if ($this->validateString('') !== TRUE) {
         cecho("Validator testing issue: xsd:string with ''\n", 'RED');
     }
     if ($this->validateString('PB&amp;J') !== TRUE) {
         cecho("Validator testing issue: xsd:string with 'PB&amp;J'\n", 'RED');
     }
     if ($this->validateString('   Separated   by   3   spaces.') !== TRUE) {
         cecho("Validator testing issue: xsd:string with '   Separated   by   3   spaces.'\n", 'RED');
     }
     if ($this->validateString("This\nis on two lines.") !== TRUE) {
         cecho("Validator testing issue: xsd:string with 'This\nis on two lines.'\n", 'RED');
     }
     if ($this->validateString('AT&T') !== FALSE) {
         cecho("Validator testing issue: xsd:string with 'AT&T'\n", 'RED');
     }
     if ($this->validateString('3 < 4') !== FALSE) {
         cecho("Validator testing issue: xsd:string with '3 < 4'\n", 'RED');
     }
     // test rdf:XMLLiteral
     if ($this->validateXMLLiteral('This is a string!') !== TRUE) {
         cecho("Validator testing issue: rdf:XMLLiteral with 'This is a string!'\n", 'RED');
     }
     if ($this->validateXMLLiteral('12.5') !== TRUE) {
         cecho("Validator testing issue: rdf:XMLLiteral with '12.5'\n", 'RED');
     }
     if ($this->validateXMLLiteral('') !== TRUE) {
         cecho("Validator testing issue: rdf:XMLLiteral with ''\n", 'RED');
     }
     if ($this->validateXMLLiteral('PB&amp;J') !== TRUE) {
         cecho("Validator testing issue: rdf:XMLLiteral with 'PB&amp;J'\n", 'RED');
     }
     if ($this->validateXMLLiteral('   Separated   by   3   spaces.') !== TRUE) {
         cecho("Validator testing issue: rdf:XMLLiteral with '   Separated   by   3   spaces.'\n", 'RED');
     }
     if ($this->validateXMLLiteral("This\nis on two lines.") !== TRUE) {
         cecho("Validator testing issue: rdf:XMLLiteral with 'This\nis on two lines.'\n", 'RED');
     }
     if ($this->validateXMLLiteral('AT&T') !== FALSE) {
         cecho("Validator testing issue: rdf:XMLLiteral with 'AT&T'\n", 'RED');
     }
     if ($this->validateXMLLiteral('3 < 4') !== FALSE) {
         cecho("Validator testing issue: rdf:XMLLiteral with '3 < 4'\n", 'RED');
     }
     // test xsd:token
     if ($this->validateToken('This is a string!') !== TRUE) {
         cecho("Validator testing issue: xsd:token with 'This is a string!'\n", 'RED');
     }
     if ($this->validateToken('12.5') !== TRUE) {
         cecho("Validator testing issue: xsd:token with '12.5'\n", 'RED');
     }
     if ($this->validateToken('') !== TRUE) {
         cecho("Validator testing issue: xsd:token with ''\n", 'RED');
     }
     if ($this->validateToken('PB&amp;J') !== TRUE) {
         cecho("Validator testing issue: xsd:token with 'PB&amp;J'\n", 'RED');
     }
     if ($this->validateToken('   Separated   by   3   spaces.') !== TRUE) {
         cecho("Validator testing issue: xsd:token with '   Separated   by   3   spaces.'\n", 'RED');
     }
     if ($this->validateToken("This\nis on two lines.") !== TRUE) {
         cecho("Validator testing issue: xsd:token with 'This\nis on two lines.'\n", 'RED');
     }
     if ($this->validateToken('AT&T') !== FALSE) {
         cecho("Validator testing issue: xsd:token with 'AT&T'\n", 'RED');
     }
     if ($this->validateToken('3 < 4') !== FALSE) {
         cecho("Validator testing issue: xsd:token with '3 < 4'\n", 'RED');
     }
     // test xsd:normalizedString
     if ($this->validateNormalizedString('This is a string!') !== TRUE) {
         cecho("Validator testing issue: xsd:normalizedString with 'This is a string!'\n", 'RED');
     }
     if ($this->validateNormalizedString('12.5') !== TRUE) {
         cecho("Validator testing issue: xsd:normalizedString with '12.5'\n", 'RED');
     }
     if ($this->validateNormalizedString('') !== TRUE) {
         cecho("Validator testing issue: xsd:normalizedString with ''\n", 'RED');
     }
     if ($this->validateNormalizedString('PB&amp;J') !== TRUE) {
         cecho("Validator testing issue: xsd:normalizedString with 'PB&amp;J'\n", 'RED');
     }
     if ($this->validateNormalizedString('   Separated   by   3   spaces.') !== TRUE) {
         cecho("Validator testing issue: xsd:normalizedString with '   Separated   by   3   spaces.'\n", 'RED');
     }
     if ($this->validateNormalizedString("This\nis on two lines.") !== TRUE) {
         cecho("Validator testing issue: xsd:normalizedString with 'This\nis on two lines.'\n", 'RED');
     }
     if ($this->validateNormalizedString('AT&T') !== FALSE) {
         cecho("Validator testing issue: xsd:normalizedString with 'AT&T'\n", 'RED');
     }
     if ($this->validateNormalizedString('3 < 4') !== FALSE) {
         cecho("Validator testing issue: xsd:normalizedString with '3 < 4'\n", 'RED');
     }
     // test rdf:PlainLiteral
     if ($this->validatePlainLiteral('Family Guy@en') !== TRUE) {
         cecho("Validator testing issue: xsd:normalizedString with 'Family Guy@en'\n", 'RED');
     }
     if ($this->validatePlainLiteral('Family Guy@EN') !== TRUE) {
         cecho("Validator testing issue: xsd:normalizedString with 'Family Guy@EN'\n", 'RED');
     }
     if ($this->validatePlainLiteral('Family Guy@FOX@en') !== TRUE) {
         cecho("Validator testing issue: xsd:normalizedString with 'Family Guy@FOX@en'\n", 'RED');
     }
     if ($this->validatePlainLiteral('Family Guy@') !== TRUE) {
         cecho("Validator testing issue: xsd:normalizedString with 'Family Guy@'\n", 'RED');
     }
     if ($this->validatePlainLiteral('Family Guy@FOX@') !== TRUE) {
         cecho("Validator testing issue: xsd:normalizedString with 'Family Guy@FOX@'\n", 'RED');
     }
     if ($this->validatePlainLiteral('Family Guy') !== FALSE) {
         cecho("Validator testing issue: xsd:normalizedString with 'Family Guy'\n", 'RED');
     }
     if ($this->validatePlainLiteral('Family Guy@12') !== FALSE) {
         cecho("Validator testing issue: xsd:normalizedString with 'Family Guy@12'\n", 'RED');
     }
 }
 private function getAffectedRecords($objectProperty, $value)
 {
     $affectedRecords = array();
     $sparqlAffectedRecords = new SparqlQuery($this->network);
     $from = '';
     foreach ($this->checkOnDatasets as $dataset) {
         $from .= 'from <' . $dataset . '> ';
     }
     $sparqlAffectedRecords->mime("application/sparql-results+json")->query('select distinct ?s
                   ' . $from . '
                   where
                   {
                     ?s <' . $objectProperty . '> <' . $value . '>.
                   }')->send();
     if ($sparqlAffectedRecords->isSuccessful()) {
         $results = json_decode($sparqlAffectedRecords->getResultset(), TRUE);
         if (isset($results['results']['bindings']) && count($results['results']['bindings']) > 0) {
             foreach ($results['results']['bindings'] as $result) {
                 $s = $result['s']['value'];
                 $affectedRecords[] = $s;
             }
         }
     } else {
         cecho("We couldn't get the list of affected records from the structWSF instance\n", 'YELLOW');
         $this->errors[] = array('id' => 'OBJECT-PROPERTIES-RANGE-55', 'type' => 'warning');
     }
     return $affectedRecords;
 }
function defaultConverter($file, $dataset, $setup = array())
{
    cecho("Importing dataset: " . cecho($setup["datasetURI"], 'UNDERSCORE', TRUE) . "\n\n", 'CYAN');
    /*
      We have to split it. The procesure is simple:
      
      (1) we index the big file into a temporary Virtuoso graph
      (2) we get 100 records to index at a time
      (3) we index the records slices using CRUD: Update
      (4) we delete the temporary graph
    */
    $importDataset = rtrim($setup["datasetURI"], '/') . '/import';
    if (isset($dataset['forceReloadSolrIndex']) && strtolower($dataset['forceReloadSolrIndex']) == 'true') {
        $importDataset = $dataset['datasetURI'];
    }
    // Create a connection to the triple store
    $data_ini = parse_ini_file(WebService::$data_ini . "data.ini", TRUE);
    $db = new DBVirtuoso($data_ini["triplestore"]["username"], $data_ini["triplestore"]["password"], $data_ini["triplestore"]["dsn"], $data_ini["triplestore"]["host"]);
    // Check if the dataset is existing, if it doesn't, we try to create it
    $datasetRead = new DatasetReadQuery($setup["targetStructWSF"]);
    $datasetRead->excludeMeta()->uri($setup["datasetURI"])->send(isset($dataset['targetStructWSFQueryExtension']) ? new $dataset['targetStructWSFQueryExtension']() : NULL);
    if (!$datasetRead->isSuccessful()) {
        if ($datasetRead->error->id == 'WS-DATASET-READ-304') {
            // not existing, so we create it
            $datasetCreate = new DatasetCreateQuery($setup["targetStructWSF"]);
            $datasetCreate->creator(isset($dataset['creator']) ? $dataset['creator'] : '')->uri($dataset["datasetURI"])->description(isset($dataset['description']) ? $dataset['description'] : '')->title(isset($dataset['title']) ? $dataset['title'] : '')->globalPermissions(new CRUDPermission(FALSE, TRUE, FALSE, FALSE))->send(isset($dataset['targetStructWSFQueryExtension']) ? new $dataset['targetStructWSFQueryExtension']() : NULL);
            if (!$datasetCreate->isSuccessful()) {
                $debugFile = md5(microtime()) . '.error';
                file_put_contents('/tmp/' . $debugFile, var_export($datasetCreate, TRUE));
                @cecho('Can\'t create the dataset for reloading it. ' . $datasetCreate->getStatusMessage() . $datasetCreate->getStatusMessageDescription() . "\nDebug file: /tmp/{$debugFile}\n", 'RED');
                exit(1);
            } else {
                cecho('Dataset not existing, creating it: ' . $dataset["datasetURI"] . "\n", 'MAGENTA');
            }
        }
    }
    if (isset($dataset['forceReloadSolrIndex']) && strtolower($dataset['forceReloadSolrIndex']) == 'true') {
        cecho('Reloading dataset in Solr: ' . $dataset["datasetURI"] . "\n", 'MAGENTA');
    }
    // If we want to reload the dataset, we first delete it in structWSF
    if (isset($dataset['forceReload']) && strtolower($dataset['forceReload']) == 'true') {
        cecho('Reloading dataset: ' . $dataset["datasetURI"] . "\n", 'MAGENTA');
        // First we get information about the dataset (creator, title, description, etc)
        $datasetRead = new DatasetReadQuery($setup["targetStructWSF"]);
        $datasetRead->excludeMeta()->uri($setup["datasetURI"])->send(isset($dataset['targetStructWSFQueryExtension']) ? new $dataset['targetStructWSFQueryExtension']() : NULL);
        if (!$datasetRead->isSuccessful()) {
            $debugFile = md5(microtime()) . '.error';
            file_put_contents('/tmp/' . $debugFile, var_export($datasetRead, TRUE));
            @cecho('Can\'t read the dataset for reloading it. ' . $datasetRead->getStatusMessage() . $datasetRead->getStatusMessageDescription() . "\nDebug file: /tmp/{$debugFile}\n", 'RED');
            exit(1);
        } else {
            cecho('Dataset description read: ' . $dataset["datasetURI"] . "\n", 'MAGENTA');
            $datasetRecord = $datasetRead->getResultset()->getResultset();
            $datasetRecord = $datasetRecord['unspecified'][$setup["datasetURI"]];
            // Then we delete it
            $datasetDelete = new DatasetDeleteQuery($setup["targetStructWSF"]);
            $datasetDelete->uri($setup["datasetURI"])->send(isset($dataset['targetStructWSFQueryExtension']) ? new $dataset['targetStructWSFQueryExtension']() : NULL);
            if (!$datasetDelete->isSuccessful()) {
                $debugFile = md5(microtime()) . '.error';
                file_put_contents('/tmp/' . $debugFile, var_export($datasetDelete, TRUE));
                @cecho('Can\'t delete the dataset for reloading it. ' . $datasetDelete->getStatusMessage() . $datasetDelete->getStatusMessageDescription() . "\nDebug file: /tmp/{$debugFile}\n", 'RED');
                exit(1);
            } else {
                cecho('Dataset deleted: ' . $dataset["datasetURI"] . "\n", 'MAGENTA');
                // Finally we re-create it
                $datasetCreate = new DatasetCreateQuery($setup["targetStructWSF"]);
                $datasetCreate->creator($datasetRecord[Namespaces::$dcterms . 'creator'][0]['uri'])->uri($setup["datasetURI"])->description($datasetRecord['description'])->title($datasetRecord['prefLabel'])->globalPermissions(new CRUDPermission(FALSE, TRUE, FALSE, FALSE))->send(isset($dataset['targetStructWSFQueryExtension']) ? new $dataset['targetStructWSFQueryExtension']() : NULL);
                if (!$datasetCreate->isSuccessful()) {
                    $debugFile = md5(microtime()) . '.error';
                    file_put_contents('/tmp/' . $debugFile, var_export($datasetCreate, TRUE));
                    @cecho('Can\'t create the dataset for reloading it. ' . $datasetCreate->getStatusMessage() . $datasetCreate->getStatusMessageDescription() . "\nDebug file: /tmp/{$debugFile}\n", 'RED');
                    exit(1);
                } else {
                    cecho('Dataset re-created: ' . $dataset["datasetURI"] . "\n", 'MAGENTA');
                }
            }
        }
        echo "\n";
    }
    // Start by deleting the import graph that may have been left over.
    if (!isset($dataset['forceReloadSolrIndex']) || strtolower($dataset['forceReloadSolrIndex']) == 'false') {
        $sqlQuery = "sparql clear graph <" . $importDataset . ">";
        $resultset = $db->query($sqlQuery);
        if (odbc_error()) {
            cecho("Error: can't delete the graph used for importing the file [" . odbc_errormsg() . "]\n", 'RED');
            return;
        }
        unset($resultset);
        // Import the big file into Virtuoso
        if (stripos($file, ".n3") !== FALSE) {
            $sqlQuery = "DB.DBA.TTLP_MT(file_to_string_output('" . $file . "'),'" . $importDataset . "','" . $importDataset . "')";
        } else {
            $sqlQuery = "DB.DBA.RDF_LOAD_RDFXML_MT(file_to_string_output('" . $file . "'),'" . $importDataset . "','" . $importDataset . "')";
        }
        $resultset = $db->query($sqlQuery);
        if (odbc_error()) {
            cecho("Error: can't import the file: {$file}, into the triple store  [" . odbc_errormsg() . "]\n", 'RED');
            return;
        }
        unset($resultset);
    }
    // count the number of records
    $sparqlQuery = "\n  \n    select count(distinct ?s) as ?nb from <" . $importDataset . ">\n    where\n    {\n      ?s a ?o .\n    }\n  \n  ";
    $resultset = $db->query($db->build_sparql_query($sparqlQuery, array('nb'), FALSE));
    $nb = odbc_result($resultset, 1);
    unset($resultset);
    $nbRecordsDone = 0;
    while ($nbRecordsDone < $nb && $nb > 0) {
        // Create slices of records
        $sparqlQuery = "\n      \n      select ?s ?p ?o (DATATYPE(?o)) as ?otype (LANG(?o)) as ?olang\n      where \n      {\n        {\n          select distinct ?s from <" . $importDataset . "> \n          where \n          {\n            ?s a ?type.\n          } \n          limit " . $setup["sliceSize"] . " \n          offset " . $nbRecordsDone . "\n        } \n        \n        ?s ?p ?o\n      }\n    \n    ";
        $crudCreates = '';
        $crudUpdates = '';
        $crudDeletes = array();
        $rdfDocumentN3 = "";
        $start = microtime_float();
        $currentSubject = "";
        $subjectDescription = "";
        $data_ini = parse_ini_file(WebService::$data_ini . "data.ini", TRUE);
        $ch = curl_init();
        curl_setopt($ch, CURLOPT_URL, $data_ini['triplestore']['host'] . ":" . $data_ini['triplestore']['port'] . "/sparql/");
        curl_setopt($ch, CURLOPT_HTTPHEADER, array("Accept: application/sparql-results+xml"));
        curl_setopt($ch, CURLOPT_FOLLOWLOCATION, 1);
        curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
        curl_setopt($ch, CURLOPT_POST, 1);
        curl_setopt($ch, CURLOPT_POSTFIELDS, "default-graph-uri=" . urlencode($importDataset) . "&query=" . urlencode($sparqlQuery) . "&format=" . urlencode("application/sparql-results+xml") . "&debug=on");
        curl_setopt($ch, CURLOPT_HEADER, TRUE);
        $xml_data = curl_exec($ch);
        if ($xml_data === FALSE) {
        }
        $header = substr($xml_data, 0, strpos($xml_data, "\r\n\r\n"));
        $data = substr($xml_data, strpos($xml_data, "\r\n\r\n") + 4, strlen($xml_data) - (strpos($xml_data, "\r\n\r\n") - 4));
        curl_close($ch);
        $resultset = new SimpleXMLElement($data);
        $crudAction = "create";
        foreach ($resultset->results->result as $result) {
            $s = "";
            $p = "";
            $o = "";
            $olang = "";
            $otype = "";
            foreach ($result->binding as $binding) {
                switch ((string) $binding["name"]) {
                    case "s":
                        $s = (string) $binding->uri;
                        break;
                    case "p":
                        $p = (string) $binding->uri;
                        break;
                    case "o":
                        if ($binding->uri) {
                            $o = (string) $binding->uri;
                        } else {
                            $o = (string) $binding->literal;
                        }
                        break;
                    case "olang":
                        $olang = (string) $binding->literal;
                        break;
                    case "otype":
                        $otype = (string) $binding->uri;
                        break;
                }
            }
            if ($s != $currentSubject) {
                switch (strtolower($crudAction)) {
                    case "update":
                        $crudUpdates .= $subjectDescription;
                        break;
                    case "delete":
                        array_push($crudDeletes, $currentSubject);
                        break;
                    case "create":
                    default:
                        $crudCreates .= $subjectDescription;
                        break;
                }
                $subjectDescription = "";
                $crudAction = "create";
                $currentSubject = $s;
            }
            // Check to see if a "crudAction" property/value has been defined for this record. If not,
            // then we simply consider it as "create"
            if ($p != "http://purl.org/ontology/wsf#crudAction") {
                if ($otype != "" || $olang != "") {
                    if ($olang != "") {
                        $subjectDescription .= "<{$s}> <{$p}> \"\"\"" . n3Encode($o) . "\"\"\"@{$olang} .\n";
                    } elseif ($otype != 'http://www.w3.org/2001/XMLSchema#string') {
                        $subjectDescription .= "<{$s}> <{$p}> \"\"\"" . n3Encode($o) . "\"\"\"^^<{$otype}>.\n";
                    } else {
                        $subjectDescription .= "<{$s}> <{$p}> \"\"\"" . n3Encode($o) . "\"\"\" .\n";
                    }
                } else {
                    $subjectDescription .= "<{$s}> <{$p}> <{$o}> .\n";
                }
            } else {
                switch (strtolower($o)) {
                    case "update":
                        $crudAction = "update";
                        break;
                    case "delete":
                        $crudAction = "delete";
                        break;
                    case "create":
                    default:
                        $crudAction = "create";
                        break;
                }
            }
        }
        // Add the last record that got processed above
        switch (strtolower($crudAction)) {
            case "update":
                $crudUpdates .= $subjectDescription;
                break;
            case "delete":
                array_push($crudDeletes, $currentSubject);
                break;
            case "create":
            default:
                $crudCreates .= $subjectDescription;
                break;
        }
        $end = microtime_float();
        cecho('Create N3 file(s): ' . round($end - $start, 3) . ' seconds' . "\n", 'WHITE');
        unset($resultset);
        if ($crudCreates != "") {
            $crudCreates = "@prefix rdfs: <http://www.w3.org/2000/01/rdf-schema#> .\n\n" . $crudCreates;
            $start = microtime_float();
            $crudCreate = new CrudCreateQuery($dataset["targetStructWSF"]);
            $crudCreate->dataset($dataset["datasetURI"])->documentMimeIsRdfN3()->document($crudCreates)->registeredIp('self');
            if (isset($dataset['forceReloadSolrIndex']) && strtolower($dataset['forceReloadSolrIndex']) == 'true') {
                $crudCreate->enableSearchIndexationMode();
            } else {
                $crudCreate->enableFullIndexationMode();
            }
            $crudCreate->send(isset($dataset['targetStructWSFQueryExtension']) ? new $dataset['targetStructWSFQueryExtension']() : NULL);
            if (!$crudCreate->isSuccessful()) {
                $debugFile = md5(microtime()) . '.error';
                file_put_contents('/tmp/' . $debugFile, var_export($crudCreate, TRUE));
                @cecho('Can\'t commit (CRUD Create) a slice to the target dataset. ' . $crudCreate->getStatusMessage() . $crudCreate->getStatusMessageDescription() . "\nDebug file: /tmp/{$debugFile}\n", 'RED');
            }
            $end = microtime_float();
            if (isset($dataset['forceReloadSolrIndex']) && strtolower($dataset['forceReloadSolrIndex']) == 'true') {
                cecho('Records created in Solr: ' . round($end - $start, 3) . ' seconds' . "\n", 'WHITE');
            } else {
                cecho('Records created in Virtuoso & Solr: ' . round($end - $start, 3) . ' seconds' . "\n", 'WHITE');
            }
            unset($wsq);
        }
        if ($crudUpdates != "") {
            $crudUpdates = "@prefix rdfs: <http://www.w3.org/2000/01/rdf-schema#> .\n\n" . $crudUpdates;
            $start = microtime_float();
            $crudUpdate = new CrudUpdateQuery($dataset["targetStructWSF"]);
            $crudUpdate->dataset($dataset["datasetURI"])->documentMimeIsRdfN3()->document($crudUpdates)->registeredIp('self')->send(isset($dataset['targetStructWSFQueryExtension']) ? new $dataset['targetStructWSFQueryExtension']() : NULL);
            if (!$crudUpdate->isSuccessful()) {
                $debugFile = md5(microtime()) . '.error';
                file_put_contents('/tmp/' . $debugFile, var_export($crudUpdate, TRUE));
                @cecho('Can\'t commit (CRUD Updates) a slice to the target dataset. ' . $crudUpdate->getStatusMessage() . $crudUpdate->getStatusMessageDescription() . "\nDebug file: /tmp/{$debugFile}\n", 'RED');
            }
            $end = microtime_float();
            cecho('Records updated: ' . round($end - $start, 3) . ' seconds' . "\n", 'WHITE');
            unset($wsq);
        }
        if (count($crudDeletes) > 0) {
            $start = microtime_float();
            foreach ($crudDeletes as $uri) {
                $crudDelete = new CrudDeleteQuery($dataset["targetStructWSF"]);
                $crudDelete->dataset($setup["datasetURI"])->uri($uri)->registeredIp('self')->send(isset($dataset['targetStructWSFQueryExtension']) ? new $dataset['targetStructWSFQueryExtension']() : NULL);
                if (!$crudDelete->isSuccessful()) {
                    $debugFile = md5(microtime()) . '.error';
                    file_put_contents('/tmp/' . $debugFile, var_export($crudDelete, TRUE));
                    @cecho('Can\'t commit (CRUD Delete) a record to the target dataset. ' . $crudDelete->getStatusMessage() . $crudDelete->getStatusMessageDescription() . "\nDebug file: /tmp/{$debugFile}\n", 'RED');
                }
            }
            $end = microtime_float();
            cecho('Records deleted: ' . round($end - $start, 3) . ' seconds' . "\n", 'WHITE');
            unset($wsq);
        }
        $nbRecordsDone += $setup["sliceSize"];
        cecho("{$nbRecordsDone}/{$nb} records for file: {$file}\n", 'WHITE');
    }
    // Now check what are the properties and types used in this dataset, check which ones
    // are existing in the ontology, and report the ones that are not defined in the loaded
    // ontologies.
    if (!isset($dataset['forceReloadSolrIndex']) || strtolower($dataset['forceReloadSolrIndex']) == 'false') {
        $usedProperties = array();
        $usedTypes = array();
        // Get used properties
        $sparqlQuery = "\n      \n      select distinct ?p from <" . $importDataset . ">\n      where \n      {\n        ?s ?p ?o .\n      }\n    \n    ";
        $data_ini = parse_ini_file(WebService::$data_ini . "data.ini", TRUE);
        $ch = curl_init();
        curl_setopt($ch, CURLOPT_URL, $data_ini['triplestore']['host'] . ":" . $data_ini['triplestore']['port'] . "/sparql/");
        curl_setopt($ch, CURLOPT_HTTPHEADER, array("Accept: application/sparql-results+xml"));
        curl_setopt($ch, CURLOPT_FOLLOWLOCATION, 1);
        curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
        curl_setopt($ch, CURLOPT_POST, 1);
        curl_setopt($ch, CURLOPT_POSTFIELDS, "default-graph-uri=" . urlencode($importDataset) . "&query=" . urlencode($sparqlQuery) . "&format=" . urlencode("application/sparql-results+xml") . "&debug=on");
        curl_setopt($ch, CURLOPT_HEADER, TRUE);
        $xml_data = curl_exec($ch);
        $header = substr($xml_data, 0, strpos($xml_data, "\r\n\r\n"));
        $data = substr($xml_data, strpos($xml_data, "\r\n\r\n") + 4, strlen($xml_data) - (strpos($xml_data, "\r\n\r\n") - 4));
        curl_close($ch);
        $resultset = new SimpleXMLElement($data);
        foreach ($resultset->results->result as $result) {
            foreach ($result->binding as $binding) {
                switch ((string) $binding["name"]) {
                    case "p":
                        $p = (string) $binding->uri;
                        if (!in_array($p, $usedProperties)) {
                            array_push($usedProperties, $p);
                        }
                        break;
                }
            }
        }
        // Get used types
        $sparqlQuery = "\n      \n      select distinct ?o from <" . $importDataset . ">\n      where \n      {\n        ?s a ?o .\n      }\n    \n    ";
        $data_ini = parse_ini_file(WebService::$data_ini . "data.ini", TRUE);
        $ch = curl_init();
        curl_setopt($ch, CURLOPT_URL, $data_ini['triplestore']['host'] . ":" . $data_ini['triplestore']['port'] . "/sparql/");
        curl_setopt($ch, CURLOPT_HTTPHEADER, array("Accept: application/sparql-results+xml"));
        curl_setopt($ch, CURLOPT_FOLLOWLOCATION, 1);
        curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
        curl_setopt($ch, CURLOPT_POST, 1);
        curl_setopt($ch, CURLOPT_POSTFIELDS, "default-graph-uri=" . urlencode($importDataset) . "&query=" . urlencode($sparqlQuery) . "&format=" . urlencode("application/sparql-results+xml") . "&debug=on");
        curl_setopt($ch, CURLOPT_HEADER, TRUE);
        $xml_data = curl_exec($ch);
        $header = substr($xml_data, 0, strpos($xml_data, "\r\n\r\n"));
        $data = substr($xml_data, strpos($xml_data, "\r\n\r\n") + 4, strlen($xml_data) - (strpos($xml_data, "\r\n\r\n") - 4));
        curl_close($ch);
        $resultset = new SimpleXMLElement($data);
        foreach ($resultset->results->result as $result) {
            foreach ($result->binding as $binding) {
                switch ((string) $binding["name"]) {
                    case "o":
                        $o = (string) $binding->uri;
                        if (!in_array($o, $usedTypes)) {
                            array_push($usedTypes, $o);
                        }
                        break;
                }
            }
        }
        // Now check to make sure that all the predicates and types are in the ontological structure.
        $undefinedPredicates = array();
        $undefinedTypes = array();
        $filename = $setup["ontologiesStructureFiles"] . 'classHierarchySerialized.srz';
        $f = fopen($filename, "r");
        $classHierarchy = fread($f, filesize($filename));
        $classHierarchy = unserialize($classHierarchy);
        fclose($f);
        $filename = $setup["ontologiesStructureFiles"] . 'propertyHierarchySerialized.srz';
        $f = fopen($filename, "r");
        $propertyHierarchy = fread($f, filesize($filename));
        $propertyHierarchy = unserialize($propertyHierarchy);
        fclose($f);
        foreach ($usedProperties as $usedPredicate) {
            $found = FALSE;
            foreach ($propertyHierarchy->properties as $property) {
                if ($property->name == $usedPredicate) {
                    $found = TRUE;
                    break;
                }
            }
            if ($found === FALSE) {
                array_push($undefinedPredicates, $usedPredicate);
            }
        }
        foreach ($usedTypes as $type) {
            $found = FALSE;
            foreach ($classHierarchy->classes as $class) {
                if ($class->name == $type) {
                    $found = TRUE;
                    break;
                }
            }
            if ($found === FALSE) {
                array_push($undefinedTypes, $type);
            }
        }
        $filename = substr($file, strrpos($file, "/") + 1);
        $filename = substr($filename, 0, strlen($filename) - 3);
        file_put_contents($setup["missingVocabulary"] . $filename . ".undefined.types.log", implode("\n", $undefinedTypes));
        file_put_contents($setup["missingVocabulary"] . $filename . ".undefined.predicates.log", implode("\n", $undefinedPredicates));
        // Now delete the graph we used to import the file
        $sqlQuery = "sparql clear graph <" . $importDataset . ">";
        $resultset = $db->query($sqlQuery);
        if (odbc_error()) {
            cecho("Error: can't delete the graph used for importing the file [" . odbc_errormsg() . "]\n", 'RED');
            return;
        }
        unset($resultset);
    }
    $db->close();
    echo "\n";
}
 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');
     }
 }
    public function run()
    {
        cecho("\n\n");
        cecho("Data validation test: " . $this->description . "...\n\n", 'LIGHT_BLUE');
        // First, get the list of all the possible custom datatypes and their possible base datatype
        $customDatatypes = $this->getCustomDatatypes();
        // Check for universal restriction on Datatype Properties
        $sparql = new SparqlQuery($this->network);
        $from = '';
        foreach ($this->checkOnDatasets as $dataset) {
            $from .= 'from <' . $dataset . '> ';
        }
        foreach ($this->checkUsingOntologies as $ontology) {
            $from .= 'from named <' . $ontology . '> ';
        }
        $sparql->mime("application/sparql-results+json")->query('select distinct ?restriction ?class ?onProperty ?dataRange
                      ' . $from . '
                      where
                      {
                        ?s a ?class.

                        graph ?g {
                            ?class <http://www.w3.org/2000/01/rdf-schema#subClassOf> ?restriction .
                            ?restriction a <http://www.w3.org/2002/07/owl#Restriction> ;
                                         <http://www.w3.org/2002/07/owl#onProperty> ?onProperty ;
                                         <http://www.w3.org/2002/07/owl#allValuesFrom> ?dataRange .
                                         
                            ?onProperty a <http://www.w3.org/2002/07/owl#DatatypeProperty> .
                        }
                      }')->send();
        if ($sparql->isSuccessful()) {
            $results = json_decode($sparql->getResultset(), TRUE);
            if (isset($results['results']['bindings']) && count($results['results']['bindings']) > 0) {
                cecho("Here is the list of all the OWL universal restrictions defined for the classes currently used in the target datasets applied on datatype properties:\n\n", 'LIGHT_BLUE');
                $universals = array();
                foreach ($results['results']['bindings'] as $result) {
                    $class = $result['class']['value'];
                    $dataRange = $result['dataRange']['value'];
                    $onProperty = $result['onProperty']['value'];
                    cecho('  -> Record of type "' . $class . '" has an universal restriction  when using datatype property "' . $onProperty . '" with value type "' . $dataRange . '"' . "\n", 'LIGHT_BLUE');
                    if (!isset($universals[$class])) {
                        $universals[$class] = array();
                    }
                    $universals[$class][] = array('onProperty' => $onProperty, 'dataRange' => $dataRange);
                }
                cecho("\n\n");
                foreach ($universals as $class => $univs) {
                    foreach ($univs as $universal) {
                        $sparql = new SparqlQuery($this->network);
                        $from = '';
                        foreach ($this->checkOnDatasets as $dataset) {
                            $from .= 'from <' . $dataset . '> ';
                        }
                        foreach ($this->checkUsingOntologies as $ontology) {
                            $from .= 'from <' . $ontology . '> ';
                        }
                        $datatypeFilter = '';
                        // Here, if rdfs:Literal is used, we have to filter for xsd:string also since it is the default
                        // used by Virtuoso...
                        if (!empty($universal['dataRange'])) {
                            if ($universal['dataRange'] == 'http://www.w3.org/2000/01/rdf-schema#Literal') {
                                $datatypeFilter = 'filter((str(datatype(?value)) != \'http://www.w3.org/2000/01/rdf-schema#Literal\' && str(datatype(?value)) != \'http://www.w3.org/2001/XMLSchema#string\') && ?onProperty in (<' . $universal['onProperty'] . '>))';
                            } else {
                                if (!empty($customDatatypes[$universal['dataRange']])) {
                                    if ($customDatatypes[$universal['dataRange']] === 'http://www.w3.org/2001/XMLSchema#anySimpleType') {
                                        $datatypeFilter = 'filter((str(datatype(?value)) = \'' . $universal['dataRange'] . '\' || str(datatype(?value)) = \'' . $customDatatypes[$universal['dataRange']] . '\' || str(datatype(?value)) = \'http://www.w3.org/2001/XMLSchema#string\') && ?onProperty in (<' . $universal['onProperty'] . '>))';
                                    } else {
                                        $datatypeFilter = 'filter((str(datatype(?value)) != \'' . $universal['dataRange'] . '\' && str(datatype(?value)) != \'' . $customDatatypes[$universal['dataRange']] . '\') && ?onProperty in (<' . $universal['onProperty'] . '>))';
                                    }
                                } else {
                                    $datatypeFilter = 'filter((str(datatype(?value)) != \'' . $universal['dataRange'] . '\') && ?onProperty in (<' . $universal['onProperty'] . '>))';
                                }
                            }
                        }
                        // With this query, we make sure that if we have a universal restriction for that property and class
                        // that all the values uses the specified datatype. Otherwise, we return the ones that doesn't.
                        $sparql->mime("application/sparql-results+json")->query('select ?s
                              ' . $from . '
                              where
                              {
                                ?s a <' . $class . '> .
                                ?s ?onProperty ?value .

                                ' . $datatypeFilter . '
                              }
                             ')->send();
                        if ($sparql->isSuccessful()) {
                            $results = json_decode($sparql->getResultset(), TRUE);
                            if (isset($results['results']['bindings']) && count($results['results']['bindings']) > 0) {
                                foreach ($results['results']['bindings'] as $result) {
                                    $subject = $result['s']['value'];
                                    cecho('  -> record: ' . $subject . "\n", 'LIGHT_RED');
                                    cecho('     -> property: ' . $universal['onProperty'] . "\n", 'LIGHT_RED');
                                    $this->errors[] = array('id' => 'OWL-RESTRICTION-ONLY-100', 'type' => 'error', 'invalidRecordURI' => $subject, 'invalidPropertyURI' => $universal['onProperty'], 'dataRange' => $universal['dataRange']);
                                }
                            }
                        } else {
                            cecho("We couldn't get the number of properties per record from the structWSF instance\n", 'YELLOW');
                            // Error: can't get the number of properties used per record
                            $this->errors[] = array('id' => 'OWL-RESTRICTION-ONLY-51', 'type' => 'warning');
                        }
                        // Now let's make sure that there is at least one triple where the value belongs
                        // to the defined datatype
                        $values = array();
                        if (!empty($universal['dataRange'])) {
                            if ($universal['dataRange'] == 'http://www.w3.org/2000/01/rdf-schema#Literal') {
                                $datatypeFilter = 'filter(str(datatype(?value)) = \'http://www.w3.org/2000/01/rdf-schema#Literal\' || str(datatype(?value)) = \'http://www.w3.org/2001/XMLSchema#string\')';
                            } else {
                                if (!empty($customDatatypes[$universal['dataRange']])) {
                                    if ($customDatatypes[$universal['dataRange']] === 'http://www.w3.org/2001/XMLSchema#anySimpleType') {
                                        $datatypeFilter = 'filter(str(datatype(?value)) = \'' . $universal['dataRange'] . '\' || str(datatype(?value)) = \'' . $customDatatypes[$universal['dataRange']] . '\'  || str(datatype(?value)) = \'http://www.w3.org/2001/XMLSchema#string\')';
                                    } else {
                                        $datatypeFilter = 'filter(str(datatype(?value)) = \'' . $universal['dataRange'] . '\' || str(datatype(?value)) = \'' . $customDatatypes[$universal['dataRange']] . '\' )';
                                    }
                                } else {
                                    $datatypeFilter = 'filter(str(datatype(?value)) = \'' . $universal['dataRange'] . '\')';
                                }
                            }
                        }
                        $sparql = new SparqlQuery($this->network);
                        $from = '';
                        foreach ($this->checkOnDatasets as $dataset) {
                            $from .= 'from <' . $dataset . '> ';
                        }
                        $sparql->mime("application/sparql-results+json")->query('select distinct ?value ?s
                              ' . $from . '
                              where
                              {
                                ?s a <' . $class . '> ;
                                   <' . $universal['onProperty'] . '> ?value.
                                ' . $datatypeFilter . '
                              }')->send();
                        if ($sparql->isSuccessful()) {
                            $results = json_decode($sparql->getResultset(), TRUE);
                            $values = array();
                            if (isset($results['results']['bindings']) && count($results['results']['bindings']) > 0) {
                                foreach ($results['results']['bindings'] as $result) {
                                    $value = $result['value']['value'];
                                    $s = '';
                                    if (isset($result['s'])) {
                                        $s = $result['s']['value'];
                                    }
                                    $values[] = array('value' => $value, 'type' => $universal['dataRange'], 'affectedRecord' => $s);
                                }
                            }
                            // For each value/type(s), we do validate that the range is valid
                            // We just need to get one triple where the value comply with the defined datatype
                            // to have this check validated
                            foreach ($values as $value) {
                                // First, check if we have a type defined for the value. If not, then we infer it is rdfs:Literal
                                if (empty($value['type'])) {
                                    $value['type'] = array('http://www.w3.org/2000/01/rdf-schema#Literal');
                                }
                                // If then match, then we make sure that the value is valid according to the
                                // internal Check datatype validation tests
                                $datatypeValidationError = FALSE;
                                switch ($value['type']) {
                                    case "http://www.w3.org/2001/XMLSchema#anySimpleType":
                                        if (!$this->validateAnySimpleType($value['value'])) {
                                            $datatypeValidationError = TRUE;
                                        }
                                        break;
                                    case "http://www.w3.org/2001/XMLSchema#base64Binary":
                                        if (!$this->validateBase64Binary($value['value'])) {
                                            $datatypeValidationError = TRUE;
                                        }
                                        break;
                                    case "http://www.w3.org/2001/XMLSchema#boolean":
                                        if (!$this->validateBoolean($value['value'])) {
                                            $datatypeValidationError = TRUE;
                                        }
                                        break;
                                    case "http://www.w3.org/2001/XMLSchema#byte":
                                        if (!$this->validateByte($value['value'])) {
                                            $datatypeValidationError = TRUE;
                                        }
                                        break;
                                    case "http://www.w3.org/2001/XMLSchema#dateTimeStamp":
                                        if (!$this->validateDateTimeStampISO8601($value['value'])) {
                                            $datatypeValidationError = TRUE;
                                        }
                                        break;
                                    case "http://www.w3.org/2001/XMLSchema#dateTime":
                                        if (!$this->validateDateTimeISO8601($value['value'])) {
                                            $datatypeValidationError = TRUE;
                                        }
                                        break;
                                    case "http://www.w3.org/2001/XMLSchema#decimal":
                                        if (!$this->validateDecimal($value['value'])) {
                                            $datatypeValidationError = TRUE;
                                        }
                                        break;
                                    case "http://www.w3.org/2001/XMLSchema#double":
                                        if (!$this->validateDouble($value['value'])) {
                                            $datatypeValidationError = TRUE;
                                        }
                                        break;
                                    case "http://www.w3.org/2001/XMLSchema#float":
                                        if (!$this->validateFloat($value['value'])) {
                                            $datatypeValidationError = TRUE;
                                        }
                                        break;
                                    case "http://www.w3.org/2001/XMLSchema#hexBinary":
                                        if (!$this->validateHexBinary($value['value'])) {
                                            $datatypeValidationError = TRUE;
                                        }
                                        break;
                                    case "http://www.w3.org/2001/XMLSchema#int":
                                        if (!$this->validateInt($value['value'])) {
                                            $datatypeValidationError = TRUE;
                                        }
                                        break;
                                    case "http://www.w3.org/2001/XMLSchema#integer":
                                        if (!$this->validateInteger($value['value'])) {
                                            $datatypeValidationError = TRUE;
                                        }
                                        break;
                                    case "http://www.w3.org/2001/XMLSchema#language":
                                        if (!$this->validateLanguage($value['value'])) {
                                            $datatypeValidationError = TRUE;
                                        }
                                        break;
                                    case "http://www.w3.org/2001/XMLSchema#long":
                                        if (!$this->validateLong($value['value'])) {
                                            $datatypeValidationError = TRUE;
                                        }
                                        break;
                                    case "http://www.w3.org/2001/XMLSchema#Name":
                                        if (!$this->validateName($value['value'])) {
                                            $datatypeValidationError = TRUE;
                                        }
                                        break;
                                    case "http://www.w3.org/2001/XMLSchema#NCName":
                                        if (!$this->validateNCName($value['value'])) {
                                            $datatypeValidationError = TRUE;
                                        }
                                        break;
                                    case "http://www.w3.org/2001/XMLSchema#negativeInteger":
                                        if (!$this->validateNegativeInteger($value['value'])) {
                                            $datatypeValidationError = TRUE;
                                        }
                                        break;
                                    case "http://www.w3.org/2001/XMLSchema#NMTOKEN":
                                        if (!$this->validateNMTOKEN($value['value'])) {
                                            $datatypeValidationError = TRUE;
                                        }
                                        break;
                                    case "http://www.w3.org/2001/XMLSchema#nonNegativeInteger":
                                        if (!$this->validateNonNegativeInteger($value['value'])) {
                                            $datatypeValidationError = TRUE;
                                        }
                                        break;
                                    case "http://www.w3.org/2001/XMLSchema#nonPositiveInteger":
                                        if (!$this->validateNonPositiveInteger($value['value'])) {
                                            $datatypeValidationError = TRUE;
                                        }
                                        break;
                                    case "http://www.w3.org/2001/XMLSchema#normalizedString":
                                        if (!$this->validateNormalizedString($value['value'])) {
                                            $datatypeValidationError = TRUE;
                                        }
                                        break;
                                    case "http://www.w3.org/1999/02/22-rdf-syntax-ns#PlainLiteral":
                                        if (!$this->validatePlainLiteral($value['value'])) {
                                            $datatypeValidationError = TRUE;
                                        }
                                        break;
                                    case "http://www.w3.org/2001/XMLSchema#positiveInteger":
                                        if (!$this->validatePositiveInteger($value['value'])) {
                                            $datatypeValidationError = TRUE;
                                        }
                                        break;
                                    case "http://www.w3.org/2001/XMLSchema#short":
                                        if (!$this->validateShort($value['value'])) {
                                            $datatypeValidationError = TRUE;
                                        }
                                        break;
                                    case "http://www.w3.org/2001/XMLSchema#string":
                                        if (!$this->validateString($value['value'])) {
                                            $datatypeValidationError = TRUE;
                                        }
                                        break;
                                    case "http://www.w3.org/2001/XMLSchema#token":
                                        if (!$this->validateToken($value['value'])) {
                                            $datatypeValidationError = TRUE;
                                        }
                                        break;
                                    case "http://www.w3.org/2001/XMLSchema#unsignedByte":
                                        if (!$this->validateUnsignedByte($value['value'])) {
                                            $datatypeValidationError = TRUE;
                                        }
                                        break;
                                    case "http://www.w3.org/2001/XMLSchema#unsignedInt":
                                        if (!$this->validateUnsignedInt($value['value'])) {
                                            $datatypeValidationError = TRUE;
                                        }
                                        break;
                                    case "http://www.w3.org/2001/XMLSchema#unsignedLong":
                                        if (!$this->validateUnsignedLong($value['value'])) {
                                            $datatypeValidationError = TRUE;
                                        }
                                        break;
                                    case "http://www.w3.org/2001/XMLSchema#unsignedShort":
                                        if (!$this->validateUnsignedShort($value['value'])) {
                                            $datatypeValidationError = TRUE;
                                        }
                                        break;
                                    case "http://www.w3.org/1999/02/22-rdf-syntax-ns#XMLLiteral":
                                        if (!$this->validateXMLLiteral($value['value'])) {
                                            $datatypeValidationError = TRUE;
                                        }
                                        break;
                                    case "http://www.w3.org/2001/XMLSchema#anyURI":
                                        if (!$this->validateAnyURI($value['value'])) {
                                            $datatypeValidationError = TRUE;
                                        }
                                        break;
                                    default:
                                        // Custom type, try to validate it according to the
                                        // description of that custom datatype within the
                                        // ontology
                                        if (!$this->validateCustomDatatype($value['type'], $value['value'])) {
                                            $datatypeValidationError = TRUE;
                                        }
                                        break;
                                }
                                if ($datatypeValidationError === TRUE) {
                                    cecho('  -> Couldn\'t validate that this value: "' . $value['value'] . '" belong to the datatype "' . $universal['dataRange'] . '"' . "\n", 'LIGHT_RED');
                                    cecho('     -> Affected record: "' . $value['affectedRecord'] . '"' . "\n", 'YELLOW');
                                    // If it doesn't match, then we report an error directly
                                    $this->errors[] = array('id' => 'OWL-RESTRICTION-ONLY-102', 'type' => 'error', 'datatypeProperty' => $universal['onProperty'], 'expectedDatatype' => $universal['dataRange'], 'invalidValue' => $value['value'], 'affectedRecord' => $value['affectedRecord']);
                                }
                            }
                        } else {
                            cecho("We couldn't get the list of values for the {$datatypePropety} property\n", 'YELLOW');
                            $this->errors[] = array('id' => 'OWL-RESTRICTION-ONLY-54', 'type' => 'warning');
                        }
                    }
                }
                if (count($this->errors) <= 0) {
                    cecho("\n\n  All records respects the universal restrictions cardinality specified in the ontologies...\n\n\n", 'LIGHT_GREEN');
                }
            } else {
                cecho("No classes have any universal restriction cardinality defined in any ontologies. Move on to the next check...\n\n\n", 'LIGHT_GREEN');
            }
        } else {
            cecho("We couldn't get the list of universal restriction from the structWSF instance\n", 'YELLOW');
            // Error: can't get the list of retrictions
            $this->errors[] = array('id' => 'OWL-RESTRICTION-ONLY-50', 'type' => 'warning');
        }
        // Check for universal restriction on Object Properties
        $sparql = new SparqlQuery($this->network);
        $from = '';
        foreach ($this->checkOnDatasets as $dataset) {
            $from .= 'from <' . $dataset . '> ';
        }
        foreach ($this->checkUsingOntologies as $ontology) {
            $from .= 'from named <' . $ontology . '> ';
        }
        $sparql->mime("application/sparql-results+json")->query('select distinct ?restriction ?class ?onProperty ?classExpression
                      ' . $from . '
                      where
                      {
                        ?s a ?class.

                        graph ?g {
                            ?class <http://www.w3.org/2000/01/rdf-schema#subClassOf> ?restriction .
                            ?restriction a <http://www.w3.org/2002/07/owl#Restriction> ;
                                         <http://www.w3.org/2002/07/owl#onProperty> ?onProperty ;
                                         <http://www.w3.org/2002/07/owl#allValuesFrom> ?classExpression .                                            
                                         
                            ?onProperty a <http://www.w3.org/2002/07/owl#ObjectProperty> .
                        }
                      }')->send();
        if ($sparql->isSuccessful()) {
            $results = json_decode($sparql->getResultset(), TRUE);
            if (isset($results['results']['bindings']) && count($results['results']['bindings']) > 0) {
                cecho("Here is the list of all the OWL universal restrictions defined for the classes currently used in the target datasets applied on object properties:\n\n", 'LIGHT_BLUE');
                $universals = array();
                foreach ($results['results']['bindings'] as $result) {
                    $class = $result['class']['value'];
                    $onProperty = $result['onProperty']['value'];
                    $classExpression = $result['classExpression']['value'];
                    cecho('  -> Record of type "' . $class . '" have an universal restriction when using object property "' . $onProperty . '"' . "\n", 'LIGHT_BLUE');
                    if (!isset($universals[$class])) {
                        $universals[$class] = array();
                    }
                    $universals[$class][] = array('onProperty' => $onProperty, 'classExpression' => $classExpression);
                }
                cecho("\n\n");
                foreach ($universals as $class => $univs) {
                    foreach ($univs as $universal) {
                        $sparql = new SparqlQuery($this->network);
                        $from = '';
                        foreach ($this->checkOnDatasets as $dataset) {
                            $from .= 'from <' . $dataset . '> ';
                        }
                        foreach ($this->checkUsingOntologies as $ontology) {
                            $from .= 'from <' . $ontology . '> ';
                        }
                        $classExpressionFilter = '';
                        if (!empty($universal['classExpression']) && $universal['classExpression'] != 'http://www.w3.org/2002/07/owl#Thing') {
                            $subClasses = array($universal['classExpression']);
                            // Get all the classes that belong to the class expression defined in this restriction
                            $getSubClassesFunction = new GetSubClassesFunction();
                            $getSubClassesFunction->getClassesUris()->allSubClasses()->uri($universal['classExpression']);
                            $ontologyRead = new OntologyReadQuery($this->network);
                            $ontologyRead->ontology($this->getClassOntology($universal['classExpression']))->getSubClasses($getSubClassesFunction)->enableReasoner()->mime('resultset')->send();
                            if ($ontologyRead->isSuccessful()) {
                                $resultset = $ontologyRead->getResultset()->getResultset();
                                $subClasses = array_merge($subClasses, array_keys($resultset['unspecified']));
                                if (!empty($subClasses)) {
                                    $filter = '';
                                    foreach ($subClasses as $subClass) {
                                        $filter .= ' str(?value_type) = "' . $subClass . '" ||';
                                    }
                                    $classExpressionFilter .= " filter(" . trim(trim($filter, '||')) . ") \n";
                                }
                            } else {
                                cecho("We couldn't get sub-classes of class expression from the structWSF instance\n", 'YELLOW');
                                // Error: can't get the subclasses of a target ontology
                                $this->errors[] = array('id' => 'OWL-RESTRICTION-ONLY-53', 'type' => 'warning');
                            }
                        }
                        $sparql->mime("application/sparql-results+json")->query('select ?s
                              ' . $from . '
                              where
                              {
                                ?s a <' . $class . '> .

                                ?s <' . $universal['onProperty'] . '> ?value .

                                filter not exists { 
                                  ?value a ?value_type .
                                  ' . $classExpressionFilter . '
                                }                              
                              }
                             ')->send();
                        if ($sparql->isSuccessful()) {
                            $results = json_decode($sparql->getResultset(), TRUE);
                            if (isset($results['results']['bindings']) && count($results['results']['bindings']) > 0) {
                                foreach ($results['results']['bindings'] as $result) {
                                    $subject = $result['s']['value'];
                                    cecho('  -> record: ' . $subject . "\n", 'LIGHT_RED');
                                    cecho('     -> property: ' . $universal['onProperty'] . "\n", 'LIGHT_RED');
                                    $this->errors[] = array('id' => 'OWL-RESTRICTION-ONLY-101', 'type' => 'error', 'invalidRecordURI' => $subject, 'invalidPropertyURI' => $universal['onProperty'], 'classExpression' => $universal['classExpression']);
                                }
                            }
                        } else {
                            cecho("We couldn't check if the universal restriction was respected from the structWSF instance\n", 'YELLOW');
                            $this->errors[] = array('id' => 'OWL-RESTRICTION-ONLY-53', 'type' => 'warning');
                        }
                    }
                    if (count($this->errors) <= 0) {
                        cecho("\n\n  All records respects the universal restrictions specified in the ontologies...\n\n\n", 'LIGHT_GREEN');
                    }
                }
            }
        } else {
            cecho("We couldn't get the number of object properties used per record from the structWSF instance\n", 'YELLOW');
            // Error: can't get the number of object properties used per record
            $this->errors[] = array('id' => 'OWL-RESTRICTION-ONLY-52', 'type' => 'warning');
        }
    }
Ejemplo n.º 24
0
 /**
  * Show the Menu (its list of tasks)
  * 
  * @param Menu $menu The Menu to show
  * @param bool $zero [optional] If true, we also show the Task with the ID 0, which is almost "Quit"
  * 
  * @return void
  */
 function showMenu(Menu $menu, $zero = false)
 {
     $this->present($menu->name);
     echo "\nSelect a task:\n\n";
     $menu->showTasks();
     if ($zero) {
         echo "-------------------------------------------------------\n";
         echo "[ 0] " . $menu->task_list[0]->description . "\n";
     }
     // Waiting for input
     $task = recup("\nSelected task: ");
     $success = false;
     foreach ($menu->task_list as $key => $oneTask) {
         if ("{$key}" === $task) {
             if (is_callable($oneTask->procedure)) {
                 $success = true;
                 $this->clearScreen();
                 $oneTask->presentTask();
                 call_user_func($oneTask->procedure, $menu);
             } else {
                 if ($oneTask->procedure === "quit") {
                     $this->quit();
                 }
             }
             break;
         }
     }
     if (!$success) {
         $this->clearScreen();
         cecho("Incorrect input", "red");
         echo "\n";
     }
     echo "\n";
     $this->showMenu($menu, true);
 }