[XQZone General] phpxdbc patch

Ron Hitchens ron.hitchens at marklogic.com
Mon Dec 20 12:27:51 PST 2004


Matt,

    Thanks for contributing this code.

    Since Raffaele has commit access to the code repository,
and he knows his code a lot better than I do, I'll let him
merge in your changes.

    Thanks again.

On Dec 20, 2004, at 12:04 PM, Matt Griffin wrote:

> This is a patch to add parameter (externally defined variable) support 
> to the phpxdbc interface written by Raffaele Sena.  It has not been 
> very well tested yet but appears to accomplish its mission. If it is 
> useful to anyone please make some noise :)
>
> Matt Griffin
> XQuery n00b
>
> Index: xdbc.php
> ===================================================================
> --- xdbc.php	(revision 266)
> +++ xdbc.php	(working copy)
> @@ -1,157 +1,172 @@
> -<?php
> -//
> -// Copyright (c)2004 Aromatic Productions
> -//
> -// Licensed under the Apache License, Version 2.0 (the "License");
> -// you may not use this file except in compliance with the License.
> -// You may obtain a copy of the License at
> -//
> -// http://www.apache.org/licenses/LICENSE-2.0
> -//
> -// Unless required by applicable law or agreed to in writing, software
> -// distributed under the License is distributed on an "AS IS" BASIS,
> -// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or 
> implied.
> -// See the License for the specific language governing permissions and
> -// limitations under the License.
> -//
> -// The use of the Apache License does not indicate that this project 
> is
> -// affiliated with the Apache Software Foundation.
> -//
> -define('XDBC_ARRAY',  0);
> -define('XDBC_XML',    1);
> -define('XDBC_HEADER', 2);
> -define('XDBC_QUERY',  4);
> -
> -function build_result($list, $flags)
> -{
> -  if (($flags & XDBC_XML) == 0)
> -    return $list;
> -
> -  $result = "<result>\n";
> -
> -  foreach($list as $l) {
> -    if ($l["x-type"] == "node()") // valid XML node
> -      $result .= $l["body"];
> -    else
> -      $result .= "<entry type=\"" . $l["x-type"]
> -              . "\" content-type=\"" . $l["content-type"]
> -              . "\">"
> -              . $l["body"] . "</entry>\n";
> -  }
> -
> -  $result .= "</result>\n";
> -  return $result;
> -}
> -
> -function build_error($err, $flags)
> -{
> -  $err = Array(Array(
> -    "content-type" => "text/xml", "x-type" => "x-error", "body" => 
> $err));
> -
> -  return build_result($err, $flags);
> -}
> -
> -function xdbc_query($host, $query, $userpass="", $flags=XDBC_ARRAY)
> -{
> -  $h = curl_init();
> -
> -  curl_setopt($h, CURLOPT_URL, "http://" . $host . "/eval");
> -  curl_setopt($h, CURLOPT_HEADER, 1);
> -  curl_setopt($h, CURLOPT_POST, 1);
> -  curl_setopt($h, CURLOPT_RETURNTRANSFER, 1);
> -  curl_setopt($h, CURLOPT_HTTPAUTH, CURLAUTH_ANY);
> -  if (!empty($userpass))
> -    curl_setopt($h, CURLOPT_USERPWD, $userpass);
> -  curl_setopt($h, CURLOPT_USERAGENT, "PhpXDBC/0.1 MarkXDBC/2.2-1");
> -
> -  curl_setopt($h, CURLOPT_POSTFIELDS, "xquery=" . urlencode($query));
> -
> -  $ret = curl_exec($h);
> -
> -  if (curl_errno($h) != 0) {
> -
> -    $err = "<error code=\"" . curl_errno($h) . "\">" . curl_error($h)
> -         . "</error>\n";
> -    curl_close($h);
> -
> -    return build_error($err, $flags);
> -  }
> -
> -  $code = curl_getinfo($h, CURLINFO_HTTP_CODE);
> -  $hsize = curl_getinfo($h, CURLINFO_HEADER_SIZE);
> -  $header = substr($ret, 0, $hsize-2);
> -  $headers = explode("\n", $header);
> -  curl_close($h);
> -
> -  $content = substr($ret, $hsize);
> -
> -  if ($code > 400 && $code < 500)
> -    $content = "<error code=\"http-" . $code . 
> "\">unauthorized</error>\n";
> -  if ($code != 200) {
> -    return build_error($content, $flags);
> -  }
> -
> -  foreach($headers as $h) {
> -    $h = rtrim($h);
> -    if (strlen($h)==0) continue;
> -    if (!strpos($h, ":")) continue;
> -    list($name, $value) = explode(":", $h);
> -
> -    if ($name == "Content-Type") {
> -      $ctype = ltrim($value);
> -      if (strncmp($ctype, "multipart/", 10)==0) {
> -        $pos = strpos($value, "boundary=");
> -        $boundary = substr($value, $pos+9);
> -      } else {
> -        $boundary = "";
> -      }
> -//    break;
> -    }
> -  }
> -
> -  $arr = Array();
> -
> -  if ($flags & XDBC_QUERY) {
> -    $arr[] = Array(
> -      "content-type" => "",
> -      "x-type" => "x-query",
> -      "body" => "<![CDATA[" . $query . "]]>"
> -    );
> -  }
> -  if ($flags & XDBC_HEADER) {
> -    $arr[] = Array(
> -      "content-type" => "",
> -      "x-type" => "x-header",
> -      "body" => $header
> -    );
> -  }
> -
> -  if ($boundary != "") {
> -    $list = explode("--$boundary", $content);
> -    foreach($list as $l) {
> -      $l = ltrim($l);
> -      if (empty($l)) continue; // first entry
> -      if ($l == "--\n") break; // last entry
> -
> -      list($ctype, $remain) = explode("\n", $l, 2);
> -      list($xtype, $body) = explode("\n\n", $remain, 2);
> -
> -      if (substr($ctype, 0, 14)=="Content-Type: ")
> -	$ctype = substr($ctype, 14);
> -      else {
> -      }
> -      if (substr($xtype, 0, 13)=="X-Primitive: ")
> -	$xtype = substr($xtype, 13);
> -      else {
> -      }
> -
> -      $arr[] = Array(
> -	"content-type" => $ctype, "x-type" => $xtype, "body" => $body);
> -    }
> -  } else {
> -      $arr[] = Array(
> -	"content-type" => $ctype, "x-type" => "x-body", "body" => $content);
> -  }
> -
> -  return build_result($arr, $flags);
> -}
> +<?php
> +//
> +// Copyright (c)2004 Aromatic Productions
> +//
> +// Licensed under the Apache License, Version 2.0 (the "License");
> +// you may not use this file except in compliance with the License.
> +// You may obtain a copy of the License at
> +//
> +// http://www.apache.org/licenses/LICENSE-2.0
> +//
> +// Unless required by applicable law or agreed to in writing, software
> +// distributed under the License is distributed on an "AS IS" BASIS,
> +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or 
> implied.
> +// See the License for the specific language governing permissions and
> +// limitations under the License.
> +//
> +// The use of the Apache License does not indicate that this project 
> is
> +// affiliated with the Apache Software Foundation.
> +//
> +define('XDBC_ARRAY',  0);
> +define('XDBC_XML',    1);
> +define('XDBC_HEADER', 2);
> +define('XDBC_QUERY',  4);
> +
> +function build_result($list, $flags)
> +{
> +  if (($flags & XDBC_XML) == 0)
> +    return $list;
> +
> +  $result = "<result>\n";
> +
> +  foreach($list as $l) {
> +    if ($l["x-type"] == "node()") // valid XML node
> +      $result .= $l["body"];
> +    else
> +      $result .= "<entry type=\"" . $l["x-type"]
> +              . "\" content-type=\"" . $l["content-type"]
> +              . "\">"
> +              . $l["body"] . "</entry>\n";
> +  }
> +
> +  $result .= "</result>\n";
> +  return $result;
> +}
> +
> +function build_error($err, $flags)
> +{
> +  $err = Array(Array(
> +    "content-type" => "text/xml", "x-type" => "x-error", "body" => 
> $err));
> +
> +  return build_result($err, $flags);
> +}
> +
> +function xdbc_bind_param(&$array, $lname, $stype, $value, $ns = '')
> +{
> +  $array[(string)$lname] = array($stype, (string)$value, $ns);
> +}
> +
> +function xdbc_query($host, $query, $userpass="", $flags=XDBC_ARRAY, 
> $params=array())
> +{
> +  $h = curl_init();
> +
> +  curl_setopt($h, CURLOPT_URL, "http://" . $host . "/eval");
> +  curl_setopt($h, CURLOPT_HEADER, 1);
> +  curl_setopt($h, CURLOPT_POST, 1);
> +  curl_setopt($h, CURLOPT_RETURNTRANSFER, 1);
> +  curl_setopt($h, CURLOPT_HTTPAUTH, CURLAUTH_ANY);
> +  if (!empty($userpass))
> +    curl_setopt($h, CURLOPT_USERPWD, $userpass);
> +  curl_setopt($h, CURLOPT_USERAGENT, "PhpXDBC/0.1 MarkXDBC/2.2-1");
> +
> +  $postfields =  "xquery=" . urlencode($query);
> +  if (0 < count($params)) {
> +    $keys = array_keys($params);
> +	for ($i=0; $i < count($keys); $i++) {
> +	  $postfields .= "&evn$i=" . urlencode($params[$keys[$i]][2]); // 
> namespace
> +	  $postfields .= "&evl$i=" . urlencode($keys[$i]); // local name
> +	  $postfields .= "&evt$i=" . urlencode($params[$keys[$i]][0]); // 
> schema type
> +	  $postfields .= "&evv$i=" . urlencode($params[$keys[$i]][1]); // 
> value
> +	}
> +  }
> +
> +  curl_setopt($h, CURLOPT_POSTFIELDS, $postfields);
> +  $ret = curl_exec($h);
> +
> +  if (curl_errno($h) != 0) {
> +
> +    $err = "<error code=\"" . curl_errno($h) . "\">" . curl_error($h)
> +         . "</error>\n";
> +    curl_close($h);
> +
> +    return build_error($err, $flags);
> +  }
> +
> +  $code = curl_getinfo($h, CURLINFO_HTTP_CODE);
> +  $hsize = curl_getinfo($h, CURLINFO_HEADER_SIZE);
> +  $header = substr($ret, 0, $hsize-2);
> +  $headers = explode("\n", $header);
> +  curl_close($h);
> +
> +  $content = substr($ret, $hsize);
> +
> +  if ($code > 400 && $code < 500)
> +    $content = "<error code=\"http-" . $code . 
> "\">unauthorized</error>\n";
> +  if ($code != 200) {
> +    return build_error($content, $flags);
> +  }
> +
> +  foreach($headers as $h) {
> +    $h = rtrim($h);
> +    if (strlen($h)==0) continue;
> +    if (!strpos($h, ":")) continue;
> +    list($name, $value) = explode(":", $h);
> +
> +    if ($name == "Content-Type") {
> +      $ctype = ltrim($value);
> +      if (strncmp($ctype, "multipart/", 10)==0) {
> +        $pos = strpos($value, "boundary=");
> +        $boundary = substr($value, $pos+9);
> +      } else {
> +        $boundary = "";
> +      }
> +//    break;
> +    }
> +  }
> +
> +  $arr = Array();
> +
> +  if ($flags & XDBC_QUERY) {
> +    $arr[] = Array(
> +      "content-type" => "",
> +      "x-type" => "x-query",
> +      "body" => "<![CDATA[" . $query . "]]>"
> +    );
> +  }
> +  if ($flags & XDBC_HEADER) {
> +    $arr[] = Array(
> +      "content-type" => "",
> +      "x-type" => "x-header",
> +      "body" => $header
> +    );
> +  }
> +
> +  if ($boundary != "") {
> +    $list = explode("--$boundary", $content);
> +    foreach($list as $l) {
> +      $l = ltrim($l);
> +      if (empty($l)) continue; // first entry
> +      if ($l == "--\n") break; // last entry
> +
> +      list($ctype, $remain) = explode("\n", $l, 2);
> +      list($xtype, $body) = explode("\n\n", $remain, 2);
> +
> +      if (substr($ctype, 0, 14)=="Content-Type: ")
> +	$ctype = substr($ctype, 14);
> +      else {
> +      }
> +      if (substr($xtype, 0, 13)=="X-Primitive: ")
> +	$xtype = substr($xtype, 13);
> +      else {
> +      }
> +
> +      $arr[] = Array(
> +	"content-type" => $ctype, "x-type" => $xtype, "body" => $body);
> +    }
> +  } else {
> +      $arr[] = Array(
> +	"content-type" => $ctype, "x-type" => "x-body", "body" => $content);
> +  }
> +
> +  return build_result($arr, $flags);
> +}
> _______________________________________________
> General mailing list
> General at xqzone.marklogic.com
> http://xqzone.com/mailman/listinfo/general
>
---
Ron Hitchens {ron.hitchens at marklogic.com}  650-655-2351

This e-mail and any accompanying attachments are confidential. The 
information is intended solely for the use of the individual to whom it 
is addressed. Any review, disclosure, copying, distribution, or use of 
this e-mail communication by others is strictly prohibited. If you are 
not the intended recipient, please notify us immediately by returning 
this message to the sender and delete all copies.  Thank you for your 
cooperation.




More information about the General mailing list