SHARE
TWEET

Untitled

a guest Oct 21st, 2019 69 Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. <?php
  2.  
  3. use Doctrine\DBAL\Connection;
  4. use Symfony\Component\Console\Helper\ProgressBar;
  5. use Symfony\Component\Console\Output\ConsoleOutput;
  6. use Symfony\Component\Console\Output\OutputInterface;
  7. use Symfony\Component\DependencyInjection\ContainerInterface;
  8.  
  9. /**
  10.  * Class RemoveDoubleRoleLimitations.
  11.  *
  12.  * Script that remove all limitation doublons
  13.  */
  14. class RemoveDoubleRoleLimitations implements \Kaliop\eZMigrationBundle\API\MigrationInterface
  15. {
  16.     /** @var Connection */
  17.     protected $dbHandler;
  18.  
  19.     /** @var OutputInterface */
  20.     protected $output;
  21.  
  22.     /**
  23.      * RemoveDoubleRoleLimitations constructor.
  24.      * @param Connection $dbHandler
  25.      * @param OutputInterface $output
  26.      */
  27.     public function __construct( Connection $dbHandler, OutputInterface $output )
  28.     {
  29.         $this->dbHandler = $dbHandler;
  30.         $this->output = $output;
  31.     }
  32.  
  33.     /**
  34.      * @param ContainerInterface $container
  35.      */
  36.     public static function execute( ContainerInterface $container )
  37.     {
  38.         $command = new self(
  39.             $container->get( 'ezpublish.persistence.connection' ),
  40.             new ConsoleOutput()
  41.         );
  42.  
  43.         return $command->run();
  44.     }
  45.  
  46.     /**
  47.      * {@inheritdoc}
  48.      */
  49.     protected function run()
  50.     {
  51.         $i = 0;
  52.         $querySelectDoublon = $this->getQuerySelectDoublon();
  53.  
  54.         $statementSelect = $this->dbHandler->prepare($querySelectDoublon);
  55.         $statementSelect->execute();
  56.         $rows = $statementSelect->fetchAll( \PDO::FETCH_ASSOC );
  57.  
  58.         while(count($rows) > 0) {
  59.             $i += count($rows);
  60.  
  61.             $this->deleteLimitations($rows);
  62.  
  63.             $this->output->writeln($i);
  64.  
  65.             $statementSelect = $this->dbHandler->prepare($this->getQuerySelectDoublon());
  66.             $statementSelect->execute();
  67.             $rows = $statementSelect->fetchAll( \PDO::FETCH_ASSOC );
  68.         }
  69.     }
  70.  
  71.  
  72.     /**
  73.      * Get query for limitation doublons
  74.      *
  75.      * @return string
  76.      */
  77.     private function getQuerySelectDoublon(){
  78.         return 'SELECT
  79.               v1.id
  80.             FROM ezpolicy_limitation_value v1
  81.                    INNER JOIN (
  82.               SELECT v2.limitation_id,
  83.                      count(value) as count,
  84.                      value
  85.               FROM ezpolicy_limitation_value v2
  86.               GROUP BY limitation_id, value
  87.               HAVING COUNT(value) > 1
  88.               ORDER BY limitation_id, id
  89.             ) doublon_limitation_id ON doublon_limitation_id.limitation_id = v1.limitation_id AND doublon_limitation_id.value = v1.value
  90.             WHERE v1.id not in (
  91.               SELECT v3.id
  92.               FROM ezpolicy_limitation_value v3
  93.               GROUP BY limitation_id, value
  94.               HAVING COUNT(value) > 1
  95.               ORDER BY limitation_id, id
  96.             )
  97.             ORDER BY v1.limitation_id, v1.value, v1.id
  98.             LIMIT 500';
  99.     }
  100.  
  101.     /**
  102.      * Remove limitation Ids
  103.      *
  104.      * @param array $limitationIds
  105.      */
  106.     private function deleteLimitations(array $limitationIds) {
  107.         if(is_null($limitationIds)) {
  108.             $this->output->writeln('no limitation');
  109.             return;
  110.         }
  111.  
  112.         $progress = new ProgressBar( $this->output );
  113.         $this->dbHandler->beginTransaction();
  114.  
  115.         $progress->start( count($limitationIds) );
  116.  
  117.         foreach($limitationIds as $limitation) {
  118.             $queryDelete = sprintf('DELETE FROM ezpolicy_limitation_value WHERE id = %s', $limitation['id']);
  119.  
  120.             $statementDelete = $this->dbHandler->prepare($queryDelete);
  121.             $statementDelete->execute();
  122.  
  123.             $progress->advance();
  124.         }
  125.         $this->dbHandler->commit();
  126.         $progress->finish();
  127.         $this->output->writeln('');
  128.     }
  129. }
RAW Paste Data
We use cookies for various purposes including analytics. By continuing to use Pastebin, you agree to our use of cookies as described in the Cookies Policy. OK, I Understand
 
Top