You are here

function ARC_sparql2sql_rewriter::parse_triples_pattern in Taxonomy import/export via XML 5

Same name and namespace in other branches
  1. 5.2 arc/ARC_sparql2sql_rewriter.php \ARC_sparql2sql_rewriter::parse_triples_pattern()
  2. 6.2 arc/ARC_sparql2sql_rewriter.php \ARC_sparql2sql_rewriter::parse_triples_pattern()
  3. 6 arc/ARC_sparql2sql_rewriter.php \ARC_sparql2sql_rewriter::parse_triples_pattern()

File

arc/ARC_sparql2sql_rewriter.php, line 983

Class

ARC_sparql2sql_rewriter

Code

function parse_triples_pattern($args = "") {
  $result = "";
  $pattern = $args["pattern"];
  if (is_array($args)) {
    foreach ($args as $k => $v) {
      ${$k} = $v;
    }
  }
  if ($triples = $pattern["triples"]) {
    foreach ($triples as $cur_t) {
      if (!$in_union) {
        $this->t_count++;
        $cur_t_count = $this->t_count;
      }
      else {
        $this->union_t_counts[$union_branch_id]++;
        $cur_t_count = $this->union_t_counts[$union_branch_id];
      }
      $tbl_alias = "T" . $cur_t_count;
      if ($in_optional && !in_array($cur_t_count, $this->optional_t_counts)) {
        $this->optional_t_counts[] = $cur_t_count;
        $this->logs[] = "adding " . $cur_t_count . " to optional_t_counts";
      }
      if ($in_graph) {
        $this->graphs[$cur_t_count] = $graph;
        if ($graph["type"] == "var") {
          $graph_val = $graph["val"];
          if (!isset($this->graph_term2alias[$graph_val])) {
            $this->graph_term2alias[$graph_val] = array();
          }
          $this->graph_term2alias[$graph_val][] = array(
            "alias" => $cur_t_count,
            "col" => "g",
          );
        }
      }
      elseif (!in_array($cur_t_count, $this->non_graph_aliases)) {
        $this->non_graph_aliases[] = $cur_t_count;
      }
      $this->alias_prop_infos[$cur_t_count] = array(
        "in_graph" => $in_graph,
      );
      foreach (array(
        "s",
        "p",
        "o",
      ) as $cur_col) {
        $cur_term = $cur_t[$cur_col];
        $cur_term_type = $cur_term["type"];
        $cur_term_val = $cur_term["val"];
        $cur_term_lang = isset($cur_term["lang"]) ? $cur_term["lang"] : "";
        $cur_term_dt = isset($cur_term["dt"]) ? $cur_term["dt"] : "";
        $sub_result = "";
        if ($cur_col == "p") {
          $this->alias_prop_infos[$cur_t_count]["p_term_type"] = $cur_term_type;
          $this->alias_prop_infos[$cur_t_count]["p_term_val"] = $cur_term_val;
        }
        if ($cur_col == "o") {
          $this->alias_prop_infos[$cur_t_count]["o_term_type"] = $cur_term_type;
          $this->alias_prop_infos[$cur_t_count]["o_term_val"] = $cur_term_val;
        }
        if ($cur_term_type == "iri") {
          $sub_result .= "(\n" . $ind;
          $sub_result .= " " . $tbl_alias . "." . $cur_col . "=" . $this->api
            ->get_id($cur_term_val, 1);
          $sub_result .= " /* " . str_replace("*", "", str_replace("#", "::", htmlspecialchars($cur_term_val))) . " */ ";

          /* iri alternatives expansion */
          if (isset($this->iri_alts) && isset($this->iri_alts[$cur_term_val]) && ($iri_alts = $this->iri_alts[$cur_term_val])) {
            foreach ($iri_alts as $cur_iri_alt) {
              $sub_result .= "\n" . $ind . "OR ";
              $sub_result .= $tbl_alias . "." . $cur_col . "=" . $this->api
                ->get_id($cur_iri_alt, 1);
              $sub_result .= " /* " . str_replace("*", "", str_replace("#", "::", htmlspecialchars($cur_iri_alt))) . " */ ";
            }
          }
          $sub_result .= "\n" . $ind . ")";
        }
        elseif ($cur_term_type == "literal") {
          $sub_result .= "(\n" . $ind;
          $sub_result .= " " . $tbl_alias . "." . $cur_col . "=" . $this->api
            ->get_id($cur_term_val, 1);
          $sub_result .= " /* " . str_replace("*", "", str_replace("#", "::", htmlspecialchars($cur_term_val))) . " */ ";
          if ($cur_col == "o") {
            if ($cur_term_lang) {
              $sub_result .= "\n" . $ind . " AND ";
              $sub_result .= $tbl_alias . ".o_lang='" . rawurlencode($cur_term_lang) . "'";
            }
            elseif ($cur_term_dt) {
              $sub_result .= "\n" . $ind . " AND ";
              $sub_result .= $tbl_alias . ".o_dt=" . $this->api
                ->get_id($cur_term_dt, 1);
              $sub_result .= " /* " . str_replace("*", "", str_replace("#", "::", htmlspecialchars($cur_term_dt))) . " */ ";
            }
          }
          $sub_result .= "\n" . $ind . ")";
        }
        else {
          if ($cur_term_type == "bnode") {

            //$cur_term_val=str_replace(":", "_", $cur_term_val);
          }

          /* term2alias/optional_term2alias */
          $term2alias_array = $in_optional ? "optional_term2alias" : "term2alias";
          $term2alias_array_obj = $this->{$term2alias_array};
          if (!isset($term2alias_array_obj[$cur_term_val])) {
            eval('$this->' . $term2alias_array . '[$cur_term_val]=array();');
          }
          eval('$this->' . $term2alias_array . '[$cur_term_val][]=array("alias"=>$cur_t_count, "col"=>$cur_col, "term"=>$cur_term, "optional_count"=>$optional_count, "parent_optional_count"=>$parent_optional_count, "union_branch_id"=>$union_branch_id, "in_graph"=>$in_graph, "graph"=>$graph);');

          /* alias2term */
          if (!isset($this->alias2term[$cur_t_count])) {
            $this->alias2term[$cur_t_count] = array();
          }
          $this->alias2term[$cur_t_count][] = array(
            "term" => $cur_term,
            "col" => $cur_col,
          );

          /* col occurrence */
          if (!isset($this->var_col_occurs[$cur_term_val])) {
            $this->var_col_occurs[$cur_term_val] = array();
          }
          $this->var_col_occurs[$cur_term_val][] = $cur_col;
        }
        if ($sub_result) {
          if ($in_optional) {
            if (!isset($this->optional_patterns[$tbl_alias])) {
              $this->optional_patterns[$tbl_alias] = array();
            }
            $this->optional_patterns[$tbl_alias][] = $sub_result;
            $this->logs[] = "adding " . $sub_result . " to optional_patterns[" . $tbl_alias . "]";
          }
          else {
            $result .= strlen($result) ? "\n" . $ind . "AND\n" . $ind : "" . $ind;
            $result .= $sub_result;
          }
        }
      }
    }
  }
  return $result;
}