From 89d21a826a9e95bef7cc89c92268251b98491269 Mon Sep 17 00:00:00 2001 From: juckerf Date: Mon, 30 Jan 2023 15:19:55 +0000 Subject: [PATCH 01/11] Apply php-cs-fixer changes --- .../AdminBundle/Controller/Searchadmin/SearchController.php | 3 +++ 1 file changed, 3 insertions(+) diff --git a/bundles/AdminBundle/Controller/Searchadmin/SearchController.php b/bundles/AdminBundle/Controller/Searchadmin/SearchController.php index 7988bcf9ae9..5d7c7da863c 100644 --- a/bundles/AdminBundle/Controller/Searchadmin/SearchController.php +++ b/bundles/AdminBundle/Controller/Searchadmin/SearchController.php @@ -50,8 +50,11 @@ class SearchController extends AdminController * @return JsonResponse * * @todo: $conditionTypeParts could be undefined + * * @todo: $conditionSubtypeParts could be undefined + * * @todo: $conditionClassnameParts could be undefined + * * @todo: $data could be undefined */ public function findAction(Request $request, EventDispatcherInterface $eventDispatcher, GridHelperService $gridHelperService) From 4e1d5fd1022e30545b0ab5bc9dc0ab00c11f075e Mon Sep 17 00:00:00 2001 From: Tomasz Szustak Date: Thu, 2 Feb 2023 13:16:14 +0100 Subject: [PATCH 02/11] chore: Added caching for classes name / id mapping in attempt to reduce db queries. --- models/DataObject/ClassDefinition/Dao.php | 39 ++++++++++++++++++++--- 1 file changed, 35 insertions(+), 4 deletions(-) diff --git a/models/DataObject/ClassDefinition/Dao.php b/models/DataObject/ClassDefinition/Dao.php index 853accab0be..ca80869adf7 100644 --- a/models/DataObject/ClassDefinition/Dao.php +++ b/models/DataObject/ClassDefinition/Dao.php @@ -15,6 +15,7 @@ namespace Pimcore\Model\DataObject\ClassDefinition; +use Pimcore\Cache; use Pimcore\Logger; use Pimcore\Model; use Pimcore\Model\DataObject; @@ -39,6 +40,25 @@ class Dao extends Model\Dao\AbstractDao */ protected $tableDefinitions = null; + /** + * Helper to minimize db queries used when looking up classes id / name. + * + * Mapping is actively updated as soon as a class is saved. + * + * @see self::save() + * + * @return array + */ + protected function getClassNameIdMap($skipCache = false): array + { + static $mapping; + if ($skipCache || (!isset($mapping) && !is_array(($mapping = Cache::load(md5(__METHOD__)))))) { + $mapping = $this->db->fetchPairs('SELECT id, name FROM classes'); + Cache::save($mapping, md5(__METHOD__), ['ClassDefinitionDao']); + } + return $mapping; + } + /** * @param string $id * @@ -48,9 +68,12 @@ public function getNameById($id) { try { if (!empty($id)) { - if ($name = $this->db->fetchOne('SELECT name FROM classes WHERE id = ?', [$id])) { - return $name; - } + $mapping = $this->getClassNameIdMap(); + return $mapping[$id] ?? null; + +// if ($name = $this->db->fetchOne('SELECT name FROM classes WHERE id = ?', [$id])) { +// return $name; +// } } } catch (\Exception $e) { } @@ -71,7 +94,12 @@ public function getIdByName($name) try { if (!empty($name)) { - $id = $this->db->fetchOne('SELECT id FROM classes WHERE name = ?', [$name]); + $mapping = $this->getClassNameIdMap(); + if (($v = array_search($name, $mapping, true)) !== false) { + $id = $v; + } +// $id = $this->db->fetchOne('SELECT id FROM classes WHERE name = ?', [$name]); + } } catch (\Exception $e) { } @@ -97,6 +125,9 @@ public function save($isUpdate = true) } $this->update(); + + // Update class name / id mapping in cache. + $this->getClassNameIdMap(true); } /** From 6a140aabe3ae841a37452ccde87276be93533a4d Mon Sep 17 00:00:00 2001 From: Tomasz Szustak Date: Thu, 2 Feb 2023 14:10:35 +0100 Subject: [PATCH 03/11] chore: Changed code to use Helper::fetchPairs --- models/DataObject/ClassDefinition/Dao.php | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/models/DataObject/ClassDefinition/Dao.php b/models/DataObject/ClassDefinition/Dao.php index ca80869adf7..081fe391b8e 100644 --- a/models/DataObject/ClassDefinition/Dao.php +++ b/models/DataObject/ClassDefinition/Dao.php @@ -16,6 +16,7 @@ namespace Pimcore\Model\DataObject\ClassDefinition; use Pimcore\Cache; +use Pimcore\Db\Helper; use Pimcore\Logger; use Pimcore\Model; use Pimcore\Model\DataObject; @@ -53,7 +54,7 @@ protected function getClassNameIdMap($skipCache = false): array { static $mapping; if ($skipCache || (!isset($mapping) && !is_array(($mapping = Cache::load(md5(__METHOD__)))))) { - $mapping = $this->db->fetchPairs('SELECT id, name FROM classes'); + $mapping = Helper::fetchPairs($this->db, 'SELECT id, name FROM classes'); Cache::save($mapping, md5(__METHOD__), ['ClassDefinitionDao']); } return $mapping; From 9e328f8275710104f19c29313987da3a9280a6cc Mon Sep 17 00:00:00 2001 From: Stefan Scheu Date: Tue, 13 Jun 2023 15:29:49 +0200 Subject: [PATCH 04/11] fix: clear corresponding cache tags after adding or deleting a class definition --- .../Controller/Admin/DataObject/ClassController.php | 5 +++++ 1 file changed, 5 insertions(+) diff --git a/bundles/AdminBundle/Controller/Admin/DataObject/ClassController.php b/bundles/AdminBundle/Controller/Admin/DataObject/ClassController.php index ce44ed65340..687a18c35b1 100644 --- a/bundles/AdminBundle/Controller/Admin/DataObject/ClassController.php +++ b/bundles/AdminBundle/Controller/Admin/DataObject/ClassController.php @@ -17,6 +17,7 @@ use Pimcore\Bundle\AdminBundle\Controller\AdminController; use Pimcore\Bundle\AdminBundle\HttpFoundation\JsonResponse; +use Pimcore\Cache; use Pimcore\Controller\KernelControllerEventInterface; use Pimcore\Db; use Pimcore\Event\AdminEvents; @@ -300,6 +301,8 @@ public function addAction(Request $request) $class->setId($classId); $class->save(true); + // clear cache to invalidate cache with class definitions + Cache::clearTags(['ClassDefinitionDao']); return $this->adminJson(['success' => true, 'id' => $class->getId()]); } @@ -350,6 +353,8 @@ public function deleteAction(Request $request) $class = DataObject\ClassDefinition::getById($request->get('id')); if ($class) { $class->delete(); + // clear cache to invalidate cache with class definitions + Cache::clearTags(['ClassDefinitionDao']); } return new Response(); From 5e97c56f9440bf6a147b579bcc812f822709549b Mon Sep 17 00:00:00 2001 From: Tomasz Szustak Date: Thu, 2 Feb 2023 13:16:14 +0100 Subject: [PATCH 05/11] chore: Added caching for classes name / id mapping in attempt to reduce db queries. --- models/DataObject/ClassDefinition/Dao.php | 39 ++++++++++++++++++++--- 1 file changed, 35 insertions(+), 4 deletions(-) diff --git a/models/DataObject/ClassDefinition/Dao.php b/models/DataObject/ClassDefinition/Dao.php index 6402ff76c06..276447a6f0c 100644 --- a/models/DataObject/ClassDefinition/Dao.php +++ b/models/DataObject/ClassDefinition/Dao.php @@ -15,6 +15,7 @@ namespace Pimcore\Model\DataObject\ClassDefinition; +use Pimcore\Cache; use Pimcore\Logger; use Pimcore\Model; use Pimcore\Model\DataObject; @@ -39,6 +40,25 @@ class Dao extends Model\Dao\AbstractDao */ protected $tableDefinitions = null; + /** + * Helper to minimize db queries used when looking up classes id / name. + * + * Mapping is actively updated as soon as a class is saved. + * + * @see self::save() + * + * @return array + */ + protected function getClassNameIdMap($skipCache = false): array + { + static $mapping; + if ($skipCache || (!isset($mapping) && !is_array(($mapping = Cache::load(md5(__METHOD__)))))) { + $mapping = $this->db->fetchPairs('SELECT id, name FROM classes'); + Cache::save($mapping, md5(__METHOD__), ['ClassDefinitionDao']); + } + return $mapping; + } + /** * @param string $id * @@ -48,9 +68,12 @@ public function getNameById($id) { try { if (!empty($id)) { - if ($name = $this->db->fetchOne('SELECT name FROM classes WHERE id = ?', [$id])) { - return $name; - } + $mapping = $this->getClassNameIdMap(); + return $mapping[$id] ?? null; + +// if ($name = $this->db->fetchOne('SELECT name FROM classes WHERE id = ?', [$id])) { +// return $name; +// } } } catch (\Exception $e) { } @@ -71,7 +94,12 @@ public function getIdByName($name) try { if (!empty($name)) { - $id = $this->db->fetchOne('SELECT id FROM classes WHERE name = ?', [$name]); + $mapping = $this->getClassNameIdMap(); + if (($v = array_search($name, $mapping, true)) !== false) { + $id = $v; + } +// $id = $this->db->fetchOne('SELECT id FROM classes WHERE name = ?', [$name]); + } } catch (\Exception $e) { } @@ -97,6 +125,9 @@ public function save($isUpdate = true) } $this->update(); + + // Update class name / id mapping in cache. + $this->getClassNameIdMap(true); } /** From 10eb08b5ad24c8479f1a2bd904bd1e90c5749b9f Mon Sep 17 00:00:00 2001 From: Tomasz Szustak Date: Thu, 2 Feb 2023 14:10:35 +0100 Subject: [PATCH 06/11] chore: Changed code to use Helper::fetchPairs --- models/DataObject/ClassDefinition/Dao.php | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/models/DataObject/ClassDefinition/Dao.php b/models/DataObject/ClassDefinition/Dao.php index 276447a6f0c..244b82f48e2 100644 --- a/models/DataObject/ClassDefinition/Dao.php +++ b/models/DataObject/ClassDefinition/Dao.php @@ -16,6 +16,7 @@ namespace Pimcore\Model\DataObject\ClassDefinition; use Pimcore\Cache; +use Pimcore\Db\Helper; use Pimcore\Logger; use Pimcore\Model; use Pimcore\Model\DataObject; @@ -53,7 +54,7 @@ protected function getClassNameIdMap($skipCache = false): array { static $mapping; if ($skipCache || (!isset($mapping) && !is_array(($mapping = Cache::load(md5(__METHOD__)))))) { - $mapping = $this->db->fetchPairs('SELECT id, name FROM classes'); + $mapping = Helper::fetchPairs($this->db, 'SELECT id, name FROM classes'); Cache::save($mapping, md5(__METHOD__), ['ClassDefinitionDao']); } return $mapping; From be39acb22b24045715a17ed0dcd2c42930159d63 Mon Sep 17 00:00:00 2001 From: Stefan Scheu Date: Tue, 13 Jun 2023 15:33:30 +0200 Subject: [PATCH 07/11] chore: rebase to 10.6 --- .../Controller/Admin/DataObject/ClassController.php | 3 +++ 1 file changed, 3 insertions(+) diff --git a/bundles/AdminBundle/Controller/Admin/DataObject/ClassController.php b/bundles/AdminBundle/Controller/Admin/DataObject/ClassController.php index 8ec77028a23..75817d8e395 100644 --- a/bundles/AdminBundle/Controller/Admin/DataObject/ClassController.php +++ b/bundles/AdminBundle/Controller/Admin/DataObject/ClassController.php @@ -16,6 +16,7 @@ namespace Pimcore\Bundle\AdminBundle\Controller\Admin\DataObject; use Pimcore\Bundle\AdminBundle\Controller\AdminAbstractController; +use Pimcore\Cache; use Pimcore\Controller\KernelControllerEventInterface; use Pimcore\Db; use Pimcore\Event\AdminEvents; @@ -301,6 +302,7 @@ public function addAction(Request $request) $class->setId($classId); $class->save(true); + Cache::clearTags(['ClassDefinitionDao']); return $this->adminJson(['success' => true, 'id' => $class->getId()]); } @@ -351,6 +353,7 @@ public function deleteAction(Request $request) $class = DataObject\ClassDefinition::getById($request->get('id')); if ($class) { $class->delete(); + Cache::clearTags(['ClassDefinitionDao']); } return new Response(); From 0c5ed1fab18c97736fbbcc682f2d3685ede0757e Mon Sep 17 00:00:00 2001 From: Stefan Scheu Date: Tue, 13 Jun 2023 15:35:53 +0200 Subject: [PATCH 08/11] chore: revert changes from rebase --- .../Admin/DataObject/ClassController.php | 35 +++++++++++-------- 1 file changed, 20 insertions(+), 15 deletions(-) diff --git a/bundles/AdminBundle/Controller/Admin/DataObject/ClassController.php b/bundles/AdminBundle/Controller/Admin/DataObject/ClassController.php index 687a18c35b1..abc7b9cc8b6 100644 --- a/bundles/AdminBundle/Controller/Admin/DataObject/ClassController.php +++ b/bundles/AdminBundle/Controller/Admin/DataObject/ClassController.php @@ -15,9 +15,7 @@ namespace Pimcore\Bundle\AdminBundle\Controller\Admin\DataObject; -use Pimcore\Bundle\AdminBundle\Controller\AdminController; -use Pimcore\Bundle\AdminBundle\HttpFoundation\JsonResponse; -use Pimcore\Cache; +use Pimcore\Bundle\AdminBundle\Controller\AdminAbstractController; use Pimcore\Controller\KernelControllerEventInterface; use Pimcore\Db; use Pimcore\Event\AdminEvents; @@ -28,19 +26,21 @@ use Pimcore\Model\Translation; use Pimcore\Tool\Session; use Symfony\Component\EventDispatcher\GenericEvent; +use Symfony\Component\HttpFoundation\JsonResponse; use Symfony\Component\HttpFoundation\Request; use Symfony\Component\HttpFoundation\Response; use Symfony\Component\HttpFoundation\Session\Attribute\AttributeBagInterface; use Symfony\Component\HttpKernel\Event\ControllerEvent; use Symfony\Component\Routing\Annotation\Route; use Symfony\Contracts\EventDispatcher\EventDispatcherInterface; +use Symfony\Contracts\Translation\TranslatorInterface; /** * @Route("/class", name="pimcore_admin_dataobject_class_") * * @internal */ -class ClassController extends AdminController implements KernelControllerEventInterface +class ClassController extends AdminAbstractController implements KernelControllerEventInterface { /** * @Route("/get-document-types", name="getdocumenttypes", methods={"GET"}) @@ -301,8 +301,6 @@ public function addAction(Request $request) $class->setId($classId); $class->save(true); - // clear cache to invalidate cache with class definitions - Cache::clearTags(['ClassDefinitionDao']); return $this->adminJson(['success' => true, 'id' => $class->getId()]); } @@ -338,7 +336,7 @@ public function addCustomLayoutAction(Request $request) $data['isWriteable'] = $isWriteable; return $this->adminJson(['success' => true, 'id' => $customLayout->getId(), 'name' => $customLayout->getName(), - 'data' => $data, ]); + 'data' => $data, ]); } /** @@ -353,8 +351,6 @@ public function deleteAction(Request $request) $class = DataObject\ClassDefinition::getById($request->get('id')); if ($class) { $class->delete(); - // clear cache to invalidate cache with class definitions - Cache::clearTags(['ClassDefinitionDao']); } return new Response(); @@ -460,6 +456,14 @@ public function saveAction(Request $request) $class->rename($values['name']); } + if ($values['compositeIndices']) { + foreach ($values['compositeIndices'] as $index => $compositeIndex) { + if ($compositeIndex['index_key'] !== ($sanitizedKey = preg_replace('/[^a-za-z0-9_\-+]/', '', $compositeIndex['index_key']))) { + $values['compositeIndices'][$index]['index_key'] = $sanitizedKey; + } + } + } + unset($values['creationDate']); unset($values['userOwner']); unset($values['layoutDefinitions']); @@ -662,7 +666,7 @@ public function getAllLayoutsAction(Request $request) if (isset($mapping[$class->getId()])) { $classMapping = $mapping[$class->getId()]; $resultList[] = [ - 'type' => 'master', + 'type' => 'main', 'id' => $class->getId() . '_' . 0, 'name' => $class->getName(), ]; @@ -1949,7 +1953,7 @@ public function suggestClassIdentifierAction() $result = [ 'suggestedIdentifier' => $maxId ? $maxId + 1 : 1, 'existingIds' => $existingIds, - ]; + ]; return $this->adminJson($result); } @@ -1984,7 +1988,7 @@ public function suggestCustomLayoutIdentifierAction(Request $request) 'suggestedIdentifier' => $identifier, 'existingIds' => $existingIds, 'existingNames' => $existingNames, - ]; + ]; return $this->adminJson($result); } @@ -2005,7 +2009,7 @@ public function textLayoutPreviewAction(Request $request) $textLayout = new DataObject\ClassDefinition\Layout\Text(); $context = [ - 'data' => $request->get('renderingData'), + 'data' => $request->get('renderingData'), ]; if ($renderingClass = $request->get('renderingClass')) { @@ -2040,10 +2044,11 @@ public function textLayoutPreviewAction(Request $request) * @Route("/video-supported-types", name="videosupportedTypestypes") * * @param Request $request + * @param TranslatorInterface $translator * * @return Response */ - public function videoAllowedTypesAction(Request $request) + public function videoAllowedTypesAction(Request $request, TranslatorInterface $translator) { $videoDef = new DataObject\ClassDefinition\Data\Video(); $res = []; @@ -2051,7 +2056,7 @@ public function videoAllowedTypesAction(Request $request) foreach ($videoDef->getSupportedTypes() as $type) { $res[] = [ 'key' => $type, - 'value' => $this->trans($type), + 'value' => $translator->trans($type, [], 'admin'), ]; } From fe31f48f9c400d0df0ed1b096b7a7ce1e4da68b3 Mon Sep 17 00:00:00 2001 From: Stefan Scheu Date: Tue, 13 Jun 2023 15:36:42 +0200 Subject: [PATCH 09/11] chore: revert changes from rebase --- .../AdminBundle/Controller/Admin/DataObject/ClassController.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/bundles/AdminBundle/Controller/Admin/DataObject/ClassController.php b/bundles/AdminBundle/Controller/Admin/DataObject/ClassController.php index abc7b9cc8b6..c6f4f57169e 100644 --- a/bundles/AdminBundle/Controller/Admin/DataObject/ClassController.php +++ b/bundles/AdminBundle/Controller/Admin/DataObject/ClassController.php @@ -336,7 +336,7 @@ public function addCustomLayoutAction(Request $request) $data['isWriteable'] = $isWriteable; return $this->adminJson(['success' => true, 'id' => $customLayout->getId(), 'name' => $customLayout->getName(), - 'data' => $data, ]); + 'data' => $data, ]); } /** From 68a60a5bb5c962a3807bbba45d0fbcd0af14e472 Mon Sep 17 00:00:00 2001 From: Stefan Scheu Date: Tue, 13 Jun 2023 15:38:44 +0200 Subject: [PATCH 10/11] chore: revert changes from rebase --- .../AdminBundle/Controller/Admin/DataObject/ClassController.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/bundles/AdminBundle/Controller/Admin/DataObject/ClassController.php b/bundles/AdminBundle/Controller/Admin/DataObject/ClassController.php index c6f4f57169e..3bd0ae3987a 100644 --- a/bundles/AdminBundle/Controller/Admin/DataObject/ClassController.php +++ b/bundles/AdminBundle/Controller/Admin/DataObject/ClassController.php @@ -1953,7 +1953,7 @@ public function suggestClassIdentifierAction() $result = [ 'suggestedIdentifier' => $maxId ? $maxId + 1 : 1, 'existingIds' => $existingIds, - ]; + ]; return $this->adminJson($result); } From 38591e054e5b2ebd2b17c83775b9d21c464cef9e Mon Sep 17 00:00:00 2001 From: Stefan Scheu Date: Tue, 13 Jun 2023 15:40:24 +0200 Subject: [PATCH 11/11] chore: revert changes after rebase --- .../Controller/Admin/DataObject/ClassController.php | 7 +++++-- 1 file changed, 5 insertions(+), 2 deletions(-) diff --git a/bundles/AdminBundle/Controller/Admin/DataObject/ClassController.php b/bundles/AdminBundle/Controller/Admin/DataObject/ClassController.php index 3bd0ae3987a..75817d8e395 100644 --- a/bundles/AdminBundle/Controller/Admin/DataObject/ClassController.php +++ b/bundles/AdminBundle/Controller/Admin/DataObject/ClassController.php @@ -16,6 +16,7 @@ namespace Pimcore\Bundle\AdminBundle\Controller\Admin\DataObject; use Pimcore\Bundle\AdminBundle\Controller\AdminAbstractController; +use Pimcore\Cache; use Pimcore\Controller\KernelControllerEventInterface; use Pimcore\Db; use Pimcore\Event\AdminEvents; @@ -301,6 +302,7 @@ public function addAction(Request $request) $class->setId($classId); $class->save(true); + Cache::clearTags(['ClassDefinitionDao']); return $this->adminJson(['success' => true, 'id' => $class->getId()]); } @@ -351,6 +353,7 @@ public function deleteAction(Request $request) $class = DataObject\ClassDefinition::getById($request->get('id')); if ($class) { $class->delete(); + Cache::clearTags(['ClassDefinitionDao']); } return new Response(); @@ -1988,7 +1991,7 @@ public function suggestCustomLayoutIdentifierAction(Request $request) 'suggestedIdentifier' => $identifier, 'existingIds' => $existingIds, 'existingNames' => $existingNames, - ]; + ]; return $this->adminJson($result); } @@ -2009,7 +2012,7 @@ public function textLayoutPreviewAction(Request $request) $textLayout = new DataObject\ClassDefinition\Layout\Text(); $context = [ - 'data' => $request->get('renderingData'), + 'data' => $request->get('renderingData'), ]; if ($renderingClass = $request->get('renderingClass')) {