From d2cfc7fd1535afcec352c05c99a318cd48325734 Mon Sep 17 00:00:00 2001 From: Frederic Guillot Date: Tue, 27 Jan 2015 20:13:16 -0500 Subject: [PATCH] Update vendor --- composer.json | 2 +- models/feed.php | 10 +- models/item.php | 4 +- vendor/autoload.php | 2 +- vendor/composer/autoload_classmap.php | 3 + vendor/composer/autoload_real.php | 10 +- vendor/composer/installed.json | 98 ++--- vendor/fguillot/picodb/.gitignore | 3 +- vendor/fguillot/picodb/README.md | 389 ++++++++++++------ .../fguillot/picodb/lib/PicoDb/Database.php | 20 +- .../fguillot/picodb/lib/PicoDb/Hashtable.php | 144 +++++++ vendor/fguillot/picodb/lib/PicoDb/Table.php | 35 +- .../picofeed/lib/PicoFeed/Client/Client.php | 77 +++- .../picofeed/lib/PicoFeed/Client/Curl.php | 74 ---- .../picofeed/lib/PicoFeed/Client/Stream.php | 13 +- .../picofeed/lib/PicoFeed/Client/Url.php | 8 +- .../lib/PicoFeed/Encoding/Encoding.php | 1 + .../lib/PicoFeed/Parser/XmlParser.php | 6 +- .../picofeed/lib/PicoFeed/Reader/Favicon.php | 24 +- .../picofeed/tests/Client/CurlTest.php | 1 + .../picofeed/tests/Client/StreamTest.php | 6 +- .../picofeed/tests/Client/UrlTest.php | 65 ++- .../picofeed/tests/Parser/XmlParserTest.php | 1 + .../picofeed/tests/Reader/FaviconTest.php | 77 ---- vendor/fguillot/simple-validator/.gitignore | 2 + vendor/fguillot/simple-validator/README.md | 55 ++- .../src/SimpleValidator/Base.php | 3 + .../src/SimpleValidator/Validators/Exists.php | 58 +++ .../SimpleValidator/Validators/NotEquals.php | 43 ++ .../src/SimpleValidator/Validators/Unique.php | 68 ++- .../tests/ExistsValidatorTest.php | 74 ++++ .../tests/NotEqualsValidator.php | 35 ++ .../tests/UniqueValidatorTest.php | 45 ++ 33 files changed, 994 insertions(+), 462 deletions(-) create mode 100644 vendor/fguillot/picodb/lib/PicoDb/Hashtable.php create mode 100644 vendor/fguillot/simple-validator/src/SimpleValidator/Validators/Exists.php create mode 100644 vendor/fguillot/simple-validator/src/SimpleValidator/Validators/NotEquals.php create mode 100644 vendor/fguillot/simple-validator/tests/ExistsValidatorTest.php create mode 100644 vendor/fguillot/simple-validator/tests/NotEqualsValidator.php diff --git a/composer.json b/composer.json index 26547b7..fd2ca0d 100644 --- a/composer.json +++ b/composer.json @@ -5,7 +5,7 @@ "require": { "fguillot/simple-validator": "dev-master", "fguillot/json-rpc": "dev-master", - "fguillot/picodb": "dev-master", + "fguillot/picodb": "0.0.2", "fguillot/picofeed": "dev-master", "fguillot/picofarad": "dev-master" }, diff --git a/models/feed.php b/models/feed.php index 1155801..ac9ecde 100644 --- a/models/feed.php +++ b/models/feed.php @@ -57,9 +57,9 @@ function get_favicons(array $feed_ids) } return Database::get('db') - ->table('favicons') + ->hashtable('favicons') ->in('feed_id', $feed_ids) - ->hashmap('feed_id', 'icon'); + ->getAll('feed_id', 'icon'); } // Get all favicons for a list of items @@ -82,8 +82,8 @@ function get_all_favicons() } return Database::get('db') - ->table('favicons') - ->hashmap('feed_id', 'icon'); + ->hashtable('favicons') + ->getAll('feed_id', 'icon'); } // Update feed information @@ -290,7 +290,7 @@ function get_ids($limit = LIMIT_ALL) $query->limit((int) $limit); } - return $query->hashmap('id', 'id'); + return $query->findAllByColumn('id'); } // get number of feeds with errors diff --git a/models/item.php b/models/item.php index 612d0ea..6aeb081 100644 --- a/models/item.php +++ b/models/item.php @@ -68,10 +68,10 @@ function get_everything_since($timestamp) function get_all_status() { return Database::get('db') - ->table('items') + ->hashtable('items') ->in('status', array('read', 'unread')) ->orderBy('updated', 'desc') - ->hashmap('id', 'status'); + ->getAll('id', 'status'); } // Get all items by status diff --git a/vendor/autoload.php b/vendor/autoload.php index 59313d8..01b20b2 100644 --- a/vendor/autoload.php +++ b/vendor/autoload.php @@ -4,4 +4,4 @@ require_once __DIR__ . '/composer' . '/autoload_real.php'; -return ComposerAutoloaderInita3275f8a3fafdaf0ccb719369aca304a::getLoader(); +return ComposerAutoloaderInit489790cbd05fef7749a4adcb52f440da::getLoader(); diff --git a/vendor/composer/autoload_classmap.php b/vendor/composer/autoload_classmap.php index f7187d5..ffb214e 100644 --- a/vendor/composer/autoload_classmap.php +++ b/vendor/composer/autoload_classmap.php @@ -14,6 +14,7 @@ return array( 'PicoDb\\Driver\\Mysql' => $vendorDir . '/fguillot/picodb/lib/PicoDb/Driver/Mysql.php', 'PicoDb\\Driver\\Postgres' => $vendorDir . '/fguillot/picodb/lib/PicoDb/Driver/Postgres.php', 'PicoDb\\Driver\\Sqlite' => $vendorDir . '/fguillot/picodb/lib/PicoDb/Driver/Sqlite.php', + 'PicoDb\\Hashtable' => $vendorDir . '/fguillot/picodb/lib/PicoDb/Hashtable.php', 'PicoDb\\Schema' => $vendorDir . '/fguillot/picodb/lib/PicoDb/Schema.php', 'PicoDb\\Table' => $vendorDir . '/fguillot/picodb/lib/PicoDb/Table.php', 'PicoFeed\\Client\\Client' => $vendorDir . '/fguillot/picofeed/lib/PicoFeed/Client/Client.php', @@ -65,6 +66,7 @@ return array( 'SimpleValidator\\Validators\\Date' => $vendorDir . '/fguillot/simple-validator/src/SimpleValidator/Validators/Date.php', 'SimpleValidator\\Validators\\Email' => $vendorDir . '/fguillot/simple-validator/src/SimpleValidator/Validators/Email.php', 'SimpleValidator\\Validators\\Equals' => $vendorDir . '/fguillot/simple-validator/src/SimpleValidator/Validators/Equals.php', + 'SimpleValidator\\Validators\\Exists' => $vendorDir . '/fguillot/simple-validator/src/SimpleValidator/Validators/Exists.php', 'SimpleValidator\\Validators\\GreaterThan' => $vendorDir . '/fguillot/simple-validator/src/SimpleValidator/Validators/GreaterThan.php', 'SimpleValidator\\Validators\\InArray' => $vendorDir . '/fguillot/simple-validator/src/SimpleValidator/Validators/InArray.php', 'SimpleValidator\\Validators\\Integer' => $vendorDir . '/fguillot/simple-validator/src/SimpleValidator/Validators/Integer.php', @@ -73,6 +75,7 @@ return array( 'SimpleValidator\\Validators\\MacAddress' => $vendorDir . '/fguillot/simple-validator/src/SimpleValidator/Validators/MacAddress.php', 'SimpleValidator\\Validators\\MaxLength' => $vendorDir . '/fguillot/simple-validator/src/SimpleValidator/Validators/MaxLength.php', 'SimpleValidator\\Validators\\MinLength' => $vendorDir . '/fguillot/simple-validator/src/SimpleValidator/Validators/MinLength.php', + 'SimpleValidator\\Validators\\NotEquals' => $vendorDir . '/fguillot/simple-validator/src/SimpleValidator/Validators/NotEquals.php', 'SimpleValidator\\Validators\\NotInArray' => $vendorDir . '/fguillot/simple-validator/src/SimpleValidator/Validators/NotInArray.php', 'SimpleValidator\\Validators\\Numeric' => $vendorDir . '/fguillot/simple-validator/src/SimpleValidator/Validators/Numeric.php', 'SimpleValidator\\Validators\\Range' => $vendorDir . '/fguillot/simple-validator/src/SimpleValidator/Validators/Range.php', diff --git a/vendor/composer/autoload_real.php b/vendor/composer/autoload_real.php index 97a8bfd..55cd67d 100644 --- a/vendor/composer/autoload_real.php +++ b/vendor/composer/autoload_real.php @@ -2,7 +2,7 @@ // autoload_real.php @generated by Composer -class ComposerAutoloaderInita3275f8a3fafdaf0ccb719369aca304a +class ComposerAutoloaderInit489790cbd05fef7749a4adcb52f440da { private static $loader; @@ -19,9 +19,9 @@ class ComposerAutoloaderInita3275f8a3fafdaf0ccb719369aca304a return self::$loader; } - spl_autoload_register(array('ComposerAutoloaderInita3275f8a3fafdaf0ccb719369aca304a', 'loadClassLoader'), true, true); + spl_autoload_register(array('ComposerAutoloaderInit489790cbd05fef7749a4adcb52f440da', 'loadClassLoader'), true, true); self::$loader = $loader = new \Composer\Autoload\ClassLoader(); - spl_autoload_unregister(array('ComposerAutoloaderInita3275f8a3fafdaf0ccb719369aca304a', 'loadClassLoader')); + spl_autoload_unregister(array('ComposerAutoloaderInit489790cbd05fef7749a4adcb52f440da', 'loadClassLoader')); $map = require __DIR__ . '/autoload_namespaces.php'; foreach ($map as $namespace => $path) { @@ -42,14 +42,14 @@ class ComposerAutoloaderInita3275f8a3fafdaf0ccb719369aca304a $includeFiles = require __DIR__ . '/autoload_files.php'; foreach ($includeFiles as $file) { - composerRequirea3275f8a3fafdaf0ccb719369aca304a($file); + composerRequire489790cbd05fef7749a4adcb52f440da($file); } return $loader; } } -function composerRequirea3275f8a3fafdaf0ccb719369aca304a($file) +function composerRequire489790cbd05fef7749a4adcb52f440da($file) { require $file; } diff --git a/vendor/composer/installed.json b/vendor/composer/installed.json index f0a6931..3fd2578 100644 --- a/vendor/composer/installed.json +++ b/vendor/composer/installed.json @@ -1,43 +1,4 @@ [ - { - "name": "fguillot/simple-validator", - "version": "dev-master", - "version_normalized": "9999999-dev", - "source": { - "type": "git", - "url": "https://github.com/fguillot/simpleValidator.git", - "reference": "3bfa1ef0062906c83824ce8db1219914996d9bd4" - }, - "dist": { - "type": "zip", - "url": "https://api.github.com/repos/fguillot/simpleValidator/zipball/3bfa1ef0062906c83824ce8db1219914996d9bd4", - "reference": "3bfa1ef0062906c83824ce8db1219914996d9bd4", - "shasum": "" - }, - "require": { - "php": ">=5.3.0" - }, - "time": "2014-11-25 22:58:14", - "type": "library", - "installation-source": "dist", - "autoload": { - "psr-0": { - "SimpleValidator": "src/" - } - }, - "notification-url": "https://packagist.org/downloads/", - "license": [ - "MIT" - ], - "authors": [ - { - "name": "Frédéric Guillot", - "homepage": "http://fredericguillot.com" - } - ], - "description": "The most easy to use validator library for PHP :)", - "homepage": "https://github.com/fguillot/simpleValidator" - }, { "name": "fguillot/picofarad", "version": "dev-master", @@ -116,6 +77,45 @@ "description": "A simple Json-RPC client/server library that just works", "homepage": "https://github.com/fguillot/JsonRPC" }, + { + "name": "fguillot/simple-validator", + "version": "dev-master", + "version_normalized": "9999999-dev", + "source": { + "type": "git", + "url": "https://github.com/fguillot/simpleValidator.git", + "reference": "9e9502c88ce239901c0fee12a1f504948342ab42" + }, + "dist": { + "type": "zip", + "url": "https://api.github.com/repos/fguillot/simpleValidator/zipball/9e9502c88ce239901c0fee12a1f504948342ab42", + "reference": "9e9502c88ce239901c0fee12a1f504948342ab42", + "shasum": "" + }, + "require": { + "php": ">=5.3.0" + }, + "time": "2015-01-21 02:00:33", + "type": "library", + "installation-source": "dist", + "autoload": { + "psr-0": { + "SimpleValidator": "src/" + } + }, + "notification-url": "https://packagist.org/downloads/", + "license": [ + "MIT" + ], + "authors": [ + { + "name": "Frédéric Guillot", + "homepage": "http://fredericguillot.com" + } + ], + "description": "The most easy to use validator library for PHP :)", + "homepage": "https://github.com/fguillot/simpleValidator" + }, { "name": "fguillot/picofeed", "version": "dev-master", @@ -123,18 +123,18 @@ "source": { "type": "git", "url": "https://github.com/fguillot/picoFeed.git", - "reference": "8858f63e5380e8b1df4d8a4bbc5618931cddde7d" + "reference": "83e406cbfbd188dbfbc0330c0757b037a87b0928" }, "dist": { "type": "zip", - "url": "https://api.github.com/repos/fguillot/picoFeed/zipball/8858f63e5380e8b1df4d8a4bbc5618931cddde7d", - "reference": "8858f63e5380e8b1df4d8a4bbc5618931cddde7d", + "url": "https://api.github.com/repos/fguillot/picoFeed/zipball/83e406cbfbd188dbfbc0330c0757b037a87b0928", + "reference": "83e406cbfbd188dbfbc0330c0757b037a87b0928", "shasum": "" }, "require": { "php": ">=5.3.0" }, - "time": "2015-01-18 02:06:19", + "time": "2015-01-28 01:01:28", "type": "library", "installation-source": "dist", "autoload": { @@ -157,23 +157,23 @@ }, { "name": "fguillot/picodb", - "version": "dev-master", - "version_normalized": "9999999-dev", + "version": "v0.0.2", + "version_normalized": "0.0.2.0", "source": { "type": "git", "url": "https://github.com/fguillot/picoDb.git", - "reference": "9588cf60e74dd6ff63d96f7eb91c4f74b47a11b1" + "reference": "b3ef5f79e7e5e33729fdbf9c02c8a252a3d76b6b" }, "dist": { "type": "zip", - "url": "https://api.github.com/repos/fguillot/picoDb/zipball/9588cf60e74dd6ff63d96f7eb91c4f74b47a11b1", - "reference": "9588cf60e74dd6ff63d96f7eb91c4f74b47a11b1", + "url": "https://api.github.com/repos/fguillot/picoDb/zipball/b3ef5f79e7e5e33729fdbf9c02c8a252a3d76b6b", + "reference": "b3ef5f79e7e5e33729fdbf9c02c8a252a3d76b6b", "shasum": "" }, "require": { "php": ">=5.3.0" }, - "time": "2015-01-20 00:52:01", + "time": "2015-01-25 16:20:14", "type": "library", "installation-source": "dist", "autoload": { diff --git a/vendor/fguillot/picodb/.gitignore b/vendor/fguillot/picodb/.gitignore index dc88567..fae151a 100644 --- a/vendor/fguillot/picodb/.gitignore +++ b/vendor/fguillot/picodb/.gitignore @@ -39,4 +39,5 @@ Thumbs.db # App specific # ################ -example.php \ No newline at end of file +example.php +vendor \ No newline at end of file diff --git a/vendor/fguillot/picodb/README.md b/vendor/fguillot/picodb/README.md index 6e2d9d5..6867679 100644 --- a/vendor/fguillot/picodb/README.md +++ b/vendor/fguillot/picodb/README.md @@ -7,8 +7,9 @@ PicoDb is a minimalist database query builder for PHP. Features -------- -- No dependency -- Easy to use, fast and very lightweight +- Easy to use, easy to hack, fast and very lightweight +- Supported drivers: Sqlite, Mysql, Postgresql +- Requires only PDO - Use prepared statements - Handle schema versions (migrations) - License: [WTFPL](http://www.wtfpl.net) @@ -26,110 +27,149 @@ Documentation ### Installation ```bash -composer require fguillot/picodb dev-master +composer require fguillot/picodb @stable ``` ### Connect to your database ```php - use PicoDb\Database; +use PicoDb\Database; - // Sqlite driver - $db = new Database(['driver' => 'sqlite', 'filename' => ':memory:']); +// Sqlite driver +$db = new Database(['driver' => 'sqlite', 'filename' => ':memory:']); - // Mysql driver - // Optional options: "schema_table" (the default table name is "schema_version") - $db = new Database(array( - 'driver' => 'mysql', - 'hostname' => 'localhost', - 'username' => 'root', - 'password' => '', - 'database' => 'my_db_name', - 'charset' => 'utf8', - )); +// Mysql driver +// Optional options: "schema_table" (the default table name is "schema_version") +$db = new Database(array( + 'driver' => 'mysql', + 'hostname' => 'localhost', + 'username' => 'root', + 'password' => '', + 'database' => 'my_db_name', + 'charset' => 'utf8', +)); ``` ### Execute a SQL request ```php - $db->execute('CREATE TABLE toto (column1 TEXT)'); +$db->execute('CREATE TABLE toto (column1 TEXT)'); ``` ### Insert some data ```php - $db->table('toto')->save(['column1' => 'hey']); +$db->table('toto')->save(['column1' => 'test']); ``` -### Transations +or ```php - $db->transaction(function($db) { - $db->table('toto')->save(['column1' => 'foo']); - $db->table('toto')->save(['column1' => 'bar']); - }); +$db->table('toto')->insert(['column1' => 'test']); +``` + +### Transactions + +```php +$db->transaction(function ($db) { + $db->table('toto')->save(['column1' => 'foo']); + $db->table('toto')->save(['column1' => 'bar']); +}); +``` + +or + +```php +$db->startTransaction(); +// Do something... +$db->closeTransaction(); + +// Rollback +$db->cancelTransaction(); ``` ### Fetch all data ```php - $records = $db->table('toto')->findAll(); +$records = $db->table('toto')->findAll(); - foreach ($records as $record) { - var_dump($record['column1']); - } +foreach ($records as $record) { + var_dump($record['column1']); +} ``` ### Update something - $db->table('toto')->eq('id', 1)->save(['column1' => 'hey']); +```php +$db->table('toto')->eq('id', 1)->save(['column1' => 'hey']); +``` You just need to add a condition to perform an update. ### Remove rows ```php - $db->table('toto')->lowerThan('column1', 10)->remove(); +$db->table('toto')->lowerThan('column1', 10)->remove(); ``` ### Sorting ```php - $db->table('toto')->asc('column1')->findAll(); +$db->table('toto')->asc('column1')->findAll(); ``` or ```php - $db->table('toto')->desc('column1')->findAll(); +$db->table('toto')->desc('column1')->findAll(); +``` + +or + +```php +#db->table('toto')->orderBy('column1', 'ASC')->findAll(); ``` ### Limit and offset ```php - $db->table('toto')->limit(10)->offset(5)->findAll(); +$db->table('toto')->limit(10)->offset(5)->findAll(); ``` ### Fetch only some columns ```php - $db->table('toto')->columns('column1', 'column2')->findAll(); +$db->table('toto')->columns('column1', 'column2')->findAll(); +``` + +### Fetch only one column + +Many rows: + +```php +$db->table('toto')->findAllByColumn('column1'); +``` + +One row: + +```php +$db->table('toto')->findOneColumn('column1'); ``` ### Equals condition ```php - $db->table('toto') - ->equals('column1', 'hey') - ->findAll(); +$db->table('toto') + ->equals('column1', 'hey') + ->findAll(); ``` or ```php - $db->table('toto') - ->eq('column1', 'hey') - ->findAll(); +$db->table('toto') + ->eq('column1', 'hey') + ->findAll(); ``` Yout got: 'SELECT * FROM toto WHERE column1=?' @@ -137,9 +177,9 @@ Yout got: 'SELECT * FROM toto WHERE column1=?' ### IN condition ```php - $db->table('toto') - ->in('column1', ['hey', 'bla']) - ->findAll(); +$db->table('toto') + ->in('column1', ['hey', 'bla']) + ->findAll(); ``` ### Like condition @@ -147,81 +187,81 @@ Yout got: 'SELECT * FROM toto WHERE column1=?' Case-sensitive (only Mysql and Postgres): ```php - $db->table('toto') - ->like('column1', '%Foo%') - ->findAll(); +$db->table('toto') + ->like('column1', '%Foo%') + ->findAll(); ``` Not case-sensitive: ```php - $db->table('toto') - ->ilike('column1', '%foo%') - ->findAll(); +$db->table('toto') + ->ilike('column1', '%foo%') + ->findAll(); ``` ### Lower than ```php - $db->table('toto') - ->lowerThan('column1', 2) - ->findAll(); +$db->table('toto') + ->lowerThan('column1', 2) + ->findAll(); ``` or ```php - $db->table('toto') - ->lt('column1', 2) - ->findAll(); +$db->table('toto') + ->lt('column1', 2) + ->findAll(); ``` ### Lower than or equals ```php - $db->table('toto') - ->lowerThanOrEquals('column1', 2) - ->findAll(); +$db->table('toto') + ->lowerThanOrEquals('column1', 2) + ->findAll(); ``` or ```php - $db->table('toto') - ->lte('column1', 2) - ->findAll(); +$db->table('toto') + ->lte('column1', 2) + ->findAll(); ``` ### Greater than ```php - $db->table('toto') - ->greaterThan('column1', 3) - ->findAll(); +$db->table('toto') + ->greaterThan('column1', 3) + ->findAll(); ``` or ```php - $db->table('toto') - ->gt('column1', 3) - ->findAll(); +$db->table('toto') + ->gt('column1', 3) + ->findAll(); ``` ### Greater than or equals ```php - $db->table('toto') - ->greaterThanOrEquals('column1', 3) - ->findAll(); +$db->table('toto') + ->greaterThanOrEquals('column1', 3) + ->findAll(); ``` or ```php - $db->table('toto') - ->gte('column1', 3) - ->findAll(); +$db->table('toto') + ->gte('column1', 3) + ->findAll(); ``` ### Multiple conditions @@ -229,22 +269,113 @@ or Each condition is joined by a AND. ```php - $db->table('toto') - ->like('column2', '%toto') - ->gte('column1', 3) - ->findAll(); +$db->table('toto') + ->like('column2', '%toto') + ->gte('column1', 3) + ->findAll(); ``` How to make a OR condition: ```php - $db->table('toto') - ->beginOr() - ->like('column2', '%toto') - ->gte('column1', 3) - ->closeOr() - ->eq('column5', 'titi') - ->findAll(); +$db->table('toto') + ->beginOr() + ->like('column2', '%toto') + ->gte('column1', 3) + ->closeOr() + ->eq('column5', 'titi') + ->findAll(); +``` + +### Debugging + +Log generated queries: + +```php +$db->log_queries = true; +``` + +Mesure each query time: + +```php +$db->stopwatch = true; +``` + +Get the number of queries executed: + +```php +echo $db->nb_queries; +``` + +Get log messages: + +```php +print_r($db->getLogMessages()); +``` + +### Hashtable (key/value store) + +How to use a table as a key/value store: + +```php +$db->execute( + 'CREATE TABLE toto ( + column1 TEXT NOT NULL UNIQUE, + column2 TEXT default NULL + )' +); + +$db->table('toto')->insert(['column1' => 'option1', 'column2' => 'value1']); +``` + +Add/Replace some values: + +```php +$db->hashtable('toto') + ->columnKey('column1') + ->columnValue('column2') + ->put(['option1' => 'new value', 'option2' => 'value2'])); +``` + +Get all values: + +```php +$result = $db->hashtable('toto')->columnKey('column1')->columnValue('column2')->get(); +print_r($result); + +Array +( + [option2] => value2 + [option1] => new value +) +``` + +or + +```php +$result = $db->hashtable('toto')->getAll('column1', 'column2'); +``` + +Get a specific value: + +```php +$db->hashtable('toto') + ->columnKey('column1') + ->columnValue('column2') + ->put(['option3' => 'value3']); + +$result = $db->hashtable('toto') + ->columnKey('column1') + ->columnValue('column2') + ->get('option1', 'option3'); + +print_r($result); + +Array +( + [option1] => new value + [option3] => value3 +) ``` ### Schema migrations @@ -258,30 +389,30 @@ How to make a OR condition: Example: ```php - namespace Schema; +namespace Schema; - function version_1($pdo) - { - $pdo->exec(' - CREATE TABLE users ( - id INTEGER PRIMARY KEY, - name TEXT UNIQUE, - email TEXT UNIQUE, - password TEXT - ) - '); - } +function version_1($pdo) +{ + $pdo->exec(' + CREATE TABLE users ( + id INTEGER PRIMARY KEY, + name TEXT UNIQUE, + email TEXT UNIQUE, + password TEXT + ) + '); +} - function version_2($pdo) - { - $pdo->exec(' - CREATE TABLE tags ( - id INTEGER PRIMARY KEY, - name TEXT UNIQUE - ) - '); - } +function version_2($pdo) +{ + $pdo->exec(' + CREATE TABLE tags ( + id INTEGER PRIMARY KEY, + name TEXT UNIQUE + ) + '); +} ``` #### Run schema update automatically @@ -294,21 +425,21 @@ Example: Example: ```php - $last_schema_version = 5; +$last_schema_version = 5; - $db = new PicoDb\Database(array( - 'driver' => 'sqlite', - 'filename' => '/tmp/mydb.sqlite' - )); +$db = new PicoDb\Database(array( + 'driver' => 'sqlite', + 'filename' => '/tmp/mydb.sqlite' +)); - if ($db->schema()->check($last_schema_version)) { +if ($db->schema()->check($last_schema_version)) { - // Do something... - } - else { + // Do something... +} +else { - die('Unable to migrate database schema.'); - } + die('Unable to migrate database schema.'); +} ``` ### Use a singleton to handle database instances @@ -316,24 +447,24 @@ Example: Setup a new instance: ```php - PicoDb\Database::bootstrap('myinstance', function() { +PicoDb\Database::bootstrap('myinstance', function() { - $db = new PicoDb\Database(array( - 'driver' => 'sqlite', - 'filename' => DB_FILENAME - )); + $db = new PicoDb\Database(array( + 'driver' => 'sqlite', + 'filename' => DB_FILENAME + )); - if ($db->schema()->check(DB_VERSION)) { - return $db; - } - else { - die('Unable to migrate database schema.'); - } - }); + if ($db->schema()->check(DB_VERSION)) { + return $db; + } + else { + die('Unable to migrate database schema.'); + } +}); ``` Get this instance anywhere in your code: ```php - PicoDb\Database::get('myinstance')->table(...) -``` \ No newline at end of file +PicoDb\Database::get('myinstance')->table(...) +``` diff --git a/vendor/fguillot/picodb/lib/PicoDb/Database.php b/vendor/fguillot/picodb/lib/PicoDb/Database.php index e24e1b0..d9ac615 100644 --- a/vendor/fguillot/picodb/lib/PicoDb/Database.php +++ b/vendor/fguillot/picodb/lib/PicoDb/Database.php @@ -190,15 +190,20 @@ class Database * * @access public * @param string $value Value + * @param string $table Table name * @return string */ - public function escapeIdentifier($value) + public function escapeIdentifier($value, $table = '') { // Do not escape custom query if (strpos($value, '.') !== false || strpos($value, ' ') !== false) { return $value; } + if (! empty($table)) { + return $this->pdo->escapeIdentifier($table).'.'.$this->pdo->escapeIdentifier($value); + } + return $this->pdo->escapeIdentifier($value); } @@ -311,6 +316,19 @@ class Database return new Table($this, $table_name); } + /** + * Get a hashtable instance + * + * @access public + * @return Picodb\Hashtable + */ + public function hashtable($table_name) + { + require_once __DIR__.'/Table.php'; + require_once __DIR__.'/Hashtable.php'; + return new Hashtable($this, $table_name); + } + /** * Get a schema instance * diff --git a/vendor/fguillot/picodb/lib/PicoDb/Hashtable.php b/vendor/fguillot/picodb/lib/PicoDb/Hashtable.php new file mode 100644 index 0000000..b904c97 --- /dev/null +++ b/vendor/fguillot/picodb/lib/PicoDb/Hashtable.php @@ -0,0 +1,144 @@ +column_key = $column; + return $this; + } + + /** + * Set the value column + * + * @access public + * @param string $column + * @return \PicoDb\Table + */ + public function columnValue($column) + { + $this->column_value = $column; + return $this; + } + + /** + * Insert or update + * + * @access public + * @param array $data + * @return boolean + */ + public function put(array $data) + { + switch ($this->db->getConnection()->getAttribute(PDO::ATTR_DRIVER_NAME)) { + case 'mysql': + $sql = sprintf( + 'REPLACE INTO %s (%s) VALUES %s', + $this->db->escapeIdentifier($this->table_name), + "$this->column_key, $this->column_value", + implode(', ', array_fill(0, count($data), '(?, ?)')) + ); + + foreach ($data as $key => $value) { + $this->values[] = $key; + $this->values[] = $value; + } + + $this->db->execute($sql, $this->values); + break; + case 'sqlite': // multi insert (see mysql) requires sqlite library > 3.7.11 (bundled with PHP 5.5.11+) + // all or nothing + $this->db->startTransaction(); + + foreach($data as $key => $value) { + $sql = sprintf( + 'INSERT OR REPLACE INTO %s (%s) VALUES (?, ?)', + $this->db->escapeIdentifier($this->table_name), + $this->db->escapeIdentifier($this->column_key).', '.$this->db->escapeIdentifier($this->column_value) + ); + + $this->db->execute($sql, array($key, $value)); + } + + break; + default: // no upsert available, need to make a select/update/insert limbo + // all or nothing + $this->db->startTransaction(); + + foreach($data as $key => $value) { + // check if the key exists + $this->eq($this->column_key, $key); + + if ($this->count() === 1) { + // update the record + $this->update(array($this->column_key => $key, $this->column_value => $value)); + } + else { + // insert the record + $this->insert(array($this->column_key => $key, $this->column_value => $value)); + } + } + } + + $this->db->closeTransaction(); + + return true; + } + + /** + * Hashmap result [ [column1 => column2], [], ...] + * + * @access public + * @return array + */ + public function get() + { + $hashmap = array(); + + // setup where condition + if (func_num_args() > 0) { + $this->in($this->column_key, func_get_args()); + } + + // setup to select columns in case that there are more than two + $this->columns($this->column_key, $this->column_value); + + $rq = $this->db->execute($this->buildSelectQuery(), $this->values); + $rows = $rq->fetchAll(PDO::FETCH_NUM); + + foreach ($rows as $row) { + $hashmap[$row[0]] = $row[1]; + } + + return $hashmap; + } + + /** + * Shortcut method to get a hashmap result + * + * @access public + * @param string $key Key column + * @param string $value Value column + * @return array + */ + public function getAll($key, $value) + { + $this->column_key = $key; + $this->column_value = $value; + return $this->get(); + } +} diff --git a/vendor/fguillot/picodb/lib/PicoDb/Table.php b/vendor/fguillot/picodb/lib/PicoDb/Table.php index 806cb5b..43e6867 100644 --- a/vendor/fguillot/picodb/lib/PicoDb/Table.php +++ b/vendor/fguillot/picodb/lib/PicoDb/Table.php @@ -9,7 +9,10 @@ class Table const SORT_ASC = 'ASC'; const SORT_DESC = 'DESC'; - private $table_name = ''; + protected $db; + protected $table_name = ''; + protected $values = array(); + private $sql_limit = ''; private $sql_offset = ''; private $sql_order = ''; @@ -18,11 +21,9 @@ class Table private $or_conditions = array(); private $is_or_condition = false; private $columns = array(); - private $values = array(); private $distinct = false; private $group_by = array(); private $filter_callback = null; - private $db; /** * Constructor @@ -130,30 +131,6 @@ class Table return $result->rowCount() > 0; } - /** - * Hashmap result [ [column1 => column2], [], ...] - * - * @access public - * @param string $key Column 1 - * @param string $value Column 2 - * @return array - */ - public function hashmap($key, $value) - { - $listing = array(); - - $this->columns($key, $value); - $rq = $this->db->execute($this->buildSelectQuery(), $this->values); - - $rows = $rq->fetchAll(PDO::FETCH_NUM); - - foreach ($rows as $row) { - $listing[$row[0]] = $row[1]; - } - - return $listing; - } - /** * Add callback to alter the resultset * @@ -241,7 +218,7 @@ class Table public function buildSelectQuery() { foreach ($this->columns as $key => $value) { - $this->columns[$key] = $this->db->escapeIdentifier($value); + $this->columns[$key] = $this->db->escapeIdentifier($value, $this->table_name); } return sprintf( @@ -267,7 +244,7 @@ class Table public function count() { $sql = sprintf( - 'SELECT COUNT(*) FROM %s'.$this->conditions().$this->sql_order.$this->sql_limit.$this->sql_offset, + 'SELECT COUNT(*) FROM %s '.implode(' ', $this->joins).$this->conditions().$this->sql_order.$this->sql_limit.$this->sql_offset, $this->db->escapeIdentifier($this->table_name) ); diff --git a/vendor/fguillot/picofeed/lib/PicoFeed/Client/Client.php b/vendor/fguillot/picofeed/lib/PicoFeed/Client/Client.php index 9f358a6..55d2c56 100644 --- a/vendor/fguillot/picofeed/lib/PicoFeed/Client/Client.php +++ b/vendor/fguillot/picofeed/lib/PicoFeed/Client/Client.php @@ -157,14 +157,47 @@ abstract class Client */ protected $status_code = 0; + /** + * HTTP response body + * + * @access protected + * @var string + */ + protected $body = ''; + + /** + * Body size + * + * @access protected + * @var integer + */ + protected $body_length = 0; + + /** + * HTTP response headers + * + * @access protected + * @var array + */ + protected $headers = array(); + + /** + * Counter on the number of header received + * + * @access protected + * @var integer + */ + protected $headers_counter = 0; + /** * Do the HTTP request * * @abstract * @access public + * @param bool $follow_location Flag used when there is an open_basedir restriction * @return array */ - abstract public function doRequest(); + abstract public function doRequest($follow_location = true); /** * Get client instance: curl or stream driver @@ -262,6 +295,48 @@ abstract class Client } } + /** + * Handle manually redirections when there is an open base dir restriction + * + * @access private + * @param string $location Redirected URL + * @return array + */ + public function handleRedirection($location) + { + $nb_redirects = 0; + $result = array(); + $this->url = Url::resolve($location, $this->url); + $this->body = ''; + $this->body_length = 0; + $this->headers = array(); + $this->headers_counter = 0; + + while (true) { + + $nb_redirects++; + + if ($nb_redirects >= $this->max_redirects) { + throw new MaxRedirectException('Maximum number of redirections reached'); + } + + $result = $this->doRequest(false); + + if ($result['status'] == 301 || $result['status'] == 302) { + $this->url = $result['headers']['Location']; + $this->body = ''; + $this->body_length = 0; + $this->headers = array(); + $this->headers_counter = 0; + } + else { + break; + } + } + + return $result; + } + /** * Check if a request has been modified according to the parameters * diff --git a/vendor/fguillot/picofeed/lib/PicoFeed/Client/Curl.php b/vendor/fguillot/picofeed/lib/PicoFeed/Client/Curl.php index 21ecf5a..d45773d 100644 --- a/vendor/fguillot/picofeed/lib/PicoFeed/Client/Curl.php +++ b/vendor/fguillot/picofeed/lib/PicoFeed/Client/Curl.php @@ -12,38 +12,6 @@ use PicoFeed\Logging\Logger; */ class Curl extends Client { - /** - * HTTP response body - * - * @access private - * @var string - */ - private $body = ''; - - /** - * Body size - * - * @access private - * @var integer - */ - private $body_length = 0; - - /** - * HTTP response headers - * - * @access private - * @var array - */ - private $headers = array(); - - /** - * Counter on the number of header received - * - * @access private - * @var integer - */ - private $headers_counter = 0; - /** * cURL callback to read the HTTP body * @@ -260,48 +228,6 @@ class Curl extends Client return $follow_location && ini_get('open_basedir') !== '' && ($status == 301 || $status == 302); } - /** - * Handle manually redirections when there is an open base dir restriction - * - * @access private - * @param string $location Redirected URL - * @return array - */ - private function handleRedirection($location) - { - $nb_redirects = 0; - $result = array(); - $this->url = $location; - $this->body = ''; - $this->body_length = 0; - $this->headers = array(); - $this->headers_counter = 0; - - while (true) { - - $nb_redirects++; - - if ($nb_redirects >= $this->max_redirects) { - throw new MaxRedirectException('Maximum number of redirections reached'); - } - - $result = $this->doRequest(false); - - if ($result['status'] == 301 || $result['status'] == 302) { - $this->url = $result['headers']['Location']; - $this->body = ''; - $this->body_length = 0; - $this->headers = array(); - $this->headers_counter = 0; - } - else { - break; - } - } - - return $result; - } - /** * Handle cURL errors (throw individual exceptions) * diff --git a/vendor/fguillot/picofeed/lib/PicoFeed/Client/Stream.php b/vendor/fguillot/picofeed/lib/PicoFeed/Client/Stream.php index ba2fd8e..b80e731 100644 --- a/vendor/fguillot/picofeed/lib/PicoFeed/Client/Stream.php +++ b/vendor/fguillot/picofeed/lib/PicoFeed/Client/Stream.php @@ -61,7 +61,7 @@ class Stream extends Client 'method' => 'GET', 'protocol_version' => 1.1, 'timeout' => $this->timeout, - 'max_redirects' => $this->max_redirects, + 'follow_location' => 0, ) ); @@ -89,9 +89,10 @@ class Stream extends Client * Do the HTTP request * * @access public - * @return array HTTP response ['body' => ..., 'status' => ..., 'headers' => ...] + * @param bool $follow_location Flag used when there is an open_basedir restriction + * @return array HTTP response ['body' => ..., 'status' => ..., 'headers' => ...] */ - public function doRequest() + public function doRequest($follow_location = false) { // Create context $context = stream_context_create($this->prepareContext()); @@ -121,6 +122,12 @@ class Stream extends Client fclose($stream); + // Do redirect manual to get only the headers of the last request and + // the final url + if ($status == 301 || $status == 302) { + return $this->handleRedirection($headers['Location']); + } + return array( 'status' => $status, 'body' => $this->decodeBody($body, $headers), diff --git a/vendor/fguillot/picofeed/lib/PicoFeed/Client/Url.php b/vendor/fguillot/picofeed/lib/PicoFeed/Client/Url.php index fc1150c..f03305c 100644 --- a/vendor/fguillot/picofeed/lib/PicoFeed/Client/Url.php +++ b/vendor/fguillot/picofeed/lib/PicoFeed/Client/Url.php @@ -55,13 +55,13 @@ class Url * * @static * @access public - * @param string $item_url Unknown url (can be relative or not) - * @param mixed $website_url Website url + * @param mixed $item_url Unknown url (can be relative or not) + * @param mixed $website_url Website url * @return string */ public static function resolve($item_url, $website_url) { - $link = new Url($item_url); + $link = is_string($item_url) ? new Url($item_url) : $item_url; $website = is_string($website_url) ? new Url($website_url) : $website_url; if ($link->isRelativeUrl()) { @@ -172,7 +172,7 @@ class Url $path = $this->isRelativePath() ? '/' : ''; $path .= substr($current_path, -1) === '/' ? $current_path : dirname($current_path); - return str_replace('//', '/', $path.'/'); + return preg_replace('/\\\\\/|\/\//', '/', $path.'/'); } /** diff --git a/vendor/fguillot/picofeed/lib/PicoFeed/Encoding/Encoding.php b/vendor/fguillot/picofeed/lib/PicoFeed/Encoding/Encoding.php index 7739def..0be017f 100644 --- a/vendor/fguillot/picofeed/lib/PicoFeed/Encoding/Encoding.php +++ b/vendor/fguillot/picofeed/lib/PicoFeed/Encoding/Encoding.php @@ -159,6 +159,7 @@ class Encoding return $input; case 'windows-1251': case 'windows-1255': + case 'windows-1256': return iconv($encoding, 'UTF-8//TRANSLIT', $input); default: return self::toUTF8($input); diff --git a/vendor/fguillot/picofeed/lib/PicoFeed/Parser/XmlParser.php b/vendor/fguillot/picofeed/lib/PicoFeed/Parser/XmlParser.php index 687ce99..73a4a43 100644 --- a/vendor/fguillot/picofeed/lib/PicoFeed/Parser/XmlParser.php +++ b/vendor/fguillot/picofeed/lib/PicoFeed/Parser/XmlParser.php @@ -198,8 +198,10 @@ class XmlParser $p1 = strpos($data, 'encoding='); $p2 = strpos($data, '"', $p1 + 10); - $encoding = substr($data, $p1 + 10, $p2 - $p1 - 10); - $encoding = strtolower($encoding); + if ($p1 !== false && $p2 !== false) { + $encoding = substr($data, $p1 + 10, $p2 - $p1 - 10); + $encoding = strtolower($encoding); + } } return $encoding; diff --git a/vendor/fguillot/picofeed/lib/PicoFeed/Reader/Favicon.php b/vendor/fguillot/picofeed/lib/PicoFeed/Reader/Favicon.php index 8e674af..6b26400 100644 --- a/vendor/fguillot/picofeed/lib/PicoFeed/Reader/Favicon.php +++ b/vendor/fguillot/picofeed/lib/PicoFeed/Reader/Favicon.php @@ -172,7 +172,7 @@ class Favicon } foreach ($icons as $icon_link) { - $icon_link = $this->convertLink($website, new Url($icon_link)); + $icon_link = Url::resolve($icon_link, $website); $resource = $this->download($icon_link); $this->content = $resource->getContent(); $this->content_type = $resource->getContentType(); @@ -187,28 +187,6 @@ class Favicon return ''; } - /** - * Convert icon links to absolute url - * - * @access public - * @param \PicoFeed\Client\Url $website Website url - * @param \PicoFeed\Client\Url $icon Icon url - * @return string - */ - public function convertLink(Url $website, Url $icon) - { - $base_url = ''; - - if ($icon->isRelativeUrl()) { - $base_url = $website->getBaseUrl(); - } - else if ($icon->isProtocolRelative()) { - $icon->setScheme($website->getScheme()); - } - - return $icon->getAbsoluteUrl($base_url); - } - /** * Extract the icon links from the HTML * diff --git a/vendor/fguillot/picofeed/tests/Client/CurlTest.php b/vendor/fguillot/picofeed/tests/Client/CurlTest.php index 6688160..4509dbc 100644 --- a/vendor/fguillot/picofeed/tests/Client/CurlTest.php +++ b/vendor/fguillot/picofeed/tests/Client/CurlTest.php @@ -29,6 +29,7 @@ class CurlTest extends PHPUnit_Framework_TestCase $this->assertEquals(200, $result['status']); $this->assertEquals('assertEquals('text/html; charset=utf-8', $result['headers']['Content-Type']); + $this->assertEquals('http://miniflux.net/', $client->getUrl()); } /** diff --git a/vendor/fguillot/picofeed/tests/Client/StreamTest.php b/vendor/fguillot/picofeed/tests/Client/StreamTest.php index 8b2e2f8..91f52c8 100644 --- a/vendor/fguillot/picofeed/tests/Client/StreamTest.php +++ b/vendor/fguillot/picofeed/tests/Client/StreamTest.php @@ -30,12 +30,14 @@ class StreamTest extends PHPUnit_Framework_TestCase public function testRedirect() { $client = new Stream; - $client->setUrl('http://github.com/fguillot/picoFeed'); + $client->setUrl('http://www.miniflux.net/index.html'); $result = $client->doRequest(); + $this->assertTrue(is_array($result)); $this->assertEquals(200, $result['status']); - $this->assertEquals('', substr(trim($result['body']), 0, 15)); + $this->assertEquals('assertEquals('text/html; charset=utf-8', $result['headers']['Content-Type']); + $this->assertEquals('http://miniflux.net/', $client->getUrl()); } /** diff --git a/vendor/fguillot/picofeed/tests/Client/UrlTest.php b/vendor/fguillot/picofeed/tests/Client/UrlTest.php index 90055b3..f55d301 100644 --- a/vendor/fguillot/picofeed/tests/Client/UrlTest.php +++ b/vendor/fguillot/picofeed/tests/Client/UrlTest.php @@ -217,39 +217,76 @@ AAAFCAYAAACNbyblAAAAHElEQVQI12P4//8/w38GIAXDIBKE0DHxgljNBAAO public function testResolve() { + // relative link $this->assertEquals( - 'http://www.la-grange.net/2014/08/03/4668-noisettes', - Url::resolve('/2014/08/03/4668-noisettes', 'http://www.la-grange.net') + 'http://miniflux.net/assets/img/favicon.png', + Url::resolve('assets/img/favicon.png', 'http://miniflux.net') ); + // relative link + HTTPS $this->assertEquals( - 'http://www.la-grange.net/2014/08/03/4668-noisettes', - Url::resolve('/2014/08/03/4668-noisettes', 'http://www.la-grange.net/') + 'https://miniflux.net/assets/img/favicon.png', + Url::resolve('assets/img/favicon.png', 'https://miniflux.net') ); + // absolute link $this->assertEquals( - 'http://www.la-grange.net/2014/08/03/4668-noisettes', - Url::resolve('/2014/08/03/4668-noisettes', 'http://www.la-grange.net/feed.atom') + 'http://miniflux.net/assets/img/favicon.png', + Url::resolve('/assets/img/favicon.png', 'http://miniflux.net') ); + // absolute link + HTTPS $this->assertEquals( - 'http://what-if.xkcd.com/imgs/a/112/driving.png', - Url::resolve('/imgs/a/112/driving.png', 'http://what-if.xkcd.com/feed.atom') + 'https://miniflux.net/assets/img/favicon.png', + Url::resolve('/assets/img/favicon.png', 'https://miniflux.net') ); + // Protocol relative link $this->assertEquals( - 'http://website/subfolder/img/foo.png', - Url::resolve('img/foo.png', 'http://website/subfolder/') + 'http://google.com/assets/img/favicon.png', + Url::resolve('//google.com/assets/img/favicon.png', 'http://miniflux.net') ); + // Protocol relative link + HTTPS $this->assertEquals( - 'http://website/img/foo.png', - Url::resolve('/img/foo.png', 'http://website/subfolder/') + 'https://google.com/assets/img/favicon.png', + Url::resolve('//google.com/assets/img/favicon.png', 'https://miniflux.net') ); + // URL same fqdn $this->assertEquals( - 'http://www.lofibucket.com/articles/img/quakescope.jpg', - Url::resolve('img/quakescope.jpg', 'http://www.lofibucket.com/articles/oscilloscope_quake.html') + 'http://miniflux.net/assets/img/favicon.png', + Url::resolve('http://miniflux.net/assets/img/favicon.png', 'https://miniflux.net') + ); + + // URL different fqdn + $this->assertEquals( + 'https://www.google.com/assets/img/favicon.png', + Url::resolve('https://www.google.com/assets/img/favicon.png', 'https://miniflux.net') + ); + + // HTTPS URL + $this->assertEquals( + 'https://miniflux.net/assets/img/favicon.png', + Url::resolve('https://miniflux.net/assets/img/favicon.png', 'https://miniflux.net') + ); + + // empty string on missing website parameter + $this->assertEquals( + '', + Url::resolve('favicon.png', '') + ); + + // website only on missing icon parameter + $this->assertEquals( + 'https://miniflux.net/', + Url::resolve('', 'https://miniflux.net') + ); + + // empty string on missing website and icon parameter + $this->assertEquals( + '', + Url::resolve('', '') ); } } diff --git a/vendor/fguillot/picofeed/tests/Parser/XmlParserTest.php b/vendor/fguillot/picofeed/tests/Parser/XmlParserTest.php index b20b3f6..87bd9da 100644 --- a/vendor/fguillot/picofeed/tests/Parser/XmlParserTest.php +++ b/vendor/fguillot/picofeed/tests/Parser/XmlParserTest.php @@ -13,6 +13,7 @@ class XmlParserTest extends PHPUnit_Framework_TestCase $this->assertEquals('utf-8', XmlParser::getEncodingFromXmlTag("assertEquals('utf-8', XmlParser::getEncodingFromXmlTag('assertEquals('windows-1251', XmlParser::getEncodingFromXmlTag('')); + $this->assertEquals('', XmlParser::getEncodingFromXmlTag("assertFalse($favicon->exists('')); } - public function testConvertLink() - { - $favicon = new Favicon; - - // relative link - $this->assertEquals( - 'http://miniflux.net/assets/img/favicon.png', - $favicon->convertLink(new Url('http://miniflux.net'), new Url('assets/img/favicon.png')) - ); - - // relative link + HTTPS - $this->assertEquals( - 'https://miniflux.net/assets/img/favicon.png', - $favicon->convertLink(new Url('https://miniflux.net'), new Url('assets/img/favicon.png')) - ); - - // absolute link - $this->assertEquals( - 'http://miniflux.net/assets/img/favicon.png', - $favicon->convertLink(new Url('http://miniflux.net'), new Url('/assets/img/favicon.png')) - ); - - // absolute link + HTTPS - $this->assertEquals( - 'https://miniflux.net/assets/img/favicon.png', - $favicon->convertLink(new Url('https://miniflux.net'), new Url('/assets/img/favicon.png')) - ); - - // Protocol relative link - $this->assertEquals( - 'http://google.com/assets/img/favicon.png', - $favicon->convertLink(new Url('http://miniflux.net'), new Url('//google.com/assets/img/favicon.png')) - ); - - // Protocol relative link + HTTPS - $this->assertEquals( - 'https://google.com/assets/img/favicon.png', - $favicon->convertLink(new Url('https://miniflux.net'), new Url('//google.com/assets/img/favicon.png')) - ); - - // URL same fqdn - $this->assertEquals( - 'http://miniflux.net/assets/img/favicon.png', - $favicon->convertLink(new Url('https://miniflux.net'), new Url('http://miniflux.net/assets/img/favicon.png')) - ); - - // URL different fqdn - $this->assertEquals( - 'https://www.google.com/assets/img/favicon.png', - $favicon->convertLink(new Url('https://miniflux.net'), new Url('https://www.google.com/assets/img/favicon.png')) - ); - - // HTTPS URL - $this->assertEquals( - 'https://miniflux.net/assets/img/favicon.png', - $favicon->convertLink(new Url('https://miniflux.net'), new Url('https://miniflux.net/assets/img/favicon.png')) - ); - - // empty string on missing website parameter - $this->assertEquals( - '', - $favicon->convertLink(new Url(''), new Url('favicon.png')) - ); - - // website only on missing icon parameter - $this->assertEquals( - 'https://miniflux.net/', - $favicon->convertLink(new Url('https://miniflux.net'), new Url('')) - ); - - // empty string on missing website and icon parameter - $this->assertEquals( - '', - $favicon->convertLink(new Url(''), new Url('')) - ); - } - public function testFind_inMeta() { $favicon = new Favicon; diff --git a/vendor/fguillot/simple-validator/.gitignore b/vendor/fguillot/simple-validator/.gitignore index 7701945..804c79d 100644 --- a/vendor/fguillot/simple-validator/.gitignore +++ b/vendor/fguillot/simple-validator/.gitignore @@ -35,4 +35,6 @@ Thumbs.db *.swp *~ *.lock +.buildpath +.project vendor diff --git a/vendor/fguillot/simple-validator/README.md b/vendor/fguillot/simple-validator/README.md index 1e15116..8ead7a7 100644 --- a/vendor/fguillot/simple-validator/README.md +++ b/vendor/fguillot/simple-validator/README.md @@ -28,7 +28,7 @@ Frédéric Guillot: [http://fredericguillot.com](http://fredericguillot.com) Source code ----------- -On Github: [https://github.com/fguillot/simpleLogger](https://github.com/fguillot/simpleValidator) +On Github: [https://github.com/fguillot/simpleValidator](https://github.com/fguillot/simpleValidator) License @@ -109,6 +109,28 @@ The specified field must exists. new Validators\Required($field, $error_message); +### Equals + +The specified fields must be equals. + + new Validators\Equals($field, $field2, $error_message); + + +### NotEquals + +The specified fields must not be equals. + + new Validators\NotEquals($field, $field2, $error_message); + +### Exists + +Check inside a database if the column value exists or not. + + new Validators\Exists($field, $error_message, PDO $pdo, $table, $key = ''); + +`$pdo` must be an PDO instance, `$table` is the table name. +By default the key used in the table is the value of `$field`, but it can be override if the field doesn't have the same name in the database. + ### Unique Check inside a database if the column value is unique or not. @@ -116,5 +138,36 @@ Check inside a database if the column value is unique or not. new Validators\Unique($field, $error_message, PDO $pdo, $table, $primary_key = 'id'); `$pdo` must be an PDO instance, `$table` is the table name and by default the primary key is "id". + +`$field` can either be a string or an array of string to check inside a database if a value from several columns is unique or not. + If the primary key value is not null, we don't check the uniqueness of the column for this row. It's useful if you perform validation for an update. + +Contributors +---------- +### Install the latest version of PHPUnit + +Simply download the PHPUnit PHAR et copy the file somewhere in your $PATH: + + wget https://phar.phpunit.de/phpunit.phar + chmod +x phpunit.phar + sudo mv phpunit.phar /usr/local/bin/phpunit + phpunit --version + PHPUnit 4.2.6 by Sebastian Bergmann. + +### Running unit tests + +Sqlite tests use a in-memory database, nothing is written on the filesystem. + +Example: + + phpunit --bootstrap vendor/autoload.php tests + PHPUnit 4.4.2 by Sebastian Bergmann. + + ............ + + Time: 69 ms, Memory: 3.75Mb + + OK (12 tests, 149 assertions) + diff --git a/vendor/fguillot/simple-validator/src/SimpleValidator/Base.php b/vendor/fguillot/simple-validator/src/SimpleValidator/Base.php index 45c01a6..9c921a0 100644 --- a/vendor/fguillot/simple-validator/src/SimpleValidator/Base.php +++ b/vendor/fguillot/simple-validator/src/SimpleValidator/Base.php @@ -39,6 +39,9 @@ abstract class Base public function getField() { + if (is_array($this->field)) { + return $this->field[0]; + } return $this->field; } } \ No newline at end of file diff --git a/vendor/fguillot/simple-validator/src/SimpleValidator/Validators/Exists.php b/vendor/fguillot/simple-validator/src/SimpleValidator/Validators/Exists.php new file mode 100644 index 0000000..8f851ae --- /dev/null +++ b/vendor/fguillot/simple-validator/src/SimpleValidator/Validators/Exists.php @@ -0,0 +1,58 @@ + + * + * This source file is subject to the MIT license that is bundled + * with this source code in the file LICENSE. + */ + +namespace SimpleValidator\Validators; + +use SimpleValidator\Base; + +/** + * @author Olivier Maridat + */ +class Exists extends Base +{ + private $pdo; + private $key; + private $table; + + + public function __construct($field, $error_message, \PDO $pdo, $table, $key = '') + { + parent::__construct($field, $error_message); + + $this->pdo = $pdo; + $this->table = $table; + $this->key = $key; + } + + + public function execute(array $data) + { + if (! isset($data[$this->field]) || '' === $data[$this->field]) { + return true; + } + if ($this->key === '') { + $this->key = $this->field; + } + + $rq = $this->pdo->prepare('SELECT COUNT(*) FROM '.$this->table.' WHERE '.$this->key.'=?'); + $rq->execute(array( + $data[$this->field] + )); + + $result = $rq->fetch(\PDO::FETCH_NUM); + + if (isset($result[0]) && $result[0] >= '1') { + return true; + } + + return false; + } +} \ No newline at end of file diff --git a/vendor/fguillot/simple-validator/src/SimpleValidator/Validators/NotEquals.php b/vendor/fguillot/simple-validator/src/SimpleValidator/Validators/NotEquals.php new file mode 100644 index 0000000..0069633 --- /dev/null +++ b/vendor/fguillot/simple-validator/src/SimpleValidator/Validators/NotEquals.php @@ -0,0 +1,43 @@ + + * + * This source file is subject to the MIT license that is bundled + * with this source code in the file LICENSE. + */ + +namespace SimpleValidator\Validators; + +use SimpleValidator\Base; + +/** + * @author Olivier Maridat + */ +class NotEquals extends Base +{ + private $field2; + + + public function __construct($field1, $field2, $error_message) + { + parent::__construct($field1, $error_message); + + $this->field2 = $field2; + } + + + public function execute(array $data) + { + if (isset($data[$this->field]) && $data[$this->field] !== '') { + + if (! isset($data[$this->field2])) return true; + + return $data[$this->field] !== $data[$this->field2]; + } + + return true; + } +} \ No newline at end of file diff --git a/vendor/fguillot/simple-validator/src/SimpleValidator/Validators/Unique.php b/vendor/fguillot/simple-validator/src/SimpleValidator/Validators/Unique.php index c20dbe1..fe47980 100644 --- a/vendor/fguillot/simple-validator/src/SimpleValidator/Validators/Unique.php +++ b/vendor/fguillot/simple-validator/src/SimpleValidator/Validators/Unique.php @@ -15,6 +15,7 @@ use SimpleValidator\Base; /** * @author Frédéric Guillot + * @author Olivier Maridat */ class Unique extends Base { @@ -35,44 +36,35 @@ class Unique extends Base public function execute(array $data) { - if (isset($data[$this->field]) && $data[$this->field] !== '') { + if (! is_array($this->field)) { + $this->field = array($this->field); + } + $fields = array(); + $parameters = array(); + foreach($this->field AS $field) { + if (! isset($data[$field]) || $data[$field] === '') { + return true; + } + $fields[] = $field; + $parameters[] = $data[$field]; + } + + if (isset($data[$this->primary_key])) { + $parameters[] = $data[$this->primary_key]; + } - if (! isset($data[$this->primary_key])) { - - $rq = $this->pdo->prepare('SELECT COUNT(*) FROM '.$this->table.' WHERE '.$this->field.'=?'); - - $rq->execute(array( - $data[$this->field] - )); - - $result = $rq->fetch(\PDO::FETCH_NUM); - - if (isset($result[0]) && $result[0] === '1') { - - return false; - } - } - else { - - $rq = $this->pdo->prepare( - 'SELECT COUNT(*) FROM '.$this->table.' - WHERE '.$this->field.'=? AND '.$this->primary_key.' != ?' - ); - - $rq->execute(array( - $data[$this->field], - $data[$this->primary_key] - )); - - $result = $rq->fetch(\PDO::FETCH_NUM); - - if (isset($result[0]) && $result[0] === '1') { - - return false; - } - } - } - - return true; + $rq = $this->pdo->prepare( + 'SELECT COUNT(*) FROM '.$this->table.' WHERE '.implode('=? AND ', $fields).'=?'.(isset($data[$this->primary_key]) ? ' AND '.$this->primary_key.'!=?' : '') + ); + + $rq->execute($parameters); + + $result = $rq->fetch(\PDO::FETCH_NUM); + + if (isset($result[0]) && $result[0] === '1') { + return false; + } + + return true; } } \ No newline at end of file diff --git a/vendor/fguillot/simple-validator/tests/ExistsValidatorTest.php b/vendor/fguillot/simple-validator/tests/ExistsValidatorTest.php new file mode 100644 index 0000000..14afca1 --- /dev/null +++ b/vendor/fguillot/simple-validator/tests/ExistsValidatorTest.php @@ -0,0 +1,74 @@ +exec('CREATE TABLE mytable (id INTEGER, toto TEXT)'); + + $message = 'toto doesn\'t exist'; + + $v = new Exists('id', $message, $pdo, 'mytable'); + + $this->assertEquals($message, $v->getErrorMessage()); + + $this->assertTrue($v->execute(array('id' => ''))); + $this->assertTrue($v->execute(array('id' => null))); + + $this->assertFalse($v->execute(array('id' => '1'))); + + $pdo->exec("INSERT INTO mytable VALUES ('1', 'truc')"); + + $this->assertTrue($v->execute(array('id' => '1'))); + $this->assertFalse($v->execute(array('id' => '0'))); + $this->assertTrue($v->execute(array('id' => ''))); + $this->assertTrue($v->execute(array('id' => null))); + + $pdo->exec("INSERT INTO mytable VALUES ('2', 'muche')"); + + $this->assertTrue($v->execute(array('id' => '2'))); + $this->assertTrue($v->execute(array('id' => '1'))); + $this->assertFalse($v->execute(array('id' => '0'))); + $this->assertTrue($v->execute(array('id' => ''))); + $this->assertTrue($v->execute(array('id' => null))); + } + + public function testValidatorWithDifferentKey() + { + $pdo = new PDO('sqlite::memory:'); + $pdo->exec('CREATE TABLE mytable (id INTEGER, toto TEXT)'); + + $message = 'toto doesn\'t exist'; + + $v = new Exists('toto_id', $message, $pdo, 'mytable', 'id'); + + $this->assertEquals($message, $v->getErrorMessage()); + + $this->assertTrue($v->execute(array('toto_id' => ''))); + $this->assertTrue($v->execute(array('toto_id' => null))); + + $this->assertFalse($v->execute(array('toto_id' => '1'))); + + $pdo->exec("INSERT INTO mytable VALUES ('1', 'truc')"); + + $this->assertTrue($v->execute(array('toto_id' => '1'))); + $this->assertFalse($v->execute(array('toto_id' => '0'))); + $this->assertTrue($v->execute(array('toto_id' => ''))); + $this->assertTrue($v->execute(array('toto_id' => null))); + + $pdo->exec("INSERT INTO mytable VALUES ('2', 'muche')"); + + $this->assertTrue($v->execute(array('toto_id' => '2'))); + $this->assertTrue($v->execute(array('toto_id' => '1'))); + $this->assertFalse($v->execute(array('toto_id' => '0'))); + $this->assertTrue($v->execute(array('toto_id' => ''))); + $this->assertTrue($v->execute(array('toto_id' => null))); + } +} \ No newline at end of file diff --git a/vendor/fguillot/simple-validator/tests/NotEqualsValidator.php b/vendor/fguillot/simple-validator/tests/NotEqualsValidator.php new file mode 100644 index 0000000..92a1f1e --- /dev/null +++ b/vendor/fguillot/simple-validator/tests/NotEqualsValidator.php @@ -0,0 +1,35 @@ +assertEquals($message, $v->getErrorMessage()); + + $this->assertFalse($v->execute(array('toto' => 'test', 'titi' => 'test'))); + $this->assertTrue($v->execute(array('toto' => 'test', 'titi' => 'testest'))); + + $this->assertTrue($v->execute(array('toto' => 'test'))); + $this->assertTrue($v->execute(array('toto' => 'test', 'titi' => ''))); + $this->assertTrue($v->execute(array('toto' => 'test', 'titi' => null))); + $this->assertTrue($v->execute(array('toto' => 'test'))); + $this->assertTrue($v->execute(array('toto' => '', 'titi' => 'test'))); + $this->assertTrue($v->execute(array('toto' => null, 'titi' => 'test'))); + $this->assertTrue($v->execute(array('titi' => 'test'))); + + $this->assertTrue($v->execute(array())); + $this->assertTrue($v->execute(array('toto' => ''))); + $this->assertTrue($v->execute(array('toto' => null))); + $this->assertTrue($v->execute(array('titi' => ''))); + $this->assertTrue($v->execute(array('titi' => null))); + } +} \ No newline at end of file diff --git a/vendor/fguillot/simple-validator/tests/UniqueValidatorTest.php b/vendor/fguillot/simple-validator/tests/UniqueValidatorTest.php index c2e72e8..556f0d7 100644 --- a/vendor/fguillot/simple-validator/tests/UniqueValidatorTest.php +++ b/vendor/fguillot/simple-validator/tests/UniqueValidatorTest.php @@ -35,4 +35,49 @@ class UniqueValidatorTest extends PHPUnit_Framework_TestCase $this->assertFalse($v->execute(array('toto' => 'truc', 'id' => '2'))); } + + public function testValidatorWithArray() + { + $pdo = new PDO('sqlite::memory:'); + $pdo->exec('CREATE TABLE mytable (id INTEGER, toto TEXT, tata TEXT)'); + + $message = 'field must be unique'; + + $v = new Unique(array('toto', 'tata'), $message, $pdo, 'mytable'); + + $this->assertEquals($message, $v->getErrorMessage()); + + $this->assertTrue($v->execute(array('toto' => ''))); + $this->assertTrue($v->execute(array('toto' => null))); + + $this->assertTrue($v->execute(array('toto' => 'bidule'))); + $this->assertTrue($v->execute(array('toto' => 'bidule', 'tata' => 'machin'))); + $this->assertTrue($v->execute(array('toto' => 'bidule', 'toutou' => 'machin'))); + + $pdo->exec("INSERT INTO mytable VALUES ('1', 'truc', 'muche')"); + + $this->assertTrue($v->execute(array('toto' => 'truc'))); + $this->assertTrue($v->execute(array('toto' => 'bidule'))); + + $this->assertTrue($v->execute(array('toto' => 'bidule', 'tata' => 'miouch'))); + $this->assertTrue($v->execute(array('toto' => 'bidule', 'tata' => 'muche'))); + $this->assertTrue($v->execute(array('toto' => 'bidule', 'toutou' => 'muche'))); + + $this->assertFalse($v->execute(array('toto' => 'truc', 'tata' => 'muche'))); + $this->assertTrue($v->execute(array('toto' => 'truc', 'tata' => 'miouch'))); + + $pdo->exec("INSERT INTO mytable VALUES ('2', 'bidule', 'machin')"); + + $this->assertTrue($v->execute(array('toto' => 'bidule'))); + $this->assertTrue($v->execute(array('toto' => 'truc'))); + + $this->assertTrue($v->execute(array('toto' => 'bidule', 'tata' => 'muche'))); + $this->assertFalse($v->execute(array('toto' => 'bidule', 'tata' => 'machin'))); + $this->assertTrue($v->execute(array('toto' => 'bidule', 'tata' => 'muche', 'id' => '2'))); + $this->assertTrue($v->execute(array('toto' => 'bidule', 'tata' => 'machin', 'id' => '2'))); + + $this->assertTrue($v->execute(array('toto' => 'truc', 'id' => '2'))); + $this->assertFalse($v->execute(array('toto' => 'truc', 'tata' => 'muche', 'id' => '2'))); + $this->assertTrue($v->execute(array('toto' => 'truc', 'tata' => 'miouch', 'id' => '2'))); + } } \ No newline at end of file