|
1 <?php |
|
2 /* |
|
3 * This file is part of the WikiTagBundle package. |
|
4 * |
|
5 * (c) IRI <http://www.iri.centrepompidou.fr/> |
|
6 * |
|
7 * For the full copyright and license information, please view the LICENSE |
|
8 * file that was distributed with this source code. |
|
9 */ |
|
10 |
|
11 namespace IRI\Bundle\WikiTagBundle\Command; |
|
12 |
|
13 use Doctrine\ORM\Query; |
|
14 |
|
15 use Symfony\Bundle\FrameworkBundle\Command\ContainerAwareCommand; |
|
16 use Symfony\Component\Console\Input\InputArgument; |
|
17 use Symfony\Component\Console\Input\InputInterface; |
|
18 use Symfony\Component\Console\Input\InputOption; |
|
19 use Symfony\Component\Console\Output\OutputInterface; |
|
20 |
|
21 class SyncDocumentsCommand extends ContainerAwareCommand |
|
22 { |
|
23 protected function configure() |
|
24 { |
|
25 parent::configure(); |
|
26 |
|
27 $this |
|
28 ->setName('wikitag:sync-doc') |
|
29 ->setDescription('Synchronize and index document class') |
|
30 ->addArgument('class', InputArgument::REQUIRED, 'The document class') |
|
31 ->addOption('clear', 'c', InputOption::VALUE_NONE, "clear all docs"); |
|
32 } |
|
33 |
|
34 protected function execute(InputInterface $input, OutputInterface $output) |
|
35 { |
|
36 $class = $input->getArgument('class'); |
|
37 $clear = $input->getOption('clear'); |
|
38 |
|
39 |
|
40 $doctrine = $this->getContainer()->get('doctrine'); |
|
41 |
|
42 $docrep = $doctrine->getRepository('WikiTagBundle:Document'); |
|
43 $rep = $doctrine->getRepository($class); |
|
44 |
|
45 if(is_null($rep)) { |
|
46 //TODO : translate |
|
47 $output->writeln("$class does not have a repository : exiting."); |
|
48 return ; |
|
49 } |
|
50 |
|
51 //TODO : check class to implement DocumentInterface |
|
52 //TODO : write progress |
|
53 $doclist = $rep->findAll(); |
|
54 foreach ($doclist as $doc) { |
|
55 $output->writeln("TITLE : ".$doc->getTitle()); |
|
56 $docrep->writeDocument($doc); |
|
57 } |
|
58 $doctrine->getEntityManager()->flush(); |
|
59 |
|
60 if($clear) { |
|
61 |
|
62 $req_ids = $doctrine->getEntityManager()->createQuery("SELECT partial doc.{id} FROM $class doc"); |
|
63 $doc_ids = array(); |
|
64 foreach($req_ids->getResult(Query::HYDRATE_SCALAR) as $doc_id) { |
|
65 $doc_ids[] = strval($doc_id['doc_id']); |
|
66 } |
|
67 |
|
68 $req = $doctrine->getEntityManager()->createQuery("SELECT wtdoc FROM WikiTagBundle:Document wtdoc WHERE wtdoc.externalId NOT IN (:doc_ids)"); |
|
69 $req->setParameter('doc_ids', $doc_ids); |
|
70 foreach ($req->getResult() as $wtdoc) { |
|
71 $output->writeln("DELETE : ".$wtdoc->getId()); |
|
72 $doctrine->getEntityManager()->remove($wtdoc); |
|
73 } |
|
74 $doctrine->getEntityManager()->flush(); |
|
75 } |
|
76 |
|
77 $output->writeln(strval(count($doclist)) ." documents imported."); |
|
78 } |
|
79 } |