| author | ymh <ymh.work@gmail.com> |
| Fri, 26 Sep 2014 10:04:40 +0200 | |
| changeset 136 | f209fcb0df6c |
| parent 38 | e48c2e503945 |
| permissions | -rwxr-xr-x |
|
38
e48c2e503945
add commands to purge taf=gs and query wikipedia
ymh <ymh.work@gmail.com>
parents:
diff
changeset
|
1 |
<?php |
|
e48c2e503945
add commands to purge taf=gs and query wikipedia
ymh <ymh.work@gmail.com>
parents:
diff
changeset
|
2 |
/* |
|
e48c2e503945
add commands to purge taf=gs and query wikipedia
ymh <ymh.work@gmail.com>
parents:
diff
changeset
|
3 |
* This file is part of the WikiTagBundle package. |
|
e48c2e503945
add commands to purge taf=gs and query wikipedia
ymh <ymh.work@gmail.com>
parents:
diff
changeset
|
4 |
* |
|
e48c2e503945
add commands to purge taf=gs and query wikipedia
ymh <ymh.work@gmail.com>
parents:
diff
changeset
|
5 |
* (c) IRI <http://www.iri.centrepompidou.fr/> |
|
e48c2e503945
add commands to purge taf=gs and query wikipedia
ymh <ymh.work@gmail.com>
parents:
diff
changeset
|
6 |
* |
|
e48c2e503945
add commands to purge taf=gs and query wikipedia
ymh <ymh.work@gmail.com>
parents:
diff
changeset
|
7 |
* For the full copyright and license information, please view the LICENSE |
|
e48c2e503945
add commands to purge taf=gs and query wikipedia
ymh <ymh.work@gmail.com>
parents:
diff
changeset
|
8 |
* file that was distributed with this source code. |
|
e48c2e503945
add commands to purge taf=gs and query wikipedia
ymh <ymh.work@gmail.com>
parents:
diff
changeset
|
9 |
*/ |
|
e48c2e503945
add commands to purge taf=gs and query wikipedia
ymh <ymh.work@gmail.com>
parents:
diff
changeset
|
10 |
namespace IRI\Bundle\WikiTagBundle\Command; |
|
e48c2e503945
add commands to purge taf=gs and query wikipedia
ymh <ymh.work@gmail.com>
parents:
diff
changeset
|
11 |
|
|
e48c2e503945
add commands to purge taf=gs and query wikipedia
ymh <ymh.work@gmail.com>
parents:
diff
changeset
|
12 |
use Doctrine\ORM\QueryBuilder; |
|
e48c2e503945
add commands to purge taf=gs and query wikipedia
ymh <ymh.work@gmail.com>
parents:
diff
changeset
|
13 |
use Doctrine\ORM\Query\ResultSetMapping; |
|
e48c2e503945
add commands to purge taf=gs and query wikipedia
ymh <ymh.work@gmail.com>
parents:
diff
changeset
|
14 |
use Symfony\Bundle\FrameworkBundle\Command\ContainerAwareCommand; |
|
e48c2e503945
add commands to purge taf=gs and query wikipedia
ymh <ymh.work@gmail.com>
parents:
diff
changeset
|
15 |
use Symfony\Component\Console\Input\InputArgument; |
|
e48c2e503945
add commands to purge taf=gs and query wikipedia
ymh <ymh.work@gmail.com>
parents:
diff
changeset
|
16 |
use Symfony\Component\Console\Input\InputInterface; |
|
e48c2e503945
add commands to purge taf=gs and query wikipedia
ymh <ymh.work@gmail.com>
parents:
diff
changeset
|
17 |
use Symfony\Component\Console\Input\InputOption; |
|
e48c2e503945
add commands to purge taf=gs and query wikipedia
ymh <ymh.work@gmail.com>
parents:
diff
changeset
|
18 |
use Symfony\Component\Console\Output\OutputInterface; |
|
e48c2e503945
add commands to purge taf=gs and query wikipedia
ymh <ymh.work@gmail.com>
parents:
diff
changeset
|
19 |
|
|
e48c2e503945
add commands to purge taf=gs and query wikipedia
ymh <ymh.work@gmail.com>
parents:
diff
changeset
|
20 |
class PurgeTagsCommand extends ContainerAwareCommand |
|
e48c2e503945
add commands to purge taf=gs and query wikipedia
ymh <ymh.work@gmail.com>
parents:
diff
changeset
|
21 |
{ |
|
e48c2e503945
add commands to purge taf=gs and query wikipedia
ymh <ymh.work@gmail.com>
parents:
diff
changeset
|
22 |
protected function configure() |
|
e48c2e503945
add commands to purge taf=gs and query wikipedia
ymh <ymh.work@gmail.com>
parents:
diff
changeset
|
23 |
{ |
|
e48c2e503945
add commands to purge taf=gs and query wikipedia
ymh <ymh.work@gmail.com>
parents:
diff
changeset
|
24 |
parent::configure(); |
|
e48c2e503945
add commands to purge taf=gs and query wikipedia
ymh <ymh.work@gmail.com>
parents:
diff
changeset
|
25 |
|
|
e48c2e503945
add commands to purge taf=gs and query wikipedia
ymh <ymh.work@gmail.com>
parents:
diff
changeset
|
26 |
$this |
|
e48c2e503945
add commands to purge taf=gs and query wikipedia
ymh <ymh.work@gmail.com>
parents:
diff
changeset
|
27 |
->setName('wikitag:purge-tags') |
|
e48c2e503945
add commands to purge taf=gs and query wikipedia
ymh <ymh.work@gmail.com>
parents:
diff
changeset
|
28 |
->setDescription('Purge tags') |
|
e48c2e503945
add commands to purge taf=gs and query wikipedia
ymh <ymh.work@gmail.com>
parents:
diff
changeset
|
29 |
->addOption("list","l",InputOption::VALUE_NONE, "List tags tp remove") |
|
e48c2e503945
add commands to purge taf=gs and query wikipedia
ymh <ymh.work@gmail.com>
parents:
diff
changeset
|
30 |
->addOption("force","f",InputOption::VALUE_NONE, "Force remove tags"); |
|
e48c2e503945
add commands to purge taf=gs and query wikipedia
ymh <ymh.work@gmail.com>
parents:
diff
changeset
|
31 |
} |
|
e48c2e503945
add commands to purge taf=gs and query wikipedia
ymh <ymh.work@gmail.com>
parents:
diff
changeset
|
32 |
|
|
e48c2e503945
add commands to purge taf=gs and query wikipedia
ymh <ymh.work@gmail.com>
parents:
diff
changeset
|
33 |
|
|
e48c2e503945
add commands to purge taf=gs and query wikipedia
ymh <ymh.work@gmail.com>
parents:
diff
changeset
|
34 |
protected function execute(InputInterface $input, OutputInterface $output) |
|
e48c2e503945
add commands to purge taf=gs and query wikipedia
ymh <ymh.work@gmail.com>
parents:
diff
changeset
|
35 |
{ |
|
e48c2e503945
add commands to purge taf=gs and query wikipedia
ymh <ymh.work@gmail.com>
parents:
diff
changeset
|
36 |
$force = $input->getOption('force'); |
|
e48c2e503945
add commands to purge taf=gs and query wikipedia
ymh <ymh.work@gmail.com>
parents:
diff
changeset
|
37 |
$list = $input->getOption('list'); |
|
e48c2e503945
add commands to purge taf=gs and query wikipedia
ymh <ymh.work@gmail.com>
parents:
diff
changeset
|
38 |
|
|
e48c2e503945
add commands to purge taf=gs and query wikipedia
ymh <ymh.work@gmail.com>
parents:
diff
changeset
|
39 |
//get tags with no documents |
|
e48c2e503945
add commands to purge taf=gs and query wikipedia
ymh <ymh.work@gmail.com>
parents:
diff
changeset
|
40 |
$doctrine = $this->getContainer()->get('doctrine'); |
|
e48c2e503945
add commands to purge taf=gs and query wikipedia
ymh <ymh.work@gmail.com>
parents:
diff
changeset
|
41 |
|
|
e48c2e503945
add commands to purge taf=gs and query wikipedia
ymh <ymh.work@gmail.com>
parents:
diff
changeset
|
42 |
$qb = $doctrine->getEntityManager()->createQueryBuilder(); |
|
e48c2e503945
add commands to purge taf=gs and query wikipedia
ymh <ymh.work@gmail.com>
parents:
diff
changeset
|
43 |
$qb->select('t'); |
|
e48c2e503945
add commands to purge taf=gs and query wikipedia
ymh <ymh.work@gmail.com>
parents:
diff
changeset
|
44 |
$qb->from('WikiTagBundle:Tag','t'); |
|
e48c2e503945
add commands to purge taf=gs and query wikipedia
ymh <ymh.work@gmail.com>
parents:
diff
changeset
|
45 |
$qb->leftJoin('t.documents', 'dt', 'WITH', 't = dt.tag'); |
|
e48c2e503945
add commands to purge taf=gs and query wikipedia
ymh <ymh.work@gmail.com>
parents:
diff
changeset
|
46 |
$qb->addGroupBy('t.id'); |
|
e48c2e503945
add commands to purge taf=gs and query wikipedia
ymh <ymh.work@gmail.com>
parents:
diff
changeset
|
47 |
$qb->having($qb->expr()->eq($qb->expr()->count('dt.id'),':count')); |
|
e48c2e503945
add commands to purge taf=gs and query wikipedia
ymh <ymh.work@gmail.com>
parents:
diff
changeset
|
48 |
$qb->setParameter("count", 0); |
|
e48c2e503945
add commands to purge taf=gs and query wikipedia
ymh <ymh.work@gmail.com>
parents:
diff
changeset
|
49 |
|
|
e48c2e503945
add commands to purge taf=gs and query wikipedia
ymh <ymh.work@gmail.com>
parents:
diff
changeset
|
50 |
|
|
e48c2e503945
add commands to purge taf=gs and query wikipedia
ymh <ymh.work@gmail.com>
parents:
diff
changeset
|
51 |
$rsm = new ResultSetMapping(); |
|
e48c2e503945
add commands to purge taf=gs and query wikipedia
ymh <ymh.work@gmail.com>
parents:
diff
changeset
|
52 |
$rsm->addScalarResult("C","C"); |
|
e48c2e503945
add commands to purge taf=gs and query wikipedia
ymh <ymh.work@gmail.com>
parents:
diff
changeset
|
53 |
$count_query = $doctrine->getEntityManager()->createNativeQuery("SELECT COUNT(*) AS C FROM (".$qb->getQuery()->getSQL().") AS T", $rsm); |
|
e48c2e503945
add commands to purge taf=gs and query wikipedia
ymh <ymh.work@gmail.com>
parents:
diff
changeset
|
54 |
$count_query->setParameter(1, 0); |
|
e48c2e503945
add commands to purge taf=gs and query wikipedia
ymh <ymh.work@gmail.com>
parents:
diff
changeset
|
55 |
|
|
e48c2e503945
add commands to purge taf=gs and query wikipedia
ymh <ymh.work@gmail.com>
parents:
diff
changeset
|
56 |
$count = $count_query->getSingleScalarResult(); |
|
e48c2e503945
add commands to purge taf=gs and query wikipedia
ymh <ymh.work@gmail.com>
parents:
diff
changeset
|
57 |
|
|
e48c2e503945
add commands to purge taf=gs and query wikipedia
ymh <ymh.work@gmail.com>
parents:
diff
changeset
|
58 |
$output->writeln("<comment>$count tag(s) to delete.</comment>\n"); |
|
e48c2e503945
add commands to purge taf=gs and query wikipedia
ymh <ymh.work@gmail.com>
parents:
diff
changeset
|
59 |
|
|
e48c2e503945
add commands to purge taf=gs and query wikipedia
ymh <ymh.work@gmail.com>
parents:
diff
changeset
|
60 |
if($list) |
|
e48c2e503945
add commands to purge taf=gs and query wikipedia
ymh <ymh.work@gmail.com>
parents:
diff
changeset
|
61 |
{ |
|
e48c2e503945
add commands to purge taf=gs and query wikipedia
ymh <ymh.work@gmail.com>
parents:
diff
changeset
|
62 |
$query = $qb->getQuery(); |
|
e48c2e503945
add commands to purge taf=gs and query wikipedia
ymh <ymh.work@gmail.com>
parents:
diff
changeset
|
63 |
$result = $query->getResult(); |
|
e48c2e503945
add commands to purge taf=gs and query wikipedia
ymh <ymh.work@gmail.com>
parents:
diff
changeset
|
64 |
|
|
e48c2e503945
add commands to purge taf=gs and query wikipedia
ymh <ymh.work@gmail.com>
parents:
diff
changeset
|
65 |
$i = 1; |
|
e48c2e503945
add commands to purge taf=gs and query wikipedia
ymh <ymh.work@gmail.com>
parents:
diff
changeset
|
66 |
foreach($result as $tag) |
|
e48c2e503945
add commands to purge taf=gs and query wikipedia
ymh <ymh.work@gmail.com>
parents:
diff
changeset
|
67 |
{ |
|
e48c2e503945
add commands to purge taf=gs and query wikipedia
ymh <ymh.work@gmail.com>
parents:
diff
changeset
|
68 |
$output->writeln(strval($i++)."- ".$tag->getLabel()); |
|
e48c2e503945
add commands to purge taf=gs and query wikipedia
ymh <ymh.work@gmail.com>
parents:
diff
changeset
|
69 |
} |
|
e48c2e503945
add commands to purge taf=gs and query wikipedia
ymh <ymh.work@gmail.com>
parents:
diff
changeset
|
70 |
$output->writeln(""); |
|
e48c2e503945
add commands to purge taf=gs and query wikipedia
ymh <ymh.work@gmail.com>
parents:
diff
changeset
|
71 |
} |
|
e48c2e503945
add commands to purge taf=gs and query wikipedia
ymh <ymh.work@gmail.com>
parents:
diff
changeset
|
72 |
else |
|
e48c2e503945
add commands to purge taf=gs and query wikipedia
ymh <ymh.work@gmail.com>
parents:
diff
changeset
|
73 |
{ |
|
e48c2e503945
add commands to purge taf=gs and query wikipedia
ymh <ymh.work@gmail.com>
parents:
diff
changeset
|
74 |
if(! $force && $input->isInteractive()) |
|
e48c2e503945
add commands to purge taf=gs and query wikipedia
ymh <ymh.work@gmail.com>
parents:
diff
changeset
|
75 |
{ |
|
e48c2e503945
add commands to purge taf=gs and query wikipedia
ymh <ymh.work@gmail.com>
parents:
diff
changeset
|
76 |
$dialog = $this->getHelper('dialog'); |
|
e48c2e503945
add commands to purge taf=gs and query wikipedia
ymh <ymh.work@gmail.com>
parents:
diff
changeset
|
77 |
if (!$dialog->askConfirmation($output, '<question>Confirm deletion? (y/N) : </question>', false)) { |
|
e48c2e503945
add commands to purge taf=gs and query wikipedia
ymh <ymh.work@gmail.com>
parents:
diff
changeset
|
78 |
return; |
|
e48c2e503945
add commands to purge taf=gs and query wikipedia
ymh <ymh.work@gmail.com>
parents:
diff
changeset
|
79 |
} |
|
e48c2e503945
add commands to purge taf=gs and query wikipedia
ymh <ymh.work@gmail.com>
parents:
diff
changeset
|
80 |
} |
|
e48c2e503945
add commands to purge taf=gs and query wikipedia
ymh <ymh.work@gmail.com>
parents:
diff
changeset
|
81 |
|
|
e48c2e503945
add commands to purge taf=gs and query wikipedia
ymh <ymh.work@gmail.com>
parents:
diff
changeset
|
82 |
|
|
e48c2e503945
add commands to purge taf=gs and query wikipedia
ymh <ymh.work@gmail.com>
parents:
diff
changeset
|
83 |
$id_delete = array(); |
|
e48c2e503945
add commands to purge taf=gs and query wikipedia
ymh <ymh.work@gmail.com>
parents:
diff
changeset
|
84 |
foreach($qb->getQuery()->getResult() as $tag) |
|
e48c2e503945
add commands to purge taf=gs and query wikipedia
ymh <ymh.work@gmail.com>
parents:
diff
changeset
|
85 |
{ |
|
e48c2e503945
add commands to purge taf=gs and query wikipedia
ymh <ymh.work@gmail.com>
parents:
diff
changeset
|
86 |
$id_delete[] = $tag->getId(); |
|
e48c2e503945
add commands to purge taf=gs and query wikipedia
ymh <ymh.work@gmail.com>
parents:
diff
changeset
|
87 |
} |
|
e48c2e503945
add commands to purge taf=gs and query wikipedia
ymh <ymh.work@gmail.com>
parents:
diff
changeset
|
88 |
|
|
e48c2e503945
add commands to purge taf=gs and query wikipedia
ymh <ymh.work@gmail.com>
parents:
diff
changeset
|
89 |
$delete_qb = $doctrine->getEntityManager()->createQueryBuilder(); |
|
e48c2e503945
add commands to purge taf=gs and query wikipedia
ymh <ymh.work@gmail.com>
parents:
diff
changeset
|
90 |
$delete_qb->delete('WikiTagBundle:Tag','tag'); |
|
e48c2e503945
add commands to purge taf=gs and query wikipedia
ymh <ymh.work@gmail.com>
parents:
diff
changeset
|
91 |
$delete_qb->where($delete_qb->expr()->in('tag.id', $id_delete)); |
|
e48c2e503945
add commands to purge taf=gs and query wikipedia
ymh <ymh.work@gmail.com>
parents:
diff
changeset
|
92 |
|
|
e48c2e503945
add commands to purge taf=gs and query wikipedia
ymh <ymh.work@gmail.com>
parents:
diff
changeset
|
93 |
$result = $delete_qb->getQuery()->getResult(); |
|
e48c2e503945
add commands to purge taf=gs and query wikipedia
ymh <ymh.work@gmail.com>
parents:
diff
changeset
|
94 |
|
|
e48c2e503945
add commands to purge taf=gs and query wikipedia
ymh <ymh.work@gmail.com>
parents:
diff
changeset
|
95 |
$output->writeln("Tag deleted : $result \n"); |
|
e48c2e503945
add commands to purge taf=gs and query wikipedia
ymh <ymh.work@gmail.com>
parents:
diff
changeset
|
96 |
|
|
e48c2e503945
add commands to purge taf=gs and query wikipedia
ymh <ymh.work@gmail.com>
parents:
diff
changeset
|
97 |
} |
|
e48c2e503945
add commands to purge taf=gs and query wikipedia
ymh <ymh.work@gmail.com>
parents:
diff
changeset
|
98 |
|
|
e48c2e503945
add commands to purge taf=gs and query wikipedia
ymh <ymh.work@gmail.com>
parents:
diff
changeset
|
99 |
} |
|
e48c2e503945
add commands to purge taf=gs and query wikipedia
ymh <ymh.work@gmail.com>
parents:
diff
changeset
|
100 |
} |