| 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 |
| 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 |
||
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') |
|
| 27 | 30 |
->addOption('missing', 'm', InputOption::VALUE_NONE, "process missing") |
31 |
->addOption('tags', 't', InputOption::VALUE_NONE, "update tags") |
|
| 6 | 32 |
->addOption('clear', 'c', InputOption::VALUE_NONE, "clear all docs"); |
33 |
} |
|
34 |
||
35 |
protected function execute(InputInterface $input, OutputInterface $output) |
|
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 | 38 |
$clear = $input->getOption('clear'); |
| 27 | 39 |
$missing = $input->getOption('missing'); |
| 6 | 40 |
|
41 |
||
42 |
$doctrine = $this->getContainer()->get('doctrine'); |
|
43 |
||
44 |
$docrep = $doctrine->getRepository('WikiTagBundle:Document'); |
|
45 |
$rep = $doctrine->getRepository($class); |
|
46 |
||
47 |
if(is_null($rep)) { |
|
48 |
//TODO : translate |
|
49 |
$output->writeln("$class does not have a repository : exiting."); |
|
50 |
return ; |
|
51 |
} |
|
52 |
||
| 27 | 53 |
if($input->getOption('tags')) |
54 |
{ |
|
55 |
if(!$missing) |
|
56 |
{ |
|
57 |
$docquery = $doctrine->getEntityManager()->createQuery("SELECT doc from WikiTagBundle:Document doc"); |
|
58 |
$doccountquery = $doctrine->getEntityManager()->createQuery("SELECT COUNT(doc.id) from WikiTagBundle:Document doc"); |
|
59 |
} |
|
60 |
else |
|
61 |
{ |
|
62 |
$docquery = $doctrine->getEntityManager()->createQuery("SELECT doc from WikiTagBundle:Document doc WHERE doc.tagsStr IS NULL"); |
|
63 |
$doccountquery = $doctrine->getEntityManager()->createQuery("SELECT COUNT(doc.id) from WikiTagBundle:Document doc WHERE doc.tagsStr IS NULL"); |
|
64 |
} |
|
65 |
||
66 |
||
67 |
$total = $doccountquery->getSingleScalarResult(); |
|
68 |
$done = 0; |
|
69 |
$iterable = $docquery->iterate(); |
|
70 |
$todetach = array(); |
|
71 |
while (($row = $iterable->next()) !== false) { |
|
72 |
$done++; |
|
73 |
$memory = ((($done%10)==0)?" - mem: ".strval(memory_get_usage(true)):""); |
|
74 |
$doc = $row[0]; |
|
75 |
$todetach[] = $doc; |
|
76 |
$output->writeln("Process doc id ".$doc->getId()." $done/$total ".strval(intval(floatval($done)/floatval($total)*100.0))."%$memory"); |
|
77 |
$docrep->updateTagsStr($doc); |
|
78 |
if($done%10 == 0) |
|
79 |
{ |
|
80 |
$doctrine->getEntityManager()->flush(); |
|
81 |
foreach($todetach as $obj) |
|
82 |
{ |
|
83 |
$doctrine->getEntityManager()->detach($obj); |
|
84 |
} |
|
85 |
$todetach = array(); |
|
86 |
} |
|
87 |
} |
|
88 |
$doctrine->getEntityManager()->flush(); |
|
89 |
||
90 |
return; |
|
91 |
||
92 |
} |
|
93 |
||
| 6 | 94 |
//TODO : check class to implement DocumentInterface |
95 |
//TODO : write progress |
|
| 27 | 96 |
if($missing) |
97 |
{ |
|
98 |
$docquery = $doctrine->getEntityManager()->createQuery("SELECT doc FROM $class doc WHERE doc.id not in (SELECT wtdoc FROM WikiTagBundle:Document wtdoc)"); |
|
99 |
$doccountquery = $doctrine->getEntityManager()->createQuery("SELECT count(doc.id) FROM $class doc WHERE doc.id not in (SELECT wtdoc FROM WikiTagBundle:Document wtdoc)"); |
|
100 |
//$doclist = $doctrine->getEntityManager()->createQuery("SELECT doc FROM $class doc WHERE doc.id not in (SELECT wtdoc FROM WikiTagBundle:Document wtdoc)")->getResult(); |
|
101 |
} |
|
102 |
else |
|
103 |
{ |
|
104 |
$docquery = $doctrine->getEntityManager()->createQuery("SELECT doc FROM $class doc"); |
|
105 |
$doccountquery = $doctrine->getEntityManager()->createQuery("SELECT count(doc.id) FROM $class doc"); |
|
106 |
//$doclist = $rep->findAll(); |
|
107 |
} |
|
108 |
$total = $doccountquery->getSingleScalarResult(); |
|
| 24 | 109 |
$done = 0; |
| 27 | 110 |
$iterable = $docquery->iterate(); |
111 |
while (($row = $iterable->next()) !== false) { |
|
| 24 | 112 |
$done++; |
| 27 | 113 |
$doc = $row[0]; |
114 |
$memory = ((($done%10)==0)?" - mem: ".strval(memory_get_usage(true)):""); |
|
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 | 117 |
if($done%10 == 0) |
118 |
{ |
|
119 |
$doctrine->getEntityManager()->flush(); |
|
120 |
$doctrine->getEntityManager()->clear(); |
|
121 |
} |
|
| 6 | 122 |
} |
123 |
$doctrine->getEntityManager()->flush(); |
|
124 |
||
125 |
if($clear) { |
|
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 | 129 |
$doctrine->getEntityManager()->flush(); |
130 |
} |
|
131 |
||
132 |
$output->writeln(strval(count($doclist)) ." documents imported."); |
|
133 |
} |
|
134 |
} |