Skip to content

Commit

Permalink
[DO NOT MERGE] Duplicate fields command
Browse files Browse the repository at this point in the history
  • Loading branch information
Nattfarinn committed Aug 23, 2024
1 parent 30197d1 commit 544fccd
Show file tree
Hide file tree
Showing 2 changed files with 92 additions and 0 deletions.
8 changes: 8 additions & 0 deletions eZ/Bundle/EzPublishCoreBundle/Resources/config/commands.yml
Original file line number Diff line number Diff line change
Expand Up @@ -70,3 +70,11 @@ services:
$connection: '@ezpublish.persistence.connection'
tags:
- { name: console.command }

Ibexa\Bundle\Core\Command\DuplicateFieldsCommand:
autowire: true
autoconfigure: true
arguments:
$connection: '@ezpublish.persistence.connection'
tags:
- { name: console.command }
84 changes: 84 additions & 0 deletions src/bundle/Core/Command/DuplicateFieldsCommand.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,84 @@
<?php

/**
* @copyright Copyright (C) Ibexa AS. All rights reserved.
* @license For full copyright and license information view LICENSE file distributed with this source code.
*/
declare(strict_types=1);

namespace Ibexa\Bundle\Core\Command;

use Doctrine\DBAL\Connection;
use RuntimeException;
use Symfony\Component\Console\Command\Command;
use Symfony\Component\Console\Input\InputInterface;
use Symfony\Component\Console\Input\InputOption;
use Symfony\Component\Console\Output\OutputInterface;

final class DuplicateFieldsCommand extends Command
{
/** @var \Doctrine\DBAL\Connection */
private $connection;

public function __construct(
Connection $connection
) {
parent::__construct('ibexa:content:duplicate-fields');

$this->connection = $connection;
}

public function configure(): void
{
$this->addOption('count', null, InputOption::VALUE_REQUIRED);
$this->addOption('version-no', null, InputOption::VALUE_REQUIRED);
$this->addOption('attribute-id', null, InputOption::VALUE_REQUIRED);
$this->addOption('content-id', null, InputOption::VALUE_REQUIRED);
$this->addOption('language-id', null, InputOption::VALUE_REQUIRED);
}

protected function execute(InputInterface $input, OutputInterface $output): int
{
$query = $this->connection->createQueryBuilder();

$query
->select('*')
->from('ezcontentobject_attribute')
->andwhere('version = :version')
->andWhere('contentobject_id = :content_id')
->andWhere('contentclassattribute_id = :attribute_id')
->andWhere('language_id = :language_id')
->orderBy('id', 'desc')
->setMaxResults(1);

$attribute = $query->setParameters([
'version' => (int)$input->getOption('version-no'),
'content_id' => (int)$input->getOption('content-id'),
'attribute_id' => (int)$input->getOption('attribute-id'),
'language_id' => (int)$input->getOption('language-id'),
])->execute()->fetchAssociative();

if (false === $attribute) {
throw new RuntimeException('Attribute not found');
}

unset($attribute['id']);

$count = (int)$input->getOption('count');

$query = $this->connection->createQueryBuilder();
$query
->insert('ezcontentobject_attribute');

foreach ($attribute as $key => $value) {
$query->setValue($key, ':' . $key);
$query->setParameter(':' . $key, $value);
}

for ($i = 0; $i < $count; ++$i) {
$query->execute();
}

return Command::SUCCESS;
}
}

0 comments on commit 544fccd

Please sign in to comment.