/Couchbase/CouchDB.php

https://github.com/beberlei/php-couchbase · PHP · 210 lines · 103 code · 20 blank · 87 comment · 7 complexity · 97dfdc82fbfb69296c4ad30238b3a1b7 MD5 · raw file

  1. <?php
  2. /**
  3. * Interface to the CouchDB HTTP API.
  4. *
  5. * @package Couchbase
  6. */
  7. class Couchbase_CouchDB
  8. {
  9. /**
  10. * CouchDB server url, parsed into an object.
  11. *
  12. * @var URL CouchDB Server URL
  13. */
  14. var $server = null;
  15. /**
  16. * Constructor, takes a URL spcifying the CouchDB server and database.
  17. *
  18. * @param string $dsn URL to a CouchDB server and database
  19. * @example http://localhost:5984/database
  20. */
  21. function __construct($dsn)
  22. {
  23. $this->server = new stdClass;
  24. $this->dsn = $dsn;
  25. foreach(parse_url($dsn) AS $k => $v) {
  26. $this->server->$k = $v;
  27. }
  28. }
  29. /**
  30. * Create a new database.
  31. *
  32. * @param string $name database name, must match [a-z][a-z0-9$()/_-].
  33. * @return string JSON success or error message.
  34. */
  35. function createDb($name)
  36. {
  37. return $this->send("PUT", $this->server->path);
  38. }
  39. /**
  40. * Delete a datbase
  41. *
  42. * @param string $name database name.
  43. * @return string JSON success or error message.
  44. */
  45. function deleteDb($name)
  46. {
  47. return $this->send("DELETE", $this->server->path);
  48. }
  49. /**
  50. * Save a document.
  51. *
  52. * @param string $doc JSON representation of a document.
  53. * @return string JSON success or error message.
  54. */
  55. function saveDoc($doc)
  56. {
  57. return $this->send("POST", $this->server->path, $doc);
  58. }
  59. /**
  60. * Open a document by id.
  61. *
  62. * @param string $id The documents's id.
  63. * @return string document or error message as a JSON string.
  64. */
  65. function open($id)
  66. {
  67. return $this->send("GET", $this->server->path . "/$id");
  68. }
  69. /**
  70. * Query a CouchDB view.
  71. *
  72. * @param string $group Design document / view group name.
  73. * @param string $name View name.
  74. * @param string $options Associative array of CouchDB view query options.
  75. * @return string JSON result set of a CouchDB view Query.
  76. */
  77. function view($group, $name, $options = array())
  78. {
  79. $qs = $this->_options_to_query_string($options);
  80. return $this->send("GET",
  81. $this->server->path . "/_design/$group/_view/$name?$qs");
  82. }
  83. /**
  84. * Query the built-in _all_docs view.
  85. *
  86. * @param string $options Associative array of CouchDB view query options.
  87. */
  88. function allDocs($options)
  89. {
  90. $qs = $this->_options_to_query_string($options);
  91. return $this->send("GET", $this->server->path . "/_all_docs?$qs");
  92. }
  93. /**
  94. * Utility function that turns an options array into CouchDB view
  95. * parameter query string. Including proper quoting for *key* options.
  96. *
  97. * @param string $options Associative array of CouchDBview query options.
  98. * @return string URL query string to be appended to a view query.
  99. */
  100. function _options_to_query_string($options)
  101. {
  102. // TODO: keys POST
  103. $qs = array();
  104. foreach($options AS $option => $value) {
  105. // ignore null values that come in from optional arguments in
  106. // the public API like getResutlsByRange($start = null, $end = null)
  107. if(null === $value) {
  108. continue;
  109. }
  110. switch($option) {
  111. case "key":
  112. case "startkey":
  113. case "start_key":
  114. case "endkey":
  115. case "end_key":
  116. $qs[] = "$option=" . urlencode(json_encode($value));
  117. break;
  118. case "descending":
  119. case "group":
  120. case "reduce":
  121. case "include_docs":
  122. case "inclusive_end":
  123. $qs[] = "$option=" . ($value?"true":"false");
  124. break;
  125. default:
  126. $qs[] = "$option=" . urlencode($value);
  127. break;
  128. }
  129. }
  130. return join("&", $qs);
  131. }
  132. /**
  133. * Utility^W Gehtto method, send an HTTP request to CouchDB
  134. *
  135. * TODO: This really needs to be moved to a proper HTTP client.
  136. *
  137. * @param string $method HTTP method, GET, PUT, POST, DELETE etc.
  138. * @param string $url The path component of a URL.
  139. * @param string $post_data Data to send with a POST or PUT request.
  140. * @param string $content_type request contet-type header, defaults to
  141. * application/json.
  142. * @return string JSON response.
  143. */
  144. function send($method, $url, $post_data = NULL, $content_type = "application/json")
  145. {
  146. $s = fsockopen(
  147. $this->server->host,
  148. $this->server->port,
  149. $errno,
  150. $errstr,
  151. 10);
  152. if(!$s) {
  153. echo "$errno: $errstr\n";
  154. return false;
  155. }
  156. $host = $this->server->host;
  157. $request = "$method $url HTTP/1.0\r\nHost: $host\r\n";
  158. if(isset($this->server->user)) {
  159. $request .= "Authorization: Basic ".base64_encode("{$this->server->user}:{$this->server->pass}")."\r\n";
  160. }
  161. if($post_data) {
  162. $request .= "Content-Type: $content_type\r\n";
  163. $request .= "Content-Length: ".strlen($post_data)."\r\n\r\n";
  164. $request .= "$post_data\r\n";
  165. } else {
  166. $request .= "\r\n";
  167. }
  168. // var_dump("--------------------------------");
  169. // var_dump($request);
  170. // var_dump("-------------");
  171. fwrite($s, $request);
  172. $response = "";
  173. while(!feof($s)) {
  174. $response .= fgets($s);
  175. }
  176. list($this->headers, $this->body) = explode("\r\n\r\n", $response);
  177. if($response == "") {
  178. // var_dump(" -------------------------------");
  179. // var_dump(" ERROR EMPTY SERVER RESPONSE");
  180. // var_dump($request);
  181. // var_dump(" -------------------------------");
  182. // var_dump($response);
  183. // var_dump(" -------------------------------");
  184. // exit (1);
  185. }
  186. // var_dump($response);
  187. // var_dump("--------------------------------");
  188. return $this->body;
  189. }
  190. }