Skip to content

Commit

Permalink
Fixups after CR vol2
Browse files Browse the repository at this point in the history
  • Loading branch information
mateuszbieniek committed Jul 19, 2019
1 parent af7ea23 commit 3f7b2ba
Show file tree
Hide file tree
Showing 7 changed files with 161 additions and 102 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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
Expand All @@ -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
*/
Expand All @@ -52,11 +51,6 @@ class FixImagesVarDirCommand extends Command
*/
private $configResolver;

/**
* @var \Doctrine\DBAL\Connection
*/
private $connection;

/**
* @var \eZ\Publish\Core\MVC\Symfony\SiteAccess
*/
Expand Down Expand Up @@ -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;
Expand Down Expand Up @@ -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];

Expand Down Expand Up @@ -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);
}
Expand Down Expand Up @@ -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');
Expand Down Expand Up @@ -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
*
Expand Down
Original file line number Diff line number Diff line change
@@ -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"
Expand Down
10 changes: 9 additions & 1 deletion eZ/Publish/Core/FieldType/Image/ImageStorage/Gateway.php
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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.
*/
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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
{
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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();
Expand Down
18 changes: 18 additions & 0 deletions eZ/Publish/Core/Persistence/Legacy/Content/Gateway.php
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}
}
Loading

0 comments on commit 3f7b2ba

Please sign in to comment.