Command/SyncDocumentsCommand.php
changeset 27 8551d844b4f3
parent 24 cd389bf882f1
child 34 21fab44f46fe
equal deleted inserted replaced
25:11fd79666374 27:8551d844b4f3
    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             ->addOption('missing', 'm', InputOption::VALUE_NONE, "process missing")
       
    31             ->addOption('tags', 't', InputOption::VALUE_NONE, "update tags")
    30             ->addOption('clear', 'c', InputOption::VALUE_NONE, "clear all docs");
    32             ->addOption('clear', 'c', InputOption::VALUE_NONE, "clear all docs");
    31     }
    33     }
    32 
    34 
    33     protected function execute(InputInterface $input, OutputInterface $output)
    35     protected function execute(InputInterface $input, OutputInterface $output)
    34     {
    36     {
    35         $class = $this->getContainer()->getParameter('wiki_tag.document_class');
    37         $class = $this->getContainer()->getParameter('wiki_tag.document_class');
    36         $clear = $input->getOption('clear');
    38         $clear = $input->getOption('clear');
       
    39         $missing = $input->getOption('missing');
    37      
    40      
    38         
    41         
    39         $doctrine = $this->getContainer()->get('doctrine');
    42         $doctrine = $this->getContainer()->get('doctrine');
    40         
    43         
    41         $docrep = $doctrine->getRepository('WikiTagBundle:Document');
    44         $docrep = $doctrine->getRepository('WikiTagBundle:Document');
    45             //TODO : translate
    48             //TODO : translate
    46             $output->writeln("$class does not have a repository : exiting.");
    49             $output->writeln("$class does not have a repository : exiting.");
    47             return ;
    50             return ;
    48         }
    51         }
    49         
    52         
       
    53         if($input->getOption('tags'))
       
    54         {
       
    55             if(!$missing)
       
    56             {
       
    57                 $docquery = $doctrine->getEntityManager()->createQuery("SELECT doc from WikiTagBundle:Document doc");
       
    58                 $doccountquery = $doctrine->getEntityManager()->createQuery("SELECT COUNT(doc.id) from WikiTagBundle:Document doc");
       
    59             }
       
    60             else
       
    61             {
       
    62                 $docquery = $doctrine->getEntityManager()->createQuery("SELECT doc from WikiTagBundle:Document doc WHERE doc.tagsStr IS NULL");
       
    63                 $doccountquery = $doctrine->getEntityManager()->createQuery("SELECT COUNT(doc.id) from WikiTagBundle:Document doc WHERE doc.tagsStr IS NULL");
       
    64             }
       
    65             
       
    66             
       
    67             $total = $doccountquery->getSingleScalarResult();
       
    68             $done = 0;
       
    69             $iterable = $docquery->iterate();
       
    70             $todetach = array();
       
    71             while (($row = $iterable->next()) !== false) {
       
    72                 $done++;
       
    73                 $memory = ((($done%10)==0)?" - mem: ".strval(memory_get_usage(true)):"");
       
    74                 $doc = $row[0];
       
    75                 $todetach[] = $doc;
       
    76                 $output->writeln("Process doc id ".$doc->getId()." $done/$total ".strval(intval(floatval($done)/floatval($total)*100.0))."%$memory");
       
    77                 $docrep->updateTagsStr($doc);
       
    78                 if($done%10 == 0)
       
    79                 {
       
    80                     $doctrine->getEntityManager()->flush();
       
    81                     foreach($todetach as $obj)
       
    82                     {
       
    83                         $doctrine->getEntityManager()->detach($obj);
       
    84                     }
       
    85                     $todetach = array();
       
    86                 }
       
    87             }
       
    88             $doctrine->getEntityManager()->flush();
       
    89                         
       
    90             return;
       
    91             
       
    92         }
       
    93         
    50         //TODO : check class to implement DocumentInterface
    94         //TODO : check class to implement DocumentInterface
    51         //TODO : write progress
    95         //TODO : write progress
    52         $doclist = $rep->findAll();
    96         if($missing)
    53         $total = count($doclist);
    97         {
       
    98             $docquery = $doctrine->getEntityManager()->createQuery("SELECT doc FROM $class doc WHERE doc.id not in (SELECT wtdoc FROM WikiTagBundle:Document wtdoc)");
       
    99             $doccountquery = $doctrine->getEntityManager()->createQuery("SELECT count(doc.id) FROM $class doc WHERE doc.id not in (SELECT wtdoc FROM WikiTagBundle:Document wtdoc)");
       
   100             //$doclist = $doctrine->getEntityManager()->createQuery("SELECT doc FROM $class doc WHERE doc.id not in (SELECT wtdoc FROM WikiTagBundle:Document wtdoc)")->getResult();
       
   101         }
       
   102         else
       
   103         {
       
   104             $docquery = $doctrine->getEntityManager()->createQuery("SELECT doc FROM $class doc");
       
   105             $doccountquery = $doctrine->getEntityManager()->createQuery("SELECT count(doc.id) FROM $class doc");
       
   106             //$doclist = $rep->findAll();
       
   107         }
       
   108         $total = $doccountquery->getSingleScalarResult();
    54         $done = 0;
   109         $done = 0;
    55         foreach ($doclist as $doc) {
   110         $iterable = $docquery->iterate();
       
   111         while (($row = $iterable->next()) !== false) {
    56             $done++;
   112             $done++;
    57             $output->writeln("Process doc id ".$doc->getId()." $done/$total ".strval(intval(floatval($done)/floatval($total)*100.0))."%");
   113             $doc = $row[0];
       
   114             $memory = ((($done%10)==0)?" - mem: ".strval(memory_get_usage(true)):"");
       
   115             $output->writeln("Process doc id ".$doc->getId()." $done/$total ".strval(intval(floatval($done)/floatval($total)*100.0))."%$memory");
    58             $docrep->writeDocument($doc, $this->getContainer()->getParameter('wiki_tag.document_id_column'), $this->getContainer()->getParameter('wiki_tag.fields'));
   116             $docrep->writeDocument($doc, $this->getContainer()->getParameter('wiki_tag.document_id_column'), $this->getContainer()->getParameter('wiki_tag.fields'));
       
   117             if($done%10 == 0)
       
   118             {
       
   119                 $doctrine->getEntityManager()->flush();
       
   120                 $doctrine->getEntityManager()->clear();
       
   121             }
    59         }
   122         }
    60         $doctrine->getEntityManager()->flush();
   123         $doctrine->getEntityManager()->flush();
    61         
   124         
    62         if($clear) {
   125         if($clear) {
    63             
   126