From aac1bbb34e73cb91fe9cf712bb8c2915bb592caf Mon Sep 17 00:00:00 2001 From: aina-esokia Date: Fri, 23 Feb 2018 11:22:44 +0400 Subject: [PATCH 1/6] add record_ids to api/v1/quarantine --- lib/Alchemy/Phrasea/Border/Manager.php | 5 ++ .../Phrasea/Controller/Api/V1Controller.php | 1 + .../Phrasea/Model/Entities/LazaretFile.php | 52 +++++++++++++++++++ .../Version20180222113704.php | 28 ++++++++++ 4 files changed, 86 insertions(+) create mode 100644 lib/Alchemy/Phrasea/Setup/DoctrineMigrations/Version20180222113704.php diff --git a/lib/Alchemy/Phrasea/Border/Manager.php b/lib/Alchemy/Phrasea/Border/Manager.php index d8abc9ac99..0b16e7f254 100644 --- a/lib/Alchemy/Phrasea/Border/Manager.php +++ b/lib/Alchemy/Phrasea/Border/Manager.php @@ -401,6 +401,11 @@ class Manager } } + $recordIds = $lazaretFile->getRecordIdsByCheckers($this->app); + + /** $recordIds will be serialized */ + $lazaretFile->setRecordIds($recordIds); + $this->app['orm.em']->flush(); return $lazaretFile; diff --git a/lib/Alchemy/Phrasea/Controller/Api/V1Controller.php b/lib/Alchemy/Phrasea/Controller/Api/V1Controller.php index c9e6eeb935..efb5a8e875 100644 --- a/lib/Alchemy/Phrasea/Controller/Api/V1Controller.php +++ b/lib/Alchemy/Phrasea/Controller/Api/V1Controller.php @@ -707,6 +707,7 @@ class V1Controller extends Controller 'id' => $file->getId(), 'quarantine_session' => $session, 'base_id' => $file->getBaseId(), + 'record_id' => $file->getRecordIds()?:[], 'original_name' => $file->getOriginalName(), 'sha256' => $file->getSha256(), 'uuid' => $file->getUuid(), diff --git a/lib/Alchemy/Phrasea/Model/Entities/LazaretFile.php b/lib/Alchemy/Phrasea/Model/Entities/LazaretFile.php index 9c8f2952af..ab3ea98f39 100644 --- a/lib/Alchemy/Phrasea/Model/Entities/LazaretFile.php +++ b/lib/Alchemy/Phrasea/Model/Entities/LazaretFile.php @@ -49,6 +49,11 @@ class LazaretFile */ private $base_id; + /** + * @ORM\Column(type="string", length=512) + */ + private $record_ids; + /** * @ORM\Column(type="string", length=36) */ @@ -205,6 +210,29 @@ class LazaretFile return $this->base_id; } + /** + * Set record_ids + * + * @param mixed $recordIds + * @return LazaretFile + */ + public function setRecordIds($recordIds) + { + $this->record_ids = serialize($recordIds); + + return $this; + } + + /** + * Get record_ids + * + * @return mixed + */ + public function getRecordIds() + { + return unserialize($this->record_ids); + } + /** * Get the Destination Collection * @@ -450,4 +478,28 @@ class LazaretFile return $merged; } + /** + * Get an array of record_id + * + * @param Application $app + * @return array + */ + public function getRecordIdsByCheckers(Application $app) + { + $recordIds = []; + /** @var LazaretCheck $check */ + foreach($this->getChecks() as $check) { + /** @var record_adapter $record */ + $conflicts = $check->listConflicts($app); + + foreach ($conflicts as $record) { + if(!in_array($record, $recordIds)){ + $recordIds[] = $record->getRecordId(); + } + } + } + + return $recordIds; + } + } diff --git a/lib/Alchemy/Phrasea/Setup/DoctrineMigrations/Version20180222113704.php b/lib/Alchemy/Phrasea/Setup/DoctrineMigrations/Version20180222113704.php new file mode 100644 index 0000000000..39423d2944 --- /dev/null +++ b/lib/Alchemy/Phrasea/Setup/DoctrineMigrations/Version20180222113704.php @@ -0,0 +1,28 @@ +addSql('ALTER TABLE LazaretFiles ADD record_ids VARCHAR(512) NOT NULL AFTER base_id'); + } + + /** + * @param Schema $schema + */ + public function down(Schema $schema) + { + $this->addSql('ALTER TABLE LazaretFiles DROP record_ids'); + } +} From bea0f98d1906820a469f5dbcfa67941ae95b88cf Mon Sep 17 00:00:00 2001 From: aina-esokia Date: Fri, 23 Feb 2018 11:44:23 +0400 Subject: [PATCH 2/6] Update LazaretFile --- lib/Alchemy/Phrasea/Model/Entities/LazaretFile.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/lib/Alchemy/Phrasea/Model/Entities/LazaretFile.php b/lib/Alchemy/Phrasea/Model/Entities/LazaretFile.php index ab3ea98f39..afa818c327 100644 --- a/lib/Alchemy/Phrasea/Model/Entities/LazaretFile.php +++ b/lib/Alchemy/Phrasea/Model/Entities/LazaretFile.php @@ -493,7 +493,7 @@ class LazaretFile $conflicts = $check->listConflicts($app); foreach ($conflicts as $record) { - if(!in_array($record, $recordIds)){ + if(!in_array($record->getRecordId(), $recordIds)){ $recordIds[] = $record->getRecordId(); } } From d6ac9d6c6e6891122028e7183f39c89e57c3e519 Mon Sep 17 00:00:00 2001 From: aina-esokia Date: Fri, 23 Feb 2018 12:44:57 +0400 Subject: [PATCH 3/6] update migration --- .../Setup/DoctrineMigrations/Version20180222113704.php | 5 ++--- 1 file changed, 2 insertions(+), 3 deletions(-) diff --git a/lib/Alchemy/Phrasea/Setup/DoctrineMigrations/Version20180222113704.php b/lib/Alchemy/Phrasea/Setup/DoctrineMigrations/Version20180222113704.php index 39423d2944..a54af53e2c 100644 --- a/lib/Alchemy/Phrasea/Setup/DoctrineMigrations/Version20180222113704.php +++ b/lib/Alchemy/Phrasea/Setup/DoctrineMigrations/Version20180222113704.php @@ -2,7 +2,6 @@ namespace Alchemy\Phrasea\Setup\DoctrineMigrations; -use Doctrine\DBAL\Migrations\AbstractMigration; use Doctrine\DBAL\Schema\Schema; /** @@ -13,7 +12,7 @@ class Version20180222113704 extends AbstractMigration /** * @param Schema $schema */ - public function up(Schema $schema) + public function doUpSql(Schema $schema) { $this->addSql('ALTER TABLE LazaretFiles ADD record_ids VARCHAR(512) NOT NULL AFTER base_id'); } @@ -21,7 +20,7 @@ class Version20180222113704 extends AbstractMigration /** * @param Schema $schema */ - public function down(Schema $schema) + public function doDownSql(Schema $schema) { $this->addSql('ALTER TABLE LazaretFiles DROP record_ids'); } From 9ddd86932ddc2cd993e1d50c6c0117b3d2417b88 Mon Sep 17 00:00:00 2001 From: aina-esokia Date: Fri, 23 Feb 2018 14:20:09 +0400 Subject: [PATCH 4/6] update LazaretTest --- tests/Alchemy/Tests/Phrasea/Controller/Prod/LazaretTest.php | 1 + 1 file changed, 1 insertion(+) diff --git a/tests/Alchemy/Tests/Phrasea/Controller/Prod/LazaretTest.php b/tests/Alchemy/Tests/Phrasea/Controller/Prod/LazaretTest.php index 82c916e197..abde12c72e 100644 --- a/tests/Alchemy/Tests/Phrasea/Controller/Prod/LazaretTest.php +++ b/tests/Alchemy/Tests/Phrasea/Controller/Prod/LazaretTest.php @@ -507,6 +507,7 @@ class LazaretTest extends \PhraseanetAuthenticatedWebTestCase $lazaretFile->setUuid('7b8ef0e3-dc8f-4b66-9e2f-bd049d175124'); $lazaretFile->setCreated(new \DateTime('-1 day')); $lazaretFile->setUpdated(new \DateTime('now')); + $lazaretFile->setRecordIds([1]); return $lazaretFile; } From 27fd458f30a6ec7301c788ecf965cde12a338a1f Mon Sep 17 00:00:00 2001 From: aina-esokia Date: Wed, 28 Feb 2018 10:59:45 +0400 Subject: [PATCH 5/6] fetched conflicting recordids each time on called --- lib/Alchemy/Phrasea/Border/Manager.php | 5 ---- .../Phrasea/Controller/Api/V1Controller.php | 2 +- .../Phrasea/Model/Entities/LazaretFile.php | 28 ------------------- .../Version20180222113704.php | 27 ------------------ .../Phrasea/Controller/Prod/LazaretTest.php | 1 - 5 files changed, 1 insertion(+), 62 deletions(-) delete mode 100644 lib/Alchemy/Phrasea/Setup/DoctrineMigrations/Version20180222113704.php diff --git a/lib/Alchemy/Phrasea/Border/Manager.php b/lib/Alchemy/Phrasea/Border/Manager.php index 0b16e7f254..d8abc9ac99 100644 --- a/lib/Alchemy/Phrasea/Border/Manager.php +++ b/lib/Alchemy/Phrasea/Border/Manager.php @@ -401,11 +401,6 @@ class Manager } } - $recordIds = $lazaretFile->getRecordIdsByCheckers($this->app); - - /** $recordIds will be serialized */ - $lazaretFile->setRecordIds($recordIds); - $this->app['orm.em']->flush(); return $lazaretFile; diff --git a/lib/Alchemy/Phrasea/Controller/Api/V1Controller.php b/lib/Alchemy/Phrasea/Controller/Api/V1Controller.php index efb5a8e875..eb5fe4646f 100644 --- a/lib/Alchemy/Phrasea/Controller/Api/V1Controller.php +++ b/lib/Alchemy/Phrasea/Controller/Api/V1Controller.php @@ -707,7 +707,7 @@ class V1Controller extends Controller 'id' => $file->getId(), 'quarantine_session' => $session, 'base_id' => $file->getBaseId(), - 'record_id' => $file->getRecordIds()?:[], + 'record_id' => $file->getRecordIdsByCheckers($this->app)?:[], 'original_name' => $file->getOriginalName(), 'sha256' => $file->getSha256(), 'uuid' => $file->getUuid(), diff --git a/lib/Alchemy/Phrasea/Model/Entities/LazaretFile.php b/lib/Alchemy/Phrasea/Model/Entities/LazaretFile.php index afa818c327..1ff2d26236 100644 --- a/lib/Alchemy/Phrasea/Model/Entities/LazaretFile.php +++ b/lib/Alchemy/Phrasea/Model/Entities/LazaretFile.php @@ -49,11 +49,6 @@ class LazaretFile */ private $base_id; - /** - * @ORM\Column(type="string", length=512) - */ - private $record_ids; - /** * @ORM\Column(type="string", length=36) */ @@ -210,29 +205,6 @@ class LazaretFile return $this->base_id; } - /** - * Set record_ids - * - * @param mixed $recordIds - * @return LazaretFile - */ - public function setRecordIds($recordIds) - { - $this->record_ids = serialize($recordIds); - - return $this; - } - - /** - * Get record_ids - * - * @return mixed - */ - public function getRecordIds() - { - return unserialize($this->record_ids); - } - /** * Get the Destination Collection * diff --git a/lib/Alchemy/Phrasea/Setup/DoctrineMigrations/Version20180222113704.php b/lib/Alchemy/Phrasea/Setup/DoctrineMigrations/Version20180222113704.php deleted file mode 100644 index a54af53e2c..0000000000 --- a/lib/Alchemy/Phrasea/Setup/DoctrineMigrations/Version20180222113704.php +++ /dev/null @@ -1,27 +0,0 @@ -addSql('ALTER TABLE LazaretFiles ADD record_ids VARCHAR(512) NOT NULL AFTER base_id'); - } - - /** - * @param Schema $schema - */ - public function doDownSql(Schema $schema) - { - $this->addSql('ALTER TABLE LazaretFiles DROP record_ids'); - } -} diff --git a/tests/Alchemy/Tests/Phrasea/Controller/Prod/LazaretTest.php b/tests/Alchemy/Tests/Phrasea/Controller/Prod/LazaretTest.php index abde12c72e..82c916e197 100644 --- a/tests/Alchemy/Tests/Phrasea/Controller/Prod/LazaretTest.php +++ b/tests/Alchemy/Tests/Phrasea/Controller/Prod/LazaretTest.php @@ -507,7 +507,6 @@ class LazaretTest extends \PhraseanetAuthenticatedWebTestCase $lazaretFile->setUuid('7b8ef0e3-dc8f-4b66-9e2f-bd049d175124'); $lazaretFile->setCreated(new \DateTime('-1 day')); $lazaretFile->setUpdated(new \DateTime('now')); - $lazaretFile->setRecordIds([1]); return $lazaretFile; } From 7ebcd8ae9b7497e24dd46db1ae722ed8fca4e7fa Mon Sep 17 00:00:00 2001 From: aina-esokia Date: Fri, 2 Mar 2018 18:06:01 +0400 Subject: [PATCH 6/6] add info to api/quarantine --- .../Phrasea/Controller/Api/V1Controller.php | 15 +++++- .../Phrasea/Model/Entities/LazaretFile.php | 50 +++++++++---------- 2 files changed, 38 insertions(+), 27 deletions(-) diff --git a/lib/Alchemy/Phrasea/Controller/Api/V1Controller.php b/lib/Alchemy/Phrasea/Controller/Api/V1Controller.php index eb5fe4646f..ee2c361cc6 100644 --- a/lib/Alchemy/Phrasea/Controller/Api/V1Controller.php +++ b/lib/Alchemy/Phrasea/Controller/Api/V1Controller.php @@ -688,8 +688,18 @@ class V1Controller extends Controller $checks = array_map(function (LazaretCheck $checker) use ($manager, $translator) { $checkerFQCN = $checker->getCheckClassname(); + return $manager->getCheckerFromFQCN($checkerFQCN)->getMessage($translator); - }, iterator_to_array($file->getChecks())); + }, $file->getChecksWhithNameKey()); + + $recordsMatch = array_map(function ($recordsTab){ + $record = $recordsTab['record']; + $matched['record_id'] = $record->getRecordId(); + $matched['collection'] = $record->getCollectionName(); + $matched['checks'] = $recordsTab['reasons']; + + return $matched; + }, array_values($file->getRecordsToSubstitute($this->app, true))); $usr_id = $user = null; if ($file->getSession()->getUser()) { @@ -707,12 +717,13 @@ class V1Controller extends Controller 'id' => $file->getId(), 'quarantine_session' => $session, 'base_id' => $file->getBaseId(), - 'record_id' => $file->getRecordIdsByCheckers($this->app)?:[], 'original_name' => $file->getOriginalName(), + 'collection' => $file->getCollection($this->app)->get_label($this->app['locale']), 'sha256' => $file->getSha256(), 'uuid' => $file->getUuid(), 'forced' => $file->getForced(), 'checks' => $file->getForced() ? [] : $checks, + 'records_match' => $recordsMatch?:[], 'created_on' => $file->getCreated()->format(DATE_ATOM), 'updated_on' => $file->getUpdated()->format(DATE_ATOM), ]; diff --git a/lib/Alchemy/Phrasea/Model/Entities/LazaretFile.php b/lib/Alchemy/Phrasea/Model/Entities/LazaretFile.php index 1ff2d26236..9355acc471 100644 --- a/lib/Alchemy/Phrasea/Model/Entities/LazaretFile.php +++ b/lib/Alchemy/Phrasea/Model/Entities/LazaretFile.php @@ -386,6 +386,17 @@ class LazaretFile $this->checks->removeElement($checks); } + /** + * @param LazaretCheck $checks + * @return string + */ + public function getCheckerName(LazaretCheck $checks) + { + $checkNameTab = explode('\\', $checks->getCheckClassname()); + + return $checkNameTab[4]; + } + /** * Get checks * @@ -396,6 +407,19 @@ class LazaretFile return $this->checks; } + /** + * @return array $checkers + */ + public function getChecksWhithNameKey() + { + $checkers = []; + foreach($this->checks as $check){ + $checkers[$this->getCheckerName($check)] = $check; + } + + return $checkers; + } + /** * Set session * @@ -439,7 +463,7 @@ class LazaretFile 'reasons' => [] ]; } - $merged[$record->getRecordId()]['reasons'][] = $check->getReason($app['translator']); + $merged[$record->getRecordId()]['reasons'][$this->getCheckerName($check)] = $check->getReason($app['translator']); } else { $merged[$record->getRecordId()] = $record; @@ -450,28 +474,4 @@ class LazaretFile return $merged; } - /** - * Get an array of record_id - * - * @param Application $app - * @return array - */ - public function getRecordIdsByCheckers(Application $app) - { - $recordIds = []; - /** @var LazaretCheck $check */ - foreach($this->getChecks() as $check) { - /** @var record_adapter $record */ - $conflicts = $check->listConflicts($app); - - foreach ($conflicts as $record) { - if(!in_array($record->getRecordId(), $recordIds)){ - $recordIds[] = $record->getRecordId(); - } - } - } - - return $recordIds; - } - }