From 50e4b4072107b7e393e4649ffa9d84d588dfe4bb Mon Sep 17 00:00:00 2001 From: Marcin Kurczewski Date: Fri, 23 May 2014 23:34:50 +0200 Subject: [PATCH] Upgraded to newest chibi - Separate non-static router class - Moved some setup code to new method, Core::init - Persistent database connection between tests --- lib/chibi-core | 2 +- lib/chibi-sql | 2 +- src/Api/Api.php | 6 +- src/Api/JobArgs/JobArgsAlternative.php | 2 +- src/Controllers/AbstractController.php | 2 +- src/Controllers/ErrorController.php | 2 +- src/Controllers/LogController.php | 2 +- src/Controllers/PostController.php | 6 +- src/Controllers/UserController.php | 4 +- src/CustomMarkdown.php | 8 +- src/Dispatcher.php | 6 +- src/Mailer.php | 2 +- src/Models/AbstractCrudModel.php | 31 ++-- src/Models/CommentModel.php | 23 ++- src/Models/Entities/AbstractEntity.php | 6 +- src/Models/Entities/PostEntity.php | 43 +++-- src/Models/Entities/TagEntity.php | 9 +- src/Models/Entities/UserEntity.php | 47 +++-- src/Models/PostModel.php | 99 ++++++----- src/Models/PropertyModel.php | 23 +-- .../SearchParsers/AbstractSearchParser.php | 12 +- src/Models/SearchParsers/PostSearchParser.php | 100 +++++------ src/Models/SearchParsers/TagSearchParser.php | 12 +- src/Models/SearchParsers/UserSearchParser.php | 8 +- .../SearchServices/AbstractSearchService.php | 19 +- .../SearchServices/PostSearchService.php | 35 ++-- .../SearchServices/TagSearchService.php | 41 +++-- src/Models/TagModel.php | 83 +++++---- src/Models/TokenModel.php | 15 +- src/Models/UserModel.php | 89 +++++----- src/Router.php | 166 ++++++++++++++++++ src/View.php | 2 +- src/Views/auth-login.phtml | 10 +- src/Views/comment/comment-add.phtml | 2 +- src/Views/comment/comment-edit.phtml | 2 +- src/Views/comment/comment-list.phtml | 2 +- src/Views/comment/comment-small.phtml | 8 +- src/Views/debug.phtml | 8 +- src/Views/layout-normal.phtml | 6 +- src/Views/log/log-list.phtml | 2 +- src/Views/log/log-view.phtml | 4 +- src/Views/paginator.phtml | 2 +- src/Views/post/post-edit.phtml | 2 +- src/Views/post/post-file-render.phtml | 10 +- src/Views/post/post-list-wrapper.phtml | 10 +- src/Views/post/post-small.phtml | 6 +- src/Views/post/post-upload.phtml | 4 +- src/Views/post/post-view.phtml | 38 ++-- src/Views/static/static-help.phtml | 2 +- src/Views/static/static-main.phtml | 6 +- src/Views/tag/tag-list-wrapper.phtml | 2 +- src/Views/tag/tag-list.phtml | 4 +- src/Views/tag/tag-mass-tag.phtml | 2 +- src/Views/tag/tag-merge.phtml | 2 +- src/Views/tag/tag-rename.phtml | 2 +- src/Views/top-navigation.phtml | 26 +-- src/Views/user/user-delete.phtml | 2 +- src/Views/user/user-edit.phtml | 2 +- src/Views/user/user-list.phtml | 6 +- src/Views/user/user-registration.phtml | 2 +- src/Views/user/user-settings.phtml | 2 +- src/Views/user/user-view.phtml | 24 +-- src/core.php | 93 ++++++---- src/routes.php | 127 -------------- tests/Support/Mockers/AbstractMocker.php | 2 +- tests/SzurubooruTestRunner.php | 44 +++-- 66 files changed, 714 insertions(+), 659 deletions(-) create mode 100644 src/Router.php delete mode 100644 src/routes.php diff --git a/lib/chibi-core b/lib/chibi-core index a6c610e5..9f48e700 160000 --- a/lib/chibi-core +++ b/lib/chibi-core @@ -1 +1 @@ -Subproject commit a6c610e5c68220cf672debe765752662807c0d39 +Subproject commit 9f48e7007a9c35caa991522d730d4f3778a1df3e diff --git a/lib/chibi-sql b/lib/chibi-sql index 6bb18c1c..b88f3a05 160000 --- a/lib/chibi-sql +++ b/lib/chibi-sql @@ -1 +1 @@ -Subproject commit 6bb18c1c6ed7ea952ae7a8dab792d5364a334201 +Subproject commit b88f3a056d3f34b15f5e7e58f892cedaf9009395 diff --git a/src/Api/Api.php b/src/Api/Api.php index 86fe6d63..bdd316de 100644 --- a/src/Api/Api.php +++ b/src/Api/Api.php @@ -3,14 +3,14 @@ final class Api { public static function getUrl() { - return \Chibi\Router::linkTo(['ApiController', 'runAction']); + return Core::getRouter()->linkTo(['ApiController', 'runAction']); } public static function run(IJob $job, $jobArgs) { $user = Auth::getCurrentUser(); - return \Chibi\Database::transaction(function() use ($job, $jobArgs) + return Core::getDatabase()->transaction(function() use ($job, $jobArgs) { $job->setArguments($jobArgs); @@ -27,7 +27,7 @@ final class Api public static function runMultiple($jobs) { $statuses = []; - \Chibi\Database::transaction(function() use ($jobs, &$statuses) + Core::getDatabase()->transaction(function() use ($jobs, &$statuses) { foreach ($jobs as $jobItem) { diff --git a/src/Api/JobArgs/JobArgsAlternative.php b/src/Api/JobArgs/JobArgsAlternative.php index 636707c1..5a7a8f5f 100644 --- a/src/Api/JobArgs/JobArgsAlternative.php +++ b/src/Api/JobArgs/JobArgsAlternative.php @@ -2,7 +2,7 @@ class JobArgsAlternative extends JobArgsNestedStruct { /** - * simplifies the structure as much as possible + * Simplifies the structure as much as possible * and returns new class or existing args. */ public static function factory(array $args) diff --git a/src/Controllers/AbstractController.php b/src/Controllers/AbstractController.php index 8682edb4..5c694491 100644 --- a/src/Controllers/AbstractController.php +++ b/src/Controllers/AbstractController.php @@ -57,7 +57,7 @@ class AbstractController { $targetUrl = SessionHelper::getLastVisitedUrl($filter); if (!$targetUrl) - $targetUrl = \Chibi\Router::linkTo(['StaticPagesController', 'mainPageView']); + $targetUrl = Core::getRouter()->linkTo(['StaticPagesController', 'mainPageView']); $this->redirect($targetUrl); } diff --git a/src/Controllers/ErrorController.php b/src/Controllers/ErrorController.php index f6a51d0d..3dcc365f 100644 --- a/src/Controllers/ErrorController.php +++ b/src/Controllers/ErrorController.php @@ -21,7 +21,7 @@ class ErrorController extends AbstractController Messenger::fail($exception->getMessage()); $context = Core::getContext(); $context->transport->exception = $exception; - $context->transport->queries = \Chibi\Database::getLogs(); + $context->transport->queries = Core::getDatabase()->getLogs(); if ($this->isAjax()) $this->renderAjax(); diff --git a/src/Controllers/LogController.php b/src/Controllers/LogController.php index d12b2ce9..01a91b45 100644 --- a/src/Controllers/LogController.php +++ b/src/Controllers/LogController.php @@ -14,7 +14,7 @@ class LogController extends AbstractController $formQuery = InputHelper::get('query'); if ($formQuery !== null) { - $this->redirect(\Chibi\Router::linkTo( + $this->redirect(Core::getRouter()->linkTo( ['LogController', 'logView'], [ 'name' => $name, diff --git a/src/Controllers/PostController.php b/src/Controllers/PostController.php index 1cb08fe7..61445dcd 100644 --- a/src/Controllers/PostController.php +++ b/src/Controllers/PostController.php @@ -66,7 +66,7 @@ class PostController extends AbstractController $params['page'] = 1; - $url = \Chibi\Router::linkTo(['PostController', 'listView'], $params); + $url = Core::getRouter()->linkTo(['PostController', 'listView'], $params); $this->redirect($url); } @@ -364,12 +364,12 @@ class PostController extends AbstractController private function redirectToPostList() { - $this->redirect(\Chibi\Router::linkTo(['PostController', 'listView'])); + $this->redirect(Core::getRouter()->linkTo(['PostController', 'listView'])); } private function redirectToGenericView($identifier) { - $this->redirect(\Chibi\Router::linkTo( + $this->redirect(Core::getRouter()->linkTo( ['PostController', 'genericView'], ['identifier' => $identifier])); } diff --git a/src/Controllers/UserController.php b/src/Controllers/UserController.php index 24db3cbc..ed6f95b8 100644 --- a/src/Controllers/UserController.php +++ b/src/Controllers/UserController.php @@ -388,13 +388,13 @@ class UserController extends AbstractController private function redirectToMainPage() { - $this->redirect(\Chibi\Router::linkTo(['StaticPagesController', 'mainPageView'])); + $this->redirect(Core::getRouter()->linkTo(['StaticPagesController', 'mainPageView'])); exit; } private function redirectToGenericView($identifier) { - $this->redirect(\Chibi\Router::linkTo( + $this->redirect(Core::getRouter()->linkTo( ['UserController', 'genericView'], ['identifier' => $identifier])); } diff --git a/src/CustomMarkdown.php b/src/CustomMarkdown.php index b92be4b7..fa93ad6b 100644 --- a/src/CustomMarkdown.php +++ b/src/CustomMarkdown.php @@ -127,7 +127,7 @@ class CustomMarkdown extends \Michelf\MarkdownExtra protected function doPosts($text) { - $link = \Chibi\Router::linkTo(['PostController', 'genericView'], ['identifier' => '_post_']); + $link = Core::getRouter()->linkTo(['PostController', 'genericView'], ['identifier' => '_post_']); return preg_replace_callback('/(?:(?hashPart('' . $x[0] . ''); @@ -136,7 +136,7 @@ class CustomMarkdown extends \Michelf\MarkdownExtra protected function doTags($text) { - $link = \Chibi\Router::linkTo(['PostController', 'listView'], ['query' => '_query_']); + $link = Core::getRouter()->linkTo(['PostController', 'listView'], ['query' => '_query_']); return preg_replace_callback('/(?:(?hashPart('' . $x[0] . ''); @@ -145,7 +145,7 @@ class CustomMarkdown extends \Michelf\MarkdownExtra protected function doUsers($text) { - $link = \Chibi\Router::linkTo(['UserController', 'genericView'], ['identifier' => '_name_']); + $link = Core::getRouter()->linkTo(['UserController', 'genericView'], ['identifier' => '_name_']); return preg_replace_callback('/(?:(?hashPart('' . $x[0] . ''); @@ -154,7 +154,7 @@ class CustomMarkdown extends \Michelf\MarkdownExtra protected function doSearchPermalinks($text) { - $link = \Chibi\Router::linkTo(['PostController', 'listView'], ['query' => '_query_']); + $link = Core::getRouter()->linkTo(['PostController', 'listView'], ['query' => '_query_']); return preg_replace_callback('{\[search\]((?:[^\[]|\[(?!\/?search\]))+)\[\/search\]}is', function($x) use ($link) { return $this->hashPart('' . $x[1] . ''); diff --git a/src/Dispatcher.php b/src/Dispatcher.php index 448cfda0..7eb76676 100644 --- a/src/Dispatcher.php +++ b/src/Dispatcher.php @@ -23,9 +23,9 @@ class Dispatcher { try { - \Chibi\Router::run($query); + Core::getRouter()->run($query); } - catch (\Chibi\UnhandledRouteException $e) + catch (\Chibi\Routing\UnhandledRouteException $e) { $errorController = new ErrorController; $errorController->simpleExceptionView(new SimpleNotFoundException($query . ' not found.')); @@ -67,7 +67,7 @@ class Dispatcher private function setRouterObserver() { - \Chibi\Router::setObserver(function($route, $args) + Core::getRouter()->setObserver(function($route, $args) { $context = Core::getContext(); $context->route = $route; diff --git a/src/Mailer.php b/src/Mailer.php index 416afc4e..9029906e 100644 --- a/src/Mailer.php +++ b/src/Mailer.php @@ -92,7 +92,7 @@ class Mailer $token->setExpirationTime(null); TokenModel::save($token); - $tokens['link'] = \Chibi\Router::linkTo($linkDestination, ['tokenText' => $token->getText()]); + $tokens['link'] = Core::getRouter()->linkTo($linkDestination, ['tokenText' => $token->getText()]); $tokens['token'] = $token->getText(); //yeah return self::sendMail($mail, $tokens); diff --git a/src/Models/AbstractCrudModel.php b/src/Models/AbstractCrudModel.php index 47aed478..36fe418b 100644 --- a/src/Models/AbstractCrudModel.php +++ b/src/Models/AbstractCrudModel.php @@ -1,6 +1,5 @@ transaction(function() use ($entities, $cb) { foreach ($entities as $entity) { @@ -63,7 +62,7 @@ abstract class AbstractCrudModel implements IModel protected static function saveMultiple($entities) { $cb = [get_called_class(), 'saveSingle']; - return Database::transaction(function() use ($entities, $cb) + return Core::getDatabase()->transaction(function() use ($entities, $cb) { $ret = []; foreach ($entities as $entity) @@ -90,12 +89,12 @@ abstract class AbstractCrudModel implements IModel public static function tryGetById($key) { - $stmt = new Sql\SelectStatement(); + $stmt = Sql\Statements::select(); $stmt->setColumn('*'); $stmt->setTable(static::getTableName()); - $stmt->setCriterion(new Sql\EqualsFunctor('id', new Sql\Binding($key))); + $stmt->setCriterion(Sql\Functors::equals('id', new Sql\Binding($key))); - $row = Database::fetchOne($stmt); + $row = Core::getDatabase()->fetchOne($stmt); return $row ? static::spawnFromDatabaseRow($row) : null; @@ -103,12 +102,12 @@ abstract class AbstractCrudModel implements IModel public static function getAllByIds(array $ids) { - $stmt = new Sql\SelectStatement(); + $stmt = Sql\Statements::select(); $stmt->setColumn('*'); $stmt->setTable(static::getTableName()); - $stmt->setCriterion(Sql\InFunctor::fromArray('id', Sql\Binding::fromArray(array_unique($ids)))); + $stmt->setCriterion(Sql\Functors::in('id', Sql\Binding::fromArray(array_unique($ids)))); - $rows = Database::fetchAll($stmt); + $rows = Core::getDatabase()->fetchAll($stmt); if ($rows) return static::spawnFromDatabaseRows($rows); @@ -117,10 +116,10 @@ abstract class AbstractCrudModel implements IModel public static function getCount() { - $stmt = new Sql\SelectStatement(); - $stmt->setColumn(new Sql\AliasFunctor(new Sql\CountFunctor('1'), 'count')); + $stmt = Sql\Statements::select(); + $stmt->setColumn(Sql\Functors::alias(Sql\Functors::count('1'), 'count')); $stmt->setTable(static::getTableName()); - return (int) Database::fetchOne($stmt)['count']; + return (int) Core::getDatabase()->fetchOne($stmt)['count']; } @@ -136,11 +135,11 @@ abstract class AbstractCrudModel implements IModel public static function forgeId($entity) { $table = static::getTableName(); - if (!Database::inTransaction()) + if (!Core::getDatabase()->inTransaction()) throw new Exception('Can be run only within transaction'); if (!$entity->getId()) { - $stmt = new Sql\InsertStatement(); + $stmt = Sql\Statements::insert(); $stmt->setTable($table); foreach ($entity as $key => $val) { @@ -150,8 +149,8 @@ abstract class AbstractCrudModel implements IModel $stmt->setColumn($key, new Sql\Binding($val)); } - Database::exec($stmt); - $entity->setId((int) Database::lastInsertId()); + Core::getDatabase()->execute($stmt); + $entity->setId((int) Core::getDatabase()->lastInsertId()); } } diff --git a/src/Models/CommentModel.php b/src/Models/CommentModel.php index 38aa9065..85741d13 100644 --- a/src/Models/CommentModel.php +++ b/src/Models/CommentModel.php @@ -1,6 +1,5 @@ validate(); $comment->getPost()->removeCache('comment_count'); - Database::transaction(function() use ($comment) + Core::getDatabase()->transaction(function() use ($comment) { self::forgeId($comment); @@ -24,14 +23,14 @@ final class CommentModel extends AbstractCrudModel 'comment_date' => $comment->getCreationTime(), 'commenter_id' => $comment->getCommenterId()]; - $stmt = new Sql\UpdateStatement(); + $stmt = Sql\Statements::update(); $stmt->setTable('comment'); - $stmt->setCriterion(new Sql\EqualsFunctor('id', new Sql\Binding($comment->getId()))); + $stmt->setCriterion(Sql\Functors::equals('id', new Sql\Binding($comment->getId()))); foreach ($bindings as $key => $val) $stmt->setColumn($key, new Sql\Binding($val)); - Database::exec($stmt); + Core::getDatabase()->execute($stmt); }); return $comment; @@ -39,13 +38,13 @@ final class CommentModel extends AbstractCrudModel protected static function removeSingle($comment) { - Database::transaction(function() use ($comment) + Core::getDatabase()->transaction(function() use ($comment) { $comment->getPost()->removeCache('comment_count'); - $stmt = new Sql\DeleteStatement(); + $stmt = Sql\Statements::delete(); $stmt->setTable('comment'); - $stmt->setCriterion(new Sql\EqualsFunctor('id', new Sql\Binding($comment->getId()))); - Database::exec($stmt); + $stmt->setCriterion(Sql\Functors::equals('id', new Sql\Binding($comment->getId()))); + Core::getDatabase()->execute($stmt); }); } @@ -53,12 +52,12 @@ final class CommentModel extends AbstractCrudModel public static function getAllByPostId($key) { - $stmt = new Sql\SelectStatement(); + $stmt = Sql\Statements::select(); $stmt->setColumn('comment.*'); $stmt->setTable('comment'); - $stmt->setCriterion(new Sql\EqualsFunctor('post_id', new Sql\Binding($key))); + $stmt->setCriterion(Sql\Functors::equals('post_id', new Sql\Binding($key))); - $rows = Database::fetchAll($stmt); + $rows = Core::getDatabase()->fetchAll($stmt); if ($rows) return self::spawnFromDatabaseRows($rows); return []; diff --git a/src/Models/Entities/AbstractEntity.php b/src/Models/Entities/AbstractEntity.php index fc0cba6d..d6fccf25 100644 --- a/src/Models/Entities/AbstractEntity.php +++ b/src/Models/Entities/AbstractEntity.php @@ -55,11 +55,11 @@ abstract class AbstractEntity implements IValidatable if ($this->hasCache($columnName)) return $this->getCache($columnName); - $stmt = new \Chibi\Sql\SelectStatement(); + $stmt = \Chibi\Sql\Statements::select(); $stmt->setTable($this->model->getTableName()); $stmt->setColumn($columnName); - $stmt->setCriterion(new \Chibi\Sql\EqualsFunctor('id', new \Chibi\Sql\Binding($this->getId()))); - $value = \Chibi\Database::fetchOne($stmt)[$columnName]; + $stmt->setCriterion(\Chibi\Sql\Functors::equals('id', new \Chibi\Sql\Binding($this->getId()))); + $value = Core::getDatabase()->fetchOne($stmt)[$columnName]; $this->setCache($columnName, $value); return $value; } diff --git a/src/Models/Entities/PostEntity.php b/src/Models/Entities/PostEntity.php index f939a45a..1b0a9452 100644 --- a/src/Models/Entities/PostEntity.php +++ b/src/Models/Entities/PostEntity.php @@ -1,6 +1,5 @@ hasCache('favoritee')) return $this->getCache('favoritee'); - $stmt = new Sql\SelectStatement(); + $stmt = \Chibi\Sql\Statements::select(); $stmt->setColumn('user.*'); $stmt->setTable('user'); - $stmt->addInnerJoin('favoritee', new Sql\EqualsFunctor('favoritee.user_id', 'user.id')); - $stmt->setCriterion(new Sql\EqualsFunctor('favoritee.post_id', new Sql\Binding($this->getId()))); - $rows = Database::fetchAll($stmt); + $stmt->addInnerJoin('favoritee', Sql\Functors::equals('favoritee.user_id', 'user.id')); + $stmt->setCriterion(Sql\Functors::equals('favoritee.post_id', new Sql\Binding($this->getId()))); + $rows = Core::getDatabase()->fetchAll($stmt); $favorites = UserModel::spawnFromDatabaseRows($rows); $this->setCache('favoritee', $favorites); return $favorites; @@ -160,21 +159,21 @@ final class PostEntity extends AbstractEntity implements IValidatable, ISerializ if ($this->hasCache('relations')) return $this->getCache('relations'); - $stmt = new Sql\SelectStatement(); + $stmt = \Chibi\Sql\Statements::select(); $stmt->setColumn('post.*'); $stmt->setTable('post'); $binding = new Sql\Binding($this->getId()); - $stmt->addInnerJoin('crossref', (new Sql\DisjunctionFunctor) + $stmt->addInnerJoin('crossref', Sql\Functors::disjunction() ->add( - (new Sql\ConjunctionFunctor) - ->add(new Sql\EqualsFunctor('post.id', 'crossref.post2_id')) - ->add(new Sql\EqualsFunctor('crossref.post_id', $binding))) + Sql\Functors::conjunction() + ->add(Sql\Functors::equals('post.id', 'crossref.post2_id')) + ->add(Sql\Functors::equals('crossref.post_id', $binding))) ->add( - (new Sql\ConjunctionFunctor) - ->add(new Sql\EqualsFunctor('post.id', 'crossref.post_id')) - ->add(new Sql\EqualsFunctor('crossref.post2_id', $binding)))); - $rows = Database::fetchAll($stmt); - $posts = PostModel::spawnFromDatabaseRows($rows); + Sql\Functors::conjunction() + ->add(Sql\Functors::equals('post.id', 'crossref.post_id')) + ->add(Sql\Functors::equals('crossref.post2_id', $binding)))); + $rows = Core::getDatabase()->fetchAll($stmt); + $posts = $this->model->spawnFromDatabaseRows($rows); $this->setCache('relations', $posts); return $posts; } @@ -343,27 +342,27 @@ final class PostEntity extends AbstractEntity implements IValidatable, ISerializ public function tryGetWorkingFullPath() { - return PostModel::tryGetWorkingFullPath($this->getName()); + return $this->model->tryGetWorkingFullPath($this->getName()); } public function getFullPath() { - return PostModel::getFullPath($this->getName()); + return $this->model->getFullPath($this->getName()); } public function tryGetWorkingThumbnailPath() { - return PostModel::tryGetWorkingThumbnailPath($this->getName()); + return $this->model->tryGetWorkingThumbnailPath($this->getName()); } public function getCustomThumbnailSourcePath() { - return PostModel::getCustomThumbnailSourcePath($this->getName()); + return $this->model->getCustomThumbnailSourcePath($this->getName()); } public function getThumbnailPath() { - return PostModel::getThumbnailPath($this->getName()); + return $this->model->getThumbnailPath($this->getName()); } public function hasCustomThumbnail() @@ -463,7 +462,7 @@ final class PostEntity extends AbstractEntity implements IValidatable, ISerializ throw new SimpleException('Invalid file type "%s"', $this->getMimeType()); } - $duplicatedPost = PostModel::tryGetByHash($this->getFileHash()); + $duplicatedPost = $this->model->tryGetByHash($this->getFileHash()); if ($duplicatedPost !== null and (!$this->getId() or $this->getId() != $duplicatedPost->getId())) { throw new SimpleException( @@ -501,7 +500,7 @@ final class PostEntity extends AbstractEntity implements IValidatable, ISerializ if (file_exists($thumbnailPath)) unlink($thumbnailPath); - $duplicatedPost = PostModel::tryGetByHash($youtubeId); + $duplicatedPost = $this->model->tryGetByHash($youtubeId); if ($duplicatedPost !== null and (!$this->getId() or $this->getId() != $duplicatedPost->getId())) { throw new SimpleException( diff --git a/src/Models/Entities/TagEntity.php b/src/Models/Entities/TagEntity.php index e5aa6a87..bed9542e 100644 --- a/src/Models/Entities/TagEntity.php +++ b/src/Models/Entities/TagEntity.php @@ -1,6 +1,5 @@ hasCache('post_count')) return $this->getCache('post_count'); - $stmt = new Sql\SelectStatement(); - $stmt->setColumn(new Sql\AliasFunctor(new Sql\CountFunctor('1'), 'post_count')); + $stmt = \Chibi\Sql\Statements::select(); + $stmt->setColumn(Sql\Functors::alias(Sql\Functors::count('1'), 'post_count')); $stmt->setTable('post_tag'); - $stmt->setCriterion(new Sql\EqualsFunctor('tag_id', new Sql\Binding($this->getId()))); - $row = Database::fetchOne($stmt); + $stmt->setCriterion(Sql\Functors::equals('tag_id', new Sql\Binding($this->getId()))); + $row = Core::getDatabase()->fetchOne($stmt); $this->setCache('post_count', (int) $row['post_count']); return $this->getCache('post_count'); } diff --git a/src/Models/Entities/UserEntity.php b/src/Models/Entities/UserEntity.php index 9e78cdc1..86630e8c 100644 --- a/src/Models/Entities/UserEntity.php +++ b/src/Models/Entities/UserEntity.php @@ -1,6 +1,5 @@ setColumn(new Sql\AliasFunctor(new Sql\CountFunctor('1'), 'count')); + $stmt = \Chibi\Sql\Statements::select(); + $stmt->setColumn(Sql\Functors::alias(Sql\Functors::count('1'), 'count')); $stmt->setTable('favoritee'); - $stmt->setCriterion((new Sql\ConjunctionFunctor) - ->add(new Sql\EqualsFunctor('user_id', new Sql\Binding($this->getId()))) - ->add(new Sql\EqualsFunctor('post_id', new Sql\Binding($post->getId())))); - return Database::fetchOne($stmt)['count'] == 1; + $stmt->setCriterion(Sql\Functors::conjunction() + ->add(Sql\Functors::equals('user_id', new Sql\Binding($this->getId()))) + ->add(Sql\Functors::equals('post_id', new Sql\Binding($post->getId())))); + return Core::getDatabase()->fetchOne($stmt)['count'] == 1; } public function getScore($post) { - $stmt = new Sql\SelectStatement(); + $stmt = \Chibi\Sql\Statements::select(); $stmt->setColumn('score'); $stmt->setTable('post_score'); - $stmt->setCriterion((new Sql\ConjunctionFunctor) - ->add(new Sql\EqualsFunctor('user_id', new Sql\Binding($this->getId()))) - ->add(new Sql\EqualsFunctor('post_id', new Sql\Binding($post->getId())))); - $row = Database::fetchOne($stmt); + $stmt->setCriterion(Sql\Functors::conjunction() + ->add(Sql\Functors::equals('user_id', new Sql\Binding($this->getId()))) + ->add(Sql\Functors::equals('post_id', new Sql\Binding($post->getId())))); + $row = Core::getDatabase()->fetchOne($stmt); if ($row) return intval($row['score']); return null; @@ -361,29 +360,29 @@ final class UserEntity extends AbstractEntity implements IValidatable, ISerializ public function getFavoriteCount() { - $stmt = new Sql\SelectStatement(); - $stmt->setColumn(new Sql\AliasFunctor(new Sql\CountFunctor('1'), 'count')); + $stmt = \Chibi\Sql\Statements::select(); + $stmt->setColumn(Sql\Functors::alias(Sql\Functors::count('1'), 'count')); $stmt->setTable('favoritee'); - $stmt->setCriterion(new Sql\EqualsFunctor('user_id', new Sql\Binding($this->getId()))); - return (int) Database::fetchOne($stmt)['count']; + $stmt->setCriterion(Sql\Functors::equals('user_id', new Sql\Binding($this->getId()))); + return (int) Core::getDatabase()->fetchOne($stmt)['count']; } public function getCommentCount() { - $stmt = new Sql\SelectStatement(); - $stmt->setColumn(new Sql\AliasFunctor(new Sql\CountFunctor('1'), 'count')); + $stmt = \Chibi\Sql\Statements::select(); + $stmt->setColumn(Sql\Functors::alias(Sql\Functors::count('1'), 'count')); $stmt->setTable('comment'); - $stmt->setCriterion(new Sql\EqualsFunctor('commenter_id', new Sql\Binding($this->getId()))); - return (int) Database::fetchOne($stmt)['count']; + $stmt->setCriterion(Sql\Functors::equals('commenter_id', new Sql\Binding($this->getId()))); + return (int) Core::getDatabase()->fetchOne($stmt)['count']; } public function getPostCount() { - $stmt = new Sql\SelectStatement(); - $stmt->setColumn(new Sql\AliasFunctor(new Sql\CountFunctor('1'), 'count')); + $stmt = \Chibi\Sql\Statements::select(); + $stmt->setColumn(Sql\Functors::alias(Sql\Functors::count('1'), 'count')); $stmt->setTable('post'); - $stmt->setCriterion(new Sql\EqualsFunctor('uploader_id', new Sql\Binding($this->getId()))); - return (int) Database::fetchOne($stmt)['count']; + $stmt->setCriterion(Sql\Functors::equals('uploader_id', new Sql\Binding($this->getId()))); + return (int) Core::getDatabase()->fetchOne($stmt)['count']; } diff --git a/src/Models/PostModel.php b/src/Models/PostModel.php index 473a6c7a..7ac3626b 100644 --- a/src/Models/PostModel.php +++ b/src/Models/PostModel.php @@ -1,6 +1,5 @@ validate(); - Database::transaction(function() use ($post) + Core::getDatabase()->transaction(function() use ($post) { self::forgeId($post); @@ -33,50 +32,50 @@ final class PostModel extends AbstractCrudModel 'source' => $post->getSource(), ]; - $stmt = new Sql\UpdateStatement(); + $stmt = Sql\Statements::update(); $stmt->setTable('post'); foreach ($bindings as $key => $value) $stmt->setColumn($key, new Sql\Binding($value)); - $stmt->setCriterion(new Sql\EqualsFunctor('id', new Sql\Binding($post->getId()))); - Database::exec($stmt); + $stmt->setCriterion(Sql\Functors::equals('id', new Sql\Binding($post->getId()))); + Core::getDatabase()->execute($stmt); //tags $tags = $post->getTags(); - $stmt = new Sql\DeleteStatement(); + $stmt = Sql\Statements::delete(); $stmt->setTable('post_tag'); - $stmt->setCriterion(new Sql\EqualsFunctor('post_id', new Sql\Binding($post->getId()))); - Database::exec($stmt); + $stmt->setCriterion(Sql\Functors::equals('post_id', new Sql\Binding($post->getId()))); + Core::getDatabase()->execute($stmt); foreach ($tags as $postTag) { - $stmt = new Sql\InsertStatement(); + $stmt = Sql\Statements::insert(); $stmt->setTable('post_tag'); $stmt->setColumn('post_id', new Sql\Binding($post->getId())); $stmt->setColumn('tag_id', new Sql\Binding($postTag->getId())); - Database::exec($stmt); + Core::getDatabase()->execute($stmt); } //relations $relations = $post->getRelations(); - $stmt = new Sql\DeleteStatement(); + $stmt = Sql\Statements::delete(); $stmt->setTable('crossref'); $binding = new Sql\Binding($post->getId()); - $stmt->setCriterion((new Sql\DisjunctionFunctor) - ->add(new Sql\EqualsFunctor('post_id', $binding)) - ->add(new Sql\EqualsFunctor('post2_id', $binding))); - Database::exec($stmt); + $stmt->setCriterion(Sql\Functors::disjunction() + ->add(Sql\Functors::equals('post_id', $binding)) + ->add(Sql\Functors::equals('post2_id', $binding))); + Core::getDatabase()->execute($stmt); foreach ($relations as $relatedPost) { - $stmt = new Sql\InsertStatement(); + $stmt = Sql\Statements::insert(); $stmt->setTable('crossref'); $stmt->setColumn('post_id', new Sql\Binding($post->getId())); $stmt->setColumn('post2_id', new Sql\Binding($relatedPost->getId())); - Database::exec($stmt); + Core::getDatabase()->execute($stmt); } }); @@ -85,33 +84,33 @@ final class PostModel extends AbstractCrudModel protected static function removeSingle($post) { - Database::transaction(function() use ($post) + Core::getDatabase()->transaction(function() use ($post) { $binding = new Sql\Binding($post->getId()); - $stmt = new Sql\DeleteStatement(); + $stmt = Sql\Statements::delete(); $stmt->setTable('post_score'); - $stmt->setCriterion(new Sql\EqualsFunctor('post_id', $binding)); - Database::exec($stmt); + $stmt->setCriterion(Sql\Functors::equals('post_id', $binding)); + Core::getDatabase()->execute($stmt); $stmt->setTable('post_tag'); - Database::exec($stmt); + Core::getDatabase()->execute($stmt); $stmt->setTable('favoritee'); - Database::exec($stmt); + Core::getDatabase()->execute($stmt); $stmt->setTable('comment'); - Database::exec($stmt); + Core::getDatabase()->execute($stmt); $stmt->setTable('crossref'); - $stmt->setCriterion((new Sql\DisjunctionFunctor) - ->add(new Sql\EqualsFunctor('post_id', $binding)) - ->add(new Sql\EqualsFunctor('post_id', $binding))); - Database::exec($stmt); + $stmt->setCriterion(Sql\Functors::disjunction() + ->add(Sql\Functors::equals('post_id', $binding)) + ->add(Sql\Functors::equals('post_id', $binding))); + Core::getDatabase()->execute($stmt); $stmt->setTable('post'); - $stmt->setCriterion(new Sql\EqualsFunctor('id', $binding)); - Database::exec($stmt); + $stmt->setCriterion(Sql\Functors::equals('id', $binding)); + Core::getDatabase()->execute($stmt); }); } @@ -128,12 +127,12 @@ final class PostModel extends AbstractCrudModel public static function tryGetByName($key, $throw = true) { - $stmt = new Sql\SelectStatement(); + $stmt = Sql\Statements::select(); $stmt->setColumn('*'); $stmt->setTable('post'); - $stmt->setCriterion(new Sql\EqualsFunctor('name', new Sql\Binding($key))); + $stmt->setCriterion(Sql\Functors::equals('name', new Sql\Binding($key))); - $row = Database::fetchOne($stmt); + $row = Core::getDatabase()->fetchOne($stmt); return $row ? self::spawnFromDatabaseRow($row) : null; @@ -158,12 +157,12 @@ final class PostModel extends AbstractCrudModel public static function tryGetByHash($key) { - $stmt = new Sql\SelectStatement(); + $stmt = Sql\Statements::select(); $stmt->setColumn('*'); $stmt->setTable('post'); - $stmt->setCriterion(new Sql\EqualsFunctor('file_hash', new Sql\Binding($key))); + $stmt->setCriterion(Sql\Functors::equals('file_hash', new Sql\Binding($key))); - $row = Database::fetchOne($stmt); + $row = Core::getDatabase()->fetchOne($stmt); return $row ? self::spawnFromDatabaseRow($row) : null; @@ -186,12 +185,12 @@ final class PostModel extends AbstractCrudModel } $postIds = array_unique(array_keys($postMap)); - $stmt = new Sql\SelectStatement(); + $stmt = Sql\Statements::select(); $stmt->setTable('comment'); $stmt->addColumn('comment.*'); $stmt->addColumn('post_id'); - $stmt->setCriterion(Sql\InFunctor::fromArray('post_id', Sql\Binding::fromArray($postIds))); - $rows = Database::fetchAll($stmt); + $stmt->setCriterion(Sql\Functors::in('post_id', Sql\Binding::fromArray($postIds))); + $rows = Core::getDatabase()->fetchAll($stmt); foreach ($rows as $row) { @@ -226,13 +225,13 @@ final class PostModel extends AbstractCrudModel } $postIds = array_unique(array_keys($postMap)); - $stmt = new Sql\SelectStatement(); + $stmt = Sql\Statements::select(); $stmt->setTable('tag'); $stmt->addColumn('tag.*'); $stmt->addColumn('post_id'); - $stmt->addInnerJoin('post_tag', new Sql\EqualsFunctor('post_tag.tag_id', 'tag.id')); - $stmt->setCriterion(Sql\InFunctor::fromArray('post_id', Sql\Binding::fromArray($postIds))); - $rows = Database::fetchAll($stmt); + $stmt->addInnerJoin('post_tag', Sql\Functors::equals('post_tag.tag_id', 'tag.id')); + $stmt->setCriterion(Sql\Functors::in('post_id', Sql\Binding::fromArray($postIds))); + $rows = Core::getDatabase()->fetchAll($stmt); foreach ($rows as $row) { @@ -352,15 +351,15 @@ final class PostModel extends AbstractCrudModel public static function featureRandomPost() { - $stmt = (new Sql\SelectStatement) + $stmt = Sql\Statements::select() ->setColumn('id') ->setTable('post') - ->setCriterion((new Sql\ConjunctionFunctor) - ->add(new Sql\NegationFunctor(new Sql\StringExpression('hidden'))) - ->add(new Sql\EqualsFunctor('type', new Sql\Binding(PostType::Image))) - ->add(new Sql\EqualsFunctor('safety', new Sql\Binding(PostSafety::Safe)))) - ->setOrderBy(new Sql\RandomFunctor(), Sql\SelectStatement::ORDER_DESC); - $featuredPostId = Database::fetchOne($stmt)['id']; + ->setCriterion(Sql\Functors::conjunction() + ->add(Sql\Functors::negation(new Sql\StringExpression('hidden'))) + ->add(Sql\Functors::equals('type', new Sql\Binding(PostType::Image))) + ->add(Sql\Functors::equals('safety', new Sql\Binding(PostSafety::Safe)))) + ->setOrderBy(Sql\Functors::random(), Sql\Statements\SelectStatement::ORDER_DESC); + $featuredPostId = Core::getDatabase()->fetchOne($stmt)['id']; if (!$featuredPostId) return null; diff --git a/src/Models/PropertyModel.php b/src/Models/PropertyModel.php index 4f7ecb11..e4c3a6ce 100644 --- a/src/Models/PropertyModel.php +++ b/src/Models/PropertyModel.php @@ -1,6 +1,5 @@ setColumn('*'); $stmt ->setTable('property'); - foreach (Database::fetchAll($stmt) as $row) + foreach (self::$database->fetchAll($stmt) as $row) self::$allProperties[$row['prop_id']] = $row['value']; } @@ -51,28 +52,28 @@ final class PropertyModel implements IModel public static function set($propertyId, $value) { self::loadIfNecessary(); - Database::transaction(function() use ($propertyId, $value) + self::$database->transaction(function() use ($propertyId, $value) { - $stmt = new Sql\SelectStatement(); + $stmt = Sql\Statements::select(); $stmt->setColumn('id'); $stmt->setTable('property'); - $stmt->setCriterion(new Sql\EqualsFunctor('prop_id', new Sql\Binding($propertyId))); - $row = Database::fetchOne($stmt); + $stmt->setCriterion(Sql\Functors::equals('prop_id', new Sql\Binding($propertyId))); + $row = self::$database->fetchOne($stmt); if ($row) { - $stmt = new Sql\UpdateStatement(); - $stmt->setCriterion(new Sql\EqualsFunctor('prop_id', new Sql\Binding($propertyId))); + $stmt = Sql\Statements::update(); + $stmt->setCriterion(Sql\Functors::equals('prop_id', new Sql\Binding($propertyId))); } else { - $stmt = new Sql\InsertStatement(); + $stmt = Sql\Statements::insert(); $stmt->setColumn('prop_id', new Sql\Binding($propertyId)); } $stmt->setTable('property'); $stmt->setColumn('value', new Sql\Binding($value)); - Database::exec($stmt); + self::$database->execute($stmt); self::$allProperties[$propertyId] = $value; }); diff --git a/src/Models/SearchParsers/AbstractSearchParser.php b/src/Models/SearchParsers/AbstractSearchParser.php index 33e69c9a..da0daff7 100644 --- a/src/Models/SearchParsers/AbstractSearchParser.php +++ b/src/Models/SearchParsers/AbstractSearchParser.php @@ -5,7 +5,7 @@ abstract class AbstractSearchParser { protected $statement; - public function decorate(Sql\SelectStatement $statement, $filterString) + public function decorate($statement, $filterString) { $this->statement = $statement; @@ -67,17 +67,17 @@ abstract class AbstractSearchParser $orderByString = strtolower(array_shift($arr)); $orderDirString = strtolower(array_shift($arr)); if ($orderDirString == 'asc') - $orderDir = Sql\SelectStatement::ORDER_ASC; + $orderDir = Sql\Statements\SelectStatement::ORDER_ASC; elseif ($orderDirString == 'desc') - $orderDir = Sql\SelectStatement::ORDER_DESC; + $orderDir = Sql\Statements\SelectStatement::ORDER_DESC; else throw new SimpleException('Invalid search order direction "%s"', $searchOrderDir); if ($neg) { - $orderDir = $orderDir == Sql\SelectStatement::ORDER_ASC - ? Sql\SelectStatement::ORDER_DESC - : Sql\SelectStatement::ORDER_ASC; + $orderDir = $orderDir == Sql\Statements\SelectStatement::ORDER_ASC + ? Sql\Statements\SelectStatement::ORDER_DESC + : Sql\Statements\SelectStatement::ORDER_ASC; } if (!$this->processOrderToken($orderByString, $orderDir)) diff --git a/src/Models/SearchParsers/PostSearchParser.php b/src/Models/SearchParsers/PostSearchParser.php index 25f17e0e..652024a2 100644 --- a/src/Models/SearchParsers/PostSearchParser.php +++ b/src/Models/SearchParsers/PostSearchParser.php @@ -13,7 +13,7 @@ class PostSearchParser extends AbstractSearchParser $config = Core::getConfig(); $this->tags = []; - $crit = new Sql\ConjunctionFunctor(); + $crit = Sql\Functors::conjunction(); $allowedSafety = array_map( function($safety) @@ -21,7 +21,7 @@ class PostSearchParser extends AbstractSearchParser return $safety->toInteger(); }, Access::getAllowedSafety()); - $crit->add(Sql\InFunctor::fromArray('post.safety', Sql\Binding::fromArray($allowedSafety))); + $crit->add(Sql\Functors::in('post.safety', Sql\Binding::fromArray($allowedSafety))); $this->statement->setCriterion($crit); if (count($tokens) > $config->browsing->maxSearchTokens) @@ -43,20 +43,20 @@ class PostSearchParser extends AbstractSearchParser { list ($tagName, $neg) = $item; $tag = TagModel::getByName($tagName); - $innerStmt = new Sql\SelectStatement(); + $innerStmt = Sql\Statements::select(); $innerStmt->setTable('post_tag'); - $innerStmt->setCriterion((new Sql\ConjunctionFunctor) - ->add(new Sql\EqualsFunctor('post_tag.post_id', 'post.id')) - ->add(new Sql\EqualsFunctor('post_tag.tag_id', new Sql\Binding($tag->getId())))); - $operator = new Sql\ExistsFunctor($innerStmt); + $innerStmt->setCriterion(Sql\Functors::conjunction() + ->add(Sql\Functors::equals('post_tag.post_id', 'post.id')) + ->add(Sql\Functors::equals('post_tag.tag_id', new Sql\Binding($tag->getId())))); + $operator = Sql\Functors::exists($innerStmt); if ($neg) - $operator = new Sql\NegationFunctor($operator); + $operator = Sql\Functors::negation($operator); $this->statement->getCriterion()->add($operator); } $this->statement->addOrderBy('post.id', empty($this->statement->getOrderBy()) - ? Sql\SelectStatement::ORDER_DESC + ? Sql\Statements\SelectStatement::ORDER_DESC : $this->statement->getOrderBy()[0][1]); } @@ -72,91 +72,91 @@ class PostSearchParser extends AbstractSearchParser { $ids = preg_split('/[;,]/', $value); $ids = array_map('intval', $ids); - return Sql\InFunctor::fromArray('post.id', Sql\Binding::fromArray($ids)); + return Sql\Functors::in('post.id', Sql\Binding::fromArray($ids)); } if (in_array($key, ['name', 'names', 'hash', 'hashes'])) { $ids = preg_split('/[;,]/', $value); - return Sql\InFunctor::fromArray('post.name', Sql\Binding::fromArray($ids)); + return Sql\Functors::in('post.name', Sql\Binding::fromArray($ids)); } elseif (in_array($key, ['fav', 'favs', 'favd'])) { $user = UserModel::getByName($value); - $innerStmt = (new Sql\SelectStatement) + $innerStmt = Sql\Statements::select() ->setTable('favoritee') - ->setCriterion((new Sql\ConjunctionFunctor) - ->add(new Sql\EqualsFunctor('favoritee.post_id', 'post.id')) - ->add(new Sql\EqualsFunctor('favoritee.user_id', new Sql\Binding($user->getId())))); - return new Sql\ExistsFunctor($innerStmt); + ->setCriterion(Sql\Functors::conjunction() + ->add(Sql\Functors::equals('favoritee.post_id', 'post.id')) + ->add(Sql\Functors::equals('favoritee.user_id', new Sql\Binding($user->getId())))); + return Sql\Functors::exists($innerStmt); } elseif (in_array($key, ['comment', 'comments', 'commenter', 'commented'])) { $user = UserModel::getByName($value); - $innerStmt = (new Sql\SelectStatement) + $innerStmt = Sql\Statements::select() ->setTable('comment') - ->setCriterion((new Sql\ConjunctionFunctor) - ->add(new Sql\EqualsFunctor('comment.post_id', 'post.id')) - ->add(new Sql\EqualsFunctor('comment.commenter_id', new Sql\Binding($user->getId())))); - return new Sql\ExistsFunctor($innerStmt); + ->setCriterion(Sql\Functors::conjunction() + ->add(Sql\Functors::equals('comment.post_id', 'post.id')) + ->add(Sql\Functors::equals('comment.commenter_id', new Sql\Binding($user->getId())))); + return Sql\Functors::exists($innerStmt); } elseif (in_array($key, ['submit', 'upload', 'uploads', 'uploader', 'uploaded'])) { $user = UserModel::getByName($value); - return new Sql\EqualsFunctor('post.uploader_id', new Sql\Binding($user->getId())); + return Sql\Functors::equals('post.uploader_id', new Sql\Binding($user->getId())); } elseif (in_array($key, ['idmin', 'id_min'])) - return new Sql\EqualsOrGreaterFunctor('post.id', new Sql\Binding(intval($value))); + return Sql\Functors::equalsOrGreater('post.id', new Sql\Binding(intval($value))); elseif (in_array($key, ['idmax', 'id_max'])) - return new Sql\EqualsOrLesserFunctor('post.id', new Sql\Binding(intval($value))); + return Sql\Functors::equalsOrLesser('post.id', new Sql\Binding(intval($value))); elseif (in_array($key, ['scoremin', 'score_min'])) - return new Sql\EqualsOrGreaterFunctor('post.score', new Sql\Binding(intval($value))); + return Sql\Functors::equalsOrGreater('post.score', new Sql\Binding(intval($value))); elseif (in_array($key, ['scoremax', 'score_max'])) - return new Sql\EqualsOrLesserFunctor('post.score', new Sql\Binding(intval($value))); + return Sql\Functors::equalsOrLesser('post.score', new Sql\Binding(intval($value))); elseif (in_array($key, ['tagmin', 'tag_min'])) - return new Sql\EqualsOrGreaterFunctor('post.tag_count', new Sql\Binding(intval($value))); + return Sql\Functors::equalsOrGreater('post.tag_count', new Sql\Binding(intval($value))); elseif (in_array($key, ['tagmax', 'tag_max'])) - return new Sql\EqualsOrLesserFunctor('post.tag_count', new Sql\Binding(intval($value))); + return Sql\Functors::equalsOrLesser('post.tag_count', new Sql\Binding(intval($value))); elseif (in_array($key, ['favmin', 'fav_min'])) - return new Sql\EqualsOrGreaterFunctor('post.fav_count', new Sql\Binding(intval($value))); + return Sql\Functors::equalsOrGreater('post.fav_count', new Sql\Binding(intval($value))); elseif (in_array($key, ['favmax', 'fav_max'])) - return new Sql\EqualsOrLesserFunctor('post.fav_count', new Sql\Binding(intval($value))); + return Sql\Functors::equalsOrLesser('post.fav_count', new Sql\Binding(intval($value))); elseif (in_array($key, ['commentmin', 'comment_min'])) - return new Sql\EqualsOrGreaterFunctor('post.comment_count', new Sql\Binding(intval($value))); + return Sql\Functors::equalsOrGreater('post.comment_count', new Sql\Binding(intval($value))); elseif (in_array($key, ['commentmax', 'comment_max'])) - return new Sql\EqualsOrLesserFunctor('post.comment_count', new Sql\Binding(intval($value))); + return Sql\Functors::equalsOrLesser('post.comment_count', new Sql\Binding(intval($value))); elseif (in_array($key, ['date'])) { list ($dateMin, $dateMax) = self::parseDate($value); - return (new Sql\ConjunctionFunctor) - ->add(new Sql\EqualsOrLesserFunctor('post.upload_date', new Sql\Binding($dateMax))) - ->add(new Sql\EqualsOrGreaterFunctor('post.upload_date', new Sql\Binding($dateMin))); + return Sql\Functors::conjunction() + ->add(Sql\Functors::equalsOrLesser('post.upload_date', new Sql\Binding($dateMax))) + ->add(Sql\Functors::equalsOrGreater('post.upload_date', new Sql\Binding($dateMin))); } elseif (in_array($key, ['datemin', 'date_min'])) { list ($dateMin, $dateMax) = self::parseDate($value); - return new Sql\EqualsOrGreaterFunctor('post.upload_date', new Sql\Binding($dateMin)); + return Sql\Functors::equalsOrGreater('post.upload_date', new Sql\Binding($dateMin)); } elseif (in_array($key, ['datemax', 'date_max'])) { list ($dateMin, $dateMax) = self::parseDate($value); - return new Sql\EqualsOrLesserFunctor('post.upload_date', new Sql\Binding($dateMax)); + return Sql\Functors::equalsOrLesser('post.upload_date', new Sql\Binding($dateMax)); } elseif ($key == 'special') @@ -173,11 +173,11 @@ class PostSearchParser extends AbstractSearchParser { if (!$this->statement->isTableJoined('post_score')) { - $this->statement->addLeftOuterJoin('post_score', (new Sql\ConjunctionFunctor) - ->add(new Sql\EqualsFunctor('post_score.post_id', 'post.id')) - ->add(new Sql\EqualsFunctor('post_score.user_id', new Sql\Binding($activeUser->getId())))); + $this->statement->addLeftOuterJoin('post_score', Sql\Functors::conjunction() + ->add(Sql\Functors::equals('post_score.post_id', 'post.id')) + ->add(Sql\Functors::equals('post_score.user_id', new Sql\Binding($activeUser->getId())))); } - return new Sql\EqualsFunctor(new Sql\IfNullFunctor('post_score.score', '0'), '1'); + return Sql\Functors::equals(Sql\Functors::ifNull('post_score.score', '0'), '1'); } elseif (in_array($value, ['dislike', 'disliked', 'dislikes'])) @@ -185,11 +185,11 @@ class PostSearchParser extends AbstractSearchParser $this->showDisliked = true; if (!$this->statement->isTableJoined('post_score')) { - $this->statement->addLeftOuterJoin('post_score', (new Sql\ConjunctionFunctor) - ->add(new Sql\EqualsFunctor('post_score.post_id', 'post.id')) - ->add(new Sql\EqualsFunctor('post_score.user_id', new Sql\Binding($activeUser->getId())))); + $this->statement->addLeftOuterJoin('post_score', Sql\Functors::conjunction() + ->add(Sql\Functors::equals('post_score.post_id', 'post.id')) + ->add(Sql\Functors::equals('post_score.user_id', new Sql\Binding($activeUser->getId())))); } - return new Sql\EqualsFunctor(new Sql\IfNullFunctor('post_score.score', '0'), '-1'); + return Sql\Functors::equals(Sql\Functors::ifNull('post_score.score', '0'), '-1'); } elseif ($value == 'hidden') @@ -216,7 +216,7 @@ class PostSearchParser extends AbstractSearchParser else throw new SimpleException('Invalid post type "%s"', $value); - return new Sql\EqualsFunctor('type', new Sql\Binding($type)); + return Sql\Functors::equals('type', new Sql\Binding($type)); } return null; @@ -229,7 +229,7 @@ class PostSearchParser extends AbstractSearchParser return false; if ($neg) - $criterion = new Sql\NegationFunctor($criterion); + $criterion = Sql\Functors::negation($criterion); $this->statement->getCriterion()->add($criterion); return true; @@ -275,9 +275,9 @@ class PostSearchParser extends AbstractSearchParser if (!isset($_SESSION['browsing-seed'])) $_SESSION['browsing-seed'] = mt_rand(); $seed = $_SESSION['browsing-seed']; - $orderColumn = new Sql\SubstrFunctor( - new Sql\MultiplicationFunctor('post.id', $seed), - new Sql\AdditionFunctor(new Sql\LengthFunctor('post.id'), '2')); + $orderColumn = Sql\Functors::substr( + Sql\Functors::multiplication('post.id', $seed), + Sql\Functors::addition(Sql\Functors::length('post.id'), '2')); } else diff --git a/src/Models/SearchParsers/TagSearchParser.php b/src/Models/SearchParsers/TagSearchParser.php index 169fa9f8..7d4a1acc 100644 --- a/src/Models/SearchParsers/TagSearchParser.php +++ b/src/Models/SearchParsers/TagSearchParser.php @@ -12,10 +12,10 @@ class TagSearchParser extends AbstractSearchParser }, Access::getAllowedSafety()); $this->statement - ->addInnerJoin('post_tag', new Sql\EqualsFunctor('tag.id', 'post_tag.tag_id')) - ->addInnerJoin('post', new Sql\EqualsFunctor('post.id', 'post_tag.post_id')) - ->setCriterion((new Sql\ConjunctionFunctor) - ->add(Sql\InFunctor::fromArray('safety', Sql\Binding::fromArray($allowedSafety)))) + ->addInnerJoin('post_tag', Sql\Functors::equals('tag.id', 'post_tag.tag_id')) + ->addInnerJoin('post', Sql\Functors::equals('post.id', 'post_tag.post_id')) + ->setCriterion(Sql\Functors::conjunction() + ->add(Sql\Functors::in('safety', Sql\Binding::fromArray($allowedSafety)))) ->setGroupBy('tag.id'); } @@ -29,7 +29,7 @@ class TagSearchParser extends AbstractSearchParser $value .= '%'; $this->statement->getCriterion() - ->add(new Sql\NoCaseFunctor(new Sql\LikeFunctor('tag.name', new Sql\Binding($value)))); + ->add(Sql\Functors::noCase(Sql\Functors::like('tag.name', new Sql\Binding($value)))); return true; } @@ -39,7 +39,7 @@ class TagSearchParser extends AbstractSearchParser if ($orderByString == 'popularity') $this->statement->setOrderBy('post_count', $orderDir); elseif ($orderByString == 'alpha') - $this->statement->setOrderBy(new Sql\CaseFunctor('tag.name'), $orderDir); + $this->statement->setOrderBy(Sql\Functors::{'case'}('tag.name'), $orderDir); else return false; return true; diff --git a/src/Models/SearchParsers/UserSearchParser.php b/src/Models/SearchParsers/UserSearchParser.php index 1b187dfc..ea4e401b 100644 --- a/src/Models/SearchParsers/UserSearchParser.php +++ b/src/Models/SearchParsers/UserSearchParser.php @@ -10,9 +10,9 @@ class UserSearchParser extends AbstractSearchParser if ($value == 'pending') { - $this->statement->setCriterion((new Sql\DisjunctionFunctor) - ->add(new Sql\IsFunctor('staff_confirmed', new Sql\NullFunctor())) - ->add(new Sql\EqualsFunctor('staff_confirmed', '0'))); + $this->statement->setCriterion(Sql\Functors::disjunction() + ->add(Sql\Functors::is('staff_confirmed', Sql\Functors::null())) + ->add(Sql\Functors::equals('staff_confirmed', '0'))); return true; } return false; @@ -21,7 +21,7 @@ class UserSearchParser extends AbstractSearchParser protected function processOrderToken($orderByString, $orderDir) { if ($orderByString == 'alpha') - $this->statement->setOrderBy(new Sql\NoCaseFunctor('name'), $orderDir); + $this->statement->setOrderBy(Sql\Functors::noCase('name'), $orderDir); elseif ($orderByString == 'date') $this->statement->setOrderBy('join_date', $orderDir); else diff --git a/src/Models/SearchServices/AbstractSearchService.php b/src/Models/SearchServices/AbstractSearchService.php index 29af6c89..ba928bd4 100644 --- a/src/Models/SearchServices/AbstractSearchService.php +++ b/src/Models/SearchServices/AbstractSearchService.php @@ -1,6 +1,5 @@ decorate($stmt, $searchQuery); } - protected static function decorateCustom(Sql\SelectStatement $stmt) + protected static function decorateCustom($stmt) { } - protected static function decoratePager(Sql\SelectStatement $stmt, $perPage, $page) + protected static function decoratePager($stmt, $perPage, $page) { if ($perPage === null) return; @@ -42,14 +41,14 @@ abstract class AbstractSearchService $modelClassName = self::getModelClassName(); $table = $modelClassName::getTableName(); - $stmt = new Sql\SelectStatement(); + $stmt = Sql\Statements::select(); $stmt->setColumn($table . '.*'); $stmt->setTable($table); static::decorateParser($stmt, $searchQuery); static::decorateCustom($stmt); static::decoratePager($stmt, $perPage, $page); - return Database::fetchAll($stmt); + return Core::getDatabase()->fetchAll($stmt); } public static function getEntities($searchQuery, $perPage = null, $page = 1) @@ -64,16 +63,16 @@ abstract class AbstractSearchService $modelClassName = self::getModelClassName(); $table = $modelClassName::getTableName(); - $innerStmt = new Sql\SelectStatement(); + $innerStmt = Sql\Statements::select(); $innerStmt->setTable($table); static::decorateParser($innerStmt, $searchQuery); static::decorateCustom($innerStmt); $innerStmt->resetOrderBy(); - $stmt = new Sql\SelectStatement(); - $stmt->setColumn(new Sql\AliasFunctor(new Sql\CountFunctor('1'), 'count')); + $stmt = Sql\Statements::select(); + $stmt->setColumn(Sql\Functors::alias(Sql\Functors::count('1'), 'count')); $stmt->setSource($innerStmt, 'inner_stmt'); - return Database::fetchOne($stmt)['count']; + return Core::getDatabase()->fetchOne($stmt)['count']; } } diff --git a/src/Models/SearchServices/PostSearchService.php b/src/Models/SearchServices/PostSearchService.php index 8344f9d1..76305e11 100644 --- a/src/Models/SearchServices/PostSearchService.php +++ b/src/Models/SearchServices/PostSearchService.php @@ -1,37 +1,36 @@ transaction(function() use ($searchQuery, $postId) { - if (Database::getDriver() == 'sqlite') - $stmt = new Sql\RawStatement('CREATE TEMPORARY TABLE IF NOT EXISTS post_search(id INTEGER PRIMARY KEY AUTOINCREMENT, post_id INTEGER)'); + if (Core::getDatabase()->getDriver() == 'sqlite') + $stmt = Sql\Statements::raw('CREATE TEMPORARY TABLE IF NOT EXISTS post_search(id INTEGER PRIMARY KEY AUTOINCREMENT, post_id INTEGER)'); else - $stmt = new Sql\RawStatement('CREATE TEMPORARY TABLE IF NOT EXISTS post_search(id INTEGER PRIMARY KEY AUTO_INCREMENT, post_id INTEGER)'); - Database::exec($stmt); + $stmt = Sql\Statements::raw('CREATE TEMPORARY TABLE IF NOT EXISTS post_search(id INTEGER PRIMARY KEY AUTO_INCREMENT, post_id INTEGER)'); + Core::getDatabase()->execute($stmt); - $stmt = new Sql\DeleteStatement(); + $stmt = Sql\Statements::delete(); $stmt->setTable('post_search'); - Database::exec($stmt); + Core::getDatabase()->execute($stmt); - $innerStmt = new Sql\SelectStatement($searchQuery); + $innerStmt = Sql\Statements::select($searchQuery); $innerStmt->setColumn('post.id'); $innerStmt->setTable('post'); self::decorateParser($innerStmt, $searchQuery); - $stmt = new Sql\InsertStatement(); + $stmt = Sql\Statements::insert(); $stmt->setTable('post_search'); $stmt->setSource(['post_id'], $innerStmt); - Database::exec($stmt); + Core::getDatabase()->execute($stmt); - $stmt = new Sql\SelectStatement(); + $stmt = Sql\Statements::select(); $stmt->setTable('post_search'); $stmt->setColumn('id'); - $stmt->setCriterion(new Sql\EqualsFunctor('post_id', new Sql\Binding($postId))); - $rowId = Database::fetchOne($stmt)['id']; + $stmt->setCriterion(Sql\Functors::equals('post_id', new Sql\Binding($postId))); + $rowId = Core::getDatabase()->fetchOne($stmt)['id']; //it's possible that given post won't show in search results: //it can be hidden, it can have prohibited safety etc. @@ -41,11 +40,11 @@ class PostSearchService extends AbstractSearchService $rowId = intval($rowId); $stmt->setColumn('post_id'); - $stmt->setCriterion(new Sql\EqualsFunctor('id', new Sql\Binding($rowId - 1))); - $nextPostId = Database::fetchOne($stmt)['post_id']; + $stmt->setCriterion(Sql\Functors::equals('id', new Sql\Binding($rowId - 1))); + $nextPostId = Core::getDatabase()->fetchOne($stmt)['post_id']; - $stmt->setCriterion(new Sql\EqualsFunctor('id', new Sql\Binding($rowId + 1))); - $prevPostId = Database::fetchOne($stmt)['post_id']; + $stmt->setCriterion(Sql\Functors::equals('id', new Sql\Binding($rowId + 1))); + $prevPostId = Core::getDatabase()->fetchOne($stmt)['post_id']; return [$prevPostId, $nextPostId]; }); diff --git a/src/Models/SearchServices/TagSearchService.php b/src/Models/SearchServices/TagSearchService.php index 83363af2..9cd00002 100644 --- a/src/Models/SearchServices/TagSearchService.php +++ b/src/Models/SearchServices/TagSearchService.php @@ -1,12 +1,11 @@ addColumn(new Sql\AliasFunctor(new Sql\CountFunctor('post_tag.post_id'), 'post_count')); + $stmt->addColumn(Sql\Functors::alias(Sql\Functors::count('post_tag.post_id'), 'post_count')); } public static function getRelatedTags($parentTagName) @@ -51,52 +50,52 @@ class TagSearchService extends AbstractSearchService public static function getMostUsedTag() { - $stmt = (new Sql\SelectStatement) + $stmt = Sql\Statements::select() ->setTable('post_tag') - ->addInnerJoin('tag', new Sql\EqualsFunctor('post_tag.tag_id', 'tag.id')) + ->addInnerJoin('tag', Sql\Functors::equals('post_tag.tag_id', 'tag.id')) ->addColumn('tag.*') - ->addColumn(new Sql\AliasFunctor(new Sql\CountFunctor('post_tag.post_id'), 'post_count')) + ->addColumn(Sql\Functors::alias(Sql\Functors::count('post_tag.post_id'), 'post_count')) ->setGroupBy('post_tag.tag_id') - ->setOrderBy('post_count', Sql\SelectStatement::ORDER_DESC) + ->setOrderBy('post_count', Sql\Statements\SelectStatement::ORDER_DESC) ->setLimit(1, 0); - return TagModel::spawnFromDatabaseRow(Database::fetchOne($stmt)); + return TagModel::spawnFromDatabaseRow(Core::getDatabase()->fetchOne($stmt)); } private static function getSiblingTagsWithOccurences($parentTagId) { - $stmt = (new Sql\SelectStatement) + $stmt = Sql\Statements::select() ->setTable('tag') ->addColumn('tag.*') - ->addColumn(new Sql\AliasFunctor(new Sql\CountFunctor('post_tag.post_id'), 'post_count')) - ->addInnerJoin('post_tag', new Sql\EqualsFunctor('post_tag.tag_id', 'tag.id')) + ->addColumn(Sql\Functors::alias(Sql\Functors::count('post_tag.post_id'), 'post_count')) + ->addInnerJoin('post_tag', Sql\Functors::equals('post_tag.tag_id', 'tag.id')) ->setGroupBy('tag.id') - ->setOrderBy('post_count', Sql\SelectStatement::ORDER_DESC) - ->setCriterion(new Sql\ExistsFunctor((new Sql\SelectStatement) + ->setOrderBy('post_count', Sql\Statements\SelectStatement::ORDER_DESC) + ->setCriterion(Sql\Functors::exists(Sql\Statements::select() ->setTable('post_tag pt2') - ->setCriterion((new Sql\ConjunctionFunctor) - ->add(new Sql\EqualsFunctor('pt2.post_id', 'post_tag.post_id')) - ->add(new Sql\EqualsFunctor('pt2.tag_id', new Sql\Binding($parentTagId))) + ->setCriterion(Sql\Functors::conjunction() + ->add(Sql\Functors::equals('pt2.post_id', 'post_tag.post_id')) + ->add(Sql\Functors::equals('pt2.tag_id', new Sql\Binding($parentTagId))) ))); $rows = []; - foreach (Database::fetchAll($stmt) as $row) + foreach (Core::getDatabase()->fetchAll($stmt) as $row) $rows[$row['id']] = $row; return $rows; } private static function getGlobalOccurencesForTags($tagIds) { - $stmt = (new Sql\SelectStatement) + $stmt = Sql\Statements::select() ->setTable('tag') ->addColumn('tag.*') - ->addColumn(new Sql\AliasFunctor(new Sql\CountFunctor('post_tag.post_id'), 'post_count')) - ->addInnerJoin('post_tag', new Sql\EqualsFunctor('post_tag.tag_id', 'tag.id')) + ->addColumn(Sql\Functors::alias(Sql\Functors::count('post_tag.post_id'), 'post_count')) + ->addInnerJoin('post_tag', Sql\Functors::equals('post_tag.tag_id', 'tag.id')) ->setCriterion(Sql\InFunctor::fromArray('tag.id', Sql\Binding::fromArray($tagIds))) ->setGroupBy('tag.id'); $rows = []; - foreach (Database::fetchAll($stmt) as $row) + foreach (Core::getDatabase()->fetchAll($stmt) as $row) $rows[$row['id']] = $row; return $rows; } diff --git a/src/Models/TagModel.php b/src/Models/TagModel.php index b968eb0a..79f814e6 100644 --- a/src/Models/TagModel.php +++ b/src/Models/TagModel.php @@ -1,6 +1,5 @@ validate(); - Database::transaction(function() use ($tag) + Core::getDatabase()->transaction(function() use ($tag) { self::forgeId($tag, 'tag'); - $stmt = new Sql\UpdateStatement(); + $stmt = Sql\Statements::update(); $stmt->setTable('tag'); $stmt->setColumn('name', new Sql\Binding($tag->getName())); - $stmt->setCriterion(new Sql\EqualsFunctor('id', new Sql\Binding($tag->getId()))); + $stmt->setCriterion(Sql\Functors::equals('id', new Sql\Binding($tag->getId()))); - Database::exec($stmt); + Core::getDatabase()->execute($stmt); }); return $tag; @@ -32,20 +31,20 @@ final class TagModel extends AbstractCrudModel { $binding = new Sql\Binding($tag->getId()); - $stmt = new Sql\DeleteStatement(); + $stmt = Sql\Statements::delete(); $stmt->setTable('post_tag'); - $stmt->setCriterion(new Sql\EqualsFunctor('tag_id', $binding)); - Database::exec($stmt); + $stmt->setCriterion(Sql\Functors::equals('tag_id', $binding)); + Core::getDatabase()->execute($stmt); - $stmt = new Sql\DeleteStatement(); + $stmt = Sql\Statements::delete(); $stmt->setTable('tag'); - $stmt->setCriterion(new Sql\EqualsFunctor('id', $binding)); - Database::exec($stmt); + $stmt->setCriterion(Sql\Functors::equals('id', $binding)); + Core::getDatabase()->execute($stmt); } public static function rename($sourceName, $targetName) { - Database::transaction(function() use ($sourceName, $targetName) + Core::getDatabase()->transaction(function() use ($sourceName, $targetName) { $sourceTag = self::getByName($sourceName); $targetTag = self::tryGetByName($targetName); @@ -65,7 +64,7 @@ final class TagModel extends AbstractCrudModel public static function merge($sourceName, $targetName) { - Database::transaction(function() use ($sourceName, $targetName) + Core::getDatabase()->transaction(function() use ($sourceName, $targetName) { $sourceTag = self::getByName($sourceName); $targetTag = self::getByName($targetName); @@ -73,40 +72,40 @@ final class TagModel extends AbstractCrudModel if ($sourceTag->getId() == $targetTag->getId()) throw new SimpleException('Source and target tag are the same'); - $stmt = new Sql\SelectStatement(); + $stmt = Sql\Statements::select(); $stmt->setColumn('post.id'); $stmt->setTable('post'); $stmt->setCriterion( - (new Sql\ConjunctionFunctor) + Sql\Functors::conjunction() ->add( - new Sql\ExistsFunctor( - (new Sql\SelectStatement) + Sql\Functors::exists( + Sql\Statements::select() ->setTable('post_tag') ->setCriterion( - (new Sql\ConjunctionFunctor) - ->add(new Sql\EqualsFunctor('post_tag.post_id', 'post.id')) - ->add(new Sql\EqualsFunctor('post_tag.tag_id', new Sql\Binding($sourceTag->getId())))))) + Sql\Functors::conjunction() + ->add(Sql\Functors::equals('post_tag.post_id', 'post.id')) + ->add(Sql\Functors::equals('post_tag.tag_id', new Sql\Binding($sourceTag->getId())))))) ->add( - new Sql\NegationFunctor( - new Sql\ExistsFunctor( - (new Sql\SelectStatement) + Sql\Functors::negation( + Sql\Functors::exists( + Sql\Statements::select() ->setTable('post_tag') ->setCriterion( - (new Sql\ConjunctionFunctor) - ->add(new Sql\EqualsFunctor('post_tag.post_id', 'post.id')) - ->add(new Sql\EqualsFunctor('post_tag.tag_id', new Sql\Binding($targetTag->getId())))))))); - $rows = Database::fetchAll($stmt); + Sql\Functors::conjunction() + ->add(Sql\Functors::equals('post_tag.post_id', 'post.id')) + ->add(Sql\Functors::equals('post_tag.tag_id', new Sql\Binding($targetTag->getId())))))))); + $rows = Core::getDatabase()->fetchAll($stmt); $postIds = array_map(function($row) { return $row['id']; }, $rows); self::remove($sourceTag); foreach ($postIds as $postId) { - $stmt = new Sql\InsertStatement(); + $stmt = Sql\Statements::insert(); $stmt->setTable('post_tag'); $stmt->setColumn('post_id', new Sql\Binding($postId)); $stmt->setColumn('tag_id', new Sql\Binding($targetTag->getId())); - Database::exec($stmt); + Core::getDatabase()->execute($stmt); } }); } @@ -114,13 +113,13 @@ final class TagModel extends AbstractCrudModel public static function getAllByPostId($key) { - $stmt = new Sql\SelectStatement(); + $stmt = Sql\Statements::select(); $stmt->setColumn('tag.*'); $stmt->setTable('tag'); - $stmt->addInnerJoin('post_tag', new Sql\EqualsFunctor('post_tag.tag_id', 'tag.id')); - $stmt->setCriterion(new Sql\EqualsFunctor('post_tag.post_id', new Sql\Binding($key))); + $stmt->addInnerJoin('post_tag', Sql\Functors::equals('post_tag.tag_id', 'tag.id')); + $stmt->setCriterion(Sql\Functors::equals('post_tag.post_id', new Sql\Binding($key))); - $rows = Database::fetchAll($stmt); + $rows = Core::getDatabase()->fetchAll($stmt); if ($rows) return self::spawnFromDatabaseRows($rows); return []; @@ -136,12 +135,12 @@ final class TagModel extends AbstractCrudModel public static function tryGetByName($key) { - $stmt = new Sql\SelectStatement(); + $stmt = Sql\Statements::select(); $stmt->setColumn('tag.*'); $stmt->setTable('tag'); - $stmt->setCriterion(new Sql\NoCaseFunctor(new Sql\EqualsFunctor('name', new Sql\Binding($key)))); + $stmt->setCriterion(Sql\Functors::noCase(Sql\Functors::equals('name', new Sql\Binding($key)))); - $row = Database::fetchOne($stmt); + $row = Core::getDatabase()->fetchOne($stmt); return $row ? self::spawnFromDatabaseRow($row) : null; @@ -151,15 +150,15 @@ final class TagModel extends AbstractCrudModel public static function removeUnused() { - $stmt = (new Sql\DeleteStatement) + $stmt = Sql\Statements::delete() ->setTable('tag') ->setCriterion( - new Sql\NegationFunctor( - new Sql\ExistsFunctor( - (new Sql\SelectStatement) + Sql\Functors::negation( + Sql\Functors::exists( + Sql\Statements::select() ->setTable('post_tag') - ->setCriterion(new Sql\EqualsFunctor('post_tag.tag_id', 'tag.id'))))); - Database::exec($stmt); + ->setCriterion(Sql\Functors::equals('post_tag.tag_id', 'tag.id'))))); + Core::getDatabase()->execute($stmt); } public static function spawnFromNames(array $tagNames) diff --git a/src/Models/TokenModel.php b/src/Models/TokenModel.php index 97a3d762..9c97115f 100644 --- a/src/Models/TokenModel.php +++ b/src/Models/TokenModel.php @@ -1,6 +1,5 @@ validate(); - Database::transaction(function() use ($token) + Core::getDatabase()->transaction(function() use ($token) { self::forgeId($token); @@ -24,14 +23,14 @@ final class TokenModel extends AbstractCrudModel 'expires' => $token->getExpirationTime(), ]; - $stmt = new Sql\UpdateStatement(); + $stmt = Sql\Statements::update(); $stmt->setTable('user_token'); - $stmt->setCriterion(new Sql\EqualsFunctor('id', new Sql\Binding($token->getId()))); + $stmt->setCriterion(Sql\Functors::equals('id', new Sql\Binding($token->getId()))); foreach ($bindings as $key => $val) $stmt->setColumn($key, new Sql\Binding($val)); - Database::exec($stmt); + Core::getDatabase()->execute($stmt); }); return $token; @@ -50,12 +49,12 @@ final class TokenModel extends AbstractCrudModel if (empty($key)) throw new SimpleNotFoundException('Invalid security token'); - $stmt = new Sql\SelectStatement(); + $stmt = Sql\Statements::select(); $stmt->setTable('user_token'); $stmt->setColumn('*'); - $stmt->setCriterion(new Sql\EqualsFunctor('token', new Sql\Binding($key))); + $stmt->setCriterion(Sql\Functors::equals('token', new Sql\Binding($key))); - $row = Database::fetchOne($stmt); + $row = Core::getDatabase()->fetchOne($stmt); return $row ? self::spawnFromDatabaseRow($row) : null; diff --git a/src/Models/UserModel.php b/src/Models/UserModel.php index 050de316..770d1129 100644 --- a/src/Models/UserModel.php +++ b/src/Models/UserModel.php @@ -1,6 +1,5 @@ validate(); - Database::transaction(function() use ($user) + Core::getDatabase()->transaction(function() use ($user) { self::forgeId($user); @@ -32,14 +31,14 @@ final class UserModel extends AbstractCrudModel 'avatar_style' => $user->getAvatarStyle()->toInteger(), ]; - $stmt = (new Sql\UpdateStatement) + $stmt = Sql\Statements::update() ->setTable('user') - ->setCriterion(new Sql\EqualsFunctor('id', new Sql\Binding($user->getId()))); + ->setCriterion(Sql\Functors::equals('id', new Sql\Binding($user->getId()))); foreach ($bindings as $key => $val) $stmt->setColumn($key, new Sql\Binding($val)); - Database::exec($stmt); + Core::getDatabase()->execute($stmt); }); return $user; @@ -47,33 +46,33 @@ final class UserModel extends AbstractCrudModel protected static function removeSingle($user) { - Database::transaction(function() use ($user) + Core::getDatabase()->transaction(function() use ($user) { $binding = new Sql\Binding($user->getId()); - $stmt = new Sql\DeleteStatement(); + $stmt = Sql\Statements::delete(); $stmt->setTable('post_score'); - $stmt->setCriterion(new Sql\EqualsFunctor('user_id', $binding)); - Database::exec($stmt); + $stmt->setCriterion(Sql\Functors::equals('user_id', $binding)); + Core::getDatabase()->execute($stmt); $stmt->setTable('favoritee'); - Database::exec($stmt); + Core::getDatabase()->execute($stmt); $stmt->setTable('user'); - $stmt->setCriterion(new Sql\EqualsFunctor('id', $binding)); - Database::exec($stmt); + $stmt->setCriterion(Sql\Functors::equals('id', $binding)); + Core::getDatabase()->execute($stmt); - $stmt = new Sql\UpdateStatement(); + $stmt = Sql\Statements::update(); $stmt->setTable('comment'); - $stmt->setCriterion(new Sql\EqualsFunctor('commenter_id', $binding)); - $stmt->setColumn('commenter_id', new Sql\NullFunctor()); - Database::exec($stmt); + $stmt->setCriterion(Sql\Functors::equals('commenter_id', $binding)); + $stmt->setColumn('commenter_id', Sql\Functors::null()); + Core::getDatabase()->execute($stmt); - $stmt = new Sql\UpdateStatement(); + $stmt = Sql\Statements::update(); $stmt->setTable('post'); - $stmt->setCriterion(new Sql\EqualsFunctor('uploader_id', $binding)); - $stmt->setColumn('uploader_id', new Sql\NullFunctor()); - Database::exec($stmt); + $stmt->setCriterion(Sql\Functors::equals('uploader_id', $binding)); + $stmt->setColumn('uploader_id', Sql\Functors::null()); + Core::getDatabase()->execute($stmt); }); } @@ -89,12 +88,12 @@ final class UserModel extends AbstractCrudModel public static function tryGetByName($key) { - $stmt = new Sql\SelectStatement(); + $stmt = Sql\Statements::select(); $stmt->setColumn('*'); $stmt->setTable('user'); - $stmt->setCriterion(new Sql\NoCaseFunctor(new Sql\EqualsFunctor('name', new Sql\Binding(trim($key))))); + $stmt->setCriterion(Sql\Functors::noCase(Sql\Functors::equals('name', new Sql\Binding(trim($key))))); - $row = Database::fetchOne($stmt); + $row = Core::getDatabase()->fetchOne($stmt); return $row ? self::spawnFromDatabaseRow($row) : null; @@ -112,14 +111,14 @@ final class UserModel extends AbstractCrudModel { $key = trim($key); - $stmt = new Sql\SelectStatement(); + $stmt = Sql\Statements::select(); $stmt->setColumn('*'); $stmt->setTable('user'); - $stmt->setCriterion((new Sql\DisjunctionFunctor) - ->add(new Sql\NoCaseFunctor(new Sql\EqualsFunctor('email_unconfirmed', new Sql\Binding($key)))) - ->add(new Sql\NoCaseFunctor(new Sql\EqualsFunctor('email_confirmed', new Sql\Binding($key))))); + $stmt->setCriterion(Sql\Functors::disjunction() + ->add(Sql\Functors::noCase(Sql\Functors::equals('email_unconfirmed', new Sql\Binding($key)))) + ->add(Sql\Functors::noCase(Sql\Functors::equals('email_confirmed', new Sql\Binding($key))))); - $row = Database::fetchOne($stmt); + $row = Core::getDatabase()->fetchOne($stmt); return $row ? self::spawnFromDatabaseRow($row) : null; @@ -129,56 +128,56 @@ final class UserModel extends AbstractCrudModel public static function updateUserScore($user, $post, $score) { - Database::transaction(function() use ($user, $post, $score) + Core::getDatabase()->transaction(function() use ($user, $post, $score) { $post->removeCache('score'); - $stmt = new Sql\DeleteStatement(); + $stmt = Sql\Statements::delete(); $stmt->setTable('post_score'); - $stmt->setCriterion((new Sql\ConjunctionFunctor) - ->add(new Sql\EqualsFunctor('post_id', new Sql\Binding($post->getId()))) - ->add(new Sql\EqualsFunctor('user_id', new Sql\Binding($user->getId())))); - Database::exec($stmt); + $stmt->setCriterion(Sql\Functors::conjunction() + ->add(Sql\Functors::equals('post_id', new Sql\Binding($post->getId()))) + ->add(Sql\Functors::equals('user_id', new Sql\Binding($user->getId())))); + Core::getDatabase()->execute($stmt); $score = intval($score); if (abs($score) > 1) throw new SimpleException('Invalid score'); if ($score != 0) { - $stmt = new Sql\InsertStatement(); + $stmt = Sql\Statements::insert(); $stmt->setTable('post_score'); $stmt->setColumn('post_id', new Sql\Binding($post->getId())); $stmt->setColumn('user_id', new Sql\Binding($user->getId())); $stmt->setColumn('score', new Sql\Binding($score)); - Database::exec($stmt); + Core::getDatabase()->execute($stmt); } }); } public static function addToUserFavorites($user, $post) { - Database::transaction(function() use ($user, $post) + Core::getDatabase()->transaction(function() use ($user, $post) { $post->removeCache('fav_count'); self::removeFromUserFavorites($user, $post); - $stmt = new Sql\InsertStatement(); + $stmt = Sql\Statements::insert(); $stmt->setTable('favoritee'); $stmt->setColumn('post_id', new Sql\Binding($post->getId())); $stmt->setColumn('user_id', new Sql\Binding($user->getId())); $stmt->setColumn('fav_date', time()); - Database::exec($stmt); + Core::getDatabase()->execute($stmt); }); } public static function removeFromUserFavorites($user, $post) { - Database::transaction(function() use ($user, $post) + Core::getDatabase()->transaction(function() use ($user, $post) { $post->removeCache('fav_count'); - $stmt = new Sql\DeleteStatement(); + $stmt = Sql\Statements::delete(); $stmt->setTable('favoritee'); - $stmt->setCriterion((new Sql\ConjunctionFunctor) - ->add(new Sql\EqualsFunctor('post_id', new Sql\Binding($post->getId()))) - ->add(new Sql\EqualsFunctor('user_id', new Sql\Binding($user->getId())))); - Database::exec($stmt); + $stmt->setCriterion(Sql\Functors::conjunction() + ->add(Sql\Functors::equals('post_id', new Sql\Binding($post->getId()))) + ->add(Sql\Functors::equals('user_id', new Sql\Binding($user->getId())))); + Core::getDatabase()->execute($stmt); }); } diff --git a/src/Router.php b/src/Router.php new file mode 100644 index 00000000..feed8c2a --- /dev/null +++ b/src/Router.php @@ -0,0 +1,166 @@ +registerMisc(); + $this->registerStaticPages(); + $this->registerAuth(); + $this->registerPostController(); + $this->registerUserController(); + $this->registerLogController(); + $this->registerTagController(); + $this->registerCommentController(); + } + + private function registerMisc() + { + $this->register(['ApiController', 'runAction'], null, '/api'); + } + + private function registerStaticPages() + { + $this->register(['StaticPagesController', 'mainPageView'], 'GET', ''); + $this->register(['StaticPagesController', 'mainPageView'], 'GET', '/index'); + $this->register(['StaticPagesController', 'apiDocsView'], 'GET', '/api-docs'); + $this->register(['StaticPagesController', 'helpView'], 'GET', '/help'); + $this->register(['StaticPagesController', 'helpView'], 'GET', '/help/{tab}'); + $this->register(['StaticPagesController', 'fatalErrorView'], 'POST', '/fatal-error/{code}'); + $this->register(['StaticPagesController', 'fatalErrorView'], 'GET', '/fatal-error/{code}'); + } + + private function registerAuth() + { + $this->register(['AuthController', 'loginView'], 'GET', '/auth/login'); + $this->register(['AuthController', 'loginAction'], 'POST', '/auth/login'); + $this->register(['AuthController', 'logoutAction'], 'POST', '/auth/logout'); + $this->register(['AuthController', 'logoutAction'], 'GET', '/auth/logout'); + } + + private function registerPostController() + { + $postValidation = + [ + 'tag' => '[^\/]*', + 'enable' => '0|1', + 'source' => 'posts|mass-tag', + 'query' => '[^\/]*', + 'additionalInfo' => '[^\/]*', + 'score' => '-1|0|1', + 'page' => '\d*', + ]; + + $this->register(['PostController', 'uploadView'], 'GET', '/posts/upload', $postValidation); + $this->register(['PostController', 'uploadAction'], 'POST', '/posts/upload', $postValidation); + $this->register(['PostController', 'editView'], 'GET', '/post/{identifier}/edit', $postValidation); + $this->register(['PostController', 'editAction'], 'POST', '/post/{identifier}/edit', $postValidation); + $this->register(['PostController', 'deleteAction'], null, '/post/{identifier}/delete', $postValidation); + + $this->register(['PostController', 'listView'], 'GET', '/{source}', $postValidation); + $this->register(['PostController', 'listView'], 'GET', '/{source}/{page}', $postValidation); + $this->register(['PostController', 'listView'], 'GET', '/{source}/{query}/{page}', $postValidation); + $this->register(['PostController', 'listView'], 'GET', '/{source}/{query}/{additionalInfo}/{page}', $postValidation); + $this->register(['PostController', 'listRedirectAction'], 'POST', '/{source}-redirect', $postValidation); + + $this->register(['PostController', 'randomView'], 'GET', '/random', $postValidation); + $this->register(['PostController', 'randomView'], 'GET', '/random/{page}', $postValidation); + $this->register(['PostController', 'favoritesView'], 'GET', '/favorites', $postValidation); + $this->register(['PostController', 'favoritesView'], 'GET', '/favorites/{page}', $postValidation); + $this->register(['PostController', 'upvotedView'], 'GET', '/upvoted', $postValidation); + $this->register(['PostController', 'upvotedView'], 'GET', '/upvoted/{page}', $postValidation); + + $this->register(['PostController', 'genericView'], 'GET', '/post/{identifier}', $postValidation); + $this->register(['PostController', 'fileView'], 'GET', '/post/{name}/retrieve', $postValidation); + $this->register(['PostController', 'thumbnailView'], 'GET', '/post/{name}/thumb', $postValidation); + + $this->register(['PostController', 'toggleTagAction'], null, '/post/{identifier}/toggle-tag/{tag}/{enable}', $postValidation); + $this->register(['PostController', 'flagAction'], null, '/post/{identifier}/flag', $postValidation); + $this->register(['PostController', 'hideAction'], null, '/post/{identifier}/hide', $postValidation); + $this->register(['PostController', 'unhideAction'], null, '/post/{identifier}/unhide', $postValidation); + $this->register(['PostController', 'removeFavoriteAction'], null, '/post/{identifier}/rem-fav', $postValidation); + $this->register(['PostController', 'addFavoriteAction'], null, '/post/{identifier}/add-fav', $postValidation); + $this->register(['PostController', 'scoreAction'], null, '/post/{identifier}/score/{score}', $postValidation); + $this->register(['PostController', 'featureAction'], null, '/post/{identifier}/feature', $postValidation); + } + + private function registerUserController() + { + $userValidation = + [ + 'identifier' => '[^\/]+', + 'page' => '\d*', + 'tab' => 'favs|uploads|settings|edit|delete', + 'filter' => '[^\/]+', + ]; + + $this->register(['UserController', 'listView'], 'GET', '/users', $userValidation); + $this->register(['UserController', 'listView'], 'GET', '/users/{page}', $userValidation); + $this->register(['UserController', 'listView'], 'GET', '/users/{filter}/{page}', $userValidation); + $this->register(['UserController', 'genericView'], 'GET', '/user/{identifier}/{tab}', $userValidation); + $this->register(['UserController', 'genericView'], 'GET', '/user/{identifier}/{tab}/{page}', $userValidation); + + $this->register(['UserController', 'editAction'], 'POST', '/user/{identifier}/edit', $userValidation); + + $this->register(['UserController', 'registrationView'], 'GET', '/register', $userValidation); + $this->register(['UserController', 'registrationAction'], 'POST', '/register', $userValidation); + + $this->register(['UserController', 'activationView'], 'GET', '/activation', $userValidation); + $this->register(['UserController', 'activationAction'], 'POST', '/activation', $userValidation); + $this->register(['UserController', 'activationAction'], 'GET', '/activation/{tokenText}', $userValidation); + $this->register(['UserController', 'passwordResetView'], 'GET', '/password-reset', $userValidation); + $this->register(['UserController', 'passwordResetAction'], 'POST', '/password-reset', $userValidation); + $this->register(['UserController', 'passwordResetAction'], 'GET', '/password-reset/{tokenText}', $userValidation); + + $this->register(['UserController', 'flagAction'], null, '/user/{identifier}/flag', $userValidation); + $this->register(['UserController', 'banAction'], null, '/user/{identifier}/ban', $userValidation); + $this->register(['UserController', 'unbanAction'], null, '/user/{identifier}/unban', $userValidation); + $this->register(['UserController', 'acceptRegistrationAction'], null, '/user/{identifier}/accept-registration', $userValidation); + $this->register(['UserController', 'deleteAction'], null, '/user/{identifier}/delete', $userValidation); + $this->register(['UserController', 'settingsAction'], null, '/user/{identifier}/settings', $userValidation); + $this->register(['UserController', 'toggleSafetyAction'], null, '/user/toggle-safety/{safety}', $userValidation); + } + + private function registerLogController() + { + $this->register(['LogController', 'listView'], 'GET', '/logs'); + $this->register(['LogController', 'logView'], 'GET', '/log/{name}', ['name' => '[0-9a-zA-Z._-]+']); + $this->register(['LogController', 'logView'], 'GET', '/log/{name}/{page}', ['name' => '[0-9a-zA-Z._-]+', 'page' => '\d*']); + $this->register(['LogController', 'logView'], 'GET', '/log/{name}/{page}/{filter}', ['name' => '[0-9a-zA-Z._-]+', 'page' => '\d*', 'filter' => '.*']); + } + + private function registerTagController() + { + $tagValidation = + [ + 'page' => '\d*', + 'filter' => '[^\/]+', + ]; + + $this->register(['TagController', 'listView'], 'GET', '/tags', $tagValidation); + $this->register(['TagController', 'listView'], 'GET', '/tags/{page}', $tagValidation); + $this->register(['TagController', 'listView'], 'GET', '/tags/{filter}/{page}', $tagValidation); + $this->register(['TagController', 'autoCompleteView'], 'GET', '/tags-autocomplete', $tagValidation); + $this->register(['TagController', 'relatedView'], 'GET', '/tags-related', $tagValidation); + $this->register(['TagController', 'renameView'], 'GET', '/tags-rename', $tagValidation); + $this->register(['TagController', 'renameAction'], 'POST', '/tags-rename', $tagValidation); + $this->register(['TagController', 'mergeView'], 'GET', '/tags-merge', $tagValidation); + $this->register(['TagController', 'mergeAction'], 'POST', '/tags-merge', $tagValidation); + $this->register(['TagController', 'massTagRedirectView'], 'GET', '/mass-tag-redirect', $tagValidation); + } + + private function registerCommentController() + { + $commentValidation = + [ + 'id' => '\d+', + 'page' => '\d+', + ]; + + $this->register(['CommentController', 'listView'], 'GET', '/comments', $commentValidation); + $this->register(['CommentController', 'listView'], 'GET', '/comments/{page}', $commentValidation); + $this->register(['CommentController', 'addAction'], 'POST', '/comment/add', $commentValidation); + $this->register(['CommentController', 'deleteAction'], null, '/comment/{id}/delete', $commentValidation); + $this->register(['CommentController', 'editView'], 'GET', '/comment/{id}/edit', $commentValidation); + $this->register(['CommentController', 'editAction'], 'POST', '/comment/{id}/edit', $commentValidation); + } +} diff --git a/src/View.php b/src/View.php index c7c109b1..3b84ffe4 100644 --- a/src/View.php +++ b/src/View.php @@ -1,5 +1,5 @@ assets->addStylesheet('auth.css');
linkTo( ['AuthController', 'loginAction']) ?>" class="auth">

If you don't have an account yet,
- click here to create a new one. + click here to create a new one.

assets->addStylesheet('auth.css');

Problems logging in?

diff --git a/src/Views/comment/comment-add.phtml b/src/Views/comment/comment-add.phtml index 98463342..a6ce797e 100644 --- a/src/Views/comment/comment-add.phtml +++ b/src/Views/comment/comment-add.phtml @@ -5,7 +5,7 @@ $this->assets->addScript('comment-edit.js');

add comment

diff --git a/src/Views/comment/comment-edit.phtml b/src/Views/comment/comment-edit.phtml index 414df6da..be46109a 100644 --- a/src/Views/comment/comment-edit.phtml +++ b/src/Views/comment/comment-edit.phtml @@ -5,7 +5,7 @@ $this->assets->addScript('comment-edit.js'); linkTo( ['CommentController', 'editAction'], ['id' => $this->context->transport->comment->getId()]) ?>" class="edit-comment"> diff --git a/src/Views/comment/comment-list.phtml b/src/Views/comment/comment-list.phtml index 32c815ed..5c7ab1ed 100644 --- a/src/Views/comment/comment-list.phtml +++ b/src/Views/comment/comment-list.phtml @@ -31,7 +31,7 @@ $this->assets->setSubTitle('comments'); count($commentsToDisplay)): ?> - linkTo( ['PostController', 'genericView'], ['id' => $this->context->post->getId()]) ?>"> (more…) diff --git a/src/Views/comment/comment-small.phtml b/src/Views/comment/comment-small.phtml index f1a129c9..7b31f666 100644 --- a/src/Views/comment/comment-small.phtml +++ b/src/Views/comment/comment-small.phtml @@ -8,7 +8,7 @@
context->comment->getCommenter() ?> - linkTo( ['UserController', 'genericView'], ['identifier' => $commenter->getName()]) ?>"> <?= $commenter->getName() ?> @@ -24,7 +24,7 @@
- linkTo( ['UserController', 'genericView'], ['identifier' => $commenter->getName()]) ?>"> getName() ?> @@ -42,7 +42,7 @@ Privilege::EditComment, Access::getIdentity($commenter)))): ?> - linkTo( ['CommentController', 'editView'], ['id' => $this->context->comment->getId()]) ?>"> edit @@ -56,7 +56,7 @@ linkTo( ['CommentController', 'deleteAction'], ['id' => $this->context->comment->getId()]) ?>"> delete diff --git a/src/Views/debug.phtml b/src/Views/debug.phtml index f274002f..71d24974 100644 --- a/src/Views/debug.phtml +++ b/src/Views/debug.phtml @@ -3,10 +3,10 @@ $this->assets->addStylesheet('debug.css'); ?>
- + getLogs() as $log): ?>
getStatement()->getAsString(); + $query = $log->getStatement(); $query = str_replace('(', '(', $query); $query = str_replace(')', ')', $query); ?> @@ -14,9 +14,9 @@ $this->assets->addStylesheet('debug.css');
' . $log->getStatement()->getBindings()[$key] . '';
+					return $key . '=' . $log->getBindings()[$key] . '';
 				},
-				array_keys($log->getStatement()->getBindings()))) ?>
+ array_keys($log->getBindings()))) ?> diff --git a/src/Views/layout-normal.phtml b/src/Views/layout-normal.phtml index d6defab4..15c392fd 100644 --- a/src/Views/layout-normal.phtml +++ b/src/Views/layout-normal.phtml @@ -45,15 +45,15 @@ $this->assets->addScript('core.js'); - API + API - Logs + Logs
context->startTime) ?>s - + getLogs()) ?>

diff --git a/src/Views/log/log-list.phtml b/src/Views/log/log-list.phtml index bbab28f6..3ce88fd3 100644 --- a/src/Views/log/log-list.phtml +++ b/src/Views/log/log-list.phtml @@ -8,7 +8,7 @@ $this->context->subTitle = 'latest logs';
Execution:getExecutionTime()) ?>