| 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 |
| 6 | 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 |
||
| 34 | 13 |
use IRI\Bundle\WikiTagBundle\Event\WikiTagEvents; |
14 |
use IRI\Bundle\WikiTagBundle\Event\DocumentTagEvent; |
|
| 6 | 15 |
use Doctrine\ORM\Query; |
16 |
use Symfony\Bundle\FrameworkBundle\Command\ContainerAwareCommand; |
|
17 |
use Symfony\Component\Console\Input\InputArgument; |
|
18 |
use Symfony\Component\Console\Input\InputInterface; |
|
19 |
use Symfony\Component\Console\Input\InputOption; |
|
20 |
use Symfony\Component\Console\Output\OutputInterface; |
|
21 |
||
22 |
class SyncDocumentsCommand extends ContainerAwareCommand |
|
23 |
{ |
|
24 |
protected function configure() |
|
25 |
{ |
|
26 |
parent::configure(); |
|
27 |
||
28 |
$this |
|
29 |
->setName('wikitag:sync-doc') |
|
30 |
->setDescription('Synchronize and index document class') |
|
| 27 | 31 |
->addOption('missing', 'm', InputOption::VALUE_NONE, "process missing") |
32 |
->addOption('tags', 't', InputOption::VALUE_NONE, "update tags") |
|
| 6 | 33 |
->addOption('clear', 'c', InputOption::VALUE_NONE, "clear all docs"); |
34 |
} |
|
35 |
||
36 |
protected function execute(InputInterface $input, OutputInterface $output) |
|
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 | 39 |
$clear = $input->getOption('clear'); |
| 27 | 40 |
$missing = $input->getOption('missing'); |
| 6 | 41 |
|
42 |
||
43 |
$doctrine = $this->getContainer()->get('doctrine'); |
|
44 |
||
45 |
$docrep = $doctrine->getRepository('WikiTagBundle:Document'); |
|
46 |
$rep = $doctrine->getRepository($class); |
|
47 |
||
48 |
if(is_null($rep)) { |
|
49 |
//TODO : translate |
|
50 |
$output->writeln("$class does not have a repository : exiting."); |
|
51 |
return ; |
|
52 |
} |
|
53 |
||
| 27 | 54 |
if($input->getOption('tags')) |
55 |
{ |
|
56 |
if(!$missing) |
|
57 |
{ |
|
58 |
$docquery = $doctrine->getEntityManager()->createQuery("SELECT doc from WikiTagBundle:Document doc"); |
|
59 |
$doccountquery = $doctrine->getEntityManager()->createQuery("SELECT COUNT(doc.id) from WikiTagBundle:Document doc"); |
|
60 |
} |
|
61 |
else |
|
62 |
{ |
|
63 |
$docquery = $doctrine->getEntityManager()->createQuery("SELECT doc from WikiTagBundle:Document doc WHERE doc.tagsStr IS NULL"); |
|
64 |
$doccountquery = $doctrine->getEntityManager()->createQuery("SELECT COUNT(doc.id) from WikiTagBundle:Document doc WHERE doc.tagsStr IS NULL"); |
|
65 |
} |
|
66 |
||
67 |
||
68 |
$total = $doccountquery->getSingleScalarResult(); |
|
69 |
$done = 0; |
|
70 |
$iterable = $docquery->iterate(); |
|
71 |
$todetach = array(); |
|
72 |
while (($row = $iterable->next()) !== false) { |
|
73 |
$done++; |
|
74 |
$memory = ((($done%10)==0)?" - mem: ".strval(memory_get_usage(true)):""); |
|
75 |
$doc = $row[0]; |
|
76 |
$todetach[] = $doc; |
|
77 |
$output->writeln("Process doc id ".$doc->getId()." $done/$total ".strval(intval(floatval($done)/floatval($total)*100.0))."%$memory"); |
|
78 |
$docrep->updateTagsStr($doc); |
|
| 34 | 79 |
//dispatch event |
80 |
$event_dispatcher = $this->getContainer()->get('event_dispatcher'); |
|
81 |
$event = new DocumentTagEvent($doc); |
|
82 |
$event_dispatcher->dispatch(WikiTagEvents::onTagChanged, $event); |
|
83 |
||
| 27 | 84 |
if($done%10 == 0) |
85 |
{ |
|
86 |
$doctrine->getEntityManager()->flush(); |
|
87 |
foreach($todetach as $obj) |
|
88 |
{ |
|
89 |
$doctrine->getEntityManager()->detach($obj); |
|
90 |
} |
|
91 |
$todetach = array(); |
|
92 |
} |
|
93 |
} |
|
94 |
$doctrine->getEntityManager()->flush(); |
|
95 |
||
96 |
return; |
|
97 |
||
98 |
} |
|
99 |
||
| 6 | 100 |
//TODO : check class to implement DocumentInterface |
101 |
//TODO : write progress |
|
| 27 | 102 |
if($missing) |
103 |
{ |
|
104 |
$docquery = $doctrine->getEntityManager()->createQuery("SELECT doc FROM $class doc WHERE doc.id not in (SELECT wtdoc FROM WikiTagBundle:Document wtdoc)"); |
|
105 |
$doccountquery = $doctrine->getEntityManager()->createQuery("SELECT count(doc.id) FROM $class doc WHERE doc.id not in (SELECT wtdoc FROM WikiTagBundle:Document wtdoc)"); |
|
106 |
//$doclist = $doctrine->getEntityManager()->createQuery("SELECT doc FROM $class doc WHERE doc.id not in (SELECT wtdoc FROM WikiTagBundle:Document wtdoc)")->getResult(); |
|
107 |
} |
|
108 |
else |
|
109 |
{ |
|
110 |
$docquery = $doctrine->getEntityManager()->createQuery("SELECT doc FROM $class doc"); |
|
111 |
$doccountquery = $doctrine->getEntityManager()->createQuery("SELECT count(doc.id) FROM $class doc"); |
|
112 |
//$doclist = $rep->findAll(); |
|
113 |
} |
|
114 |
$total = $doccountquery->getSingleScalarResult(); |
|
| 24 | 115 |
$done = 0; |
| 27 | 116 |
$iterable = $docquery->iterate(); |
117 |
while (($row = $iterable->next()) !== false) { |
|
| 24 | 118 |
$done++; |
| 27 | 119 |
$doc = $row[0]; |
120 |
$memory = ((($done%10)==0)?" - mem: ".strval(memory_get_usage(true)):""); |
|
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 | 123 |
if($done%10 == 0) |
124 |
{ |
|
125 |
$doctrine->getEntityManager()->flush(); |
|
126 |
$doctrine->getEntityManager()->clear(); |
|
127 |
} |
|
| 6 | 128 |
} |
129 |
$doctrine->getEntityManager()->flush(); |
|
130 |
||
131 |
if($clear) { |
|
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 | 135 |
$doctrine->getEntityManager()->flush(); |
136 |
} |
|
137 |
||
138 |
$output->writeln(strval(count($doclist)) ." documents imported."); |
|
139 |
} |
|
140 |
} |