From e20c915775a44bacaa2e0b0e784830cb9db3ddb7 Mon Sep 17 00:00:00 2001 From: Romain Neutron Date: Thu, 20 Feb 2014 15:54:01 +0100 Subject: [PATCH] Avoid using getPartialReference --- .travis.yml | 1 + .../Repositories/UsrListOwnerRepository.php | 1 + .../PreSchemaUpgrade/Upgrade39Users.php | 11 ++++++-- lib/classes/patch/320alpha4b.php | 7 +++-- lib/classes/patch/370alpha7a.php | 7 +++-- lib/classes/patch/380alpha11a.php | 8 ++++-- lib/classes/patch/383alpha2a.php | 11 ++++++-- lib/classes/patch/390alpha1a.php | 19 ++++++++++--- lib/classes/patch/390alpha3a.php | 6 ++-- lib/classes/patch/390alpha4a.php | 6 ++-- lib/classes/patch/390alpha5a.php | 6 ++-- lib/classes/patch/390alpha6a.php | 4 +-- lib/classes/patch/390alpha7a.php | 14 +++++++--- lib/classes/patchAbstract.php | 28 +++++++++++++++++++ 14 files changed, 100 insertions(+), 29 deletions(-) create mode 100644 lib/classes/patchAbstract.php diff --git a/.travis.yml b/.travis.yml index c9cdabdeb7..cf5c52cc4c 100644 --- a/.travis.yml +++ b/.travis.yml @@ -35,6 +35,7 @@ before_script: - sudo mysql -e "GRANT ALL PRIVILEGES ON ab_unitTests.* TO 'phraseaUnitTests'@'localhost' IDENTIFIED BY 'iWvGxPE8' WITH GRANT OPTION" - sudo mysql -e "GRANT ALL PRIVILEGES ON db_unitTests.* TO 'phraseaUnitTests'@'localhost' IDENTIFIED BY 'iWvGxPE8' WITH GRANT OPTION" - mysql -e 'SET @@global.sql_mode= "";' + - mysql -e 'SET @@global.max_allowed_packet= 33554432;' - mysql -e 'SET @@global.wait_timeout= 999999;' - git clone git://github.com/alchemy-fr/Phraseanet-Extension.git - sh -c "cd Phraseanet-Extension && phpize && ./configure --quiet && make -j --quiet && sudo make install" diff --git a/lib/Alchemy/Phrasea/Model/Repositories/UsrListOwnerRepository.php b/lib/Alchemy/Phrasea/Model/Repositories/UsrListOwnerRepository.php index d8a4c2d5d4..4ece2eb632 100644 --- a/lib/Alchemy/Phrasea/Model/Repositories/UsrListOwnerRepository.php +++ b/lib/Alchemy/Phrasea/Model/Repositories/UsrListOwnerRepository.php @@ -14,6 +14,7 @@ namespace Alchemy\Phrasea\Model\Repositories; use Alchemy\Phrasea\Model\Entities\UsrListOwner; use Alchemy\Phrasea\Model\Entities\UsrList; use Doctrine\ORM\EntityRepository; +use Doctrine\ORM\NoResultException; use Symfony\Component\HttpKernel\Exception\AccessDeniedHttpException; use Symfony\Component\HttpKernel\Exception\NotFoundHttpException; diff --git a/lib/Alchemy/Phrasea/Setup/Version/PreSchemaUpgrade/Upgrade39Users.php b/lib/Alchemy/Phrasea/Setup/Version/PreSchemaUpgrade/Upgrade39Users.php index fa621cffc0..699e5ab3db 100644 --- a/lib/Alchemy/Phrasea/Setup/Version/PreSchemaUpgrade/Upgrade39Users.php +++ b/lib/Alchemy/Phrasea/Setup/Version/PreSchemaUpgrade/Upgrade39Users.php @@ -16,6 +16,7 @@ use Alchemy\Phrasea\Model\Entities\User; use Alchemy\Phrasea\Model\Entities\FtpCredential; use Doctrine\DBAL\Migrations\Configuration\Configuration; use Doctrine\ORM\EntityManager; +use Doctrine\ORM\NoResultException; use Doctrine\ORM\Query; use Doctrine\ORM\Query\ResultSetMapping; @@ -268,7 +269,7 @@ class Upgrade39Users implements PreSchemaUpgradeInterface } $sql = sprintf('SELECT id FROM %s WHERE %s %s', $tableName, $field, $wrongIds); - $rs = $em->getConnection()->fetchAll(\PDO::FETCH_COLUMN); + $rs = $em->getConnection()->executeQuery($sql)->fetchAll(\PDO::FETCH_COLUMN); if (count($rs) === 0) { return; @@ -379,9 +380,15 @@ class Upgrade39Users implements PreSchemaUpgradeInterface $rs = $em->getConnection()->fetchAll($sql); foreach ($rs as $row) { - if (null === $user = $em->getPartialReference('Phraseanet:User', $row['usr_id'])) { + try { + $user = $em->createQuery('SELECT PARTIAL u.{id} FROM Phraseanet:User u WHERE u.id = :id') + ->setParameters(['id' => $row['usr_id']]) + ->setHint(Query::HINT_FORCE_PARTIAL_LOAD, true) + ->getSingleResult(); + } catch (NoResultException $e) { continue; } + $credential = new FtpCredential(); $credential->setActive($row['activeFTP']); $credential->setAddress($row['addrFTP']); diff --git a/lib/classes/patch/320alpha4b.php b/lib/classes/patch/320alpha4b.php index 07bf79177b..dda41b1179 100644 --- a/lib/classes/patch/320alpha4b.php +++ b/lib/classes/patch/320alpha4b.php @@ -17,7 +17,7 @@ use Alchemy\Phrasea\Model\Entities\FeedPublisher; use Gedmo\Timestampable\TimestampableListener; use Symfony\Component\HttpKernel\Exception\NotFoundHttpException; -class patch_320alpha4b implements patchInterface +class patch_320alpha4b extends patchAbstract { /** @var string */ private $release = '3.2.0-alpha.4'; @@ -86,8 +86,9 @@ class patch_320alpha4b implements patchInterface $app['EM']->getEventManager()->removeEventSubscriber(new TimestampableListener()); foreach ($rs as $row) { - $user = $app['EM']->getPartialReference('Phraseanet:User', $row['usr_id']); - + if (null === $user = $this->loadUser($app['EM'], $row['usr_id'])) { + continue; + } $feed = $this->get_feed($appbox, $user, $row['pub_restrict'], $row['homelink'], $app); if (! $feed instanceof Feed) { diff --git a/lib/classes/patch/370alpha7a.php b/lib/classes/patch/370alpha7a.php index 8a924d6fb2..fbe13f784e 100644 --- a/lib/classes/patch/370alpha7a.php +++ b/lib/classes/patch/370alpha7a.php @@ -15,7 +15,7 @@ use Alchemy\Phrasea\Model\Entities\LazaretSession; use MediaAlchemyst\Exception\ExceptionInterface as MediaAlchemystException; use MediaAlchemyst\Specification\Image as ImageSpec; -class patch_370alpha7a implements patchInterface +class patch_370alpha7a extends patchAbstract { /** @var string */ private $release = '3.7.0-alpha.7'; @@ -88,6 +88,9 @@ class patch_370alpha7a implements patchInterface foreach ($rs as $row) { $filePath = $app['root.path'] . '/tmp/lazaret/' . $row['filepath']; + if (null === $user = $this->loadUser($app['EM'], $row['usr_id'])) { + continue; + } if (file_exists($filePath)) { $spec = new ImageSpec(); @@ -109,8 +112,6 @@ class patch_370alpha7a implements patchInterface $borderFile = new \Alchemy\Phrasea\Border\File($app, $media, $collection); - $user = $user = $app['EM']->getPartialReference('Phraseanet:User', $row['usr_id']); - $lazaretSession = new LazaretSession(); $lazaretSession->setUser($user); diff --git a/lib/classes/patch/380alpha11a.php b/lib/classes/patch/380alpha11a.php index f895cc0fb8..581ed6e401 100644 --- a/lib/classes/patch/380alpha11a.php +++ b/lib/classes/patch/380alpha11a.php @@ -13,7 +13,7 @@ use Alchemy\Phrasea\Application; use Alchemy\Phrasea\Model\Entities\Session; use Alchemy\Phrasea\Model\Entities\SessionModule; -class patch_380alpha11a implements patchInterface +class patch_380alpha11a extends patchAbstract { /** @var string */ private $release = '3.8.0-alpha.11'; @@ -72,6 +72,10 @@ class patch_380alpha11a implements patchInterface } foreach ($rs as $row) { + if (null === $user = $this->loadUser($app['EM'], $row['usr_id'])) { + continue; + } + $created = $updated = null; if ('0000-00-00 00:00:00' !== $row['created_on']) { $created = \DateTime::createFromFormat('Y-m-d H:i:s', $row['created_on']); @@ -80,8 +84,6 @@ class patch_380alpha11a implements patchInterface $updated = \DateTime::createFromFormat('Y-m-d H:i:s', $row['lastaccess']); } - $user = $app['EM']->getPartialReference('Phraseanet:User', $row['usr_id']); - $session = new Session(); $session ->setUser($user) diff --git a/lib/classes/patch/383alpha2a.php b/lib/classes/patch/383alpha2a.php index 5d6a66d636..ae41104d8d 100644 --- a/lib/classes/patch/383alpha2a.php +++ b/lib/classes/patch/383alpha2a.php @@ -10,8 +10,9 @@ */ use Alchemy\Phrasea\Application; +use Doctrine\ORM\NoResultException; -class patch_383alpha2a implements patchInterface +class patch_383alpha2a extends patchAbstract { /** @var string */ private $release = '3.8.3-alpha.2'; @@ -64,8 +65,14 @@ class patch_383alpha2a implements patchInterface $stmt->closeCursor(); foreach ($rows as $row) { - if (null !== $vsession = $app['EM']->getPartialReference('Phraseanet:ValidationSession', $row['validation_session_id'])) { + try { + $vsession = $app['EM']->createQuery('SELECT PARTIAL s.{id} FROM Phraseanet:ValidationSession s WHERE s.id = :id') + ->setParameters(['id' => $row['validation_session_id']]) + ->setHint(Query::HINT_FORCE_PARTIAL_LOAD, true) + ->getSingleResult(); $app['EM']->remove($vsession); + } catch (NoResultException $e) { + } } diff --git a/lib/classes/patch/390alpha1a.php b/lib/classes/patch/390alpha1a.php index a790516fa4..b43f976107 100644 --- a/lib/classes/patch/390alpha1a.php +++ b/lib/classes/patch/390alpha1a.php @@ -12,9 +12,10 @@ use Alchemy\Phrasea\Application; use Alchemy\Phrasea\Model\Entities\Order; use Alchemy\Phrasea\Model\Entities\OrderElement; +use Doctrine\ORM\NoResultException; use Gedmo\Timestampable\TimestampableListener; -class patch_390alpha1a implements patchInterface +class patch_390alpha1a extends patchAbstract { /** @var string */ private $release = '3.9.0-alpha.1'; @@ -91,7 +92,16 @@ class patch_390alpha1a implements patchInterface $todo = $stmt->fetch(\PDO::FETCH_ASSOC); $stmt->closeCursor(); - $user = $em->getPartialReference('Phraseanet:User', $row['usr_id']); + $user = $this->loadUser($app['EM'], $row['usr_id']); + + try { + $basket = $app['EM']->createQuery('SELECT PARTIAL b.{id} FROM Phraseanet:Basket b WHERE b.id = :id') + ->setParameters(['id' => $row['ssel_id']]) + ->setHint(Query::HINT_FORCE_PARTIAL_LOAD, true) + ->getSingleResult(); + } catch (NoResultException $e) { + continue; + } $order = new Order(); $order->setUser($user) @@ -99,7 +109,7 @@ class patch_390alpha1a implements patchInterface ->setOrderUsage($row['usage']) ->setDeadline(new \DateTime($row['deadline'])) ->setCreatedOn(new \DateTime($row['created_on'])) - ->setBasket($em->getPartialReference('Phraseanet:Basket', $row['ssel_id'])); + ->setBasket($basket); $em->persist($order); @@ -114,10 +124,11 @@ class patch_390alpha1a implements patchInterface foreach ($elements as $element) { $orderElement = new OrderElement(); + $user = $this->loadUser($app['EM'], $row['usr_id']); $orderElement->setBaseId($element['base_id']) ->setDeny($element['deny'] === null ? null : (Boolean) $element['deny']) ->setOrder($order) - ->setOrderMaster($em->getPartialReference('Phraseanet:User',$element['order_master_id'])) + ->setOrderMaster($user) ->setRecordId($element['record_id']); $order->addElement($orderElement); diff --git a/lib/classes/patch/390alpha3a.php b/lib/classes/patch/390alpha3a.php index 2ccbf7a988..0a90796cf8 100644 --- a/lib/classes/patch/390alpha3a.php +++ b/lib/classes/patch/390alpha3a.php @@ -12,7 +12,7 @@ use Alchemy\Phrasea\Application; use Alchemy\Phrasea\Model\Entities\UserQuery; -class patch_390alpha3a implements patchInterface +class patch_390alpha3a extends patchAbstract { /** @var string */ private $release = '3.9.0-alpha.3'; @@ -73,7 +73,9 @@ class patch_390alpha3a implements patchInterface $em = $app['EM']; foreach ($rs as $row) { - $user = $em->getPartialReference('Phraseanet:User', $row['usr_id']); + if (null === $user = $this->loadUser($app['EM'], $row['usr_id'])) { + continue; + } $userQuery = new UserQuery(); $userQuery->setQuery($row['query']); diff --git a/lib/classes/patch/390alpha4a.php b/lib/classes/patch/390alpha4a.php index 0a68faa990..341ae9b9f7 100644 --- a/lib/classes/patch/390alpha4a.php +++ b/lib/classes/patch/390alpha4a.php @@ -12,7 +12,7 @@ use Alchemy\Phrasea\Application; use Alchemy\Phrasea\Model\Entities\UserSetting; -class patch_390alpha4a implements patchInterface +class patch_390alpha4a extends patchAbstract { /** @var string */ private $release = '3.9.0-alpha.4'; @@ -77,7 +77,9 @@ class patch_390alpha4a implements patchInterface continue; } - $user = $em->getPartialReference('Phraseanet:User', $row['usr_id']); + if (null === $user = $this->loadUser($app['EM'], $row['usr_id'])) { + continue; + } $userSetting = new UserSetting(); $userSetting->setName($row['prop']); diff --git a/lib/classes/patch/390alpha5a.php b/lib/classes/patch/390alpha5a.php index 55d17cfd55..85743d89ae 100644 --- a/lib/classes/patch/390alpha5a.php +++ b/lib/classes/patch/390alpha5a.php @@ -12,7 +12,7 @@ use Alchemy\Phrasea\Application; use Alchemy\Phrasea\Model\Entities\UserNotificationSetting; -class patch_390alpha5a implements patchInterface +class patch_390alpha5a extends patchAbstract { /** @var string */ private $release = '3.9.0-alpha.5'; @@ -74,7 +74,9 @@ class patch_390alpha5a implements patchInterface $em = $app['EM']; foreach ($rs as $row) { - $user = $em->getPartialReference('Phraseanet:User', $row['usr_id']); + if (null === $user = $this->loadUser($app['EM'], $row['usr_id'])) { + continue; + } $userSetting = new UserNotificationSetting(); $userSetting->setName($row['prop']); diff --git a/lib/classes/patch/390alpha6a.php b/lib/classes/patch/390alpha6a.php index f521c38584..18868b0fdd 100644 --- a/lib/classes/patch/390alpha6a.php +++ b/lib/classes/patch/390alpha6a.php @@ -15,7 +15,7 @@ use Alchemy\Phrasea\Model\Entities\FtpExportElement; use Gedmo\Timestampable\TimestampableListener; use Doctrine\ORM\NoResultException; -class patch_390alpha6a implements patchInterface +class patch_390alpha6a extends patchAbstract { /** @var string */ private $release = '3.9.0-alpha.6'; @@ -94,7 +94,7 @@ class patch_390alpha6a implements patchInterface $n = 0; foreach ($rs as $row) { - if (null === $user = $em->getPartialReference('Phraseanet:User', $row['usr_id'])) { + if (null === $user = $this->loadUser($app['EM'], $row['usr_id'])) { continue; } diff --git a/lib/classes/patch/390alpha7a.php b/lib/classes/patch/390alpha7a.php index f353d54a1c..ecdb7e931e 100644 --- a/lib/classes/patch/390alpha7a.php +++ b/lib/classes/patch/390alpha7a.php @@ -18,7 +18,7 @@ use Alchemy\Phrasea\Model\Entities\FeedPublisher; use Alchemy\Phrasea\Model\Entities\FeedToken; use Doctrine\ORM\Query\ResultSetMapping; -class patch_390alpha7a implements patchInterface +class patch_390alpha7a extends patchAbstract { /** @var string */ private $release = '3.9.0-alpha.7'; @@ -133,7 +133,9 @@ class patch_390alpha7a implements patchInterface $fpRes = $fpStmt->fetchAll(\PDO::FETCH_ASSOC); foreach ($fpRes as $fpRow) { - $user = $em->getPartialReference('Phraseanet:User', $fpRow['usr_id']); + if (null === $user = $this->loadUser($app['EM'], $fpRow['usr_id'])) { + continue; + } $feedPublisher = new FeedPublisher(); $feedPublisher->setFeed($feed); @@ -179,7 +181,9 @@ class patch_390alpha7a implements patchInterface $ftRes = $ftStmt->fetchAll(\PDO::FETCH_ASSOC); foreach ($ftRes as $ftRow) { - $user = $em->getPartialReference('Phraseanet:User', $ftRow['usr_id']); + if (null === $user = $this->loadUser($app['EM'], $ftRow['usr_id'])) { + continue; + } $token = new FeedToken(); $token->setFeed($feed); @@ -208,7 +212,9 @@ class patch_390alpha7a implements patchInterface $faRes = $faStmt->fetchAll(\PDO::FETCH_ASSOC); foreach ($faRes as $faRow) { - $user = $em->getPartialReference('Phraseanet:User', $faRow['usr_id']); + if (null === $user = $this->loadUser($app['EM'], $faRow['usr_id'])) { + continue; + } $token = new AggregateToken(); $token->setUser($user); diff --git a/lib/classes/patchAbstract.php b/lib/classes/patchAbstract.php new file mode 100644 index 0000000000..ec094e4f10 --- /dev/null +++ b/lib/classes/patchAbstract.php @@ -0,0 +1,28 @@ +createQuery('SELECT PARTIAL u.{id} FROM Phraseanet:User u WHERE u.id = :id') + ->setParameters(['id' => $usrId]) + ->setHint(Query::HINT_FORCE_PARTIAL_LOAD, true) + ->getSingleResult(); + } catch (NoResultException $e) { + + } + } +}