Command/QueryWikipediaCommand.php
changeset 47 34718ebfb3c0
parent 43 54f204bceb28
child 58 87bf6ec8af90
equal deleted inserted replaced
46:6f643fc1de26 47:34718ebfb3c0
     9  */
     9  */
    10 namespace IRI\Bundle\WikiTagBundle\Command;
    10 namespace IRI\Bundle\WikiTagBundle\Command;
    11 
    11 
    12 use IRI\Bundle\WikiTagBundle\Utils\WikiTagUtils;
    12 use IRI\Bundle\WikiTagBundle\Utils\WikiTagUtils;
    13 
    13 
       
    14 use IRI\Bundle\WikiTagBundle\Model\Tag;
    14 use Doctrine\ORM\QueryBuilder;
    15 use Doctrine\ORM\QueryBuilder;
    15 use Symfony\Bundle\FrameworkBundle\Command\ContainerAwareCommand;
       
    16 use Symfony\Component\Console\Input\InputArgument;
    16 use Symfony\Component\Console\Input\InputArgument;
    17 use Symfony\Component\Console\Input\InputInterface;
    17 use Symfony\Component\Console\Input\InputInterface;
    18 use Symfony\Component\Console\Input\InputOption;
    18 use Symfony\Component\Console\Input\InputOption;
    19 use Symfony\Component\Console\Output\OutputInterface;
    19 use Symfony\Component\Console\Output\OutputInterface;
    20 
    20 
    21 class QueryWikipediaCommand extends ContainerAwareCommand
    21 class QueryWikipediaCommand extends ProgressContainerAwareCommand
    22 {
    22 {
    23 
    23 
    24     private function showProgress(OutputInterface $output, $current, $total, $label, $width)
       
    25     {
       
    26         $percent = (floatval($current)/floatval($total)) * 100.0;
       
    27         $marks = intval(floor(floatval($width) * ($percent / 100.0) ));
       
    28         $spaces = $width - $marks;
       
    29         
       
    30         $status_bar="\r[";
       
    31         $status_bar.=str_repeat("=", $marks);
       
    32         if($marks<$width){
       
    33             $status_bar.=">";
       
    34             $status_bar.=str_repeat(" ", $spaces);
       
    35         } else {
       
    36             $status_bar.="=";
       
    37         }
       
    38         
       
    39         $disp=str_pad(number_format($percent, 0),3, " ", STR_PAD_LEFT);
       
    40         
       
    41         $label = str_pad(substr($label,0,50), 50, " ");
       
    42         $current_str = str_pad($current, strlen("$total"), " ", STR_PAD_LEFT);
       
    43         
       
    44         $status_bar.="] $disp%  $current_str/$total : $label";
       
    45         
       
    46         $output->write("$status_bar  ");
       
    47         
       
    48         if($current == $total) {
       
    49             $output->writeln("");
       
    50         }
       
    51         
       
    52     }
       
    53     
       
    54     private function processTag($tag, $em)
    24     private function processTag($tag, $em)
    55     {
    25     {
    56         $tag_label_normalized = WikiTagUtils::normalizeTag($tag->getLabel());
    26         $tag_label_normalized = WikiTagUtils::normalizeTag($tag->getLabel());
    57         $wp_response = WikiTagUtils::getWikipediaInfo($tag_label_normalized);
    27         $wp_response = WikiTagUtils::getWikipediaInfo($tag_label_normalized);
    58         
    28         
    59         $new_label = $wp_response['new_label'];
    29         $tag->setWikipediaInfo($wp_response);
    60         $status = $wp_response['status'];
       
    61         $url = $wp_response['wikipedia_url'];
       
    62         $pageid = $wp_response['pageid'];
       
    63         $dbpedia_uri = $wp_response["dbpedia_uri"];
       
    64         $wikipedia_revision_id = $wp_response['revision_id'];
       
    65         
       
    66         # We save the datas
       
    67         if($new_label!=null){
       
    68             $tag->setLabel($new_label);
       
    69         }
       
    70         if($status!=null){
       
    71             $tag->setUrlStatus($status);
       
    72         }
       
    73         $tag->setWikipediaUrl($url);
       
    74         $tag->setWikipediaPageId($pageid);
       
    75         $tag->setDbpediaUri($dbpedia_uri);
       
    76         
    30         
    77         // Save datas.
    31         // Save datas.
    78         $em->persist($tag);
    32         $em->persist($tag);
    79         
    33         
    80     }
    34     }
    86 
    40 
    87         $this
    41         $this
    88             ->setName('wikitag:query-wikipedia')
    42             ->setName('wikitag:query-wikipedia')
    89             ->setDescription('Query wikipedia for tags.')
    43             ->setDescription('Query wikipedia for tags.')
    90             ->addOption("force","f",InputOption::VALUE_NONE, "Force remove tags")
    44             ->addOption("force","f",InputOption::VALUE_NONE, "Force remove tags")
    91             ->addOption("all","a",InputOption::VALUE_NONE, "Force remove tags")
    45             ->addOption("all","a",InputOption::VALUE_NONE, "all")
       
    46             ->addOption("redirection",null,InputOption::VALUE_NONE, "Treat redirections")
    92             ->addOption("random","r",InputOption::VALUE_NONE, "randomize query on tags")
    47             ->addOption("random","r",InputOption::VALUE_NONE, "randomize query on tags")
    93             ->addOption("site","S",InputOption::VALUE_OPTIONAL, "the url for the wikipedia site", "http://fr.wikipedia.org/w/api.php")
    48             ->addOption("site","S",InputOption::VALUE_OPTIONAL, "the url for the wikipedia site", "http://fr.wikipedia.org/w/api.php")
    94             ->addOption("limit","l",InputOption::VALUE_OPTIONAL, "number of tag to process", -1)
    49             ->addOption("limit","l",InputOption::VALUE_OPTIONAL, "number of tag to process", -1)
    95             ->addOption("start",null,InputOption::VALUE_OPTIONAL, "number of tag to ignore", 0);
    50             ->addOption("start",null,InputOption::VALUE_OPTIONAL, "number of tag to ignore", 0);
    96     }
    51     }
    99     {
    54     {
   100         
    55         
   101         $force = $input->getOption('force');
    56         $force = $input->getOption('force');
   102         $all = $input->getOption('all');
    57         $all = $input->getOption('all');
   103         $random = $input->getOption('random');
    58         $random = $input->getOption('random');
       
    59         $redirection = $input->getOption('redirection');
   104         $site = $input->getOption('site');
    60         $site = $input->getOption('site');
   105         $limit = intval($input->getOption('limit'));
    61         $limit = intval($input->getOption('limit'));
   106         $start = intval($input->getOption('start'));
    62         $start = intval($input->getOption('start'));
   107         
    63         
   108         $doctrine = $this->getContainer()->get('doctrine');
    64         $doctrine = $this->getContainer()->get('doctrine');
   111         
    67         
   112         $qb->from('WikiTagBundle:Tag','t');
    68         $qb->from('WikiTagBundle:Tag','t');
   113         
    69         
   114         if(!$all)
    70         if(!$all)
   115         {
    71         {
   116             $qb->where($qb->expr()->isNull("t.urlStatus"));
    72             if($redirection) {
       
    73                 $qb->where($qb->expr()->andx($qb->expr()->eq("t.urlStatus",Tag::$TAG_URL_STATUS_DICT['redirection']), $qb->expr()->isNull("t.alternativeLabel")));
       
    74             }
       
    75             else {
       
    76                 $qb->where($qb->expr()->isNull("t.urlStatus"));
       
    77             }
   117         }
    78         }
   118         
    79         
   119         if($start > 0)
    80         if($start > 0)
   120         {
    81         {
   121             $qb->setFirstResult($start);
    82             $qb->setFirstResult($start);
   130         
    91         
   131         $qb_count->select("t.id");
    92         $qb_count->select("t.id");
   132         
    93         
   133         $count = count($qb_count->getQuery()->getScalarResult());
    94         $count = count($qb_count->getQuery()->getScalarResult());
   134         $doctrine->getEntityManager()->clear();
    95         $doctrine->getEntityManager()->clear();
       
    96         
       
    97         if($count === 0)
       
    98         {
       
    99             $output->writeln("No tag to process, exit.");
       
   100             return;
       
   101         }
   135         
   102         
   136         if(! $force && $input->isInteractive())
   103         if(! $force && $input->isInteractive())
   137         {
   104         {
   138             $dialog = $this->getHelper('dialog');
   105             $dialog = $this->getHelper('dialog');
   139             if (!$dialog->askConfirmation($output, "<question>This command will process $count tag(s). Continue ? (y/N) : </question>", false)) {
   106             if (!$dialog->askConfirmation($output, "<question>This command will process $count tag(s). Continue ? (y/N) : </question>", false)) {