Command/QueryWikipediaCommand.php
changeset 47 34718ebfb3c0
parent 43 54f204bceb28
child 58 87bf6ec8af90
--- a/Command/QueryWikipediaCommand.php	Mon Nov 28 10:34:24 2011 +0100
+++ b/Command/QueryWikipediaCommand.php	Mon Nov 28 12:19:56 2011 +0100
@@ -11,68 +11,22 @@
 
 use IRI\Bundle\WikiTagBundle\Utils\WikiTagUtils;
 
+use IRI\Bundle\WikiTagBundle\Model\Tag;
 use Doctrine\ORM\QueryBuilder;
-use Symfony\Bundle\FrameworkBundle\Command\ContainerAwareCommand;
 use Symfony\Component\Console\Input\InputArgument;
 use Symfony\Component\Console\Input\InputInterface;
 use Symfony\Component\Console\Input\InputOption;
 use Symfony\Component\Console\Output\OutputInterface;
 
-class QueryWikipediaCommand extends ContainerAwareCommand
+class QueryWikipediaCommand extends ProgressContainerAwareCommand
 {
 
-    private function showProgress(OutputInterface $output, $current, $total, $label, $width)
-    {
-        $percent = (floatval($current)/floatval($total)) * 100.0;
-        $marks = intval(floor(floatval($width) * ($percent / 100.0) ));
-        $spaces = $width - $marks;
-        
-        $status_bar="\r[";
-        $status_bar.=str_repeat("=", $marks);
-        if($marks<$width){
-            $status_bar.=">";
-            $status_bar.=str_repeat(" ", $spaces);
-        } else {
-            $status_bar.="=";
-        }
-        
-        $disp=str_pad(number_format($percent, 0),3, " ", STR_PAD_LEFT);
-        
-        $label = str_pad(substr($label,0,50), 50, " ");
-        $current_str = str_pad($current, strlen("$total"), " ", STR_PAD_LEFT);
-        
-        $status_bar.="] $disp%  $current_str/$total : $label";
-        
-        $output->write("$status_bar  ");
-        
-        if($current == $total) {
-            $output->writeln("");
-        }
-        
-    }
-    
     private function processTag($tag, $em)
     {
         $tag_label_normalized = WikiTagUtils::normalizeTag($tag->getLabel());
         $wp_response = WikiTagUtils::getWikipediaInfo($tag_label_normalized);
         
-        $new_label = $wp_response['new_label'];
-        $status = $wp_response['status'];
-        $url = $wp_response['wikipedia_url'];
-        $pageid = $wp_response['pageid'];
-        $dbpedia_uri = $wp_response["dbpedia_uri"];
-        $wikipedia_revision_id = $wp_response['revision_id'];
-        
-        # We save the datas
-        if($new_label!=null){
-            $tag->setLabel($new_label);
-        }
-        if($status!=null){
-            $tag->setUrlStatus($status);
-        }
-        $tag->setWikipediaUrl($url);
-        $tag->setWikipediaPageId($pageid);
-        $tag->setDbpediaUri($dbpedia_uri);
+        $tag->setWikipediaInfo($wp_response);
         
         // Save datas.
         $em->persist($tag);
@@ -88,7 +42,8 @@
             ->setName('wikitag:query-wikipedia')
             ->setDescription('Query wikipedia for tags.')
             ->addOption("force","f",InputOption::VALUE_NONE, "Force remove tags")
-            ->addOption("all","a",InputOption::VALUE_NONE, "Force remove tags")
+            ->addOption("all","a",InputOption::VALUE_NONE, "all")
+            ->addOption("redirection",null,InputOption::VALUE_NONE, "Treat redirections")
             ->addOption("random","r",InputOption::VALUE_NONE, "randomize query on tags")
             ->addOption("site","S",InputOption::VALUE_OPTIONAL, "the url for the wikipedia site", "http://fr.wikipedia.org/w/api.php")
             ->addOption("limit","l",InputOption::VALUE_OPTIONAL, "number of tag to process", -1)
@@ -101,6 +56,7 @@
         $force = $input->getOption('force');
         $all = $input->getOption('all');
         $random = $input->getOption('random');
+        $redirection = $input->getOption('redirection');
         $site = $input->getOption('site');
         $limit = intval($input->getOption('limit'));
         $start = intval($input->getOption('start'));
@@ -113,7 +69,12 @@
         
         if(!$all)
         {
-            $qb->where($qb->expr()->isNull("t.urlStatus"));
+            if($redirection) {
+                $qb->where($qb->expr()->andx($qb->expr()->eq("t.urlStatus",Tag::$TAG_URL_STATUS_DICT['redirection']), $qb->expr()->isNull("t.alternativeLabel")));
+            }
+            else {
+                $qb->where($qb->expr()->isNull("t.urlStatus"));
+            }
         }
         
         if($start > 0)
@@ -133,6 +94,12 @@
         $count = count($qb_count->getQuery()->getScalarResult());
         $doctrine->getEntityManager()->clear();
         
+        if($count === 0)
+        {
+            $output->writeln("No tag to process, exit.");
+            return;
+        }
+        
         if(! $force && $input->isInteractive())
         {
             $dialog = $this->getHelper('dialog');