Skip to content

Commit

Permalink
Fixups after CR part 1
Browse files Browse the repository at this point in the history
  • Loading branch information
mateuszbieniek committed Jun 18, 2019
1 parent 676f2c4 commit af7ea23
Show file tree
Hide file tree
Showing 5 changed files with 224 additions and 86 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -8,9 +8,15 @@
*/
namespace eZ\Bundle\EzPublishMigrationBundle\Command\LegacyStorage;

use eZ\Bundle\EzPublishCoreBundle\DependencyInjection\Configuration\ConfigResolver;
use Doctrine\DBAL\Connection;
use Exception;
use eZ\Bundle\EzPublishCoreBundle\DependencyInjection\Configuration\ChainConfigResolver;
use eZ\Publish\Core\MVC\Symfony\SiteAccess;
use eZ\Publish\Core\Persistence\Database\DatabaseHandler;
use eZ\Publish\Core\Persistence\Legacy\Content\Gateway as ContentGateway;
use eZ\Publish\Core\FieldType\Image\ImageStorage\Gateway as ImageGateway;
use Symfony\Component\Console\Command\Command;
use Symfony\Component\Process\Process;
use Symfony\Bundle\FrameworkBundle\Command\ContainerAwareCommand;
use Symfony\Component\Console\Input\InputInterface;
use Symfony\Component\Console\Output\OutputInterface;
use Symfony\Component\Console\Input\InputArgument;
Expand All @@ -22,44 +28,55 @@
use DOMDocument;
use Symfony\Component\Console\Exception\RuntimeException;

set_error_handler(function ($errno, $errstr, $errfile, $errline, array $errcontext) {
if (0 === error_reporting()) {
return false;
}

throw new RuntimeException($errstr, 0);
});

class FixImagesVarDirCommand extends ContainerAwareCommand
class FixImagesVarDirCommand extends Command
{
const DEFAULT_ITERATION_COUNT = 100;
const STORAGE_IMAGES_PATH = '/storage/images/';

/**
* @var int
* @var \eZ\Publish\Core\Persistence\Database\DatabaseHandler
*/
protected $done = 0;

private $db;
/**
* @var string
* @var \eZ\Publish\Core\Persistence\Legacy\Content\Gateway
*/
private $phpPath;
private $contentGateway;

/**
* @var bool
* @var \eZ\Publish\Core\FieldType\Image\ImageStorage\Gateway
*/
private $dryRun;
private $imageGateway;

/**
* @var bool
* @var \eZ\Bundle\EzPublishCoreBundle\DependencyInjection\Configuration\ChainConfigResolver
*/
private $moveFiles;
private $configResolver;

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

/**
* @var \eZ\Publish\Core\MVC\Symfony\SiteAccess
*/
private $siteaccess;

/**
* @var int
*/
protected $done = 0;

/**
* @var string
*/
private $phpPath;

/**
* @var bool
*/
private $dryRun;

/**
* @var int
*/
Expand All @@ -70,6 +87,25 @@ class FixImagesVarDirCommand extends ContainerAwareCommand
*/
private $imageAttributes = [];

/**
* @param ChainConfigResolver $configResolver
* @param DatabaseHandler $db
* @param Connection $connection
* @param SiteAccess $siteaccess
* @param ContentGateway $contentGateway
* @param ImageGateway $imageGateway
*/
function __construct(ChainConfigResolver $configResolver, DatabaseHandler $db, Connection $connection, 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;
}

protected function configure()
{
$this
Expand All @@ -83,17 +119,11 @@ protected function configure()
InputOption::VALUE_NONE,
'Execute a dry run'
)
->addOption(
'move-files',
null,
InputOption::VALUE_NONE,
'Move image files between var directories'
)
->addOption(
'iteration-count',
null,
InputArgument::OPTIONAL,
'Limit how much records get updated by single process',
'Limit how many records get updated by single process',
self::DEFAULT_ITERATION_COUNT
)
->setHelp(
Expand All @@ -110,14 +140,10 @@ protected function configure()
);
}

/**
* @param InputInterface $input
* @param OutputInterface $output
*/
protected function initialize(InputInterface $input, OutputInterface $output)
{
/* @var \Doctrine\DBAL\Connection $databaseHandler */
$this->connection = $this->getContainer()->get('ezpublish.api.search_engine.legacy.connection');
parent::initialize($input, $output);
$this->imageGateway->setConnection($this->db);
}

