Command/ReorderTagsCommand.php
changeset 30 d2fba1e3b94b
child 34 21fab44f46fe
equal deleted inserted replaced
29:7496254cfead 30:d2fba1e3b94b
       
     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 
       
    11 namespace IRI\Bundle\WikiTagBundle\Command;
       
    12 
       
    13 use Symfony\Bundle\FrameworkBundle\Command\ContainerAwareCommand;
       
    14 use Symfony\Component\Console\Input\InputArgument;
       
    15 use Symfony\Component\Console\Input\InputInterface;
       
    16 use Symfony\Component\Console\Input\InputOption;
       
    17 use Symfony\Component\Console\Output\OutputInterface;
       
    18 
       
    19 class ReorderTagsCommand extends ContainerAwareCommand
       
    20 {
       
    21     
       
    22     protected function configure()
       
    23     {
       
    24         parent::configure();
       
    25     
       
    26         $this
       
    27         ->setName('wikitag:reorder-tags')
       
    28         ->setDescription('Reorder tags')
       
    29         ->addOption("force","f",InputOption::VALUE_NONE, "Force all reorder");
       
    30     }
       
    31     
       
    32     
       
    33     protected function execute(InputInterface $input, OutputInterface $output)
       
    34     {
       
    35         $doctrine = $this->getContainer()->get('doctrine');
       
    36         $configuration = $doctrine->getConnection()->getConfiguration();
       
    37         $configuration->setSQLLogger(null);
       
    38         
       
    39         $force = $input->getOption('force');
       
    40         
       
    41         if($force)
       
    42         {
       
    43             $query = $doctrine->getEntityManager()->createQuery("SELECT doc from WikiTagBundle:Document doc");
       
    44             $querycount = $doctrine->getEntityManager()->createQuery("SELECT count(doc) from WikiTagBundle:Document doc");
       
    45         }
       
    46         else
       
    47         {
       
    48             $query = $doctrine->getEntityManager()->createQuery("SELECT doc from WikiTagBundle:Document doc WHERE doc.manualOrder = FALSE");
       
    49             $querycount = $doctrine->getEntityManager()->createQuery("SELECT count(doc) from WikiTagBundle:Document doc WHERE doc.manualOrder = FALSE");
       
    50         }
       
    51         
       
    52         $total = $querycount->getSingleScalarResult();
       
    53         $search_service = $this->getContainer()->get('wiki_tag.search');
       
    54         
       
    55         $done = 0;
       
    56         $iterable = $query->iterate();
       
    57         $todetach = array();
       
    58         while (($row = $iterable->next()) !== false)
       
    59         {
       
    60             $done++;
       
    61             $doc = $row[0];
       
    62             
       
    63             $output->writeln("Process doc id ".$doc->getId()." $done/$total ".strval(intval(floatval($done)/floatval($total)*100.0))."%");
       
    64             
       
    65             $doc->setManualOrder(false);
       
    66             $doctrine->getEntityManager()->persist($doc);
       
    67             
       
    68             $search_service->reorderTagsForDocument($doc);
       
    69             $todetach[] = $doc;
       
    70             
       
    71             
       
    72             if($done%100 == 0)
       
    73             {
       
    74                 $doctrine->getEntityManager()->flush();
       
    75                 $doctrine->getEntityManager()->clear();
       
    76                 /*foreach($todetach as $obj)
       
    77                 {
       
    78                     $doctrine->getEntityManager()->detach($obj);
       
    79                 }*/
       
    80                 $todetach = array();
       
    81                 $output->writeln("memory : ".strval(memory_get_usage(true)));
       
    82             }
       
    83             
       
    84         }
       
    85         
       
    86     }
       
    87 
       
    88 }