You are here

function _rdfx_save_vocabulary in RDF Extensions 7.2

Saves the main namespace mapping for a vocabulary graph and the additional namespace mappings as defined in the document.

2 calls to _rdfx_save_vocabulary()
rdfx_admin_namespaces_form_submit in ./rdfx.admin.inc
rdfx_save_terms in ./rdfx.terms.inc
Saves vocabulary terms.

File

./rdfx.terms.inc, line 81
Functions for managing RDF Terms.

Code

function _rdfx_save_vocabulary($main_ns, $main_ns_prefix, $vocabulary) {
  $current_time = REQUEST_TIME;

  // If the vocabulary URI matches the main_ns of a vocabulary source, then
  // this is an update to that record. Otherwise, this is a newly imported
  // source.
  $gid = rdfx_get_gid($main_ns);

  // If there is an existing vocabulary, make sure that the main_ns is in the
  // namespaces array and that the user defined mapping is the last in the
  // array so the prefix reflects the user definition. Also change the
  // vocabulary graph updated date.
  if ($gid) {
    $vocabulary['namespaces'][$main_ns_prefix] = $main_ns;
    db_update('rdfx_vocabulary_graphs')
      ->fields(array(
      'date_updated' => $current_time,
    ))
      ->execute();
  }
  else {

    // @todo If the vocab URI isn't used in any terms, don't add it to ns table.
    // This may happen where multiple files are defining a vocabulary.
    // @todo This should be handled as a transaction in case there is an error
    // in the middle. If there is an error, then there will be an SQL error
    // when the user retries the import.
    // Insert this namespace to get the nsid. The vocabulary_source entry will
    // point to this nsid for the main_ns. We temporarily insert 0 for the gid,
    // then update when we have the real gid.
    $nsid = db_insert('rdfx_namespaces')
      ->fields(array(
      'uri' => $main_ns,
      'prefix' => $main_ns_prefix,
      'gid' => '0',
    ))
      ->execute();
    $gid = db_insert('rdfx_vocabulary_graphs')
      ->fields(array(
      'main_ns' => $nsid,
      'date_created' => $current_time,
      'date_updated' => $current_time,
    ))
      ->execute();
    db_update('rdfx_namespaces')
      ->condition('nsid', $nsid)
      ->fields(array(
      'gid' => $gid,
    ))
      ->execute();
  }

  // Insert/update the vocabulary title.
  if (count($vocabulary['title']) > 0) {
    foreach ($vocabulary['title'] as $langcode => $title) {
      $query = db_merge('rdfx_vocabulary_details')
        ->key(array(
        'gid' => $gid,
        'language' => $langcode,
      ))
        ->fields(array(
        'language' => $langcode,
        'label' => $title,
      ));
      $status = $query
        ->execute();
    }
  }

  // Insert/update the vocabulary description.
  if (count($vocabulary['description']) > 0) {
    foreach ($vocabulary['description'] as $langcode => $description) {
      $query = db_merge('rdfx_vocabulary_details')
        ->key(array(
        'gid' => $gid,
        'language' => $langcode,
      ))
        ->fields(array(
        'language' => $langcode,
        'description' => $description,
      ));
      $status = $query
        ->execute();
    }
  }

  // Insert/update the other namespace mappings used in this vocabulary graph.
  if (count($vocabulary['namespaces']) > 0) {
    foreach ($vocabulary['namespaces'] as $prefix => $namespace) {
      if ($namespace != $main_ns) {
        $query = db_merge('rdfx_namespaces')
          ->key(array(
          'gid' => $gid,
          'uri' => $namespace,
        ))
          ->fields(array(
          'uri' => $namespace,
          'prefix' => $prefix,
          'gid' => $gid,
        ))
          ->updateFields(array(
          'prefix' => $prefix,
        ));
        $status = $query
          ->execute();
      }
    }
  }
  $nsids = rdfx_get_nsids($main_ns);
  return $nsids;
}