diff --git a/TODO b/TODO index eec316a8..ff06bd99 100644 --- a/TODO +++ b/TODO @@ -28,8 +28,6 @@ everything related to posts: - link to iqdb and/or tineye - auto feature - - number of posts - - total size of posts - random post - regard safety settings diff --git a/public_html/css/home.css b/public_html/css/home.css index cf52b012..00ae911f 100644 --- a/public_html/css/home.css +++ b/public_html/css/home.css @@ -3,6 +3,10 @@ } #home h1 { margin-top: 0; + margin-bottom: 0; +} +#home h1+p { + margin-top: 0; } #home .post { diff --git a/public_html/js/Presenters/HomePresenter.js b/public_html/js/Presenters/HomePresenter.js index ebb1e38e..4b773b63 100644 --- a/public_html/js/Presenters/HomePresenter.js +++ b/public_html/js/Presenters/HomePresenter.js @@ -13,6 +13,7 @@ App.Presenters.HomePresenter = function( var $el = jQuery('#content'); var homeTemplate; var postContentTemplate; + var globals; var post; function init(args, loaded) { @@ -22,15 +23,18 @@ App.Presenters.HomePresenter = function( promise.waitAll( util.promiseTemplate('home'), util.promiseTemplate('post-content'), + api.get('/globals'), api.get('/posts/featured')) .then(function( homeTemplateHtml, postContentTemplateHtml, - response) { + globalsResponse, + featuredPostResponse) { homeTemplate = _.template(homeTemplateHtml); postContentTemplate = _.template(postContentTemplateHtml); - post = response.json; + globals = globalsResponse.json; + post = featuredPostResponse.json; render(); loaded(); @@ -43,8 +47,10 @@ App.Presenters.HomePresenter = function( $el.html(homeTemplate({ post: post, postContentTemplate: postContentTemplate, + globals: globals, title: topNavigationPresenter.getBaseTitle(), formatRelativeTime: util.formatRelativeTime, + formatFileSize: util.formatFileSize, })); } diff --git a/public_html/templates/home.tpl b/public_html/templates/home.tpl index 4806bcad..541597cb 100644 --- a/public_html/templates/home.tpl +++ b/public_html/templates/home.tpl @@ -1,5 +1,8 @@

<%= title %>

+

+ Serving <%= globals.postCount %> posts (<%= formatFileSize(globals.postSize) %>) +

<% if (post) { %>
diff --git a/scripts/cron-globals.php b/scripts/cron-globals.php new file mode 100644 index 00000000..d3c618c4 --- /dev/null +++ b/scripts/cron-globals.php @@ -0,0 +1,8 @@ +updatePostGlobals(); diff --git a/src/Controllers/GlobalParamController.php b/src/Controllers/GlobalParamController.php new file mode 100644 index 00000000..5338ede0 --- /dev/null +++ b/src/Controllers/GlobalParamController.php @@ -0,0 +1,29 @@ +globalParamDao = $globalParamDao; + } + + public function registerRoutes(\Szurubooru\Router $router) + { + $router->get('/api/globals', [$this, 'getGlobals']); + } + + public function getGlobals() + { + $globals = $this->globalParamDao->findAll(); + $return = []; + foreach ($globals as $global) + { + $return[$global->getKey()] = $global->getValue(); + } + return $return; + } +} diff --git a/src/Dao/PostDao.php b/src/Dao/PostDao.php index 69a6c5e1..676448ee 100644 --- a/src/Dao/PostDao.php +++ b/src/Dao/PostDao.php @@ -26,6 +26,17 @@ class PostDao extends AbstractDao implements ICrudDao $this->thumbnailService = $thumbnailService; } + public function getCount() + { + return count($this->fpdo->from($this->tableName)); + } + + public function getTotalFileSize() + { + $query = $this->fpdo->from($this->tableName)->select('SUM(originalFileSize) AS __sum'); + return intval(iterator_to_array($query)[0]['__sum']); + } + public function findByName($name) { return $this->findOneBy('name', $name); diff --git a/src/Entities/GlobalParam.php b/src/Entities/GlobalParam.php index 4a0a7b4a..1e0a53a8 100644 --- a/src/Entities/GlobalParam.php +++ b/src/Entities/GlobalParam.php @@ -4,6 +4,8 @@ namespace Szurubooru\Entities; class GlobalParam extends Entity { const KEY_FEATURED_POST = 'featuredPost'; + const KEY_POST_SIZE = 'postSize'; + const KEY_POST_COUNT = 'postCount'; private $key; private $value; diff --git a/src/Services/PostService.php b/src/Services/PostService.php index 41d6d92e..8f028b4f 100644 --- a/src/Services/PostService.php +++ b/src/Services/PostService.php @@ -229,6 +229,23 @@ class PostService $this->transactionManager->commit($transactionFunc); } + public function updatePostGlobals() + { + $transactionFunc = function() + { + $countParam = new \Szurubooru\Entities\GlobalParam(); + $countParam->setKey(\Szurubooru\Entities\GlobalParam::KEY_POST_COUNT); + $countParam->setValue($this->postDao->getCount()); + $this->globalParamDao->save($countParam); + + $fileSizeParam = new \Szurubooru\Entities\GlobalParam(); + $fileSizeParam->setKey(\Szurubooru\Entities\GlobalParam::KEY_POST_SIZE); + $fileSizeParam->setValue($this->postDao->getTotalFileSize()); + $this->globalParamDao->save($fileSizeParam); + }; + $this->transactionManager->commit($transactionFunc); + } + private function assertNoPostWithThisContentChecksum(\Szurubooru\Entities\Post $parent) { $checksumToCheck = $parent->getContentChecksum(); diff --git a/src/di.php b/src/di.php index 1d0e9e78..3cb17fe2 100644 --- a/src/di.php +++ b/src/di.php @@ -33,6 +33,7 @@ return [ $container->get(\Szurubooru\Controllers\UserAvatarController::class), $container->get(\Szurubooru\Controllers\PostController::class), $container->get(\Szurubooru\Controllers\PostContentController::class), + $container->get(\Szurubooru\Controllers\GlobalParamController::class), ]; }), ]; diff --git a/tests/Dao/PostDaoTest.php b/tests/Dao/PostDaoTest.php index e0e6ab1e..5d9e66ac 100644 --- a/tests/Dao/PostDaoTest.php +++ b/tests/Dao/PostDaoTest.php @@ -60,6 +60,28 @@ final class PostDaoTest extends \Szurubooru\Tests\AbstractDatabaseTestCase ]; $this->assertEntitiesEqual($expected, $actual); + $this->assertEquals(count($expected), $postDao->getCount()); + } + + public function testGettingTotalFileSize() + { + $postDao = $this->getPostDao(); + + $post1 = $this->getPost(); + $post2 = $this->getPost(); + $post3 = $this->getPost(); + $post1->setOriginalFileSize(1249812); + $post2->setOriginalFileSize(128); + $post3->setOriginalFileSize(null); + $postDao->save($post1); + $postDao->save($post2); + $postDao->save($post3); + $expectedFileSize = + $post1->getOriginalFileSize() + + $post2->getOriginalFileSize() + + $post3->getOriginalFileSize(); + $this->assertGreaterThan(0, $expectedFileSize); + $this->assertEquals($expectedFileSize, $postDao->getTotalFileSize()); } public function testGettingById()