Added post globals

This commit is contained in:
Marcin Kurczewski 2014-09-25 11:45:46 +02:00
parent 2bb2d1f743
commit d3015627b3
11 changed files with 105 additions and 4 deletions

2
TODO
View file

@ -28,8 +28,6 @@ everything related to posts:
- link to iqdb and/or tineye - link to iqdb and/or tineye
- auto feature - auto feature
- number of posts
- total size of posts
- random post - random post
- regard safety settings - regard safety settings

View file

@ -3,6 +3,10 @@
} }
#home h1 { #home h1 {
margin-top: 0; margin-top: 0;
margin-bottom: 0;
}
#home h1+p {
margin-top: 0;
} }
#home .post { #home .post {

View file

@ -13,6 +13,7 @@ App.Presenters.HomePresenter = function(
var $el = jQuery('#content'); var $el = jQuery('#content');
var homeTemplate; var homeTemplate;
var postContentTemplate; var postContentTemplate;
var globals;
var post; var post;
function init(args, loaded) { function init(args, loaded) {
@ -22,15 +23,18 @@ App.Presenters.HomePresenter = function(
promise.waitAll( promise.waitAll(
util.promiseTemplate('home'), util.promiseTemplate('home'),
util.promiseTemplate('post-content'), util.promiseTemplate('post-content'),
api.get('/globals'),
api.get('/posts/featured')) api.get('/posts/featured'))
.then(function( .then(function(
homeTemplateHtml, homeTemplateHtml,
postContentTemplateHtml, postContentTemplateHtml,
response) { globalsResponse,
featuredPostResponse) {
homeTemplate = _.template(homeTemplateHtml); homeTemplate = _.template(homeTemplateHtml);
postContentTemplate = _.template(postContentTemplateHtml); postContentTemplate = _.template(postContentTemplateHtml);
post = response.json; globals = globalsResponse.json;
post = featuredPostResponse.json;
render(); render();
loaded(); loaded();
@ -43,8 +47,10 @@ App.Presenters.HomePresenter = function(
$el.html(homeTemplate({ $el.html(homeTemplate({
post: post, post: post,
postContentTemplate: postContentTemplate, postContentTemplate: postContentTemplate,
globals: globals,
title: topNavigationPresenter.getBaseTitle(), title: topNavigationPresenter.getBaseTitle(),
formatRelativeTime: util.formatRelativeTime, formatRelativeTime: util.formatRelativeTime,
formatFileSize: util.formatFileSize,
})); }));
} }

View file

@ -1,5 +1,8 @@
<div id="home"> <div id="home">
<h1><%= title %></h1> <h1><%= title %></h1>
<p>
<small>Serving <%= globals.postCount %> posts (<%= formatFileSize(globals.postSize) %>)</small>
</p>
<% if (post) { %> <% if (post) { %>
<div class="post"> <div class="post">

8
scripts/cron-globals.php Normal file
View file

@ -0,0 +1,8 @@
<?php
require_once(__DIR__
. DIRECTORY_SEPARATOR . '..'
. DIRECTORY_SEPARATOR . 'src'
. DIRECTORY_SEPARATOR . 'AutoLoader.php');
$postService = Szurubooru\Injector::get(\Szurubooru\Services\PostService::class);
$postService->updatePostGlobals();

View file

@ -0,0 +1,29 @@
<?php
namespace Szurubooru\Controllers;
final class GlobalParamController extends AbstractController
{
private $globalParamDao;
public function __construct(
\Szurubooru\Dao\GlobalParamDao $globalParamDao)
{
$this->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;
}
}

View file

@ -26,6 +26,17 @@ class PostDao extends AbstractDao implements ICrudDao
$this->thumbnailService = $thumbnailService; $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) public function findByName($name)
{ {
return $this->findOneBy('name', $name); return $this->findOneBy('name', $name);

View file

@ -4,6 +4,8 @@ namespace Szurubooru\Entities;
class GlobalParam extends Entity class GlobalParam extends Entity
{ {
const KEY_FEATURED_POST = 'featuredPost'; const KEY_FEATURED_POST = 'featuredPost';
const KEY_POST_SIZE = 'postSize';
const KEY_POST_COUNT = 'postCount';
private $key; private $key;
private $value; private $value;

View file

@ -229,6 +229,23 @@ class PostService
$this->transactionManager->commit($transactionFunc); $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) private function assertNoPostWithThisContentChecksum(\Szurubooru\Entities\Post $parent)
{ {
$checksumToCheck = $parent->getContentChecksum(); $checksumToCheck = $parent->getContentChecksum();

View file

@ -33,6 +33,7 @@ return [
$container->get(\Szurubooru\Controllers\UserAvatarController::class), $container->get(\Szurubooru\Controllers\UserAvatarController::class),
$container->get(\Szurubooru\Controllers\PostController::class), $container->get(\Szurubooru\Controllers\PostController::class),
$container->get(\Szurubooru\Controllers\PostContentController::class), $container->get(\Szurubooru\Controllers\PostContentController::class),
$container->get(\Szurubooru\Controllers\GlobalParamController::class),
]; ];
}), }),
]; ];

View file

@ -60,6 +60,28 @@ final class PostDaoTest extends \Szurubooru\Tests\AbstractDatabaseTestCase
]; ];
$this->assertEntitiesEqual($expected, $actual); $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() public function testGettingById()