make get_by_tags actually work
[ajatus.git] / plugins / ajatus / couchdb / views.php
blobd3a24e2f547533f9b953663ca02d0a5288bbe4b4
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 class couchdb_views
12 private $configuration;
13 private $documents;
15 public function __construct(&$configuration)
17 $this->configuration =& $configuration;
19 $this->documents = new couchdb_documents($this->configuration);
22 public function __get($name)
24 return $this->execute($name);
27 public function __set($name, $value)
29 if ($value === false)
31 return $this->remove($name);
34 $results = $this->update($name, $value);
36 if ( !isset($results->ok)
37 || !$results->ok)
39 $reason = '';
40 if ( isset($results->error)
41 && isset($results->reason))
43 $reason = "CouchDB said error: '{$results->error}', reason: '{$results->reason}'";
46 throw new couchdb_view_exception("Couldn't save view {$name}. {$reason}");
50 public function __isset($name)
52 return $this->exists($name);
55 public function temp($function, $args=null)
57 $view = new couchdb_views_view('_temp_view', $this->configuration);
58 $view->set_args($function, true);
60 if (! is_null($args))
62 $view->set_args($args);
65 $results = $view->run();
66 if (isset($results->rows))
68 $results->rows = $this->documents->convert($results->rows);
71 return $results;
74 public function create($name, $value)
76 return $this->update($name, $value);
79 public function delete($name)
81 return false;
84 public function execute($name, $args=null)
86 $this->exists($name);
88 $view = new couchdb_views_view($name, $this->configuration);
89 $view->set_args($args);
91 $results = $view->run();
92 if (isset($results->rows))
94 $results->rows = $this->documents->convert($results->rows);
97 return $results;
100 public function update($name, $value)
102 $update_only_view = false;
104 if (strpos($name, '/') !== false)
106 $name_parts = explode('/', $name);
107 $name = $name_parts[0];
108 $update_only_view = $name_parts[1];
109 $new_view = $value;
112 $view = new couchdb_views_view($name, $this->configuration);
114 $existing_view = null;
115 if ($this->exists($name, true))
117 $existing_view = $view->get();
120 if ($update_only_view)
122 if (is_null($existing_view))
124 throw new couchdb_view_exception("View {$name} doesn't exist! Cannot update.");
127 $value = array
129 '_rev' => $existing_view->_rev,
130 'language' => $existing_view->language,
131 'views' => get_object_vars($existing_view->views)
133 $value['views'][$update_only_view] = $new_view;
135 $view->set_args($value);
137 return $view->run(true);
140 if ( !is_null($existing_view)
141 && isset($existing_view->_rev))
143 $value['_rev'] = $existing_view->_rev;
146 if (! isset($value['language']))
148 $value['language'] = 'text/javascript';
150 if ( !is_null($existing_view)
151 && isset($existing_view->language))
153 $value['language'] = $existing_view->language;
157 $view->set_args($value);
159 return $view->run(true);
162 private function exists($name, $graceful=false)
166 $view = new couchdb_views_view($name, $this->configuration);
167 $view->exists();
169 return true;
171 catch(couchdb_view_exception $e)
173 if ($graceful)
175 return false;
178 if ($e->getCode() == 404)
180 throw new couchdb_view_exception("View '{$name}' doesn't exist!");
183 throw $e;
186 return false;
189 public function clear_caches()