Command/SyncDocumentsCommand.php
author ymh <ymh.work@gmail.com>
Thu, 17 Nov 2011 11:29:26 +0100
changeset 34 21fab44f46fe
parent 27 8551d844b4f3
child 42 0e57c730bb18
permissions -rwxr-xr-x
add event on tag changed
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
6
2dcfef6e75c3 add command to sync documents
ymh <ymh.work@gmail.com>
parents:
diff changeset
     1
<?php
2dcfef6e75c3 add command to sync documents
ymh <ymh.work@gmail.com>
parents:
diff changeset
     2
/*
2dcfef6e75c3 add command to sync documents
ymh <ymh.work@gmail.com>
parents:
diff changeset
     3
 * This file is part of the WikiTagBundle package.
2dcfef6e75c3 add command to sync documents
ymh <ymh.work@gmail.com>
parents:
diff changeset
     4
 *
2dcfef6e75c3 add command to sync documents
ymh <ymh.work@gmail.com>
parents:
diff changeset
     5
 * (c) IRI <http://www.iri.centrepompidou.fr/>
2dcfef6e75c3 add command to sync documents
ymh <ymh.work@gmail.com>
parents:
diff changeset
     6
 *
2dcfef6e75c3 add command to sync documents
ymh <ymh.work@gmail.com>
parents:
diff changeset
     7
 * For the full copyright and license information, please view the LICENSE
2dcfef6e75c3 add command to sync documents
ymh <ymh.work@gmail.com>
parents:
diff changeset
     8
 * file that was distributed with this source code.
2dcfef6e75c3 add command to sync documents
ymh <ymh.work@gmail.com>
parents:
diff changeset
     9
 */
2dcfef6e75c3 add command to sync documents
ymh <ymh.work@gmail.com>
parents:
diff changeset
    10
2dcfef6e75c3 add command to sync documents
ymh <ymh.work@gmail.com>
parents:
diff changeset
    11
namespace IRI\Bundle\WikiTagBundle\Command;
2dcfef6e75c3 add command to sync documents
ymh <ymh.work@gmail.com>
parents:
diff changeset
    12
34
21fab44f46fe add event on tag changed
ymh <ymh.work@gmail.com>
parents: 27
diff changeset
    13
use IRI\Bundle\WikiTagBundle\Event\WikiTagEvents;
21fab44f46fe add event on tag changed
ymh <ymh.work@gmail.com>
parents: 27
diff changeset
    14
use IRI\Bundle\WikiTagBundle\Event\DocumentTagEvent;
6
2dcfef6e75c3 add command to sync documents
ymh <ymh.work@gmail.com>
parents:
diff changeset
    15
use Doctrine\ORM\Query;
2dcfef6e75c3 add command to sync documents
ymh <ymh.work@gmail.com>
parents:
diff changeset
    16
use Symfony\Bundle\FrameworkBundle\Command\ContainerAwareCommand;
2dcfef6e75c3 add command to sync documents
ymh <ymh.work@gmail.com>
parents:
diff changeset
    17
use Symfony\Component\Console\Input\InputArgument;
2dcfef6e75c3 add command to sync documents
ymh <ymh.work@gmail.com>
parents:
diff changeset
    18
use Symfony\Component\Console\Input\InputInterface;
2dcfef6e75c3 add command to sync documents
ymh <ymh.work@gmail.com>
parents:
diff changeset
    19
use Symfony\Component\Console\Input\InputOption;
2dcfef6e75c3 add command to sync documents
ymh <ymh.work@gmail.com>
parents:
diff changeset
    20
use Symfony\Component\Console\Output\OutputInterface;
2dcfef6e75c3 add command to sync documents
ymh <ymh.work@gmail.com>
parents:
diff changeset
    21
2dcfef6e75c3 add command to sync documents
ymh <ymh.work@gmail.com>
parents:
diff changeset
    22
