2014-09-26 20:41:28 +02:00
|
|
|
<?php
|
|
|
|
namespace Szurubooru\Services;
|
2014-10-08 14:47:47 +02:00
|
|
|
use Szurubooru\Dao\GlobalParamDao;
|
|
|
|
use Szurubooru\Dao\SnapshotDao;
|
|
|
|
use Szurubooru\Dao\TransactionManager;
|
|
|
|
use Szurubooru\Entities\GlobalParam;
|
|
|
|
use Szurubooru\Entities\Post;
|
|
|
|
use Szurubooru\Entities\Snapshot;
|
|
|
|
use Szurubooru\Helpers\EnumHelper;
|
|
|
|
use Szurubooru\SearchServices\Filters\SnapshotFilter;
|
|
|
|
use Szurubooru\SearchServices\Requirements\Requirement;
|
|
|
|
use Szurubooru\SearchServices\Requirements\RequirementSingleValue;
|
|
|
|
use Szurubooru\Services\AuthService;
|
|
|
|
use Szurubooru\Services\TimeService;
|
|
|
|
use Szurubooru\Validator;
|
2014-09-26 20:41:28 +02:00
|
|
|
|
|
|
|
class HistoryService
|
|
|
|
{
|
|
|
|
private $validator;
|
|
|
|
private $snapshotDao;
|
|
|
|
private $globalParamDao;
|
|
|
|
private $timeService;
|
|
|
|
private $authService;
|
|
|
|
private $transactionManager;
|
|
|
|
|
|
|
|
public function __construct(
|
2014-10-08 14:47:47 +02:00
|
|
|
Validator $validator,
|
|
|
|
SnapshotDao $snapshotDao,
|
|
|
|
GlobalParamDao $globalParamDao,
|
|
|
|
TransactionManager $transactionManager,
|
|
|
|
TimeService $timeService,
|
|
|
|
AuthService $authService)
|
2014-09-26 20:41:28 +02:00
|
|
|
{
|
|
|
|
$this->validator = $validator;
|
|
|
|
$this->snapshotDao = $snapshotDao;
|
|
|
|
$this->globalParamDao = $globalParamDao;
|
|
|
|
$this->timeService = $timeService;
|
|
|
|
$this->authService = $authService;
|
|
|
|
$this->transactionManager = $transactionManager;
|
|
|
|
}
|
|
|
|
|
2014-10-08 14:47:47 +02:00
|
|
|
public function getFiltered(SnapshotFilter $filter)
|
2014-09-26 20:41:28 +02:00
|
|
|
{
|
|
|
|
$transactionFunc = function() use ($filter)
|
|
|
|
{
|
|
|
|
return $this->snapshotDao->findFiltered($filter);
|
|
|
|
};
|
|
|
|
return $this->transactionManager->rollback($transactionFunc);
|
|
|
|
}
|
|
|
|
|
2014-10-08 14:47:47 +02:00
|
|
|
public function getPostHistory(Post $post)
|
2014-09-28 16:56:15 +02:00
|
|
|
{
|
2014-10-08 14:47:47 +02:00
|
|
|
$filter = new SnapshotFilter();
|
2014-09-28 16:56:15 +02:00
|
|
|
|
2014-10-08 14:47:47 +02:00
|
|
|
$requirement = new Requirement();
|
|
|
|
$requirement->setType(SnapshotFilter::REQUIREMENT_PRIMARY_KEY);
|
|
|
|
$requirement->setValue(new RequirementSingleValue($post->getId()));
|
2014-09-28 16:56:15 +02:00
|
|
|
$filter->addRequirement($requirement);
|
|
|
|
|
2014-10-08 14:47:47 +02:00
|
|
|
$requirement = new Requirement();
|
|
|
|
$requirement->setType(SnapshotFilter::REQUIREMENT_TYPE);
|
|
|
|
$requirement->setValue(new RequirementSingleValue(Snapshot::TYPE_POST));
|
2014-09-28 16:56:15 +02:00
|
|
|
$filter->addRequirement($requirement);
|
|
|
|
|
|
|
|
return $this->getFiltered($filter)->getEntities();
|
|
|
|
}
|
|
|
|
|
2014-10-08 14:47:47 +02:00
|
|
|
public function saveSnapshot(Snapshot $snapshot)
|
2014-09-26 20:41:28 +02:00
|
|
|
{
|
|
|
|
$transactionFunc = function() use ($snapshot)
|
|
|
|
{
|
|
|
|
$otherSnapshots = $this->snapshotDao->findByTypeAndKey($snapshot->getType(), $snapshot->getPrimaryKey());
|
|
|
|
if ($otherSnapshots)
|
|
|
|
{
|
|
|
|
$lastSnapshot = array_shift($otherSnapshots);
|
2014-10-12 12:44:21 +02:00
|
|
|
if (md5(json_encode($lastSnapshot->getData())) === md5(json_encode($snapshot->getData())))
|
2014-09-26 20:41:28 +02:00
|
|
|
return $lastSnapshot;
|
|
|
|
|
|
|
|
$dataDifference = $this->getSnapshotDataDifference($snapshot->getData(), $lastSnapshot->getData());
|
|
|
|
$snapshot->setDataDifference($dataDifference);
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
$dataDifference = $this->getSnapshotDataDifference($snapshot->getData(), []);
|
|
|
|
$snapshot->setDataDifference($dataDifference);
|
|
|
|
}
|
|
|
|
|
|
|
|
$snapshot->setTime($this->timeService->getCurrentTime());
|
|
|
|
$snapshot->setUser($this->authService->getLoggedInUser());
|
|
|
|
return $this->snapshotDao->save($snapshot);
|
|
|
|
};
|
|
|
|
return $this->transactionManager->commit($transactionFunc);
|
|
|
|
}
|
|
|
|
|
2014-10-08 14:47:47 +02:00
|
|
|
public function getPostDeleteSnapshot(Post $post)
|
2014-09-26 20:41:28 +02:00
|
|
|
{
|
|
|
|
$snapshot = $this->getPostSnapshot($post);
|
2014-10-04 18:42:46 +02:00
|
|
|
$snapshot->setData([]);
|
2014-10-08 14:47:47 +02:00
|
|
|
$snapshot->setOperation(Snapshot::OPERATION_DELETE);
|
2014-09-26 20:41:28 +02:00
|
|
|
return $snapshot;
|
|
|
|
}
|
|
|
|
|
2014-10-08 14:47:47 +02:00
|
|
|
public function getPostChangeSnapshot(Post $post)
|
2014-09-26 20:41:28 +02:00
|
|
|
{
|
2014-10-12 12:44:21 +02:00
|
|
|
static $featuredPostParam = null;
|
|
|
|
if ($featuredPostParam === null)
|
|
|
|
$featuredPostParam = $this->globalParamDao->findByKey(GlobalParam::KEY_FEATURED_POST);
|
2014-09-26 20:41:28 +02:00
|
|
|
$isFeatured = ($featuredPostParam and intval($featuredPostParam->getValue()) === $post->getId());
|
|
|
|
|
2014-10-09 09:45:06 +02:00
|
|
|
$flags = [];
|
|
|
|
if ($post->getFlags() & Post::FLAG_LOOP)
|
2014-10-12 12:44:21 +02:00
|
|
|
$flags[] = 'loop';
|
2014-10-09 09:45:06 +02:00
|
|
|
|
2014-09-26 20:41:28 +02:00
|
|
|
$data =
|
|
|
|
[
|
|
|
|
'source' => $post->getSource(),
|
2014-10-08 14:47:47 +02:00
|
|
|
'safety' => EnumHelper::postSafetyToString($post->getSafety()),
|
2014-09-26 20:41:28 +02:00
|
|
|
'contentChecksum' => $post->getContentChecksum(),
|
|
|
|
'featured' => $isFeatured,
|
|
|
|
|
|
|
|
'tags' =>
|
|
|
|
array_map(
|
|
|
|
function ($tag)
|
|
|
|
{
|
|
|
|
return $tag->getName();
|
|
|
|
},
|
|
|
|
$post->getTags()),
|
|
|
|
|
|
|
|
'relations' =>
|
|
|
|
array_map(
|
|
|
|
function ($post)
|
|
|
|
{
|
|
|
|
return $post->getId();
|
|
|
|
},
|
|
|
|
$post->getRelatedPosts()),
|
|
|
|
|
2014-10-09 09:45:06 +02:00
|
|
|
'flags' => $flags,
|
2014-09-26 20:41:28 +02:00
|
|
|
];
|
|
|
|
|
|
|
|
$snapshot = $this->getPostSnapshot($post);
|
2014-10-08 14:47:47 +02:00
|
|
|
$snapshot->setOperation(Snapshot::OPERATION_CHANGE);
|
2014-09-26 20:41:28 +02:00
|
|
|
$snapshot->setData($data);
|
|
|
|
return $snapshot;
|
|
|
|
}
|
|
|
|
|
|
|
|
public function getSnapshotDataDifference($newData, $oldData)
|
|
|
|
{
|
|
|
|
$diffFunction = function($base, $other)
|
|
|
|
{
|
|
|
|
$result = [];
|
|
|
|
foreach ($base as $key => $value)
|
|
|
|
{
|
|
|
|
if (is_array($base[$key]))
|
|
|
|
{
|
|
|
|
foreach ($base[$key] as $subValue)
|
|
|
|
{
|
|
|
|
if (!isset($other[$key]) or !in_array($subValue, $other[$key]))
|
|
|
|
$result[] = [$key, $subValue];
|
|
|
|
}
|
|
|
|
}
|
|
|
|
elseif (!isset($other[$key]) or $base[$key] !== $other[$key])
|
|
|
|
{
|
|
|
|
$result[] = [$key, $value];
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return $result;
|
|
|
|
|
|
|
|
};
|
|
|
|
|
|
|
|
return [
|
|
|
|
'+' => $diffFunction($newData, $oldData),
|
|
|
|
'-' => $diffFunction($oldData, $newData),
|
|
|
|
];
|
|
|
|
}
|
|
|
|
|
2014-10-08 14:47:47 +02:00
|
|
|
private function getPostSnapshot(Post $post)
|
2014-09-26 20:41:28 +02:00
|
|
|
{
|
2014-10-08 14:47:47 +02:00
|
|
|
$snapshot = new Snapshot();
|
|
|
|
$snapshot->setType(Snapshot::TYPE_POST);
|
2014-09-26 20:41:28 +02:00
|
|
|
$snapshot->setPrimaryKey($post->getId());
|
|
|
|
return $snapshot;
|
|
|
|
}
|
|
|
|
}
|