2014-08-28 10:45:55 +02:00
|
|
|
<?php
|
|
|
|
namespace Szurubooru\Dao;
|
|
|
|
|
2014-09-15 11:38:24 +02:00
|
|
|
class PostDao extends AbstractDao implements ICrudDao
|
2014-08-28 10:45:55 +02:00
|
|
|
{
|
2014-09-21 09:35:43 +02:00
|
|
|
private $tagDao;
|
2014-09-21 18:21:54 +02:00
|
|
|
private $userDao;
|
2014-09-20 12:45:56 +02:00
|
|
|
private $fileService;
|
|
|
|
private $thumbnailService;
|
|
|
|
|
|
|
|
public function __construct(
|
|
|
|
\Szurubooru\DatabaseConnection $databaseConnection,
|
2014-09-21 09:35:43 +02:00
|
|
|
\Szurubooru\Dao\TagDao $tagDao,
|
2014-09-21 18:21:54 +02:00
|
|
|
\Szurubooru\Dao\UserDao $userDao,
|
2014-09-20 12:45:56 +02:00
|
|
|
\Szurubooru\Services\FileService $fileService,
|
|
|
|
\Szurubooru\Services\ThumbnailService $thumbnailService)
|
2014-08-28 10:45:55 +02:00
|
|
|
{
|
2014-09-15 09:25:11 +02:00
|
|
|
parent::__construct(
|
|
|
|
$databaseConnection,
|
|
|
|
'posts',
|
|
|
|
new \Szurubooru\Dao\EntityConverters\PostEntityConverter());
|
2014-09-20 12:45:56 +02:00
|
|
|
|
2014-09-21 09:35:43 +02:00
|
|
|
$this->tagDao = $tagDao;
|
2014-09-21 18:21:54 +02:00
|
|
|
$this->userDao = $userDao;
|
2014-09-20 12:45:56 +02:00
|
|
|
$this->fileService = $fileService;
|
|
|
|
$this->thumbnailService = $thumbnailService;
|
2014-08-28 10:45:55 +02:00
|
|
|
}
|
2014-09-15 11:38:24 +02:00
|
|
|
|
2014-09-25 11:45:46 +02:00
|
|
|
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']);
|
|
|
|
}
|
|
|
|
|
2014-09-15 11:38:24 +02:00
|
|
|
public function findByName($name)
|
|
|
|
{
|
|
|
|
return $this->findOneBy('name', $name);
|
|
|
|
}
|
|
|
|
|
|
|
|
public function findByContentChecksum($checksum)
|
|
|
|
{
|
|
|
|
return $this->findOneBy('contentChecksum', $checksum);
|
|
|
|
}
|
|
|
|
|
2014-09-20 12:45:56 +02:00
|
|
|
protected function afterLoad(\Szurubooru\Entities\Entity $post)
|
2014-09-15 11:38:24 +02:00
|
|
|
{
|
2014-09-20 12:45:56 +02:00
|
|
|
$post->setLazyLoader(
|
|
|
|
\Szurubooru\Entities\Post::LAZY_LOADER_CONTENT,
|
2014-09-27 21:33:31 +02:00
|
|
|
function (\Szurubooru\Entities\Post $post)
|
2014-09-20 12:45:56 +02:00
|
|
|
{
|
|
|
|
return $this->fileService->load($post->getContentPath());
|
|
|
|
});
|
|
|
|
|
|
|
|
$post->setLazyLoader(
|
|
|
|
\Szurubooru\Entities\Post::LAZY_LOADER_THUMBNAIL_SOURCE_CONTENT,
|
2014-09-27 21:33:31 +02:00
|
|
|
function (\Szurubooru\Entities\Post $post)
|
2014-09-20 12:45:56 +02:00
|
|
|
{
|
|
|
|
return $this->fileService->load($post->getThumbnailSourceContentPath());
|
|
|
|
});
|
|
|
|
|
2014-09-21 18:21:54 +02:00
|
|
|
$post->setLazyLoader(
|
|
|
|
\Szurubooru\Entities\Post::LAZY_LOADER_USER,
|
2014-09-27 21:33:31 +02:00
|
|
|
function (\Szurubooru\Entities\Post $post)
|
2014-09-21 18:21:54 +02:00
|
|
|
{
|
|
|
|
return $this->getUser($post);
|
|
|
|
});
|
|
|
|
|
2014-09-20 12:45:56 +02:00
|
|
|
$post->setLazyLoader(
|
|
|
|
\Szurubooru\Entities\Post::LAZY_LOADER_TAGS,
|
2014-09-27 21:33:31 +02:00
|
|
|
function (\Szurubooru\Entities\Post $post)
|
2014-09-15 11:38:24 +02:00
|
|
|
{
|
|
|
|
return $this->getTags($post);
|
|
|
|
});
|
2014-09-25 23:53:47 +02:00
|
|
|
|
|
|
|
$post->setLazyLoader(
|
|
|
|
\Szurubooru\Entities\Post::LAZY_LOADER_RELATED_POSTS,
|
2014-09-27 21:33:31 +02:00
|
|
|
function (\Szurubooru\Entities\Post $post)
|
2014-09-25 23:53:47 +02:00
|
|
|
{
|
|
|
|
return $this->getRelatedPosts($post);
|
|
|
|
});
|
2014-09-15 11:38:24 +02:00
|
|
|
}
|
|
|
|
|
2014-09-20 12:45:56 +02:00
|
|
|
protected function afterSave(\Szurubooru\Entities\Entity $post)
|
2014-09-15 11:38:24 +02:00
|
|
|
{
|
2014-09-20 12:45:56 +02:00
|
|
|
$this->syncContent($post);
|
|
|
|
$this->syncThumbnailSourceContent($post);
|
2014-09-21 09:35:43 +02:00
|
|
|
$this->syncTags($post);
|
2014-09-25 23:53:47 +02:00
|
|
|
$this->syncPostRelations($post);
|
2014-09-15 11:38:24 +02:00
|
|
|
}
|
|
|
|
|
2014-09-27 10:59:37 +02:00
|
|
|
protected function decorateQueryFromRequirement($query, \Szurubooru\SearchServices\Requirements\Requirement $requirement)
|
|
|
|
{
|
|
|
|
if ($requirement->getType() === \Szurubooru\SearchServices\Filters\PostFilter::REQUIREMENT_TAG)
|
|
|
|
{
|
|
|
|
$sql = 'EXISTS (
|
|
|
|
SELECT 1 FROM postTags
|
|
|
|
INNER JOIN tags ON postTags.tagId = tags.id
|
|
|
|
WHERE postTags.postId = posts.id
|
|
|
|
AND LOWER(tags.name) = LOWER(?))';
|
|
|
|
|
|
|
|
if ($requirement->isNegated())
|
|
|
|
$sql = 'NOT ' . $sql;
|
|
|
|
|
|
|
|
$query->where($sql, $requirement->getValue()->getValue());
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2014-09-30 15:39:51 +02:00
|
|
|
elseif ($requirement->getType() === \Szurubooru\SearchServices\Filters\PostFilter::REQUIREMENT_UPLOADER)
|
|
|
|
{
|
|
|
|
$query->innerJoin('users uploader ON uploader.id = userId');
|
|
|
|
}
|
|
|
|
|
2014-09-27 10:59:37 +02:00
|
|
|
parent::decorateQueryFromRequirement($query, $requirement);
|
|
|
|
}
|
|
|
|
|
2014-09-15 11:38:24 +02:00
|
|
|
private function getTags(\Szurubooru\Entities\Post $post)
|
|
|
|
{
|
2014-09-21 09:35:43 +02:00
|
|
|
return $this->tagDao->findByPostId($post->getId());
|
2014-09-15 11:38:24 +02:00
|
|
|
}
|
|
|
|
|
2014-09-21 18:21:54 +02:00
|
|
|
private function getUser(\Szurubooru\Entities\Post $post)
|
|
|
|
{
|
|
|
|
return $this->userDao->findById($post->getUserId());
|
|
|
|
}
|
|
|
|
|
2014-09-25 23:53:47 +02:00
|
|
|
private function getRelatedPosts(\Szurubooru\Entities\Post $post)
|
|
|
|
{
|
|
|
|
$query = $this->fpdo
|
|
|
|
->from('postRelations')
|
|
|
|
->where('post1id = :post1id OR post2id = :post2id', [
|
|
|
|
':post1id' => $post->getId(),
|
|
|
|
':post2id' => $post->getId()]);
|
|
|
|
|
|
|
|
$relatedPostIds = [];
|
|
|
|
foreach ($query as $arrayEntity)
|
|
|
|
{
|
|
|
|
$post1id = intval($arrayEntity['post1id']);
|
|
|
|
$post2id = intval($arrayEntity['post2id']);
|
|
|
|
if ($post1id !== $post->getId())
|
|
|
|
$relatedPostIds[] = $post1id;
|
|
|
|
if ($post2id !== $post->getId())
|
|
|
|
$relatedPostIds[] = $post2id;
|
|
|
|
}
|
|
|
|
|
|
|
|
return $this->findByIds($relatedPostIds);
|
|
|
|
}
|
|
|
|
|
2014-09-20 12:45:56 +02:00
|
|
|
private function syncContent(\Szurubooru\Entities\Post $post)
|
|
|
|
{
|
|
|
|
$targetPath = $post->getContentPath();
|
|
|
|
$content = $post->getContent();
|
|
|
|
if ($content)
|
|
|
|
$this->fileService->save($targetPath, $content);
|
|
|
|
else
|
|
|
|
$this->fileService->delete($targetPath, $content);
|
|
|
|
$this->thumbnailService->deleteUsedThumbnails($targetPath);
|
|
|
|
}
|
|
|
|
|
|
|
|
private function syncThumbnailSourceContent(\Szurubooru\Entities\Post $post)
|
|
|
|
{
|
|
|
|
$targetPath = $post->getThumbnailSourceContentPath();
|
|
|
|
$content = $post->getThumbnailSourceContent();
|
|
|
|
if ($content)
|
|
|
|
$this->fileService->save($targetPath, $content);
|
|
|
|
else
|
|
|
|
$this->fileService->delete($targetPath);
|
|
|
|
$this->thumbnailService->deleteUsedThumbnails($targetPath);
|
|
|
|
}
|
|
|
|
|
2014-09-21 09:35:43 +02:00
|
|
|
private function syncTags(\Szurubooru\Entities\Post $post)
|
2014-09-15 11:38:24 +02:00
|
|
|
{
|
2014-09-21 09:35:43 +02:00
|
|
|
$tagNames = array_filter(array_unique(array_map(
|
|
|
|
function ($tag)
|
2014-09-18 19:30:36 +02:00
|
|
|
{
|
2014-09-21 09:35:43 +02:00
|
|
|
return $tag->getName();
|
2014-09-18 19:30:36 +02:00
|
|
|
},
|
2014-09-21 09:35:43 +02:00
|
|
|
$post->getTags())));
|
2014-09-15 11:38:24 +02:00
|
|
|
|
2014-09-21 09:35:43 +02:00
|
|
|
$this->tagDao->createMissingTags($tagNames);
|
|
|
|
|
|
|
|
$tagIds = array_map(
|
2014-09-27 21:33:31 +02:00
|
|
|
function ($tag)
|
2014-09-21 09:35:43 +02:00
|
|
|
{
|
|
|
|
return $tag->getId();
|
|
|
|
},
|
|
|
|
$this->tagDao->findByNames($tagNames));
|
2014-09-15 11:38:24 +02:00
|
|
|
|
2014-09-21 09:35:43 +02:00
|
|
|
$existingTagRelationIds = array_map(
|
2014-09-27 21:33:31 +02:00
|
|
|
function ($arrayEntity)
|
2014-09-15 11:38:24 +02:00
|
|
|
{
|
2014-09-21 09:35:43 +02:00
|
|
|
return $arrayEntity['tagId'];
|
2014-09-15 11:38:24 +02:00
|
|
|
},
|
2014-09-21 09:35:43 +02:00
|
|
|
iterator_to_array($this->fpdo->from('postTags')->where('postId', $post->getId())));
|
2014-09-15 11:38:24 +02:00
|
|
|
|
2014-09-21 09:35:43 +02:00
|
|
|
$tagRelationsToInsert = array_diff($tagIds, $existingTagRelationIds);
|
|
|
|
$tagRelationsToDelete = array_diff($existingTagRelationIds, $tagIds);
|
2014-09-15 11:38:24 +02:00
|
|
|
|
2014-09-21 09:35:43 +02:00
|
|
|
foreach ($tagRelationsToInsert as $tagId)
|
|
|
|
{
|
|
|
|
$this->fpdo->insertInto('postTags')->values(['postId' => $post->getId(), 'tagId' => $tagId])->execute();
|
|
|
|
}
|
|
|
|
foreach ($tagRelationsToDelete as $tagId)
|
2014-09-15 11:38:24 +02:00
|
|
|
{
|
2014-09-25 19:11:41 +02:00
|
|
|
$this->fpdo->deleteFrom('postTags')->where('postId', $post->getId())->where('tagId', $tagId)->execute();
|
2014-09-15 11:38:24 +02:00
|
|
|
}
|
2014-09-29 19:16:33 +02:00
|
|
|
|
|
|
|
$this->tagDao->exportJson();
|
2014-09-15 11:38:24 +02:00
|
|
|
}
|
2014-09-25 23:53:47 +02:00
|
|
|
|
|
|
|
private function syncPostRelations(\Szurubooru\Entities\Post $post)
|
|
|
|
{
|
|
|
|
$this->fpdo->deleteFrom('postRelations')->where('post1id', $post->getId())->execute();
|
|
|
|
$this->fpdo->deleteFrom('postRelations')->where('post2id', $post->getId())->execute();
|
|
|
|
|
|
|
|
$relatedPostIds = array_filter(array_unique(array_map(
|
|
|
|
function ($post)
|
|
|
|
{
|
|
|
|
if (!$post->getId())
|
|
|
|
throw new \RuntimeException('Unsaved entities found');
|
|
|
|
return $post->getId();
|
|
|
|
},
|
|
|
|
$post->getRelatedPosts())));
|
|
|
|
|
|
|
|
foreach ($relatedPostIds as $postId)
|
|
|
|
{
|
|
|
|
$this->fpdo
|
|
|
|
->insertInto('postRelations')
|
|
|
|
->values([
|
|
|
|
'post1id' => $post->getId(),
|
|
|
|
'post2id' => $postId])
|
|
|
|
->execute();
|
|
|
|
}
|
|
|
|
}
|
2014-08-28 10:45:55 +02:00
|
|
|
}
|