class SyncDocumentsCommand extends ContainerAwareCommand
2dcfef6e75c3 add command to sync documents
ymh <ymh.work@gmail.com>
parents:
diff changeset
    23
{
2dcfef6e75c3 add command to sync documents
ymh <ymh.work@gmail.com>
parents:
diff changeset
    24
    protected function configure()
2dcfef6e75c3 add command to sync documents
ymh <ymh.work@gmail.com>
parents:
diff changeset
    25
    {
2dcfef6e75c3 add command to sync documents
ymh <ymh.work@gmail.com>
parents:
diff changeset
    26
        parent::configure();
2dcfef6e75c3 add command to sync documents
ymh <ymh.work@gmail.com>
parents:
diff changeset
    27
        
2dcfef6e75c3 add command to sync documents
ymh <ymh.work@gmail.com>
parents:
diff changeset
    28
        $this
2dcfef6e75c3 add command to sync documents
ymh <ymh.work@gmail.com>
parents:
diff changeset
    29
            ->setName('wikitag:sync-doc')
2dcfef6e75c3 add command to sync documents
ymh <ymh.work@gmail.com>
parents:
diff changeset
    30
            ->setDescription('Synchronize and index document class')
27
8551d844b4f3 Correct memory problem
ymh <ymh.work@gmail.com>
parents: 24
diff changeset
    31
            ->addOption('missing', 'm', InputOption::VALUE_NONE, "process missing")
8551d844b4f3 Correct memory problem
ymh <ymh.work@gmail.com>
parents: 24
diff changeset
    32
            ->addOption('tags', 't', InputOption::VALUE_NONE, "update tags")
6
2dcfef6e75c3 add command to sync documents
ymh <ymh.work@gmail.com>
parents:
diff changeset
    33
            ->addOption('clear', 'c', InputOption::VALUE_NONE, "clear all docs");
2dcfef6e75c3 add command to sync documents
ymh <ymh.work@gmail.com>
parents:
diff changeset
    34
    }
2dcfef6e75c3 add command to sync documents
ymh <ymh.work@gmail.com>
parents:
diff changeset
    35
2dcfef6e75c3 add command to sync documents
ymh <ymh.work@gmail.com>
parents:
diff changeset
    36
    protected function execute(InputInterface $input, OutputInterface $output)
2dcfef6e75c3 add command to sync documents
ymh <ymh.work@gmail.com>
parents:
diff changeset
    37
    {
23
b435f8055cb4 improve dynamic docs. create and lad class dynamically
ymh <ymh.work@gmail.com>
parents: 6
diff changeset
    38
        $class = $this->getContainer()->getParameter('wiki_tag.document_class');
6
2dcfef6e75c3 add command to sync documents
ymh <ymh.work@gmail.com>
parents:
diff changeset
    39
        $clear = $input->getOption('clear');
27
8551d844b4f3 Correct memory problem
ymh <ymh.work@gmail.com>
parents: 24
diff changeset
    40
        $missing = $input->getOption('missing');
6
2dcfef6e75c3 add command to sync documents
ymh <ymh.work@gmail.com>
parents:
diff changeset
    41
     
2dcfef6e75c3 add command to sync documents
ymh <ymh.work@gmail.com>
parents:
diff changeset
    42
        
2dcfef6e75c3 add command to sync documents
ymh <ymh.work@gmail.com>
parents:
diff changeset
    43
        $doctrine = $this->getContainer()->get('doctrine');
2dcfef6e75c3 add command to sync documents
ymh <ymh.work@gmail.com>
parents:
diff changeset
    44
        
2dcfef6e75c3 add command to sync documents
ymh <ymh.work@gmail.com>
parents:
diff changeset
    45
        $docrep = $doctrine->getRepository('WikiTagBundle:Document');
2dcfef6e75c3 add command to sync documents
ymh <ymh.work@gmail.com>
parents:
diff changeset
    46
        $rep = $doctrine->getRepository($class);
2dcfef6e75c3 add command to sync documents
ymh <ymh.work@gmail.com>
parents:
diff changeset
    47
        
2dcfef6e75c3 add command to sync documents
ymh <ymh.work@gmail.com>
parents:
diff changeset
    48
        if(is_null($rep)) {
2dcfef6e75c3 add command to sync documents
ymh <ymh.work@gmail.com>
parents:
diff changeset
    49
            //TODO : translate
2dcfef6e75c3 add command to sync documents
ymh <ymh.work@gmail.com>
parents:
diff changeset
    50
            $output->writeln("$class does not have a repository : exiting.");
2dcfef6e75c3 add command to sync documents
ymh <ymh.work@gmail.com>
parents:
diff changeset
    51
            return ;
2dcfef6e75c3 add command to sync documents
ymh <ymh.work@gmail.com>
parents:
diff changeset
    52
        }
2dcfef6e75c3 add command to sync documents
ymh <ymh.work@gmail.com>
parents:
diff changeset
    53
        
27
8551d844b4f3 Correct memory problem
ymh <ymh.work@gmail.com>
parents: 24
diff changeset
    54
        if($input->getOption('tags'))
8551d844b4f3 Correct memory problem
ymh <ymh.work@gmail.com>
parents: 24
diff changeset
    55
        {
8551d844b4f3 Correct memory problem
ymh <ymh.work@gmail.com>
parents: 24
diff changeset
    56
            if(!$missing)
8551d844b4f3 Correct memory problem
ymh <ymh.work@gmail.com>
parents: 24
diff changeset
    57
            {
8551d844b4f3 Correct memory problem
ymh <ymh.work@gmail.com>
parents: 24
diff changeset
    58
                $docquery = $doctrine->getEntityManager()->createQuery("SELECT doc from WikiTagBundle:Document doc");
8551d844b4f3 Correct memory problem
ymh <ymh.work@gmail.com>
parents: 24
diff changeset
    59
                $doccountquery = $doctrine->getEntityManager()->createQuery("SELECT COUNT(doc.id) from WikiTagBundle:Document doc");
8551d844b4f3 Correct memory problem
ymh <ymh.work@gmail.com>
parents: 24
diff changeset
    60
            }
8551d844b4f3 Correct memory problem
ymh <ymh.work@gmail.com>
parents: 24
diff changeset
    61
            else
8551d844b4f3 Correct memory problem
ymh <ymh.work@gmail.com>
parents: 24
diff changeset
    62
            {
8551d844b4f3 Correct memory problem
ymh <ymh.work@gmail.com>
parents: 24
diff changeset
    63
                $docquery = $doctrine->getEntityManager()->createQuery("SELECT doc from WikiTagBundle:Document doc WHERE doc.tagsStr IS NULL");
8551d844b4f3 Correct memory problem
ymh <ymh.work@gmail.com>
parents: 24
diff changeset
    64
                $doccountquery = $doctrine->getEntityManager()->createQuery("SELECT COUNT(doc.id) from WikiTagBundle:Document doc WHERE doc.tagsStr IS NULL");
8551d844b4f3 Correct memory problem
ymh <ymh.work@gmail.com>
parents: 24
diff changeset
    65
            }
8551d844b4f3 Correct memory problem
ymh <ymh.work@gmail.com>
parents: 24
diff changeset
    66
            
8551d844b4f3 Correct memory problem
ymh <ymh.work@gmail.com>
parents: 24
diff changeset
    67
            
8551d844b4f3 Correct memory problem
ymh <ymh.work@gmail.com>
parents: 24
diff changeset
    68
            $total = $doccountquery->getSingleScalarResult();
8551d844b4f3 Correct memory problem
ymh <ymh.work@gmail.com>
parents: 24
diff changeset
    69
            $done = 0;
8551d844b4f3 Correct memory problem
ymh <ymh.work@gmail.com>
parents: 24
diff changeset
    70
            $iterable = $docquery->iterate();
8551d844b4f3 Correct memory problem
ymh <ymh.work@gmail.com>
parents: 24
diff changeset
    71
            $todetach = array();
8551d844b4f3 Correct memory problem
ymh <ymh.work@gmail.com>
parents: 24
diff changeset
    72
            while (($row = $iterable->next()) !== false) {
8551d844b4f3 Correct memory problem
ymh <ymh.work@gmail.com>
parents: 24
diff changeset
    73
                $done++;
8551d844b4f3 Correct memory problem
ymh <ymh.work@gmail.com>
parents: 24
diff changeset
    74
                $memory = ((($done%10)==0)?" - mem: ".strval(memory_get_usage(true)):"");
8551d844b4f3 Correct memory problem
ymh <ymh.work@gmail.com>
parents: 24
diff changeset
    75
                $doc = $row[0];
8551d844b4f3 Correct memory problem
ymh <ymh.work@gmail.com>
parents: 24
diff changeset
    76
                $todetach[] = $doc;
8551d844b4f3 Correct memory problem
ymh <ymh.work@gmail.com>
parents: 24
diff changeset
    77
                $output->writeln("Process doc id ".$doc->getId()." $done/$total ".strval(intval(floatval($done)/floatval($total)*100.0))."%$memory");
8551d844b4f3 Correct memory problem
ymh <ymh.work@gmail.com>
parents: 24
diff changeset
    78
                $docrep->updateTagsStr($doc);
34
21fab44f46fe add event on tag changed
ymh <ymh.work@gmail.com>
parents: 27
diff changeset
    79
                //dispatch event
21fab44f46fe add event on tag changed
ymh <ymh.work@gmail.com>
parents: 27
diff changeset
    80
                $event_dispatcher = $this->getContainer()->get('event_dispatcher');
21fab44f46fe add event on tag changed
ymh <ymh.work@gmail.com>
parents: 27
diff changeset
    81
                $event = new DocumentTagEvent($doc);
21fab44f46fe add event on tag changed
ymh <ymh.work@gmail.com>
parents: 27
diff changeset
    82
                $event_dispatcher->dispatch(WikiTagEvents::onTagChanged, $event);
21fab44f46fe add event on tag changed
ymh <ymh.work@gmail.com>
parents: 27
diff changeset
    83
                
27
8551d844b4f3 Correct memory problem
ymh <ymh.work@gmail.com>
parents: 24
diff changeset
    84
                if($done%10 == 0)
8551d844b4f3 Correct memory problem
ymh <ymh.work@gmail.com>
parents: 24
diff changeset
    85
                {
8551d844b4f3 Correct memory problem
ymh <ymh.work@gmail.com>
parents: 24
diff changeset
    86
                    $doctrine->getEntityManager()->flush();
8551d844b4f3 Correct memory problem
ymh <ymh.work@gmail.com>
parents: 24
diff changeset
    87
                    foreach($todetach as $obj)
8551d844b4f3 Correct memory problem
ymh <ymh.work@gmail.com>
parents: 24
diff changeset
    88
                    {
8551d844b4f3 Correct memory problem
ymh <ymh.work@gmail.com>
parents: 24
diff changeset
    89
                        $doctrine->getEntityManager()->detach($obj);
8551d844b4f3 Correct memory problem
ymh <ymh.work@gmail.com>
parents: 24
diff changeset
    90
                    }
8551d844b4f3 Correct memory problem
ymh <ymh.work@gmail.com>
parents: 24
diff changeset
    91
                    $todetach = array();
8551d844b4f3 Correct memory problem
ymh <ymh.work@gmail.com>
parents: 24
diff changeset
    92
                }
8551d844b4f3 Correct memory problem
ymh <ymh.work@gmail.com>
parents: 24
diff changeset
    93
            }
8551d844b4f3 Correct memory problem
ymh <ymh.work@gmail.com>
parents: 24
diff changeset
    94
            $doctrine->getEntityManager()->flush();
8551d844b4f3 Correct memory problem
ymh <ymh.work@gmail.com>
parents: 24
diff changeset
    95
                        
8551d844b4f3 Correct memory problem
ymh <ymh.work@gmail.com>
parents: 24
diff changeset
    96
            return;
8551d844b4f3 Correct memory problem
ymh <ymh.work@gmail.com>
parents: 24
diff changeset
    97
            
8551d844b4f3 Correct memory problem
ymh <ymh.work@gmail.com>
parents: 24
diff changeset
    98
        }
8551d844b4f3 Correct memory problem
ymh <ymh.work@gmail.com>
parents: 24
diff changeset
    99
        
6
2dcfef6e75c3 add command to sync documents
ymh <ymh.work@gmail.com>
parents:
diff changeset
   100
        //TODO : check class to implement DocumentInterface
2dcfef6e75c3 add command to sync documents
ymh <ymh.work@gmail.com>
parents:
diff changeset
   101
        //TODO : write progress
27
8551d844b4f3 Correct memory problem
ymh <ymh.work@gmail.com>
parents: 24
diff changeset
   102
        if($missing)
8551d844b4f3 Correct memory problem
ymh <ymh.work@gmail.com>
parents: 24
diff changeset
   103
        {
8551d844b4f3 Correct memory problem
ymh <ymh.work@gmail.com>
parents: 24
diff changeset
   104
            $docquery = $doctrine->getEntityManager()->createQuery("SELECT doc FROM $class doc WHERE doc.id not in (SELECT wtdoc FROM WikiTagBundle:Document wtdoc)");
8551d844b4f3 Correct memory problem
ymh <ymh.work@gmail.com>
parents: 24
diff changeset
   105
            $doccountquery = $doctrine->getEntityManager()->createQuery("SELECT count(doc.id) FROM $class doc WHERE doc.id not in (SELECT wtdoc FROM WikiTagBundle:Document wtdoc)");
8551d844b4f3 Correct memory problem
ymh <ymh.work@gmail.com>
parents: 24
diff changeset
   106
            //$doclist = $doctrine->getEntityManager()->createQuery("SELECT doc FROM $class doc WHERE doc.id not in (SELECT wtdoc FROM WikiTagBundle:Document wtdoc)")->getResult();
8551d844b4f3 Correct memory problem
ymh <ymh.work@gmail.com>
parents: 24
diff changeset
   107
        }
8551d844b4f3 Correct memory problem
ymh <ymh.work@gmail.com>
parents: 24
diff changeset
   108
        else
8551d844b4f3 Correct memory problem
ymh <ymh.work@gmail.com>
parents: 24
diff changeset
   109
        {
8551d844b4f3 Correct memory problem
ymh <ymh.work@gmail.com>
parents: 24
diff changeset
   110
            $docquery = $doctrine->getEntityManager()->createQuery("SELECT doc FROM $class doc");
8551d844b4f3 Correct memory problem
ymh <ymh.work@gmail.com>
parents: 24
diff changeset
   111
            $doccountquery = $doctrine->getEntityManager()->createQuery("SELECT count(doc.id) FROM $class doc");
8551d844b4f3 Correct memory problem
ymh <ymh.work@gmail.com>
parents: 24
diff changeset
   112
            //$doclist = $rep->findAll();
8551d844b4f3 Correct memory problem
ymh <ymh.work@gmail.com>
parents: 24
diff changeset
   113
        }
8551d844b4f3 Correct memory problem
ymh <ymh.work@gmail.com>
parents: 24
diff changeset
   114
        $total = $doccountquery->getSingleScalarResult();
24
cd389bf882f1 Some small corrections
ymh <ymh.work@gmail.com>
parents: 23
diff changeset
   115
        $done = 0;
27
8551d844b4f3 Correct memory problem
ymh <ymh.work@gmail.com>
parents: 24
diff changeset
   116
        $iterable = $docquery->iterate();
8551d844b4f3 Correct memory problem
ymh <ymh.work@gmail.com>
parents: 24
diff changeset
   117
        while (($row = $iterable->next()) !== false) {
24
cd389bf882f1 Some small corrections
ymh <ymh.work@gmail.com>
parents: 23
diff changeset
   118
            $done++;
27
8551d844b4f3 Correct memory problem
ymh <ymh.work@gmail.com>
parents: 24
diff changeset
   119
            $doc = $row[0];
8551d844b4f3 Correct memory problem
ymh <ymh.work@gmail.com>
parents: 24
diff changeset
   120
            $memory = ((($done%10)==0)?" - mem: ".strval(memory_get_usage(true)):"");
8551d844b4f3 Correct memory problem
ymh <ymh.work@gmail.com>
parents: 24
diff changeset
   121
            $output->writeln("Process doc id ".$doc->getId()." $done/$total ".strval(intval(floatval($done)/floatval($total)*100.0))."%$memory");
23
b435f8055cb4 improve dynamic docs. create and lad class dynamically
ymh <ymh.work@gmail.com>
parents: 6
diff changeset
   122
            $docrep->writeDocument($doc, $this->getContainer()->getParameter('wiki_tag.document_id_column'), $this->getContainer()->getParameter('wiki_tag.fields'));
27
8551d844b4f3 Correct memory problem
ymh <ymh.work@gmail.com>
parents: 24
diff changeset
   123
            if($done%10 == 0)
8551d844b4f3 Correct memory problem
ymh <ymh.work@gmail.com>
parents: 24
diff changeset
   124
            {
8551d844b4f3 Correct memory problem
ymh <ymh.work@gmail.com>
parents: 24
diff changeset
   125
                $doctrine->getEntityManager()->flush();
8551d844b4f3 Correct memory problem
ymh <ymh.work@gmail.com>
parents: 24
diff changeset
   126
                $doctrine->getEntityManager()->clear();
8551d844b4f3 Correct memory problem
ymh <ymh.work@gmail.com>
parents: 24
diff changeset
   127
            }
6
2dcfef6e75c3 add command to sync documents
ymh <ymh.work@gmail.com>
parents:
diff changeset
   128
        }
2dcfef6e75c3 add command to sync documents
ymh <ymh.work@gmail.com>
parents:
diff changeset
   129
        $doctrine->getEntityManager()->flush();
2dcfef6e75c3 add command to sync documents
ymh <ymh.work@gmail.com>
parents:
diff changeset
   130
        
2dcfef6e75c3 add command to sync documents
ymh <ymh.work@gmail.com>
parents:
diff changeset
   131
        if($clear) {
2dcfef6e75c3 add command to sync documents
ymh <ymh.work@gmail.com>
parents:
diff changeset
   132
            
23
b435f8055cb4 improve dynamic docs. create and lad class dynamically
ymh <ymh.work@gmail.com>
parents: 6
diff changeset
   133
            $req = $doctrine->getEntityManager()->createQuery("DELETE WikiTagBundle:Document wtdoc WHERE wtdoc.externalId NOT IN (SELECT doc FROM $class doc)");
b435f8055cb4 improve dynamic docs. create and lad class dynamically
ymh <ymh.work@gmail.com>
parents: 6
diff changeset
   134
            $req->getResult();
6
2dcfef6e75c3 add command to sync documents
ymh <ymh.work@gmail.com>
parents:
diff changeset
   135
            $doctrine->getEntityManager()->flush();
2dcfef6e75c3 add command to sync documents
ymh <ymh.work@gmail.com>
parents:
diff changeset
   136
        }
2dcfef6e75c3 add command to sync documents
ymh <ymh.work@gmail.com>
parents:
diff changeset
   137
2dcfef6e75c3 add command to sync documents
ymh <ymh.work@gmail.com>
parents:
diff changeset
   138
        $output->writeln(strval(count($doclist)) ." documents imported.");
2dcfef6e75c3 add command to sync documents
ymh <ymh.work@gmail.com>
parents:
diff changeset
   139
    }
2dcfef6e75c3 add command to sync documents
ymh <ymh.work@gmail.com>
parents:
diff changeset
   140
}