From 22e7d3657b641637b0e693758390fe16d1d1b945 Mon Sep 17 00:00:00 2001 From: Marcin Kurczewski Date: Fri, 3 Oct 2014 20:17:00 +0200 Subject: [PATCH] Added order:random support to post searching --- TODO | 4 ---- src/Dao/AbstractDao.php | 18 +++++++++++++++++- src/DatabaseConnection.php | 5 +++++ src/SearchServices/Filters/BasicFilter.php | 2 ++ .../Parsers/PostSearchParser.php | 3 +++ src/Upgrades/Upgrade01.php | 2 +- src/Upgrades/Upgrade03.php | 2 +- src/Upgrades/Upgrade05.php | 2 +- src/Upgrades/Upgrade07.php | 2 +- src/Upgrades/Upgrade08.php | 2 +- src/Upgrades/Upgrade09.php | 2 +- src/Upgrades/Upgrade10.php | 2 +- src/Upgrades/Upgrade11.php | 2 +- 13 files changed, 35 insertions(+), 13 deletions(-) diff --git a/TODO b/TODO index 0d818c75..09cb1499 100644 --- a/TODO +++ b/TODO @@ -15,7 +15,6 @@ everything related to posts: - search order - order:comment_count - order:comment_time - - order:random (at least unstable version) - single post view - previous and next post (difficult) @@ -25,9 +24,6 @@ everything related to posts: - editing - ability to loop video posts - - random post - - regard safety settings - - ability to paste many urls in post upload - post notes diff --git a/src/Dao/AbstractDao.php b/src/Dao/AbstractDao.php index 41f41099..301624bf 100644 --- a/src/Dao/AbstractDao.php +++ b/src/Dao/AbstractDao.php @@ -7,6 +7,7 @@ abstract class AbstractDao implements ICrudDao protected $fpdo; protected $tableName; protected $entityConverter; + protected $driver; public function __construct( \Szurubooru\DatabaseConnection $databaseConnection, @@ -240,6 +241,7 @@ abstract class AbstractDao implements ICrudDao { $this->pdo = $databaseConnection->getPDO(); $this->fpdo = new \FluentPDO($this->pdo); + $this->driver = $databaseConnection->getDriver(); } private function decorateQueryFromFilter($query, \Szurubooru\SearchServices\Filters\IFilter $filter) @@ -250,11 +252,25 @@ abstract class AbstractDao implements ICrudDao } } - private static function compileOrderBy($order) + private function compileOrderBy($order) { $orderByString = ''; foreach ($order as $orderColumn => $orderDir) + { + if ($orderColumn === \Szurubooru\SearchServices\Filters\BasicFilter::ORDER_RANDOM) + { + $driver = $this->driver; + if ($driver === 'sqlite') + { + $orderColumn = 'RANDOM()'; + } + else + { + $orderColumn = 'RAND()'; + } + } $orderByString .= $orderColumn . ' ' . ($orderDir === \Szurubooru\SearchServices\Filters\IFilter::ORDER_DESC ? 'DESC' : 'ASC') . ', '; + } return substr($orderByString, 0, -2); } diff --git a/src/DatabaseConnection.php b/src/DatabaseConnection.php index 6d67cb25..55dde42a 100644 --- a/src/DatabaseConnection.php +++ b/src/DatabaseConnection.php @@ -20,6 +20,11 @@ class DatabaseConnection return $this->pdo; } + public function getDriver() + { + return $this->getPDO()->getAttribute(\PDO::ATTR_DRIVER_NAME); + } + public function close() { $this->pdo = null; diff --git a/src/SearchServices/Filters/BasicFilter.php b/src/SearchServices/Filters/BasicFilter.php index 9256d355..53865a8b 100644 --- a/src/SearchServices/Filters/BasicFilter.php +++ b/src/SearchServices/Filters/BasicFilter.php @@ -3,6 +3,8 @@ namespace Szurubooru\SearchServices\Filters; class BasicFilter implements IFilter { + const ORDER_RANDOM = 'random'; + private $order = []; private $requirements = []; private $pageNumber; diff --git a/src/SearchServices/Parsers/PostSearchParser.php b/src/SearchServices/Parsers/PostSearchParser.php index 1326a7c5..c29ec9f1 100644 --- a/src/SearchServices/Parsers/PostSearchParser.php +++ b/src/SearchServices/Parsers/PostSearchParser.php @@ -97,6 +97,9 @@ class PostSearchParser extends AbstractSearchParser elseif ($token === 'file_size') return \Szurubooru\SearchServices\Filters\PostFilter::ORDER_FILE_SIZE; + elseif ($token === 'random') + return \Szurubooru\SearchServices\Filters\PostFilter::ORDER_RANDOM; + throw new \BadMethodCallException('Not supported'); } diff --git a/src/Upgrades/Upgrade01.php b/src/Upgrades/Upgrade01.php index 3e2edf18..1762fcdb 100644 --- a/src/Upgrades/Upgrade01.php +++ b/src/Upgrades/Upgrade01.php @@ -5,7 +5,7 @@ class Upgrade01 implements IUpgrade { public function run(\Szurubooru\DatabaseConnection $databaseConnection) { - $driver = $databaseConnection->getPDO()->getAttribute(\PDO::ATTR_DRIVER_NAME); + $driver = $databaseConnection->getDriver(); $databaseConnection->getPDO()->exec(' CREATE TABLE users diff --git a/src/Upgrades/Upgrade03.php b/src/Upgrades/Upgrade03.php index 4141595b..f9e555e7 100644 --- a/src/Upgrades/Upgrade03.php +++ b/src/Upgrades/Upgrade03.php @@ -6,7 +6,7 @@ class Upgrade03 implements IUpgrade public function run(\Szurubooru\DatabaseConnection $databaseConnection) { $pdo = $databaseConnection->getPDO(); - $driver = $pdo->getAttribute(\PDO::ATTR_DRIVER_NAME); + $driver = $databaseConnection->getDriver(); $pdo->exec('DROP TABLE IF EXISTS posts'); diff --git a/src/Upgrades/Upgrade05.php b/src/Upgrades/Upgrade05.php index 521e81b9..ecfcf782 100644 --- a/src/Upgrades/Upgrade05.php +++ b/src/Upgrades/Upgrade05.php @@ -6,7 +6,7 @@ class Upgrade05 implements IUpgrade public function run(\Szurubooru\DatabaseConnection $databaseConnection) { $pdo = $databaseConnection->getPDO(); - $driver = $pdo->getAttribute(\PDO::ATTR_DRIVER_NAME); + $driver = $databaseConnection->getDriver(); $pdo->exec(' CREATE TABLE tags2 diff --git a/src/Upgrades/Upgrade07.php b/src/Upgrades/Upgrade07.php index 0cc49a76..1878875e 100644 --- a/src/Upgrades/Upgrade07.php +++ b/src/Upgrades/Upgrade07.php @@ -6,7 +6,7 @@ class Upgrade07 implements IUpgrade public function run(\Szurubooru\DatabaseConnection $databaseConnection) { $pdo = $databaseConnection->getPDO(); - $driver = $pdo->getAttribute(\PDO::ATTR_DRIVER_NAME); + $driver = $databaseConnection->getDriver(); $pdo->exec('CREATE TABLE globals ( diff --git a/src/Upgrades/Upgrade08.php b/src/Upgrades/Upgrade08.php index a4e5cebf..7b34fb3b 100644 --- a/src/Upgrades/Upgrade08.php +++ b/src/Upgrades/Upgrade08.php @@ -6,7 +6,7 @@ class Upgrade08 implements IUpgrade public function run(\Szurubooru\DatabaseConnection $databaseConnection) { $pdo = $databaseConnection->getPDO(); - $driver = $pdo->getAttribute(\PDO::ATTR_DRIVER_NAME); + $driver = $databaseConnection->getDriver(); $pdo->exec('CREATE TABLE postRelations ( diff --git a/src/Upgrades/Upgrade09.php b/src/Upgrades/Upgrade09.php index 85d36516..179ac3b0 100644 --- a/src/Upgrades/Upgrade09.php +++ b/src/Upgrades/Upgrade09.php @@ -17,7 +17,7 @@ class Upgrade09 implements IUpgrade public function run(\Szurubooru\DatabaseConnection $databaseConnection) { $pdo = $databaseConnection->getPDO(); - $driver = $pdo->getAttribute(\PDO::ATTR_DRIVER_NAME); + $driver = $databaseConnection->getDriver(); $pdo->exec('DROP TABLE IF EXISTS snapshots'); diff --git a/src/Upgrades/Upgrade10.php b/src/Upgrades/Upgrade10.php index db54b55e..2515939d 100644 --- a/src/Upgrades/Upgrade10.php +++ b/src/Upgrades/Upgrade10.php @@ -6,7 +6,7 @@ class Upgrade10 implements IUpgrade public function run(\Szurubooru\DatabaseConnection $databaseConnection) { $pdo = $databaseConnection->getPDO(); - $driver = $pdo->getAttribute(\PDO::ATTR_DRIVER_NAME); + $driver = $databaseConnection->getDriver(); $pdo->exec('CREATE TABLE favorites ( diff --git a/src/Upgrades/Upgrade11.php b/src/Upgrades/Upgrade11.php index bf10bb77..ae01d61f 100644 --- a/src/Upgrades/Upgrade11.php +++ b/src/Upgrades/Upgrade11.php @@ -6,7 +6,7 @@ class Upgrade11 implements IUpgrade public function run(\Szurubooru\DatabaseConnection $databaseConnection) { $pdo = $databaseConnection->getPDO(); - $driver = $pdo->getAttribute(\PDO::ATTR_DRIVER_NAME); + $driver = $databaseConnection->getDriver(); $pdo->exec('CREATE TABLE postScores (