diff --git a/eZ/Bundle/EzPublishMigrationBundle/Command/LegacyStorage/FixImagesVarDirCommand.php b/eZ/Bundle/EzPublishMigrationBundle/Command/LegacyStorage/FixImagesVarDirCommand.php index 6e40798ebc6..010a0bfab3f 100644 --- a/eZ/Bundle/EzPublishMigrationBundle/Command/LegacyStorage/FixImagesVarDirCommand.php +++ b/eZ/Bundle/EzPublishMigrationBundle/Command/LegacyStorage/FixImagesVarDirCommand.php @@ -8,7 +8,7 @@ */ namespace eZ\Bundle\EzPublishMigrationBundle\Command\LegacyStorage; -use Doctrine\DBAL\Connection; +use DOMDocument; use Exception; use eZ\Bundle\EzPublishCoreBundle\DependencyInjection\Configuration\ChainConfigResolver; use eZ\Publish\Core\MVC\Symfony\SiteAccess; @@ -24,8 +24,6 @@ use Symfony\Component\Console\Helper\ProgressBar; use Symfony\Component\Console\Question\ConfirmationQuestion; use Symfony\Component\Process\PhpExecutableFinder; -use PDO; -use DOMDocument; use Symfony\Component\Console\Exception\RuntimeException; class FixImagesVarDirCommand extends Command @@ -37,6 +35,7 @@ class FixImagesVarDirCommand extends Command * @var \eZ\Publish\Core\Persistence\Database\DatabaseHandler */ private $db; + /** * @var \eZ\Publish\Core\Persistence\Legacy\Content\Gateway */ @@ -52,11 +51,6 @@ class FixImagesVarDirCommand extends Command */ private $configResolver; - /** - * @var \Doctrine\DBAL\Connection - */ - private $connection; - /** * @var \eZ\Publish\Core\MVC\Symfony\SiteAccess */ @@ -88,19 +82,22 @@ class FixImagesVarDirCommand extends Command private $imageAttributes = []; /** - * @param ChainConfigResolver $configResolver - * @param DatabaseHandler $db - * @param Connection $connection - * @param SiteAccess $siteaccess - * @param ContentGateway $contentGateway - * @param ImageGateway $imageGateway + * @param \eZ\Bundle\EzPublishCoreBundle\DependencyInjection\Configuration\ChainConfigResolver; $configResolver + * @param \eZ\Publish\Core\Persistence\Database\DatabaseHandler $db + * @param \eZ\Publish\Core\MVC\Symfony\SiteAccess $siteaccess + * @param \eZ\Publish\Core\Persistence\Legacy\Content\Gateway $contentGateway + * @param \eZ\Publish\Core\FieldType\Image\ImageStorage\Gateway $imageGateway */ - function __construct(ChainConfigResolver $configResolver, DatabaseHandler $db, Connection $connection, SiteAccess $siteaccess, ContentGateway $contentGateway, ImageGateway $imageGateway) - { + public function __construct( + ChainConfigResolver $configResolver, + DatabaseHandler $db, + SiteAccess $siteaccess, + ContentGateway $contentGateway, + ImageGateway $imageGateway + ) { parent::__construct(); $this->db = $db; $this->configResolver = $configResolver; - $this->connection = $connection; $this->siteaccess = $siteaccess; $this->contentGateway = $contentGateway; $this->imageGateway = $imageGateway; @@ -153,7 +150,7 @@ protected function initialize(InputInterface $input, OutputInterface $output) */ protected function execute(InputInterface $input, OutputInterface $output) { - $iterationCount = (int) $input->getOption('iteration-count'); + $iterationCount = (int)$input->getOption('iteration-count'); $this->dryRun = $input->getOption('dry-run'); $consoleScript = $_SERVER['argv'][0]; @@ -219,7 +216,7 @@ protected function execute(InputInterface $input, OutputInterface $output) throw new RuntimeException($process->getErrorOutput()); } - $doneInProcess = (int) $process->getOutput(); + $doneInProcess = (int)$process->getOutput(); $this->done += $doneInProcess; $progressBar->advance($doneInProcess); } @@ -269,22 +266,14 @@ protected function processImages($limit, OutputInterface $output) */ protected function updateImage($imageId, $contentObjectAttributeId, $oldFilePath, $newFilePath) { - $query = $this->connection->createQueryBuilder(); - $query - ->update('ezimagefile', 'i') - ->set('i.filepath', $query->expr()->literal($newFilePath)) - ->where('i.id = :id') - ->setParameter('id', $imageId); - - $query->execute(); - + $this->imageGateway->updateImageFilePath($imageId, $newFilePath); $this->imageAttributes[$contentObjectAttributeId][$oldFilePath] = $newFilePath; } protected function updateContentObjectAtributes() { foreach ($this->imageAttributes as $attributeId => $files) { - $attributeObjects = $this->getContentObjectAtrributesById($attributeId); + $attributeObjects = $this->contentGateway->getContentObjectAttributesById($attributeId); foreach ($attributeObjects as $attributeObject) { $dom = new DOMDocument('1.0', 'utf-8'); @@ -312,55 +301,11 @@ protected function updateContentObjectAtributes() } } - $this->updateContentObjectAtribute($attributeObject['id'], $attributeObject['version'], $dom->saveXML()); + $this->contentGateway->updateContentObjectAtribute($attributeObject['id'], $attributeObject['version'], $dom->saveXML()); } } } - /** - * @param int $id - * @param int $version - * @param string $dataText - */ - protected function updateContentObjectAtribute($id, $version, $dataText) - { - $query = $this->connection->createQueryBuilder(); - $query - ->update('ezcontentobject_attribute', 'oa') - ->set('oa.data_text', ':text') - ->where('oa.id = :id') - ->andWhere('oa.version = :version') - ->setParameters([ - 'text' => $dataText, - 'id' => $id, - 'version' => $version, - ],[ - 'text' => PDO::PARAM_STR, - 'id' => PDO::PARAM_INT, - 'version' => PDO::PARAM_INT, - ]); - - $query->execute(); - } - - /** - * @param int $id - * - * @return array - */ - protected function getContentObjectAtrributesById($id) - { - $query = $this->connection->createQueryBuilder(); - $query - ->select('oa.data_text, oa.id, oa.version') - ->from('ezcontentobject_attribute', 'oa') - ->where('oa.id = :id') - ->setParameter('id', $id, PDO::PARAM_INT); - $statement = $query->execute(); - - return $statement->fetchAll(PDO::FETCH_ASSOC); - } - /** * @param int $limit * diff --git a/eZ/Bundle/EzPublishMigrationBundle/Resources/config/services.yml b/eZ/Bundle/EzPublishMigrationBundle/Resources/config/services.yml index b14df843b39..3fb35be92e7 100644 --- a/eZ/Bundle/EzPublishMigrationBundle/Resources/config/services.yml +++ b/eZ/Bundle/EzPublishMigrationBundle/Resources/config/services.yml @@ -1,13 +1,9 @@ -parameters: - ezpublish.migration.fix_images_var_dir.class: eZ\Bundle\EzPublishMigrationBundle\Command\LegacyStorage\FixImagesVarDirCommand - services: ezpublish.migration.fix_images_var_dir: - class: "%ezpublish.migration.fix_images_var_dir.class%" + class: eZ\Bundle\EzPublishMigrationBundle\Command\LegacyStorage\FixImagesVarDirCommand arguments: - "@ezpublish.config.resolver" - "@ezpublish.api.storage_engine.legacy.dbhandler" - - "@ezpublish.api.search_engine.legacy.connection" - "@ezpublish.siteaccess" - "@ezpublish.persistence.legacy.content.gateway" - "@ezpublish.fieldType.ezimage.storage_gateway" diff --git a/eZ/Publish/Core/FieldType/Image/ImageStorage/Gateway.php b/eZ/Publish/Core/FieldType/Image/ImageStorage/Gateway.php index 0572007abca..acb2cbd549b 100644 --- a/eZ/Publish/Core/FieldType/Image/ImageStorage/Gateway.php +++ b/eZ/Publish/Core/FieldType/Image/ImageStorage/Gateway.php @@ -68,7 +68,7 @@ abstract public function countImageReferences($uri); abstract public function countImageReferencesOutsidePath($uri); /** - * Return references outside of the given $path + * Return references outside of the given $path. * * @param string $uri File IO uri (not legacy) * @param int $limit @@ -78,6 +78,14 @@ abstract public function countImageReferencesOutsidePath($uri); */ abstract public function getImagesOutsidePath($uri, $limit = null, $offset = 0); + /** + * Updates the filepath of given Image. + * + * @param $imageId + * @param $newFilePath + */ + abstract public function updateImageFilePath($imageId, $newFilePath); + /** * Returns the public uris for the images stored in $xml. */ diff --git a/eZ/Publish/Core/FieldType/Image/ImageStorage/Gateway/LegacyStorage.php b/eZ/Publish/Core/FieldType/Image/ImageStorage/Gateway/LegacyStorage.php index 34da2f1ba52..a21cfc925a8 100644 --- a/eZ/Publish/Core/FieldType/Image/ImageStorage/Gateway/LegacyStorage.php +++ b/eZ/Publish/Core/FieldType/Image/ImageStorage/Gateway/LegacyStorage.php @@ -11,7 +11,6 @@ use eZ\Publish\Core\IO\UrlRedecorator; use eZ\Publish\SPI\Persistence\Content\VersionInfo; use eZ\Publish\Core\FieldType\Image\ImageStorage\Gateway; -use PDO; class LegacyStorage extends Gateway { @@ -268,32 +267,41 @@ public function countImageReferencesOutsidePath($uri) { $path = $this->redecorator->redecorateFromSource($uri); - $connection = $this->getConnection(); + $connection = $this->getConnection()->getConnection(); - $selectQuery = $connection->createSelectQuery(); - $selectQuery->select( - $selectQuery->expr->count( - $connection->quoteColumn('id') - ) - )->from( - $connection->quoteTable('ezimagefile') - )->where( - $selectQuery->expr->not( - $selectQuery->expr->like( - $connection->quoteColumn('filepath'), - $selectQuery->bindValue($path . '%') - ) - ) - ); - - $statement = $selectQuery->prepare(); - $statement->execute(); + $queryBuilder = $connection->createQueryBuilder(); + $statement = $queryBuilder + ->select('count(i.id)') + ->from('ezimagefile', 'i') + ->where('i.filepath not like :path') + ->setParameter('path', $path . '%') + ->execute(); return (int)$statement->fetchColumn(); } /** - * Return references outside of the given $path + * Updates the filepath of given Image. + * + * @param $imageId + * @param $newFilePath + */ + public function updateImageFilePath($imageId, $newFilePath) + { + $connection = $this->getConnection()->getConnection(); + + $query = $connection->createQueryBuilder(); + $query + ->update('ezimagefile', 'i') + ->set('i.filepath', $query->expr()->literal($newFilePath)) + ->where('i.id = :id') + ->setParameter('id', $imageId); + + $query->execute(); + } + + /** + * Return references outside of the given $path. * * @param string $uri File IO uri (not legacy) * @param int $limit @@ -324,7 +332,7 @@ public function getImagesOutsidePath($uri, $limit = null, $offset = 0) ); if ($limit && $offset) { - $selectQuery->limit($limit, $offset); + $selectQuery->limit($limit, $offset); } $statement = $selectQuery->prepare(); diff --git a/eZ/Publish/Core/Persistence/Legacy/Content/Gateway.php b/eZ/Publish/Core/Persistence/Legacy/Content/Gateway.php index f02bff9f7b5..11a017428cf 100644 --- a/eZ/Publish/Core/Persistence/Legacy/Content/Gateway.php +++ b/eZ/Publish/Core/Persistence/Legacy/Content/Gateway.php @@ -428,4 +428,22 @@ abstract public function loadVersionedNameData($rows); * @param int|null $versionNo If specified only copy for a given version number, otherwise all. */ abstract public function copyRelations($originalContentId, $copiedContentId, $versionNo = null); + + /** + * Updates Content's attribute text value. + * + * @param int $attributeId + * @param int $version + * @param string $text + */ + abstract public function updateContentObjectAttributeText($attributeId, $version, $text); + + /** + * Returns an array containing all content attributes with the specified id. + * + * @param int $id + * + * @return array + */ + abstract public function getContentObjectAttributesById($id); } diff --git a/eZ/Publish/Core/Persistence/Legacy/Content/Gateway/DoctrineDatabase.php b/eZ/Publish/Core/Persistence/Legacy/Content/Gateway/DoctrineDatabase.php index 7438b03b57b..d36e544891f 100644 --- a/eZ/Publish/Core/Persistence/Legacy/Content/Gateway/DoctrineDatabase.php +++ b/eZ/Publish/Core/Persistence/Legacy/Content/Gateway/DoctrineDatabase.php @@ -2065,4 +2065,52 @@ public function copyRelations($originalContentId, $copiedContentId, $versionNo = $stmt->execute(); } + + /** + * Updates Content's attribute text value. + * + * @param int $attributeId + * @param int $version + * @param string $text + */ + public function updateContentObjectAttributeText($attributeId, $version, $text) + { + $query = $this->connection->createQueryBuilder(); + $query + ->update('ezcontentobject_attribute', 'oa') + ->set('oa.data_text', ':text') + ->where('oa.id = :id') + ->andWhere('oa.version = :version') + ->setParameters([ + 'text' => $text, + 'id' => $attributeId, + 'version' => $version, + ], [ + 'text' => PDO::PARAM_STR, + 'id' => PDO::PARAM_INT, + 'version' => PDO::PARAM_INT, + ]); + + $query->execute(); + } + + /** + * Returns an array containing all content attributes with the specified id. + * + * @param int $id + * + * @return array + */ + public function getContentObjectAttributesById($id) + { + $query = $this->connection->createQueryBuilder(); + $query + ->select('oa.data_text, oa.id, oa.version') + ->from('ezcontentobject_attribute', 'oa') + ->where('oa.id = :id') + ->setParameter('id', $id, PDO::PARAM_INT); + $statement = $query->execute(); + + return $statement->fetchAll(PDO::FETCH_ASSOC); + } } diff --git a/eZ/Publish/Core/Persistence/Legacy/Content/Gateway/ExceptionConversion.php b/eZ/Publish/Core/Persistence/Legacy/Content/Gateway/ExceptionConversion.php index ece3f044ed7..ac3412327ee 100644 --- a/eZ/Publish/Core/Persistence/Legacy/Content/Gateway/ExceptionConversion.php +++ b/eZ/Publish/Core/Persistence/Legacy/Content/Gateway/ExceptionConversion.php @@ -769,4 +769,40 @@ public function copyRelations($originalContentId, $copiedContentId, $versionNo = throw new RuntimeException('Database error', 0, $e); } } + + /** + * Updates Content's attribute text value. + * + * @param int $attributeId + * @param int $version + * @param string $text + */ + public function updateContentObjectAttributeText($attributeId, $version, $text) + { + try { + return $this->innerGateway->updateContentObjectAttributeText($attributeId, $version, $text); + } catch (DBALException $e) { + throw new RuntimeException('Database error', 0, $e); + } catch (PDOException $e) { + throw new RuntimeException('Database error', 0, $e); + } + } + + /** + * Returns an array containing all content attributes with the specified id. + * + * @param int $id + * + * @return array + */ + public function getContentObjectAttributesById($id) + { + try { + return $this->innerGateway->getContentObjectAttributesById($id); + } catch (DBALException $e) { + throw new RuntimeException('Database error', 0, $e); + } catch (PDOException $e) { + throw new RuntimeException('Database error', 0, $e); + } + } }