/**
Expand All @@ -129,25 +155,20 @@ protected function execute(InputInterface $input, OutputInterface $output)
{
$iterationCount = (int) $input->getOption('iteration-count');
$this->dryRun = $input->getOption('dry-run');
$this->moveFiles = $input->getOption('move-files');
$consoleScript = $_SERVER['argv'][0];

$siteAccess = $this->getContainer()->get('ezpublish.siteaccess');

/** @var ConfigResolver $configResolver */
$configResolver = $this->getContainer()->get('ezpublish.config.resolver');
$this->varDir = $configResolver->getParameter(
$this->varDir = $this->configResolver->getParameter(
'var_dir',
null,
$siteAccess->name
$this->siteaccess->name
);

if (getenv('INNER_CALL')) {
$this->processImages($iterationCount, $output);
$output->writeln($this->done);
} else {
$output->writeln([
sprintf('Fixing image references using siteaccess %s (var_dir: %s)', $siteAccess->name, $this->varDir),
sprintf('Fixing image references using siteaccess %s (var_dir: %s)', $this->siteaccess->name, $this->varDir),
'Calculating number of Images to fix...',
]);

Expand Down Expand Up @@ -184,6 +205,7 @@ protected function execute(InputInterface $input, OutputInterface $output)
$consoleScript,
$this->getName(),
'--iteration-count=' . $iterationCount,
'--siteaccess=' . $this->siteaccess->name,
];

$process = new Process(
Expand Down Expand Up @@ -214,7 +236,7 @@ protected function execute(InputInterface $input, OutputInterface $output)
* @param int $limit
* @param \Symfony\Component\Console\Output\OutputInterface $output
*/
protected function processImages(int $limit, OutputInterface $output)
protected function processImages($limit, OutputInterface $output)
{
$images = $this->getImagesToFix($limit);

Expand All @@ -229,10 +251,6 @@ protected function processImages(int $limit, OutputInterface $output)

if (!$this->dryRun) {
$this->updateImage($image['id'], $image['contentobject_attribute_id'], $filePath, $newFilePath);

if ($this->moveFiles) {
$this->moveFile($filePath, $newFilePath);
}
}

++$this->done;
Expand All @@ -243,23 +261,13 @@ protected function processImages(int $limit, OutputInterface $output)
}
}

/**
* @todo Implement this(?)
*
* @param string $oldFilePath
* @param string $newFilePath
*/
protected function moveFile(string $oldFilePath, string $newFilePath)
{
}

/**
* @param int $imageId
* @param int $contentObjectAttributeId
* @param string $oldFilePath
* @param string $newFilePath
*/
protected function updateImage(int $imageId, int $contentObjectAttributeId, string $oldFilePath, string $newFilePath)
protected function updateImage($imageId, $contentObjectAttributeId, $oldFilePath, $newFilePath)
{
$query = $this->connection->createQueryBuilder();
$query
Expand All @@ -282,8 +290,8 @@ protected function updateContentObjectAtributes()
$dom = new DOMDocument('1.0', 'utf-8');

try {
$dom->loadXML($attributeObject['data_text']);
} catch (\RuntimeException $e) {
$dom->loadXML('');
} catch (Exception $e) {
continue;
}

Expand Down Expand Up @@ -314,14 +322,23 @@ protected function updateContentObjectAtributes()
* @param int $version
* @param string $dataText
*/
protected function updateContentObjectAtribute(int $id, int $version, string $dataText)
protected function updateContentObjectAtribute($id, $version, $dataText)
{
$query = $this->connection->createQueryBuilder();
$query
->update('ezcontentobject_attribute', 'oa')
->set('oa.data_text', $query->expr()->literal($dataText))
->where('oa.id = ' . $id)
->andWhere('oa.version = ' . $version);
->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();
}
Expand All @@ -331,14 +348,14 @@ protected function updateContentObjectAtribute(int $id, int $version, string $da
*
* @return array
*/
protected function getContentObjectAtrributesById(int $id)
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);
->setParameter('id', $id, PDO::PARAM_INT);
$statement = $query->execute();

return $statement->fetchAll(PDO::FETCH_ASSOC);
Expand All @@ -349,34 +366,17 @@ protected function getContentObjectAtrributesById(int $id)
*
* @return array
*/
protected function getImagesToFix(int $limit)
protected function getImagesToFix($limit)
{
$query = $this->connection->createQueryBuilder();
$query
->select('i.id, i.contentobject_attribute_id, i.filepath')
->from('ezimagefile', 'i')
->where('i.filepath not like :var_dir')
->setParameter('var_dir', $this->varDir . '%')
->setMaxResults($limit);
$statement = $query->execute();

return $statement->fetchAll(PDO::FETCH_ASSOC);
return $this->imageGateway->getImagesOutsidePath('/' . $this->varDir . '/storage/', $limit, 0);
}

/**
* @return int
*/
protected function countImagesToFix()
{
$query = $this->connection->createQueryBuilder();
$query
->select('count(*) as count')
->from('ezimagefile', 'i')
->where('i.filepath not like :var_dir')
->setParameter('var_dir', $this->varDir . '%');
$statement = $query->execute();

return (int) $statement->fetchColumn();
return $this->imageGateway->countImageReferencesOutsidePath('/' . $this->varDir . '/storage/');
}

/**
Expand All @@ -385,7 +385,7 @@ protected function countImagesToFix()
*
* @return \Symfony\Component\Console\Helper\ProgressBar
*/
protected function getProgressBar(int $maxSteps, OutputInterface $output)
protected function getProgressBar($maxSteps, OutputInterface $output)
{
$progressBar = new ProgressBar($output, $maxSteps);
$progressBar->setFormat(
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
<?php

/**
* File containing the EzPublishMigrationExtension class.
*
* @copyright Copyright (C) eZ Systems AS. All rights reserved.
* @license For full copyright and license information view LICENSE file distributed with this source code.
*/
namespace eZ\Bundle\EzPublishMigrationBundle\DependencyInjection;

use Symfony\Component\HttpKernel\DependencyInjection\Extension;
use Symfony\Component\DependencyInjection\ContainerBuilder;
use Symfony\Component\DependencyInjection\Loader\YamlFileLoader;
use Symfony\Component\Config\FileLocator;

class EzPublishMigrationExtension extends Extension
{
public function load(array $configs, ContainerBuilder $container)
{
$loader = new YamlFileLoader(
$container,
new FileLocator(__DIR__ . '/../Resources/config')
);
$loader->load('services.yml');
}
}
15 changes: 15 additions & 0 deletions eZ/Bundle/EzPublishMigrationBundle/Resources/config/services.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
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%"
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"
tags:
- { name: console.command }
Loading

0 comments on commit af7ea23

Please sign in to comment.