Command/SyncDocumentsCommand.php
author ymh <ymh.work@gmail.com>
Fri, 04 Nov 2011 15:53:59 +0100
changeset 24 cd389bf882f1
parent 23 b435f8055cb4
child 27 8551d844b4f3
permissions -rwxr-xr-x
Some small corrections
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')
2dcfef6e75c3 add command to sync documents
ymh <ymh.work@gmail.com>
parents:
diff changeset
    30
            ->addOption('clear', 'c', InputOption::VALUE_NONE, "clear all docs");
2dcfef6e75c3 add command to sync documents
ymh <ymh.work@gmail.com>
parents:
diff changeset
    31
    }
2dcfef6e75c3 add command to sync documents
ymh <ymh.work@gmail.com>
parents:
diff changeset
    32
2dcfef6e75c3 add command to sync documents
ymh <ymh.work@gmail.com>
parents:
diff changeset
    33
    protected function execute(InputInterface $input, OutputInterface $output)
2dcfef6e75c3 add command to sync documents
ymh <ymh.work@gmail.com>
parents:
diff changeset
    34
    {
23
b435f8055cb4 improve dynamic docs. create and lad class dynamically
ymh <ymh.work@gmail.com>
parents: 6
diff changeset
    35
        $class = $this->getContainer()->getParameter('wiki_tag.document_class');
6
2dcfef6e75c3 add command to sync documents
ymh <ymh.work@gmail.com>
parents:
diff changeset
    36
        $clear = $input->getOption('clear');
2dcfef6e75c3 add command to sync documents
ymh <ymh.work@gmail.com>
parents:
diff changeset
    37
     
2dcfef6e75c3 add command to sync documents
ymh <ymh.work@gmail.com>
parents:
diff changeset
    38
        
2dcfef6e75c3 add command to sync documents
ymh <ymh.work@gmail.com>
parents:
diff changeset
    39
        $doctrine = $this->getContainer()->get('doctrine');
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
        $docrep = $doctrine->getRepository('WikiTagBundle:Document');
2dcfef6e75c3 add command to sync documents
ymh <ymh.work@gmail.com>
parents:
diff changeset
    42
        $rep = $doctrine->getRepository($class);
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
        if(is_null($rep)) {
2dcfef6e75c3 add command to sync documents
ymh <ymh.work@gmail.com>
parents:
diff changeset
    45
            //TODO : translate
2dcfef6e75c3 add command to sync documents
ymh <ymh.work@gmail.com>
parents:
diff changeset
    46
            $output->writeln("$class does not have a repository : exiting.");
2dcfef6e75c3 add command to sync documents
ymh <ymh.work@gmail.com>
parents:
diff changeset
    47
            return ;
2dcfef6e75c3 add command to sync documents
ymh <ymh.work@gmail.com>
parents:
diff changeset
    48
        }
2dcfef6e75c3 add command to sync documents
ymh <ymh.work@gmail.com>
parents:
diff changeset
    49
        
2dcfef6e75c3 add command to sync documents
ymh <ymh.work@gmail.com>
parents:
diff changeset
    50
        //TODO : check class to implement DocumentInterface
2dcfef6e75c3 add command to sync documents
ymh <ymh.work@gmail.com>
parents:
diff changeset
    51
        //TODO : write progress
2dcfef6e75c3 add command to sync documents
ymh <ymh.work@gmail.com>
parents:
diff changeset
    52
        $doclist = $rep->findAll();
24
cd389bf882f1 Some small corrections
ymh <ymh.work@gmail.com>
parents: 23
diff changeset
    53
        $total = count($doclist);
cd389bf882f1 Some small corrections
ymh <ymh.work@gmail.com>
parents: 23
diff changeset
    54
        $done = 0;
6
2dcfef6e75c3 add command to sync documents
ymh <ymh.work@gmail.com>
parents:
diff changeset
    55
        foreach ($doclist as $doc) {
24
cd389bf882f1 Some small corrections
ymh <ymh.work@gmail.com>
parents: 23
diff changeset
    56
            $done++;
cd389bf882f1 Some small corrections
ymh <ymh.work@gmail.com>
parents: 23
diff changeset
    57
            $output->writeln("Process doc id ".$doc->getId()." $done/$total ".strval(intval(floatval($done)/floatval($total)*100.0))."%");
23
b435f8055cb4 improve dynamic docs. create and lad class dynamically
ymh <ymh.work@gmail.com>
parents: 6
diff changeset
    58
            $docrep->writeDocument($doc, $this->getContainer()->getParameter('wiki_tag.document_id_column'), $this->getContainer()->getParameter('wiki_tag.fields'));
6
2dcfef6e75c3 add command to sync documents
ymh <ymh.work@gmail.com>
parents:
diff changeset
    59
        }
2dcfef6e75c3 add command to sync documents
ymh <ymh.work@gmail.com>
parents:
diff changeset
    60
        $doctrine->getEntityManager()->flush();
2dcfef6e75c3 add command to sync documents
ymh <ymh.work@gmail.com>
parents:
diff changeset
    61
        
2dcfef6e75c3 add command to sync documents
ymh <ymh.work@gmail.com>
parents:
diff changeset
    62
        if($clear) {
2dcfef6e75c3 add command to sync documents
ymh <ymh.work@gmail.com>
parents:
diff changeset
    63
            
23
b435f8055cb4 improve dynamic docs. create and lad class dynamically
ymh <ymh.work@gmail.com>
parents: 6
diff changeset
    64
            $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
    65
            $req->getResult();
6
2dcfef6e75c3 add command to sync documents
ymh <ymh.work@gmail.com>
parents:
diff changeset
    66
            $doctrine->getEntityManager()->flush();
2dcfef6e75c3 add command to sync documents
ymh <ymh.work@gmail.com>
parents:
diff changeset
    67
        }
2dcfef6e75c3 add command to sync documents
ymh <ymh.work@gmail.com>
parents:
diff changeset
    68
2dcfef6e75c3 add command to sync documents
ymh <ymh.work@gmail.com>
parents:
diff changeset
    69
        $output->writeln(strval(count($doclist)) ." documents imported.");
2dcfef6e75c3 add command to sync documents
ymh <ymh.work@gmail.com>
parents:
diff changeset
    70
    }
2dcfef6e75c3 add command to sync documents
ymh <ymh.work@gmail.com>
parents:
diff changeset
    71
}