Command/SyncDocumentsCommand.php
author ymh <ymh.work@gmail.com>
Sun, 06 Nov 2011 23:44:37 +0100
changeset 27 8551d844b4f3
parent 24 cd389bf882f1
child 34 21fab44f46fe
permissions -rwxr-xr-x
Correct memory problem
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
2dcfef6e75c3 add command to sync documents
ymh <ymh.work@gmail.com>
parents:
diff changeset
    13
use Doctrine\ORM\Query;
2dcfef6e75c3 add command to sync documents
ymh <ymh.work@gmail.com>
parents:
diff changeset
    14
2dcfef6e75c3 add command to sync documents
ymh <ymh.work@gmail.com>
parents:
diff changeset
    15
use Symfony\Bundle\FrameworkBundle\Command\ContainerAwareCommand;
2dcfef6e75c3 add command to sync documents
ymh <ymh.work@gmail.com>
parents:
diff changeset
    16
use Symfony\Component\Console\Input\InputArgument;
2dcfef6e75c3 add command to sync documents
ymh <ymh.work@gmail.com>
parents:
diff changeset
    17
use Symfony\Component\Console\Input\InputInterface;
2dcfef6e75c3 add command to sync documents
ymh <ymh.work@gmail.com>
parents:
diff changeset
    18
use Symfony\Component\Console\Input\InputOption;
2dcfef6e75c3 add command to sync documents
ymh <ymh.work@gmail.com>
parents:
diff changeset
    19
use Symfony\Component\Console\Output\OutputInterface;
2dcfef6e75c3 add command to sync documents
ymh <ymh.work@gmail.com>
parents:
diff changeset
    20
2dcfef6e75c3 add command to sync documents
ymh <ymh.work@gmail.com>
parents:
diff changeset
    21
