Command/SyncDocumentsCommand.php
changeset 23 b435f8055cb4
parent 6 2dcfef6e75c3
child 24 cd389bf882f1
equal deleted inserted replaced
22:99c15cfe420b 23:b435f8055cb4
    25         parent::configure();
    25         parent::configure();
    26         
    26         
    27         $this
    27         $this
    28             ->setName('wikitag:sync-doc')
    28             ->setName('wikitag:sync-doc')
    29             ->setDescription('Synchronize and index document class')
    29             ->setDescription('Synchronize and index document class')
    30             ->addArgument('class', InputArgument::REQUIRED, 'The document class')
       
    31             ->addOption('clear', 'c', InputOption::VALUE_NONE, "clear all docs");
    30             ->addOption('clear', 'c', InputOption::VALUE_NONE, "clear all docs");
    32     }
    31     }
    33 
    32 
    34     protected function execute(InputInterface $input, OutputInterface $output)
    33     protected function execute(InputInterface $input, OutputInterface $output)
    35     {
    34     {
    36         $class = $input->getArgument('class');
    35         $class = $this->getContainer()->getParameter('wiki_tag.document_class');
    37         $clear = $input->getOption('clear');
    36         $clear = $input->getOption('clear');
    38      
    37      
    39         
    38         
    40         $doctrine = $this->getContainer()->get('doctrine');
    39         $doctrine = $this->getContainer()->get('doctrine');
    41         
    40         
    50         
    49         
    51         //TODO : check class to implement DocumentInterface
    50         //TODO : check class to implement DocumentInterface
    52         //TODO : write progress
    51         //TODO : write progress
    53         $doclist = $rep->findAll();
    52         $doclist = $rep->findAll();
    54         foreach ($doclist as $doc) {
    53         foreach ($doclist as $doc) {
    55             $output->writeln("TITLE : ".$doc->getTitle());
    54             $docrep->writeDocument($doc, $this->getContainer()->getParameter('wiki_tag.document_id_column'), $this->getContainer()->getParameter('wiki_tag.fields'));
    56             $docrep->writeDocument($doc);
       
    57         }
    55         }
    58         $doctrine->getEntityManager()->flush();
    56         $doctrine->getEntityManager()->flush();
    59         
    57         
    60         if($clear) {
    58         if($clear) {
    61             
    59             
    62             $req_ids = $doctrine->getEntityManager()->createQuery("SELECT partial doc.{id} FROM $class doc");
    60             $req = $doctrine->getEntityManager()->createQuery("DELETE WikiTagBundle:Document wtdoc WHERE wtdoc.externalId NOT IN (SELECT doc FROM $class doc)");
    63             $doc_ids = array();
    61             $req->getResult();
    64             foreach($req_ids->getResult(Query::HYDRATE_SCALAR) as $doc_id) {
       
    65                 $doc_ids[] = strval($doc_id['doc_id']);
       
    66             }
       
    67             
       
    68             $req = $doctrine->getEntityManager()->createQuery("SELECT wtdoc FROM WikiTagBundle:Document wtdoc WHERE wtdoc.externalId NOT IN (:doc_ids)");
       
    69             $req->setParameter('doc_ids', $doc_ids);
       
    70             foreach ($req->getResult() as $wtdoc) {
       
    71                 $output->writeln("DELETE : ".$wtdoc->getId());
       
    72                 $doctrine->getEntityManager()->remove($wtdoc);
       
    73             }
       
    74             $doctrine->getEntityManager()->flush();
    62             $doctrine->getEntityManager()->flush();
    75         }
    63         }
    76 
    64 
    77         $output->writeln(strval(count($doclist)) ." documents imported.");
    65         $output->writeln(strval(count($doclist)) ." documents imported.");
    78     }
    66     }