Command/PurgeTagsCommand.php
changeset 38 e48c2e503945
equal deleted inserted replaced
37:9ba15af20acc 38:e48c2e503945
       
     1 <?php
       
     2 /*
       
     3  * This file is part of the WikiTagBundle package.
       
     4  *
       
     5  * (c) IRI <http://www.iri.centrepompidou.fr/>
       
     6  *
       
     7  * For the full copyright and license information, please view the LICENSE
       
     8  * file that was distributed with this source code.
       
     9  */
       
    10 namespace IRI\Bundle\WikiTagBundle\Command;
       
    11 
       
    12 use Doctrine\ORM\QueryBuilder;
       
    13 use Doctrine\ORM\Query\ResultSetMapping;
       
    14 use Symfony\Bundle\FrameworkBundle\Command\ContainerAwareCommand;
       
    15 use Symfony\Component\Console\Input\InputArgument;
       
    16 use Symfony\Component\Console\Input\InputInterface;
       
    17 use Symfony\Component\Console\Input\InputOption;
       
    18 use Symfony\Component\Console\Output\OutputInterface;
       
    19 
       
    20 class PurgeTagsCommand extends ContainerAwareCommand
       
    21 {
       
    22     protected function configure()
       
    23     {
       
    24         parent::configure();
       
    25     
       
    26         $this
       
    27             ->setName('wikitag:purge-tags')
       
    28             ->setDescription('Purge tags')
       
    29             ->addOption("list","l",InputOption::VALUE_NONE, "List tags tp remove")
       
    30             ->addOption("force","f",InputOption::VALUE_NONE, "Force remove tags");
       
    31     }
       
    32     
       
    33     
       
    34     protected function execute(InputInterface $input, OutputInterface $output)
       
    35     {
       
    36         $force = $input->getOption('force');
       
    37         $list = $input->getOption('list');
       
    38             
       
    39         //get tags with no documents
       
    40         $doctrine = $this->getContainer()->get('doctrine');
       
    41         
       
    42         $qb = $doctrine->getEntityManager()->createQueryBuilder();
       
    43         $qb->select('t');
       
    44         $qb->from('WikiTagBundle:Tag','t');
       
    45         $qb->leftJoin('t.documents', 'dt', 'WITH', 't = dt.tag');
       
    46         $qb->addGroupBy('t.id');
       
    47         $qb->having($qb->expr()->eq($qb->expr()->count('dt.id'),':count'));
       
    48         $qb->setParameter("count", 0);
       
    49 
       
    50 
       
    51         $rsm = new ResultSetMapping();
       
    52         $rsm->addScalarResult("C","C");
       
    53         $count_query = $doctrine->getEntityManager()->createNativeQuery("SELECT COUNT(*) AS C FROM (".$qb->getQuery()->getSQL().") AS T", $rsm);
       
    54         $count_query->setParameter(1, 0);
       
    55         
       
    56         $count = $count_query->getSingleScalarResult();
       
    57         
       
    58         $output->writeln("<comment>$count tag(s) to delete.</comment>\n");
       
    59         
       
    60         if($list)
       
    61         {
       
    62             $query = $qb->getQuery();
       
    63             $result = $query->getResult();
       
    64            
       
    65             $i = 1;
       
    66             foreach($result as $tag)
       
    67             {
       
    68                 $output->writeln(strval($i++)."- ".$tag->getLabel());
       
    69             }
       
    70             $output->writeln("");
       
    71         }
       
    72         else
       
    73         {
       
    74             if(! $force && $input->isInteractive())
       
    75             {
       
    76                 $dialog = $this->getHelper('dialog');
       
    77                 if (!$dialog->askConfirmation($output, '<question>Confirm deletion? (y/N) : </question>', false)) {
       
    78                     return;
       
    79                 }
       
    80             }
       
    81             
       
    82             
       
    83             $id_delete = array();
       
    84             foreach($qb->getQuery()->getResult() as $tag)
       
    85             {
       
    86                 $id_delete[] = $tag->getId();
       
    87             }
       
    88             
       
    89             $delete_qb = $doctrine->getEntityManager()->createQueryBuilder();
       
    90             $delete_qb->delete('WikiTagBundle:Tag','tag');
       
    91             $delete_qb->where($delete_qb->expr()->in('tag.id', $id_delete));
       
    92                         
       
    93             $result = $delete_qb->getQuery()->getResult();
       
    94             
       
    95             $output->writeln("Tag deleted : $result \n");
       
    96             
       
    97         }
       
    98         
       
    99     }
       
   100 }