Added order:random support to post searching

This commit is contained in:
Marcin Kurczewski 2014-10-03 20:17:00 +02:00
parent 0049d59354
commit 22e7d3657b
13 changed files with 35 additions and 13 deletions

4
TODO
View file

@ -15,7 +15,6 @@ everything related to posts:
- search order - search order
- order:comment_count - order:comment_count
- order:comment_time - order:comment_time
- order:random (at least unstable version)
- single post view - single post view
- previous and next post (difficult) - previous and next post (difficult)
@ -25,9 +24,6 @@ everything related to posts:
- editing - editing
- ability to loop video posts - ability to loop video posts
- random post
- regard safety settings
- ability to paste many urls in post upload - ability to paste many urls in post upload
- post notes - post notes

View file

@ -7,6 +7,7 @@ abstract class AbstractDao implements ICrudDao
protected $fpdo; protected $fpdo;
protected $tableName; protected $tableName;
protected $entityConverter; protected $entityConverter;
protected $driver;
public function __construct( public function __construct(
\Szurubooru\DatabaseConnection $databaseConnection, \Szurubooru\DatabaseConnection $databaseConnection,
@ -240,6 +241,7 @@ abstract class AbstractDao implements ICrudDao
{ {
$this->pdo = $databaseConnection->getPDO(); $this->pdo = $databaseConnection->getPDO();
$this->fpdo = new \FluentPDO($this->pdo); $this->fpdo = new \FluentPDO($this->pdo);
$this->driver = $databaseConnection->getDriver();
} }
private function decorateQueryFromFilter($query, \Szurubooru\SearchServices\Filters\IFilter $filter) 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 = ''; $orderByString = '';
foreach ($order as $orderColumn => $orderDir) 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') . ', '; $orderByString .= $orderColumn . ' ' . ($orderDir === \Szurubooru\SearchServices\Filters\IFilter::ORDER_DESC ? 'DESC' : 'ASC') . ', ';
}
return substr($orderByString, 0, -2); return substr($orderByString, 0, -2);
} }

View file

@ -20,6 +20,11 @@ class DatabaseConnection
return $this->pdo; return $this->pdo;
} }
public function getDriver()
{
return $this->getPDO()->getAttribute(\PDO::ATTR_DRIVER_NAME);
}
public function close() public function close()
{ {
$this->pdo = null; $this->pdo = null;

View file

@ -3,6 +3,8 @@ namespace Szurubooru\SearchServices\Filters;
class BasicFilter implements IFilter class BasicFilter implements IFilter
{ {
const ORDER_RANDOM = 'random';
private $order = []; private $order = [];
private $requirements = []; private $requirements = [];
private $pageNumber; private $pageNumber;

View file

@ -97,6 +97,9 @@ class PostSearchParser extends AbstractSearchParser
elseif ($token === 'file_size') elseif ($token === 'file_size')
return \Szurubooru\SearchServices\Filters\PostFilter::ORDER_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'); throw new \BadMethodCallException('Not supported');
} }

View file

@ -5,7 +5,7 @@ class Upgrade01 implements IUpgrade
{ {
public function run(\Szurubooru\DatabaseConnection $databaseConnection) public function run(\Szurubooru\DatabaseConnection $databaseConnection)
{ {
$driver = $databaseConnection->getPDO()->getAttribute(\PDO::ATTR_DRIVER_NAME); $driver = $databaseConnection->getDriver();
$databaseConnection->getPDO()->exec(' $databaseConnection->getPDO()->exec('
CREATE TABLE users CREATE TABLE users

View file

@ -6,7 +6,7 @@ class Upgrade03 implements IUpgrade
public function run(\Szurubooru\DatabaseConnection $databaseConnection) public function run(\Szurubooru\DatabaseConnection $databaseConnection)
{ {
$pdo = $databaseConnection->getPDO(); $pdo = $databaseConnection->getPDO();
$driver = $pdo->getAttribute(\PDO::ATTR_DRIVER_NAME); $driver = $databaseConnection->getDriver();
$pdo->exec('DROP TABLE IF EXISTS posts'); $pdo->exec('DROP TABLE IF EXISTS posts');

View file

@ -6,7 +6,7 @@ class Upgrade05 implements IUpgrade
public function run(\Szurubooru\DatabaseConnection $databaseConnection) public function run(\Szurubooru\DatabaseConnection $databaseConnection)
{ {
$pdo = $databaseConnection->getPDO(); $pdo = $databaseConnection->getPDO();
$driver = $pdo->getAttribute(\PDO::ATTR_DRIVER_NAME); $driver = $databaseConnection->getDriver();
$pdo->exec(' $pdo->exec('
CREATE TABLE tags2 CREATE TABLE tags2

View file

@ -6,7 +6,7 @@ class Upgrade07 implements IUpgrade
public function run(\Szurubooru\DatabaseConnection $databaseConnection) public function run(\Szurubooru\DatabaseConnection $databaseConnection)
{ {
$pdo = $databaseConnection->getPDO(); $pdo = $databaseConnection->getPDO();
$driver = $pdo->getAttribute(\PDO::ATTR_DRIVER_NAME); $driver = $databaseConnection->getDriver();
$pdo->exec('CREATE TABLE globals $pdo->exec('CREATE TABLE globals
( (

View file

@ -6,7 +6,7 @@ class Upgrade08 implements IUpgrade
public function run(\Szurubooru\DatabaseConnection $databaseConnection) public function run(\Szurubooru\DatabaseConnection $databaseConnection)
{ {
$pdo = $databaseConnection->getPDO(); $pdo = $databaseConnection->getPDO();
$driver = $pdo->getAttribute(\PDO::ATTR_DRIVER_NAME); $driver = $databaseConnection->getDriver();
$pdo->exec('CREATE TABLE postRelations $pdo->exec('CREATE TABLE postRelations
( (

View file

@ -17,7 +17,7 @@ class Upgrade09 implements IUpgrade
public function run(\Szurubooru\DatabaseConnection $databaseConnection) public function run(\Szurubooru\DatabaseConnection $databaseConnection)
{ {
$pdo = $databaseConnection->getPDO(); $pdo = $databaseConnection->getPDO();
$driver = $pdo->getAttribute(\PDO::ATTR_DRIVER_NAME); $driver = $databaseConnection->getDriver();
$pdo->exec('DROP TABLE IF EXISTS snapshots'); $pdo->exec('DROP TABLE IF EXISTS snapshots');

View file

@ -6,7 +6,7 @@ class Upgrade10 implements IUpgrade
public function run(\Szurubooru\DatabaseConnection $databaseConnection) public function run(\Szurubooru\DatabaseConnection $databaseConnection)
{ {
$pdo = $databaseConnection->getPDO(); $pdo = $databaseConnection->getPDO();
$driver = $pdo->getAttribute(\PDO::ATTR_DRIVER_NAME); $driver = $databaseConnection->getDriver();
$pdo->exec('CREATE TABLE favorites $pdo->exec('CREATE TABLE favorites
( (

View file

@ -6,7 +6,7 @@ class Upgrade11 implements IUpgrade
public function run(\Szurubooru\DatabaseConnection $databaseConnection) public function run(\Szurubooru\DatabaseConnection $databaseConnection)
{ {
$pdo = $databaseConnection->getPDO(); $pdo = $databaseConnection->getPDO();
$driver = $pdo->getAttribute(\PDO::ATTR_DRIVER_NAME); $driver = $databaseConnection->getDriver();
$pdo->exec('CREATE TABLE postScores $pdo->exec('CREATE TABLE postScores
( (