db = $database; } // // Collections // public function collectionExists ($collectionId) { return (!$this->getCollection($collectionId)) ? false : true; } public function getCollection ($collectionId) { $result = $this->db->select('*') ->from($this->collectionsFile) ->where(['id' => $collectionId]) ->get(); if (!isset($result[0])) return false; return $result[0]; } public function getCollections ($username) { $result = $this->db->select('*') ->from($this->collectionsFile) ->where(['principaluri' => $username]) ->get(); return $result; } public function getHighestCollectionId () { $data = $this->db->select('id') ->from($this->collectionsFile) ->order_by('id', JSONDB::ASC) ->get(); return end($data)['id']; } public function createCollection ($username, $uri, array $properties) { if(!preg_match('/^[\w-]+$/', $uri)) throw new Exception('URI contains not allowed characters.'); $collections = $this->getCollections($username); foreach ($collections as $collection) { if ($collection['uri'] == $uri) throw new Exception('An '.strtolower($this->collectionType).' with this URI already exist.'); } $collection = $this->newCollection($username, $uri, $properties); $this->db->insert($this->collectionsFile, $collection); mkdir($this->dataFolder.$this->collectionsFolder.$collection['id']); mkdir($this->dataFolder.$this->collectionsFolder.$collection['id'].'/data'); return $collection['id']; } public function updateCollection ($collectionId, $properties) { if (!$this->exists($id)) throw new Exception($this->collectionType.' doesn\'t exist!'); $this->db->update($properties) ->from($this->collectionsFile) ->where(['id' => $collectionId]) ->trigger(); $this->addChange($collectionId, '', 2); } public function deleteCollection ($collectionId) { $this->db->delete() ->from($this->collectionsFile) ->where(['id' => $collectionId]) ->trigger(); Helpers::delete($this->dataFolder.$this->collectionsFolder.$id); } // // Objects // public function getHighestObjectId ($collectionId) { $data = $this->db->select('id') ->from($this->collectionsFolder.$collectionId.'/objects.json') ->order_by('id', JSONDB::ASC) ->get(); return end($data)['id']; } public function getObject ($collectionId, $uri) { $objects = $result = $this->db->select('*') ->from($this->collectionsFolder.$collectionId.'/objects.json') ->where(['uri' => $uri]) ->get(); if (!isset($objects[0])) return false; $result = $objects[0]; $result[$this->datafield] = file_get_contents($this->dataFolder.$this->collectionsFolder.$collectionId.'/data/'.$objects[0]['uri']); return $result; } public function getObjects ($collectionId) { $objects = $this->db->select('*') ->from($this->collectionsFolder.$collectionId.'/objects.json') ->get(); $results = []; foreach ($objects as $object) { $results[] = get_object_vars($object); } return $results; } public function createObject ($collectionId, $uri, $data, $extraData = NULL) { $object = $this->newObject($collectionId, $uri, $data, $extraData); $this->db->insert($this->collectionsFolder.$collectionId.'/objects.json', $object); file_put_contents($this->dataFolder.$this->collectionsFolder.$collectionId.'/data/'.$uri, $data); $this->addChange($collectionId, $uri, 1); return $object['etag']; } public function updateObject ($collectionId, $uri, $data, $extraData = NULL) { $update = $this->newObjectUpdate($collectionId, $uri, $data, $extraData); $this->db->update($update) ->from($this->collectionsFolder.$collectionId.'/objects.json') ->where(['uri' => $uri]) ->trigger(); file_put_contents($this->dataFolder.$this->collectionsFolder.$collectionId.'/data/'.$uri, $data); $this->addChange($collectionId, $uri, 2); return $update['etag']; } public function deleteObject ($collectionId, $uri) { $result = $this->db->delete() ->from($this->collectionsFolder.$collectionId.'/objects.json') ->where(['uri' => $uri]) ->trigger(); unlink($this->dataFolder.$this->collectionsFolder.$collectionId.'/data/'.$uri); $this->addChange($collectionId, $uri, 3); return $result; } public function addChange ($collectionId, $uri, $operation) { $synctoken = $this->getCollection($collectionId)['synctoken']; $this->db->insert($this->collectionsFolder.$collectionId.'/changes.json', [ 'synctoken' => $synctoken, 'operation' => $operation, 'uri' => $uri, ]); $this->db->update(['synctoken' => $synctoken+1]) ->from($this->collectionsFile) ->where(['id' => $collectionId]) ->trigger(); } public function getChanges ($collectionId, $syncToken, $syncLevel) { $currentToken = $this->getCollection($collectionId)['synctoken']; if (is_null($currentToken)) { return null; } $result = [ 'syncToken' => $currentToken, 'added' => [], 'modified' => [], 'deleted' => [], ]; if ($syncToken) { $query = $this->db->select('*') ->from($this->collectionsFolder.$collectionId.'/changes.json') ->order_by('synctoken', JSONDB::ASC) ->get(); $changes = []; foreach ($query as $data) { if ($data['synctoken'] >= $syncToken) $changes[$data['uri']] = $data['operation']; if ($data['synctoken'] < $currentToken) $changes[$data['uri']] = $data['operation']; } foreach ($changes as $uri => $operation) { switch ($operation) { case 1: $result['added'][] = $uri; break; case 2: $result['modified'][] = $uri; break; case 3: $result['deleted'][] = $uri; break; } } } else { $objects = $this->getObjects($collectionId); foreach ($objects as $object) { $result['added'][] = $object['uri']; } } return $result; } }