public function tearDown() { // Remove / clear test database $db = phpillowConnection::getInstance(); try { $db->delete('/test'); } catch (phpillowResponseNotFoundErrorException $e) { /* Ignore */ } phpillowConnectionTestHelper::reset(); }
public function testOverwriteAttachment() { $db = phpillowConnection::getInstance(); $doc = phpillowUserDocument::createNew(); $doc->login = '******'; $doc->attachFile(dirname(__FILE__) . '/data/image_png.png'); $doc->save(); $doc = phpillowManager::fetchDocument('user', 'user-kore'); $doc->attachFile($file = dirname(__FILE__) . '/data/image_jpg.jpg', 'image_png.png'); $doc->save(); $doc = phpillowManager::fetchDocument('user', 'user-kore'); $response = $doc->getFile('image_png.png'); $this->assertSame(file_get_contents($file), $response->data); $this->assertSame('application/octet-stream', $response->contentType); }
public function saveDataToCouch($data) { try { phpillowConnection::createInstance($this->to_host, $this->to_port, '', ''); phpillowConnection::setDatabase($this->to_database); } catch (Exception $e) { die("Nie moge nawiazac polaczenia z baza Couch ({$this->to_host}:{$this->to_port})"); } $allowKeys = array('nazwa', 'dlugosc', 'szerokosc', 'ludnosc', 'link'); foreach ($data as $k => $value) { if (is_array($value) && !empty($value)) { $doc = new miastoDocument(); foreach ($value as $k1 => $v1) { if (in_array($k1, $allowKeys)) { $doc->{$k1} = $v1; } } $doc->save(); } } }
/** * Reset database depending on provided options * * @param array $options * @return void */ public static function resetDatabase(array $options = array()) { phpillowConnectionTestHelper::reset(); // Initialize wanted connection handler $handler = isset($options['handler']) ? $options['handler'] : 'phpillowConnection'; call_user_func(array($handler, 'createInstance')); $db = phpillowConnection::getInstance(); try { $db->delete('/test'); } catch (Exception $e) { /* Ignored */ } if (isset($options['database'])) { // Create test database phpillowConnection::setDatabase($options['database']); $db = phpillowConnection::getInstance(); $db->put('/' . $options['database']); } else { // Reset all connection settings in the end phpillowConnectionTestHelper::reset(); } }
function couchDelete($id, $rev) { // phpillowConnection::__call("delete",""); $db = phpillowConnection::getInstance(); $response = $db->__call("delete", array(0 => phpillowConnection::getDatabase() . urlencode($id) . "?rev=" . $rev)); }
public function testDeleteDocumentMultipleRevisions() { phpillowConnection::createInstance(); phpillowConnection::setDatabase('test'); // Create test database $db = phpillowConnection::getInstance(); $db->put('/test'); // Add document to fetch $author = phpillowManager::createDocument('user'); $author->login = '******'; $author->save(); $doc = phpillowManager::fetchDocument('user', 'user-kore'); $doc->name = 'Kore'; $doc->save(); // Test delete phpillowManager::deleteDocument('user', 'user-kore'); try { $user = phpillowManager::fetchDocument('user', 'user-kore'); $this->fail('Expected phpillowResponseNotFoundErrorException.'); } catch (phpillowResponseNotFoundErrorException $e) { /* Expected exception */ } // Remove / clear test database $db = phpillowConnection::getInstance(); $db->delete('/test'); phpillowConnectionTestHelper::reset(); }
public function testInvalidPath() { phpillowCustomConnection::createInstance(); $db = phpillowConnection::getInstance(); try { $response = $db->get('irrelevant'); $this->fail('Expected phpillowInvalidRequestException.'); } catch (phpillowInvalidRequestException $e) { /* Expected exception */ } }
/** * Create a new couch DB connection instance. * * Static method to create a new couch DB connection instance. This method * should be used to configure the connection for later use. * * The host and its port default to localhost:5984. * * @param string $host * @param int $port * @param string $username * @param string $password * @param string $called * @return void */ public static function createInstance($host = '127.0.0.1', $port = 5984, $username = null, $password = null, $called = "phpillowStreamConnection") { parent::createInstance($host, $port, $username, $password, $called); }
public function testHttpLog() { phpillowTestEnvironmentSetup::resetDatabase(array('database' => 'test', 'handler' => 'phpillowStreamConnection')); $db = phpillowConnection::getInstance(); $db->setOption('http-log', $logFile = tempnam(dirname(__FILE__) . '/../temp', __CLASS__)); $response = $db->put('/test/123', '{"_id":"123","data":"Foo"}'); $response = $db->get('/test/123'); $this->assertTrue(filesize($logFile) > 128); }
/** * Get file contents * * Get the contents of an attached file as a phpillowDataResponse. * * @param string $fileName * @return phpillowDataResponse */ public function getFile($fileName) { if (!isset($this->storage->_attachments[$fileName])) { throw new phpillowNoSuchPropertyException($fileName); } $db = phpillowConnection::getInstance(); $response = $db->get(phpillowConnection::getDatabase() . urlencode($this->_id) . '/' . $fileName, null, true); return $response; }
/** * Return used database * * This should always used within a document instead of * phpillowConnection::getDatabase() * * @return phpillowConnection */ public function getDatabase() { if ($this->database === null) { return phpillowConnection::getDatabase(); } return $this->database; }
<?php /** Configuration Variables **/ define('DEVELOPMENT_ENVIRONMENT', true); //RANDOM HASH STRING define('HASH_STRING', '2d5A212fE35c'); //Typ bazy define('DB_TYPE', 'couchdb'); //couchdb, mysql //MYSQL define('DB_NAME', 'yourdatabasename'); define('DB_USER', 'yourusername'); define('DB_PASSWORD', 'yourpassword'); define('DB_HOST', 'localhost'); //couchDB define('CDB_USER', ''); define('CDB_PASSWORD', ''); define('CDB_HOST', '127.0.0.1'); define('CDB_PORT', '5984'); define('CDB_NAME', 'baza'); //nazwa bazy phpillowConnection::createInstance(CDB_HOST, CDB_PORT, CDB_USER, CDB_PASSWORD); phpillowConnection::setDatabase(CDB_NAME);
<?php // Set up PHPillow autoloading include dirname(__FILE__) . '/../src/bootstrap.php'; // Set up database connection with default parameters phpillowConnection::createInstance(); // Set database, which will be used and get connection handler $db = phpillowConnection::getInstance(); phpillowConnection::setDatabase('test'); // Delete maybe existing database, ignoring the error, if it does not exist // yet. try { $db->delete('/test'); } catch (Exception $e) { /* Ignore */ } // Create new database $db->put('/test'); // Create a new document of the predefined user class $doc = new phpillowUserDocument(); $doc->login = '******'; $doc->name = 'Kore Nordmann'; $docId = $doc->save(); // Fetch the document from the database $doc = new phpillowUserDocument(); $doc->fetchById($docId); // Update the document $doc->email = '*****@*****.**'; $doc->save(); // Fetch the document again and dump the revisions $doc = new phpillowUserDocument();
<?php //Install PHPillow using PEAR require "PHPillow/bootstrap.php"; require "documents.php"; require "views.php"; require "settings.php"; phpillowConnection::createInstance('localhost', 5984, COUCHDB_USER, COUCHDB_PASSWORD); phpillowConnection::setDatabase(COUCHDB_DATABASE); function tripHash($password) { //TODO: Talk to someone smart about cryptographic hash functions //This appears to work well, but there could be a flaw. //based on wikipedia description of algo $tripcode = substr(crypt($password, TRIPCODE_SALT), -10, 10); return $tripcode; } function validMessage($json) { //make sure username is appropriate length and alphanumeric plus underscore if (strlen($json->username) > 30 || strlen($json->username) < 3) { return false; } $alphanumeric = '/^[0-9a-zA-Z_]+$/'; if (!preg_match($alphanumeric, $json->username)) { return false; } //make sure message isn't too long. if (strlen($json->message) > 1000) { return false; }
/** * Delete document by ID * * Delete the document of the given type with the given ID. Throws a * phpillowNoSuchPropertyException if the document does not exist. * * Deletion means, that all revisions, including the current one, are * removed. * * @param string $name * @param string $id * @return void */ public static function deleteDocument($name, $id) { // Check if a document with the given name exists. if (!isset(self::$documents[$name])) { throw new phpillowNoSuchPropertyException($name); } $db = phpillowConnection::getInstance(); $revision = $db->get($db->getDatabase() . $id); // Only delete the current revision. This should delete all revisions // in the database except somebody updates the document between the get // and the delete request $db->delete($db->getDatabase() . $id . '?rev=' . $revision->_rev); }
/** * Query a view * * Query the specified view to get a set of results. You may optionally use * the view query options as additional parameters to limit the returns * values, specified at: * http://wiki.apache.org/couchdb/HTTP_view_API#Querying_Options * * @param string $view * @param array $options * @return phpillowResultArray */ public function query($view, array $options = array()) { // Build query string, just as a normal HTTP GET query string $url = phpillowConnection::getDatabase() . '_design/' . $this->getViewName() . '/_view/' . $view; $url .= $this->buildViewQuery($options); // Get database connection, because we directly execute a query here. $db = phpillowConnection::getInstance(); try { // Try to execute query, a failure most probably means, the view // has not been added, yet. $response = $db->get($url); } catch (phpillowResponseErrorException $e) { // Ensure view has been created properly and then try to execute // the query again. If it still fails, there is most probably a // real problem. $this->verifyView(); $response = $db->get($url); } return $response; }
/** * Set database to use * * Set the name of database to use. You do not need to provide this as a * path, but only its name. * * @param string $database * @return void */ public static function setDatabase($database) { self::$database = '/' . $database . '/'; }
<?php // Include all required classes $autoload = (require ($base = dirname(__FILE__) . '/../src/') . 'classes/autoload.php'); foreach ($autoload as $file) { require_once $base . $file; } // Configure parameters for speed testing $puts = 1000; $gets = 5000; $views = 2000; // Set up backend connection phpillowConnection::createInstance('10.0.118.171', 5984, 'admin', 'm4!1.d3'); phpillowConnection::setDatabase('test'); $db = phpillowConnection::getInstance(); try { $db->delete('/test'); } catch (Exception $e) { /* Ignore */ } $db->put('/test'); // /* $start = microtime(true); for ($i = 0; $i < $puts; ++$i) { $doc = new phpillowUserDocument(); $doc->login = '******' . $i; $doc->name = 'Kore Nordmann'; $doc->save(); } printf("%d PUTs in %.2fs (%d req/s)\n", $puts, $time = microtime(true) - $start, $puts / $time); // */
/** * Fetch the attachment data from the couchdb and return it * * @return string */ protected function fetchData() { return $this->connection->get($this->url, null, true)->data; }