class SyncDocumentsCommand extends ContainerAwareCommand
2dcfef6e75c3 add command to sync documents
ymh <ymh.work@gmail.com>
parents:
diff changeset
    22
{
2dcfef6e75c3 add command to sync documents
ymh <ymh.work@gmail.com>
parents:
diff changeset
    23
    protected function configure()
2dcfef6e75c3 add command to sync documents
ymh <ymh.work@gmail.com>
parents:
diff changeset
    24
    {
2dcfef6e75c3 add command to sync documents
ymh <ymh.work@gmail.com>
parents:
diff changeset
    25
        parent::configure();
2dcfef6e75c3 add command to sync documents
ymh <ymh.work@gmail.com>
parents:
diff changeset
    26
        
2dcfef6e75c3 add command to sync documents
ymh <ymh.work@gmail.com>
parents:
diff changeset
    27
        $this
2dcfef6e75c3 add command to sync documents
ymh <ymh.work@gmail.com>
parents:
diff changeset
    28
            ->setName('wikitag:sync-doc')
2dcfef6e75c3 add command to sync documents
ymh <ymh.work@gmail.com>
parents:
diff changeset
    29
            ->setDescription('Synchronize and index document class')
27
8551d844b4f3 Correct memory problem
ymh <ymh.work@gmail.com>
parents: 24
diff changeset
    30
            ->addOption('missing', 'm', InputOption::VALUE_NONE, "process missing")
8551d844b4f3 Correct memory problem
ymh <ymh.work@gmail.com>
parents: 24
diff changeset
    31
            ->addOption('tags', 't', InputOption::VALUE_NONE, "update tags")
6
2dcfef6e75c3 add command to sync documents
ymh <ymh.work@gmail.com>
parents:
diff changeset
    32
            ->addOption('clear', 'c', InputOption::VALUE_NONE, "clear all docs");
2dcfef6e75c3 add command to sync documents
ymh <ymh.work@gmail.com>
parents:
diff changeset
    33
    }
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
    protected function execute(InputInterface $input, OutputInterface $output)
2dcfef6e75c3 add command to sync documents
ymh <ymh.work@gmail.com>
parents:
diff changeset
    36
    {
23
b435f8055cb4 improve dynamic docs. create and lad class dynamically
ymh <ymh.work@gmail.com>
parents: 6
diff changeset
    37
        $class = $this->getContainer()->getParameter('wiki_tag.document_class');
6
2dcfef6e75c3 add command to sync documents
ymh <ymh.work@gmail.com>
parents:
diff changeset
    38
        $clear = $input->getOption('clear');
27
8551d844b4f3 Correct memory problem
ymh <ymh.work@gmail.com>
parents: 24
diff changeset
    39
        $missing = $input->getOption('missing');
6
2dcfef6e75c3 add command to sync documents
ymh <ymh.work@gmail.com>
parents:
diff changeset
    40
     
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
        $doctrine = $this->getContainer()->get('doctrine');
2dcfef6e75c3 add command to sync documents
ymh <ymh.work@gmail.com>
parents:
diff changeset
    43
        
2dcfef6e75c3 add command to sync documents
ymh <ymh.work@gmail.com>
parents:
diff changeset
    44
        $docrep = $doctrine->getRepository('WikiTagBundle:Document');
2dcfef6e75c3 add command to sync documents
ymh <ymh.work@gmail.com>
parents:
diff changeset
    45
        $rep = $doctrine->getRepository($class);
2dcfef6e75c3 add command to sync documents
ymh <ymh.work@gmail.com>
parents:
diff changeset
    46
        
2dcfef6e75c3 add command to sync documents
ymh <ymh.work@gmail.com>
parents:
diff changeset
    47
        if(is_null($rep)) {
2dcfef6e75c3 add command to sync documents
ymh <ymh.work@gmail.com>
parents:
diff changeset
    48
            //TODO : translate
2dcfef6e75c3 add command to sync documents
ymh <ymh.work@gmail.com>
parents:
diff changeset
    49
            $output->writeln("$class does not have a repository : exiting.");
2dcfef6e75c3 add command to sync documents
ymh <ymh.work@gmail.com>
parents:
diff changeset
    50
            return ;
2dcfef6e75c3 add command to sync documents
ymh <ymh.work@gmail.com>
parents:
diff changeset
    51
        }
2dcfef6e75c3 add command to sync documents
ymh <ymh.work@gmail.com>
parents:
diff changeset
    52
        
27
8551d844b4f3 Correct memory problem
ymh <ymh.work@gmail.com>
parents: 24
diff changeset
    53
        if($input->getOption('tags'))
8551d844b4f3 Correct memory problem
ymh <ymh.work@gmail.com>
parents: 24
diff changeset
    54
        {
8551d844b4f3 Correct memory problem
ymh <ymh.work@gmail.com>
parents: 24
diff changeset
    55
            if(!$missing)
8551d844b4f3 Correct memory problem
ymh <ymh.work@gmail.com>
parents: 24
diff changeset
    56
            {
8551d844b4f3 Correct memory problem
ymh <ymh.work@gmail.com>
parents: 24
diff changeset
    57
                $docquery = $doctrine->getEntityManager()->createQuery("SELECT doc from WikiTagBundle:Document doc");
8551d844b4f3 Correct memory problem
ymh <ymh.work@gmail.com>
parents: 24
diff changeset
    58
                $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
    59
            }
8551d844b4f3 Correct memory problem
ymh <ymh.work@gmail.com>
parents: 24
diff changeset
    60
            else
8551d844b4f3 Correct memory problem
ymh <ymh.work@gmail.com>
parents: 24
diff changeset
    61
            {
8551d844b4f3 Correct memory problem
ymh <ymh.work@gmail.com>
parents: 24
diff changeset
    62
                $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
    63
                $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
    64
            }
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
            $total = $doccountquery->getSingleScalarResult();
8551d844b4f3 Correct memory problem
ymh <ymh.work@gmail.com>
parents: 24
diff changeset
    68
            $done = 0;
8551d844b4f3 Correct memory problem
ymh <ymh.work@gmail.com>
parents: 24
diff changeset
    69
            $iterable = $docquery->iterate();
8551d844b4f3 Correct memory problem
ymh <ymh.work@gmail.com>
parents: 24
diff changeset
    70
            $todetach = array();
8551d844b4f3 Correct memory problem
ymh <ymh.work@gmail.com>
parents: 24
diff changeset
    71
            while (($row = $iterable->next()) !== false) {
8551d844b4f3 Correct memory problem
ymh <ymh.work@gmail.com>
parents: 24
diff changeset
    72
                $done++;
8551d844b4f3 Correct memory problem
ymh <ymh.work@gmail.com>
parents: 24
diff changeset
    73
                $memory = ((($done%10)==0)?" - mem: ".strval(memory_get_usage(true)):"");
8551d844b4f3 Correct memory problem
ymh <ymh.work@gmail.com>
parents: 24
diff changeset
    74
                $doc = $row[0];
8551d844b4f3 Correct memory problem
ymh <ymh.work@gmail.com>
parents: 24
diff changeset
    75
                $todetach[] = $doc;
8551d844b4f3 Correct memory problem
ymh <ymh.work@gmail.com>
parents: 24
diff changeset
    76
                $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
    77
                $docrep->updateTagsStr($doc);
8551d844b4f3 Correct memory problem
ymh <ymh.work@gmail.com>
parents: 24
diff changeset
    78
                if($done%10 == 0)
8551d844b4f3 Correct memory problem
ymh <ymh.work@gmail.com>
parents: 24
diff changeset
    79
                {
8551d844b4f3 Correct memory problem
ymh <ymh.work@gmail.com>
parents: 24
diff changeset
    80
                    $doctrine->getEntityManager()->flush();
8551d844b4f3 Correct memory problem
ymh <ymh.work@gmail.com>
parents: 24
diff changeset
    81
                    foreach($todetach as $obj)
8551d844b4f3 Correct memory problem
ymh <ymh.work@gmail.com>
parents: 24
diff changeset
    82
                    {
8551d844b4f3 Correct memory problem
ymh <ymh.work@gmail.com>
parents: 24
diff changeset
    83
                        $doctrine->getEntityManager()->detach($obj);
8551d844b4f3 Correct memory problem
ymh <ymh.work@gmail.com>
parents: 24
diff changeset
    84
                    }
8551d844b4f3 Correct memory problem
ymh <ymh.work@gmail.com>
parents: 24
diff changeset
    85
                    $todetach = array();
8551d844b4f3 Correct memory problem
ymh <ymh.work@gmail.com>
parents: 24
diff changeset
    86
                }
8551d844b4f3 Correct memory problem
ymh <ymh.work@gmail.com>
parents: 24
diff changeset
    87
            }
8551d844b4f3 Correct memory problem
ymh <ymh.work@gmail.com>
parents: 24
diff changeset
    88
            $doctrine->getEntityManager()->flush();
8551d844b4f3 Correct memory problem
ymh <ymh.work@gmail.com>
parents: 24
diff changeset
    89
                        
8551d844b4f3 Correct memory problem
ymh <ymh.work@gmail.com>
parents: 24
diff changeset
    90
            return;
8551d844b4f3 Correct memory problem
ymh <ymh.work@gmail.com>
parents: 24
diff changeset
    91
            
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
        
6
2dcfef6e75c3 add command to sync documents
ymh <ymh.work@gmail.com>
parents:
diff changeset
    94
        //TODO : check class to implement DocumentInterface
2dcfef6e75c3 add command to sync documents
ymh <ymh.work@gmail.com>
parents:
diff changeset
    95
        //TODO : write progress
27
8551d844b4f3 Correct memory problem
ymh <ymh.work@gmail.com>
parents: 24
diff changeset
    96
        if($missing)
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
            $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
    99
            $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
   100
            //$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
   101
        }
8551d844b4f3 Correct memory problem
ymh <ymh.work@gmail.com>
parents: 24
diff changeset
   102
        else
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");
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");
8551d844b4f3 Correct memory problem
ymh <ymh.work@gmail.com>
parents: 24
diff changeset
   106
            //$doclist = $rep->findAll();
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
        $total = $doccountquery->getSingleScalarResult();
24
cd389bf882f1 Some small corrections
ymh <ymh.work@gmail.com>
parents: 23
diff changeset
   109
        $done = 0;
27
8551d844b4f3 Correct memory problem
ymh <ymh.work@gmail.com>
parents: 24
diff changeset
   110
        $iterable = $docquery->iterate();
8551d844b4f3 Correct memory problem
ymh <ymh.work@gmail.com>
parents: 24
diff changeset
   111
        while (($row = $iterable->next()) !== false) {
24
cd389bf882f1 Some small corrections
ymh <ymh.work@gmail.com>
parents: 23
diff changeset
   112
            $done++;
27
8551d844b4f3 Correct memory problem
ymh <ymh.work@gmail.com>
parents: 24
diff changeset
   113
            $doc = $row[0];
8551d844b4f3 Correct memory problem
ymh <ymh.work@gmail.com>
parents: 24
diff changeset
   114
            $memory = ((($done%10)==0)?" - mem: ".strval(memory_get_usage(true)):"");
8551d844b4f3 Correct memory problem
ymh <ymh.work@gmail.com>
parents: 24
diff changeset
   115
            $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
   116
            $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
   117
            if($done%10 == 0)
8551d844b4f3 Correct memory problem
ymh <ymh.work@gmail.com>
parents: 24
diff changeset
   118
            {
8551d844b4f3 Correct memory problem
ymh <ymh.work@gmail.com>
parents: 24
diff changeset
   119
                $doctrine->getEntityManager()->flush();
8551d844b4f3 Correct memory problem
ymh <ymh.work@gmail.com>
parents: 24
diff changeset
   120
                $doctrine->getEntityManager()->clear();
8551d844b4f3 Correct memory problem
ymh <ymh.work@gmail.com>
parents: 24
diff changeset
   121
            }
6
2dcfef6e75c3 add command to sync documents
ymh <ymh.work@gmail.com>
parents:
diff changeset
   122
        }
2dcfef6e75c3 add command to sync documents
ymh <ymh.work@gmail.com>
parents:
diff changeset
   123
        $doctrine->getEntityManager()->flush();
2dcfef6e75c3 add command to sync documents
ymh <ymh.work@gmail.com>
parents:
diff changeset
   124
        
2dcfef6e75c3 add command to sync documents
ymh <ymh.work@gmail.com>
parents:
diff changeset
   125
        if($clear) {
2dcfef6e75c3 add command to sync documents
ymh <ymh.work@gmail.com>
parents:
diff changeset
   126
            
23
b435f8055cb4 improve dynamic docs. create and lad class dynamically
ymh <ymh.work@gmail.com>
parents: 6
diff changeset
   127
            $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
   128
            $req->getResult();
6
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
2dcfef6e75c3 add command to sync documents
ymh <ymh.work@gmail.com>
parents:
diff changeset
   132
        $output->writeln(strval(count($doclist)) ." documents imported.");
2dcfef6e75c3 add command to sync documents
ymh <ymh.work@gmail.com>
parents:
diff changeset
   133
    }
2dcfef6e75c3 add command to sync documents
ymh <ymh.work@gmail.com>
parents:
diff changeset
   134
}