Entity/TagRepository.php
changeset 42 0e57c730bb18
parent 34 21fab44f46fe
child 50 e967654e90cb
child 58 87bf6ec8af90
equal deleted inserted replaced
39:b403086580f7 42:0e57c730bb18
     1 <?php
     1 <?php
     2 
     2 
     3 namespace IRI\Bundle\WikiTagBundle\Entity;
     3 namespace IRI\Bundle\WikiTagBundle\Entity;
     4 
     4 
     5 use Doctrine\ORM\EntityRepository;
     5 use Doctrine\ORM\EntityRepository;
       
     6 use IRI\Bundle\WikiTagBundle\Utils\WikiTagUtils;
       
     7 use IRI\Bundle\WikiTagBundle\Entity\Tag;
     6 
     8 
     7 /**
     9 /**
     8  * TagRepository
    10  * TagRepository
     9  *
    11  *
    10  * This class was generated by the Doctrine ORM. Add your own custom
    12  * This class was generated by the Doctrine ORM. Add your own custom
    39         $query = $qb->getQuery();
    41         $query = $qb->getQuery();
    40         
    42         
    41         return $query->getResult();
    43         return $query->getResult();
    42         
    44         
    43     }
    45     }
       
    46     
       
    47     /**
       
    48     * Get or create tag. Returns an array(tag:WikiTagTag, revision_id=int, created:Boolean)
       
    49     * @param $tag_label
       
    50     * @param $doctrine
       
    51     * @return multitype:boolean Ambigous <NULL, \IRI\Bundle\WikiTagBundle\Entity\Tag> Ambigous <NULL, unknown, mixed, string> (array(\IRI\Bundle\WikiTagBundle\Model\TagInterface, revision_id=int, created:Boolean))
       
    52     */
       
    53     public function getOrCreateTag($tag_label)
       
    54     {
       
    55         $tag_label_normalized = WikiTagUtils::normalizeTag($tag_label);
       
    56         // We get the wikipedia references for the tag_label
       
    57         // We get or create the tag object
       
    58         $tags = $this->findBy(array('normalizedLabel' => $tag_label_normalized));
       
    59         $tag = null;
       
    60         foreach ($tags as $t) {
       
    61             if($tag==null || $t->getUrlStatus()!=Tag::$TAG_URL_STATUS_DICT['null_result']) {
       
    62                 $tag = $t;
       
    63                 if($t->getUrlStatus()!=Tag::$TAG_URL_STATUS_DICT['null_result']) {
       
    64                     break;
       
    65                 }
       
    66             }
       
    67         }
       
    68         $wp_request_done = false;
       
    69         if($tag==null) {
       
    70             $tag = new Tag();
       
    71             $tag->setLabel($tag_label_normalized);
       
    72             $tag->setOriginalLabel($tag_label);
       
    73             $tag->setNormalizedLabel($tag_label_normalized);
       
    74             $created = true;
       
    75         }
       
    76         else {
       
    77             $created = false;
       
    78             $match_exists = false;
       
    79             // Even if a tag with the normalised label exists, IF this tag is not wikipedia semantised,
       
    80             // we search if a wikipedia semantised version exists in the base
       
    81             foreach ($tags as $t) {
       
    82                 if($t->getUrlStatus()==Tag::$TAG_URL_STATUS_DICT['match']) {
       
    83                     $tag = $t;
       
    84                     $match_exists = true;
       
    85                     break;
       
    86                 }
       
    87             }
       
    88             if($match_exists==false) {
       
    89                 $wp_response = WikiTagUtils::getWikipediaInfo($tag_label_normalized);
       
    90                 $status = $wp_response['status'];
       
    91                 if($status==Tag::$TAG_URL_STATUS_DICT['match']) {
       
    92                     $tag = new Tag();
       
    93                     $tag->setLabel($tag_label_normalized);
       
    94                     $tag->setOriginalLabel($tag_label);
       
    95                     $tag->setNormalizedLabel($tag_label_normalized);
       
    96                     $created = true;
       
    97                     $wp_request_done = true;
       
    98                 }
       
    99             }
       
   100         }
       
   101     
       
   102         // We request Wikipedia if the tag is created
       
   103         if($created==true) {
       
   104             
       
   105             if($wp_request_done==false) {
       
   106                 $wp_response = WikiTagUtils::getWikipediaInfo($tag_label_normalized);
       
   107             }
       
   108             
       
   109             $tag->setWikipediaInfo($wp_response);
       
   110     
       
   111             // Save datas.
       
   112             $em = $this->getEntityManager();
       
   113             $em->persist($tag);
       
   114             $em->flush();
       
   115                 
       
   116             $wikipedia_revision_id = $wp_response['revision_id'];
       
   117     
       
   118         }
       
   119         else if($tag!=null && $tag->getWikipediaPageId()!=null) {
       
   120             
       
   121             $wp_response = WikiTagUtils::getWikipediaInfo(null, $tag->getWikipediaPageId());
       
   122             $wikipedia_revision_id = $wp_response['revision_id'];
       
   123         }
       
   124         else {
       
   125             $wikipedia_revision_id = null;
       
   126         }
       
   127     
       
   128         return array($tag, $wikipedia_revision_id, $created);//, $wpReponse);
       
   129     }
       
   130     
       
   131     
    44 }
   132 }