You are here

function ARC_rdf_store_select_json_sub_handler::get_jsoni_result_body_index in Taxonomy import/export via XML 5

Same name and namespace in other branches
  1. 5.2 arc/ARC_rdf_store_select_json_sub_handler.php \ARC_rdf_store_select_json_sub_handler::get_jsoni_result_body_index()
  2. 6.2 arc/ARC_rdf_store_select_json_sub_handler.php \ARC_rdf_store_select_json_sub_handler::get_jsoni_result_body_index()
  3. 6 arc/ARC_rdf_store_select_json_sub_handler.php \ARC_rdf_store_select_json_sub_handler::get_jsoni_result_body_index()
1 call to ARC_rdf_store_select_json_sub_handler::get_jsoni_result_body_index()
ARC_rdf_store_select_json_sub_handler::get_json_result_body in arc/ARC_rdf_store_select_json_sub_handler.php

File

arc/ARC_rdf_store_select_json_sub_handler.php, line 277

Class

ARC_rdf_store_select_json_sub_handler

Code

function get_jsoni_result_body_index($args = "") {
  $ind = "  ";
  $ind2 = $ind . $ind;
  $ind3 = $ind2 . $ind;
  $ind4 = $ind3 . $ind;
  $nl = "\n";
  $jsoni = isset($args["result_type_args"]["jsoni"]) ? $args["result_type_args"]["jsoni"] : false;
  $row = isset($args["row"]) ? $args["row"] : false;
  $rs = isset($args["rs"]) ? $args["rs"] : false;
  $enc_vals = $this->config["encode_values"];
  $conv_id = in_array($this->config["id_type"], array(
    "hash_int",
    "incr_int",
  ));
  $fix_utf8 = isset($args["fix_utf8"]) ? $args["fix_utf8"] : false;
  $code = "";
  if (!$row) {
    return $code;
  }
  $result_vars = $args["infos"]["result_vars"];
  $api =& $this->api;
  $api_helper =& $this->api
    ->get_api_helper();
  if ($row) {

    /* index creation */
    $idxs = $this
      ->get_jsoni_indexes($jsoni);
    $val_idxs = array();
    do {
      $cur_vals = array();
      foreach ($result_vars as $cur_var) {
        if (isset($row[$cur_var])) {
          $cur_type = isset($row[$cur_var . "__type"]) ? $row[$cur_var . "__type"] : 0;
          $val = $enc_vals ? rawurldecode($row[$cur_var]) : $row[$cur_var];
          if ($cur_type == 0) {
            $cur_vals[$cur_var . "__type"] = "uri";
          }
          elseif ($cur_type == 1) {
            $cur_vals[$cur_var . "__type"] = "bnode";
            $val = substr($val, 2);
          }
          elseif ($cur_type > 1) {
            $cur_vals[$cur_var . "__type"] = "literal";
            $val = $this
              ->decode_mysql_utf8_bugs($val);
            $val = $fix_utf8 ? $api_helper
              ->escape_js_string($api_helper
              ->adjust_utf8_string($val)) : $api_helper
              ->escape_js_string($val);
            if (isset($row[$cur_var . "__lang"]) && ($lang = $row[$cur_var . "__lang"])) {
              $cur_vals[$cur_var . "__lang"] = $lang;
            }
            elseif (isset($row[$cur_var . "__dt"]) && ($dt = $row[$cur_var . "__dt"])) {
              $dt_val = $conv_id ? $api
                ->get_val("CONV('" . $dt . "', 16, 10)") : $api
                ->get_val("'" . $dt . "'");
              if ($dt_val) {
                $cur_vals[$cur_var . "__dt"] = htmlspecialchars($dt_val);
                $cur_vals[$cur_var . "__type"] = "typed-literal";
              }
            }
          }
          $cur_vals[$cur_var] = $val;
        }
      }

      /* index */
      foreach ($idxs as $cur_idx) {
        $parts = strpos($cur_idx, " ") ? explode(" ", $cur_idx) : array(
          $cur_idx,
        );
        $cur_val_idx =& $val_idxs;
        for ($i = 0, $i_max = count($parts); $i < $i_max; $i++) {
          $cur_part = $parts[$i];
          if ($cur_part) {
            $cur_part_alias = $cur_part;
            if (preg_match("/^([^:]+)\\:(.+)\$/", $cur_part, $matches)) {
              $cur_part = $matches[1];
              $cur_part_alias = $matches[2];
            }
            if (!array_key_exists($cur_part_alias, $cur_val_idx)) {
              $cur_val_idx[$cur_part_alias] = array();
            }
            $cur_val_idx =& $cur_val_idx[$cur_part_alias];
            if (isset($cur_vals[$cur_part])) {
              $cur_key = rawurlencode($cur_vals[$cur_part]);
              $cur_key .= " type:" . $cur_vals[$cur_part . "__type"];
              if (isset($cur_vals[$cur_part . "__lang"]) && ($cur_lang = $cur_vals[$cur_part . "__lang"])) {
                $cur_key .= " lang:" . rawurlencode($cur_lang);
              }
              elseif (isset($cur_vals[$cur_part . "__dt"]) && ($cur_dt = $cur_vals[$cur_part . "__dt"])) {
                $cur_key .= " datatype:" . rawurlencode($cur_dt);
              }
              if (!array_key_exists($cur_key, $cur_val_idx)) {
                $cur_val_idx[$cur_key] = array();
              }
              $cur_val_idx =& $cur_val_idx[$cur_key];
            }
          }
        }
      }
      $row = mysql_fetch_array($rs);
    } while ($row);

    /* extract result idxs from idxs array */
    $result_idxs = array();
    $result_idx = "";
    foreach ($idxs as $cur_idx) {
      if (!$result_idx || $cur_idx != $result_idx && strpos($cur_idx, $result_idx) === 0) {
        $result_idx = $cur_idx;
      }
      else {
        $result_idxs[] = $result_idx;
        $result_idx = $cur_idx;
      }
    }
    $result_idxs[] = $result_idx;

    /* generate json */
    for ($i = 0, $i_max = count($result_idxs); $i < $i_max; $i++) {
      $cur_idx = $result_idxs[$i];
      $parts = strpos($cur_idx, " ") ? explode(" ", $cur_idx) : array(
        $cur_idx,
      );
      $sibling_parts = array();
      do {
        $has_sibling = false;
        if ($i < $i_max - 1 && ($next_idx = $result_idxs[$i + 1])) {
          $next_parts = strpos($next_idx, " ") ? explode(" ", $next_idx) : array(
            $next_idx,
          );
          if ($next_parts[0] == $parts[0]) {
            $sibling_parts[] = $next_parts;
            $has_sibling = true;
            $i++;
          }
        }
      } while ($has_sibling);
      $code .= $i > 0 ? "," : "";
      $code .= $this
        ->get_jsoni_code(array(
        "my_ind" => $ind3,
        "parts" => $parts,
        "sibling_parts" => $sibling_parts,
        "val_idx" => $val_idxs,
      ));
    }
  }
  return $code;
}