| author | ymh <ymh.work@gmail.com> |
| Fri, 25 Nov 2011 18:55:42 +0100 | |
| changeset 42 | 0e57c730bb18 |
| parent 38 | e48c2e503945 |
| child 43 | 54f204bceb28 |
| permissions | -rw-r--r-- |
|
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 IRI\Bundle\WikiTagBundle\Utils\WikiTagUtils; |
|
e48c2e503945
add commands to purge taf=gs and query wikipedia
ymh <ymh.work@gmail.com>
parents:
diff
changeset
|
13 |
|
|
e48c2e503945
add commands to purge taf=gs and query wikipedia
ymh <ymh.work@gmail.com>
parents:
diff
changeset
|
14 |
use Doctrine\ORM\QueryBuilder; |
|
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 |
|
|
42
0e57c730bb18
Documentation and add alternative wp url and label + migrations
ymh <ymh.work@gmail.com>
parents:
38
diff
changeset
|
20 |
class QueryWikipediaCommand extends ProgressContainerAwareCommand |
|
38
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 |
|
|
e48c2e503945
add commands to purge taf=gs and query wikipedia
ymh <ymh.work@gmail.com>
parents:
diff
changeset
|
23 |
private function processTag($tag, $em) |
|
e48c2e503945
add commands to purge taf=gs and query wikipedia
ymh <ymh.work@gmail.com>
parents:
diff
changeset
|
24 |
{ |
|
e48c2e503945
add commands to purge taf=gs and query wikipedia
ymh <ymh.work@gmail.com>
parents:
diff
changeset
|
25 |
$tag_label_normalized = WikiTagUtils::normalizeTag($tag->getLabel()); |
|
e48c2e503945
add commands to purge taf=gs and query wikipedia
ymh <ymh.work@gmail.com>
parents:
diff
changeset
|
26 |
$wp_response = WikiTagUtils::getWikipediaInfo($tag_label_normalized); |
|
e48c2e503945
add commands to purge taf=gs and query wikipedia
ymh <ymh.work@gmail.com>
parents:
diff
changeset
|
27 |
|
|
42
0e57c730bb18
Documentation and add alternative wp url and label + migrations
ymh <ymh.work@gmail.com>
parents:
38
diff
changeset
|
28 |
$tag->setWikipediaInfo($wp_response); |
|
38
e48c2e503945
add commands to purge taf=gs and query wikipedia
ymh <ymh.work@gmail.com>
parents:
diff
changeset
|
29 |
|
|
e48c2e503945
add commands to purge taf=gs and query wikipedia
ymh <ymh.work@gmail.com>
parents:
diff
changeset
|
30 |
// Save datas. |
|
e48c2e503945
add commands to purge taf=gs and query wikipedia
ymh <ymh.work@gmail.com>
parents:
diff
changeset
|
31 |
$em->persist($tag); |
|
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 |
|
|
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 |
protected function configure() |
|
e48c2e503945
add commands to purge taf=gs and query wikipedia
ymh <ymh.work@gmail.com>
parents:
diff
changeset
|
37 |
{ |
|
e48c2e503945
add commands to purge taf=gs and query wikipedia
ymh <ymh.work@gmail.com>
parents:
diff
changeset
|
38 |
parent::configure(); |
|
e48c2e503945
add commands to purge taf=gs and query wikipedia
ymh <ymh.work@gmail.com>
parents:
diff
changeset
|
39 |
|
|
e48c2e503945
add commands to purge taf=gs and query wikipedia
ymh <ymh.work@gmail.com>
parents:
diff
changeset
|
40 |
$this |
|
e48c2e503945
add commands to purge taf=gs and query wikipedia
ymh <ymh.work@gmail.com>
parents:
diff
changeset
|
41 |
->setName('wikitag:query-wikipedia') |
|
e48c2e503945
add commands to purge taf=gs and query wikipedia
ymh <ymh.work@gmail.com>
parents:
diff
changeset
|
42 |
->setDescription('Query wikipedia for tags.') |
|
e48c2e503945
add commands to purge taf=gs and query wikipedia
ymh <ymh.work@gmail.com>
parents:
diff
changeset
|
43 |
->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
|
44 |
->addOption("all","a",InputOption::VALUE_NONE, "Force remove tags") |
|
e48c2e503945
add commands to purge taf=gs and query wikipedia
ymh <ymh.work@gmail.com>
parents:
diff
changeset
|
45 |
->addOption("random","r",InputOption::VALUE_NONE, "randomize query on tags") |
|
e48c2e503945
add commands to purge taf=gs and query wikipedia
ymh <ymh.work@gmail.com>
parents:
diff
changeset
|
46 |
->addOption("site","S",InputOption::VALUE_OPTIONAL, "the url for the wikipedia site", "http://fr.wikipedia.org/w/api.php") |
|
e48c2e503945
add commands to purge taf=gs and query wikipedia
ymh <ymh.work@gmail.com>
parents:
diff
changeset
|
47 |
->addOption("limit","l",InputOption::VALUE_OPTIONAL, "number of tag to process", -1) |
|
e48c2e503945
add commands to purge taf=gs and query wikipedia
ymh <ymh.work@gmail.com>
parents:
diff
changeset
|
48 |
->addOption("start",null,InputOption::VALUE_OPTIONAL, "number of tag to ignore", 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 |
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
|
52 |
{ |
|
e48c2e503945
add commands to purge taf=gs and query wikipedia
ymh <ymh.work@gmail.com>
parents:
diff
changeset
|
53 |
|
|
e48c2e503945
add commands to purge taf=gs and query wikipedia
ymh <ymh.work@gmail.com>
parents:
diff
changeset
|
54 |
$force = $input->getOption('force'); |
|
e48c2e503945
add commands to purge taf=gs and query wikipedia
ymh <ymh.work@gmail.com>
parents:
diff
changeset
|
55 |
$all = $input->getOption('all'); |
|
e48c2e503945
add commands to purge taf=gs and query wikipedia
ymh <ymh.work@gmail.com>
parents:
diff
changeset
|
56 |
$random = $input->getOption('random'); |
|
e48c2e503945
add commands to purge taf=gs and query wikipedia
ymh <ymh.work@gmail.com>
parents:
diff
changeset
|
57 |
$site = $input->getOption('site'); |
|
e48c2e503945
add commands to purge taf=gs and query wikipedia
ymh <ymh.work@gmail.com>
parents:
diff
changeset
|
58 |
$limit = intval($input->getOption('limit')); |
|
e48c2e503945
add commands to purge taf=gs and query wikipedia
ymh <ymh.work@gmail.com>
parents:
diff
changeset
|
59 |
$start = intval($input->getOption('start')); |
|
e48c2e503945
add commands to purge taf=gs and query wikipedia
ymh <ymh.work@gmail.com>
parents:
diff
changeset
|
60 |
|
|
e48c2e503945
add commands to purge taf=gs and query wikipedia
ymh <ymh.work@gmail.com>
parents:
diff
changeset
|
61 |
$doctrine = $this->getContainer()->get('doctrine'); |
|
e48c2e503945
add commands to purge taf=gs and query wikipedia
ymh <ymh.work@gmail.com>
parents:
diff
changeset
|
62 |
$qb = $doctrine->getEntityManager()->createQueryBuilder(); |
|
e48c2e503945
add commands to purge taf=gs and query wikipedia
ymh <ymh.work@gmail.com>
parents:
diff
changeset
|
63 |
|
|
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 |
$qb->from('WikiTagBundle:Tag','t'); |
|
e48c2e503945
add commands to purge taf=gs and query wikipedia
ymh <ymh.work@gmail.com>
parents:
diff
changeset
|
66 |
|
|
e48c2e503945
add commands to purge taf=gs and query wikipedia
ymh <ymh.work@gmail.com>
parents:
diff
changeset
|
67 |
if(!$all) |
|
e48c2e503945
add commands to purge taf=gs and query wikipedia
ymh <ymh.work@gmail.com>
parents:
diff
changeset
|
68 |
{ |
|
e48c2e503945
add commands to purge taf=gs and query wikipedia
ymh <ymh.work@gmail.com>
parents:
diff
changeset
|
69 |
$qb->where($qb->expr()->isNull("t.urlStatus")); |
|
e48c2e503945
add commands to purge taf=gs and query wikipedia
ymh <ymh.work@gmail.com>
parents:
diff
changeset
|
70 |
} |
|
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 |
if($start > 0) |
|
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 |
$qb->setFirstResult($start); |
|
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 |
|
|
e48c2e503945
add commands to purge taf=gs and query wikipedia
ymh <ymh.work@gmail.com>
parents:
diff
changeset
|
77 |
if($limit>=0) |
|
e48c2e503945
add commands to purge taf=gs and query wikipedia
ymh <ymh.work@gmail.com>
parents:
diff
changeset
|
78 |
{ |
|
e48c2e503945
add commands to purge taf=gs and query wikipedia
ymh <ymh.work@gmail.com>
parents:
diff
changeset
|
79 |
$qb->setMaxResults($limit); |
|
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 |
$qb_count = clone $qb; |
|
e48c2e503945
add commands to purge taf=gs and query wikipedia
ymh <ymh.work@gmail.com>
parents:
diff
changeset
|
83 |
|
|
e48c2e503945
add commands to purge taf=gs and query wikipedia
ymh <ymh.work@gmail.com>
parents:
diff
changeset
|
84 |
$qb_count->select("t.id"); |
|
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 |
$count = count($qb_count->getQuery()->getScalarResult()); |
|
e48c2e503945
add commands to purge taf=gs and query wikipedia
ymh <ymh.work@gmail.com>
parents:
diff
changeset
|
87 |
$doctrine->getEntityManager()->clear(); |
|
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 |
if(! $force && $input->isInteractive()) |
|
e48c2e503945
add commands to purge taf=gs and query wikipedia
ymh <ymh.work@gmail.com>
parents:
diff
changeset
|
90 |
{ |
|
e48c2e503945
add commands to purge taf=gs and query wikipedia
ymh <ymh.work@gmail.com>
parents:
diff
changeset
|
91 |
$dialog = $this->getHelper('dialog'); |
|
e48c2e503945
add commands to purge taf=gs and query wikipedia
ymh <ymh.work@gmail.com>
parents:
diff
changeset
|
92 |
if (!$dialog->askConfirmation($output, "<question>This command will process $count tag(s). Continue ? (y/N) : </question>", false)) { |
|
e48c2e503945
add commands to purge taf=gs and query wikipedia
ymh <ymh.work@gmail.com>
parents:
diff
changeset
|
93 |
return; |
|
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 |
} |
|
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 |
$qb->select("t"); |
|
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 |
$done = 0; |
|
e48c2e503945
add commands to purge taf=gs and query wikipedia
ymh <ymh.work@gmail.com>
parents:
diff
changeset
|
100 |
$iterable = $qb->getQuery()->iterate(); |
|
e48c2e503945
add commands to purge taf=gs and query wikipedia
ymh <ymh.work@gmail.com>
parents:
diff
changeset
|
101 |
$doctrine->getEntityManager()->beginTransaction(); |
|
e48c2e503945
add commands to purge taf=gs and query wikipedia
ymh <ymh.work@gmail.com>
parents:
diff
changeset
|
102 |
while (($row = $iterable->next()) !== false) |
|
e48c2e503945
add commands to purge taf=gs and query wikipedia
ymh <ymh.work@gmail.com>
parents:
diff
changeset
|
103 |
{ |
|
e48c2e503945
add commands to purge taf=gs and query wikipedia
ymh <ymh.work@gmail.com>
parents:
diff
changeset
|
104 |
$done++; |
|
e48c2e503945
add commands to purge taf=gs and query wikipedia
ymh <ymh.work@gmail.com>
parents:
diff
changeset
|
105 |
$tag = $row[0]; |
|
e48c2e503945
add commands to purge taf=gs and query wikipedia
ymh <ymh.work@gmail.com>
parents:
diff
changeset
|
106 |
|
|
e48c2e503945
add commands to purge taf=gs and query wikipedia
ymh <ymh.work@gmail.com>
parents:
diff
changeset
|
107 |
$this->showProgress($output, $done, $count, $tag->getLabel(), 50); |
|
e48c2e503945
add commands to purge taf=gs and query wikipedia
ymh <ymh.work@gmail.com>
parents:
diff
changeset
|
108 |
|
|
e48c2e503945
add commands to purge taf=gs and query wikipedia
ymh <ymh.work@gmail.com>
parents:
diff
changeset
|
109 |
// process tag |
|
e48c2e503945
add commands to purge taf=gs and query wikipedia
ymh <ymh.work@gmail.com>
parents:
diff
changeset
|
110 |
$this->processTag($tag, $doctrine->getEntityManager()); |
|
e48c2e503945
add commands to purge taf=gs and query wikipedia
ymh <ymh.work@gmail.com>
parents:
diff
changeset
|
111 |
|
|
e48c2e503945
add commands to purge taf=gs and query wikipedia
ymh <ymh.work@gmail.com>
parents:
diff
changeset
|
112 |
if($done%100 == 0) |
|
e48c2e503945
add commands to purge taf=gs and query wikipedia
ymh <ymh.work@gmail.com>
parents:
diff
changeset
|
113 |
{ |
|
e48c2e503945
add commands to purge taf=gs and query wikipedia
ymh <ymh.work@gmail.com>
parents:
diff
changeset
|
114 |
$doctrine->getEntityManager()->flush(); |
|
e48c2e503945
add commands to purge taf=gs and query wikipedia
ymh <ymh.work@gmail.com>
parents:
diff
changeset
|
115 |
$doctrine->getEntityManager()->commit(); |
|
e48c2e503945
add commands to purge taf=gs and query wikipedia
ymh <ymh.work@gmail.com>
parents:
diff
changeset
|
116 |
$doctrine->getEntityManager()->clear(); |
|
e48c2e503945
add commands to purge taf=gs and query wikipedia
ymh <ymh.work@gmail.com>
parents:
diff
changeset
|
117 |
$doctrine->getEntityManager()->beginTransaction(); |
|
e48c2e503945
add commands to purge taf=gs and query wikipedia
ymh <ymh.work@gmail.com>
parents:
diff
changeset
|
118 |
} |
|
e48c2e503945
add commands to purge taf=gs and query wikipedia
ymh <ymh.work@gmail.com>
parents:
diff
changeset
|
119 |
} |
|
e48c2e503945
add commands to purge taf=gs and query wikipedia
ymh <ymh.work@gmail.com>
parents:
diff
changeset
|
120 |
$doctrine->getEntityManager()->flush(); |
|
e48c2e503945
add commands to purge taf=gs and query wikipedia
ymh <ymh.work@gmail.com>
parents:
diff
changeset
|
121 |
$doctrine->getEntityManager()->commit(); |
|
e48c2e503945
add commands to purge taf=gs and query wikipedia
ymh <ymh.work@gmail.com>
parents:
diff
changeset
|
122 |
|
|
e48c2e503945
add commands to purge taf=gs and query wikipedia
ymh <ymh.work@gmail.com>
parents:
diff
changeset
|
123 |
} |
|
e48c2e503945
add commands to purge taf=gs and query wikipedia
ymh <ymh.work@gmail.com>
parents:
diff
changeset
|
124 |
} |