make get_by_tags actually work
[ajatus.git] / plugins / ajatus / couchdb / transport.php
blob4177ca5863d91ca85855c81c71f0d39bdd6c59b5
1 <?php
2 /**
3 *
4 * Copyright (c) 2008 Jerry Jalava <jerry.jalava@gmail.com>
5 * Licensed under the GPLv3 license
6 * http://www.gnu.org/licenses/gpl.html
7 *
8 */
10 require_once "HTTP/Request.php";
12 class couchdb_transport
14 private $configuration;
15 private $tmp_conf = array();
17 protected $lastStatusCode;
18 protected $lastRevision;
19 protected $lastDocId;
21 public function __construct(&$configuration)
23 if ( empty($configuration)
24 || !isset($configuration['host'])
25 || !isset($configuration['port']))
27 throw new couchdb_transport_exception('Invalid config defined for transport');
30 $this->configuration = $configuration;
33 public function free_execute($url=false, $options='GET', $postdata=false, $headers=array(), $tmp_conf=array())
35 if (! empty($tmp_conf))
37 $this->set_tmp_config($tmp_conf);
40 return $this->execute($url, $options, $postdata, $headers);
43 public function set_tmp_config($config)
45 $this->tmp_conf = $this->configuration;
47 foreach ($config as $key => $value)
49 $this->tmp_conf[$key] = $value;
53 private function parse_params($params)
55 if ( !isset($params[0])
56 || ( $params[0] !== false
57 && $params[0] == ''))
59 throw new couchdb_transport_exception('No url defined for transport');
62 $config = $this->configuration;
63 if (! empty($this->tmp_conf))
65 $config = $this->tmp_conf;
68 $postdata = false;
69 $headers = array(
70 'Content-Type' => 'text/javascript'
71 );
72 $options = array(
73 'method' => 'GET',
74 'allowRedirects' => true,
75 'timeout' => 3
76 );
78 $suffix = "";
80 if ( isset($config['database'])
81 && !empty($config['database']))
83 $suffix = "{$config['database']}/";
86 if (! is_null($params[0]))
88 $suffix .= "{$params[0]}";
91 $url = "http://{$config['host']}:{$config['port']}/{$suffix}";
93 if (count($params) >= 2)
95 if (is_array($params[1]))
97 foreach ($params[1] as $key => $value)
99 $options[$key] = $value;
102 else
104 $options['method'] = strtoupper($params[1]);
108 if (count($params) >= 3)
110 $postdata = $params[2];
113 if ( count($params) == 4
114 && is_array($params[3]))
116 foreach ($params[3] as $key => $value)
118 $headers[$key] = $value;
122 $this->tmp_conf = array();
124 return array(
125 $url,
126 $options,
127 $postdata,
128 $headers
132 private function clean_json($json)
134 $json = preg_replace('/"_rev":([0-9]+)/', '"_rev":"$1"', $json);
135 return json_decode($json);
138 protected function execute()
141 echo "DEBUG: execute called with params<pre>\n";
142 var_dump(func_get_args());
143 echo "</pre>\n";
145 list($url, $options, $postdata, $headers) = $this->parse_params(func_get_args());
147 $req = new HTTP_Request($url, $options);
149 if ($postdata)
151 /**
152 * addRawPostData() is deprecated, use setBody in stead
153 $req->addRawPostData($postdata);
155 $req->setBody($postdata);
158 if ($headers)
160 if ( isset($headers['_rev'])
161 && !empty($headers['_rev']))
163 $req->addHeader('_rev', (float) $headers['_rev']);
166 if ( isset($headers['Content-Type'])
167 && !empty($headers['Content-Type']))
169 $req->addHeader('Content-Type', $headers['Content-Type']);
173 $resp = $req->sendRequest();
175 if (PEAR::isError($resp))
177 throw new couchdb_transport_exception('Unexpected error occured while sending the request!' . $resp);
180 $this->lastStatusCode = $req->getResponseCode();
182 if ($this->lastStatusCode == 404)
184 throw new couchdb_transport_exception("Requested url {$url} not found.", 404);
187 if (! in_array($this->lastStatusCode, range(200, 299)))
189 $error_json = $this->clean_json($req->getResponseBody());
190 /* Try to parse the couchdb error but it seems to be weirdly formed and json_decode cannot handle it
191 if (isset($error_json->reason))
193 echo "DEBUG: \$error_json->reason<pre>\n";
194 var_dump($error_json->reason);
195 echo "</pre>\n";
196 //$error_reason_json = json_decode(str_replace('\\n', "\n", $error_json->reason));
197 $error_reason_json = json_decode(str_replace('\\n', "\n", $error_json->reason));
198 echo "DEBUG: \$error_reason_json<pre>\n";
199 var_dump($error_reason_json);
200 echo "</pre>\n";
203 throw new couchdb_transport_exception("Don't know how to handle response with code {$this->lastStatusCode}.", $this->lastStatusCode, $req);
206 $this->lastRevision = $req->getResponseHeader('x-couch-revid');
207 $this->lastDocId = $req->getResponseHeader('x-couch-docid');
209 return $this->clean_json($req->getResponseBody());