Command/SyncDocumentsCommand.php
author ymh <ymh.work@gmail.com>
Wed, 19 Oct 2011 14:33:40 +0200
changeset 6 2dcfef6e75c3
child 23 b435f8055cb4
permissions -rw-r--r--
add command to sync documents
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
            ->addArgument('class', InputArgument::REQUIRED, 'The document class')
2dcfef6e75c3 add command to sync documents
ymh <ymh.work@gmail.com>
parents:
diff changeset
    31
            ->addOption('clear', 'c', InputOption::VALUE_NONE, "clear all docs");
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
2dcfef6e75c3 add command to sync documents
ymh <ymh.work@gmail.com>
parents:
diff changeset
    34
    protected function execute(InputInterface $input, OutputInterface $output)
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
        $class = $input->getArgument('class');
2dcfef6e75c3 add command to sync documents
ymh <ymh.work@gmail.com>
parents:
diff changeset
    37
        $clear = $input->getOption('clear');
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
        
2dcfef6e75c3 add command to sync documents
ymh <ymh.work@gmail.com>
parents:
diff changeset
    40
        $doctrine = $this->getContainer()->get('doctrine');
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
        $docrep = $doctrine->getRepository('WikiTagBundle:Document');
2dcfef6e75c3 add command to sync documents
ymh <ymh.work@gmail.com>
parents:
diff changeset
    43
        $rep = $doctrine->getRepository($class);
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
        if(is_null($rep)) {
2dcfef6e75c3 add command to sync documents
ymh <ymh.work@gmail.com>
parents:
diff changeset
    46
            //TODO : translate
2dcfef6e75c3 add command to sync documents
ymh <ymh.work@gmail.com>
parents:
diff changeset
    47
            $output->writeln("$class does not have a repository : exiting.");
2dcfef6e75c3 add command to sync documents
ymh <ymh.work@gmail.com>
parents:
diff changeset
    48
            return ;
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
        
2dcfef6e75c3 add command to sync documents
ymh <ymh.work@gmail.com>
parents:
diff changeset
    51
        //TODO : check class to implement DocumentInterface
2dcfef6e75c3 add command to sync documents
ymh <ymh.work@gmail.com>
parents:
diff changeset
    52
        //TODO : write progress
2dcfef6e75c3 add command to sync documents
ymh <ymh.work@gmail.com>
parents:
diff changeset
    53
        $doclist = $rep->findAll();
2dcfef6e75c3 add command to sync documents
ymh <ymh.work@gmail.com>
parents:
diff changeset
    54
        foreach ($doclist as $doc) {
2dcfef6e75c3 add command to sync documents
ymh <ymh.work@gmail.com>
parents:
diff changeset
    55
            $output->writeln("TITLE : ".$doc->getTitle());
2dcfef6e75c3 add command to sync documents
ymh <ymh.work@gmail.com>
parents:
diff changeset
    56
            $docrep->writeDocument($doc);
2dcfef6e75c3 add command to sync documents
ymh <ymh.work@gmail.com>
parents:
diff changeset
    57
        }
2dcfef6e75c3 add command to sync documents
ymh <ymh.work@gmail.com>
parents:
diff changeset
    58
        $doctrine->getEntityManager()->flush();
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
        if($clear) {
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
            $req_ids = $doctrine->getEntityManager()->createQuery("SELECT partial doc.{id} FROM $class doc");
2dcfef6e75c3 add command to sync documents
ymh <ymh.work@gmail.com>
parents:
diff changeset
    63
            $doc_ids = array();
2dcfef6e75c3 add command to sync documents
ymh <ymh.work@gmail.com>
parents:
diff changeset
    64
            foreach($req_ids->getResult(Query::HYDRATE_SCALAR) as $doc_id) {
2dcfef6e75c3 add command to sync documents
ymh <ymh.work@gmail.com>
parents:
diff changeset
    65
                $doc_ids[] = strval($doc_id['doc_id']);
2dcfef6e75c3 add command to sync documents
ymh <ymh.work@gmail.com>
parents:
diff changeset
    66
            }
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
            $req = $doctrine->getEntityManager()->createQuery("SELECT wtdoc FROM WikiTagBundle:Document wtdoc WHERE wtdoc.externalId NOT IN (:doc_ids)");
2dcfef6e75c3 add command to sync documents
ymh <ymh.work@gmail.com>
parents:
diff changeset
    69
            $req->setParameter('doc_ids', $doc_ids);
2dcfef6e75c3 add command to sync documents
ymh <ymh.work@gmail.com>
parents:
diff changeset
    70
            foreach ($req->getResult() as $wtdoc) {
2dcfef6e75c3 add command to sync documents
ymh <ymh.work@gmail.com>
parents:
diff changeset
    71
                $output->writeln("DELETE : ".$wtdoc->getId());
2dcfef6e75c3 add command to sync documents
ymh <ymh.work@gmail.com>
parents:
diff changeset
    72
                $doctrine->getEntityManager()->remove($wtdoc);
2dcfef6e75c3 add command to sync documents
ymh <ymh.work@gmail.com>
parents:
diff changeset
    73
            }
2dcfef6e75c3 add command to sync documents
ymh <ymh.work@gmail.com>
parents:
diff changeset
    74
            $doctrine->getEntityManager()->flush();
2dcfef6e75c3 add command to sync documents
ymh <ymh.work@gmail.com>
parents:
diff changeset
    75
        }
2dcfef6e75c3 add command to sync documents
ymh <ymh.work@gmail.com>
parents:
diff changeset
    76
2dcfef6e75c3 add command to sync documents
ymh <ymh.work@gmail.com>
parents:
diff changeset
    77
        $output->writeln(strval(count($doclist)) ." documents imported.");
2dcfef6e75c3 add command to sync documents
ymh <ymh.work@gmail.com>
parents:
diff changeset
    78
    }
2dcfef6e75c3 add command to sync documents
ymh <ymh.work@gmail.com>
parents:
diff changeset
    79
}