Entity/TagRepository.php
changeset 67 989d9e117586
parent 65 ba6b8e38d90e
child 68 e7384fb35f7a
equal deleted inserted replaced
66:868d0fcdbb99 67:989d9e117586
    43         
    43         
    44         return $query->getResult();
    44         return $query->getResult();
    45         
    45         
    46     }
    46     }
    47     
    47     
    48     /**
    48     private function findSemantizedTag($entityList, $normalized_label)
    49     * Get or create tag. Returns an array(tag:WikiTagTag, revision_id=int, created:Boolean)
       
    50     * @param $tag_label
       
    51     * @param $doctrine
       
    52     * @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))
       
    53     */
       
    54     public function getOrCreateTag($tag_label)
       
    55     {
    49     {
    56         $tag_label_normalized = WikiTagUtils::normalizeTag($tag_label);
       
    57         // We get the wikipedia references for the tag_label
       
    58         // We get or create the tag object
       
    59         // TODO: search also in work unit ?
       
    60         $tags = $this->findBy(array('normalizedLabel' => $tag_label_normalized));
       
    61         $tag = null;
    50         $tag = null;
    62         foreach ($tags as $t) {
    51         foreach ($entityList as $entity) {
       
    52             if(!is_null($normalized_label) && (!is_a($entity, "\IRI\Bundle\WikiTagBundle\Model\TagInterface") || $normalized_label !== $entity->getNormalizedLabel())) {
       
    53                 continue;
       
    54             }
       
    55             $t = $entity;
    63             if($tag==null
    56             if($tag==null
    64             || $tag->getUrlStatus() === Tag::$TAG_URL_STATUS_DICT['unsemantized']
    57             || $tag->getUrlStatus() === Tag::$TAG_URL_STATUS_DICT['unsemantized']
    65             || ($tag->getUrlStatus() === Tag::$TAG_URL_STATUS_DICT['null_result'] && $t->getUrlStatus() !== Tag::$TAG_URL_STATUS_DICT['unsemantized'])) {
    58             || ($tag->getUrlStatus() === Tag::$TAG_URL_STATUS_DICT['null_result'] && $t->getUrlStatus() !== Tag::$TAG_URL_STATUS_DICT['unsemantized'])) {
    66                 $tag = $t;
    59                 $tag = $t;
    67                 if($tag->getUrlStatus()!=Tag::$TAG_URL_STATUS_DICT['unsemantized'] && $tag->getUrlStatus()!=Tag::$TAG_URL_STATUS_DICT['null_result']) {
    60                 if($tag->getUrlStatus()!=Tag::$TAG_URL_STATUS_DICT['unsemantized'] && $tag->getUrlStatus()!=Tag::$TAG_URL_STATUS_DICT['null_result']) {
    68                     break;
    61                     break;
    69                 }
    62                 }
    70             }
    63             }
       
    64         }
       
    65         
       
    66         return $tag;
       
    67         
       
    68     }
       
    69     
       
    70     /**
       
    71     * Get or create tag. Returns an array(tag:WikiTagTag, revision_id=int, created:Boolean)
       
    72     * @param $tag_label
       
    73     * @param $doctrine
       
    74     * @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))
       
    75     */
       
    76     public function getOrCreateTag($tag_label, $ignore_wikipedia_info=false)
       
    77     {
       
    78         $tag_label_normalized = WikiTagUtils::normalizeTag($tag_label);
       
    79         // We get the wikipedia references for the tag_label
       
    80         // We get or create the tag object
       
    81         $tags = $this->findBy(array('normalizedLabel' => $tag_label_normalized));
       
    82         $tag = $this->findSemantizedTag($tags, null);
       
    83         $uow = $this->getEntityManager()->getUnitOfWork();
       
    84         if(is_null($tag)) {
       
    85             $tag = $this->findSemantizedTag($uow->getScheduledEntityInsertions(), $tag_label_normalized);
       
    86         }
       
    87         if(is_null($tag)) {
       
    88             $tag = $this->findSemantizedTag($uow->getScheduledEntityUpdates(), $tag_label_normalized);
    71         }
    89         }
    72         $wp_request_done = false;
    90         $wp_request_done = false;
    73         if($tag==null) {
    91         if($tag==null) {
    74             $tag = new Tag();
    92             $tag = new Tag();
    75             $tag->setLabel($tag_label_normalized);
    93             $tag->setLabel($tag_label_normalized);
    83     
   101     
    84         // We request Wikipedia if the tag is created or if this is a null result
   102         // We request Wikipedia if the tag is created or if this is a null result
    85         if($created==true || $tag->getUrlStatus()===Tag::$TAG_URL_STATUS_DICT['null_result']) {
   103         if($created==true || $tag->getUrlStatus()===Tag::$TAG_URL_STATUS_DICT['null_result']) {
    86             
   104             
    87             if($wp_request_done==false) {
   105             if($wp_request_done==false) {
    88                 $wp_response = WikiTagUtils::getWikipediaInfo($tag_label_normalized);
   106                 $wp_response = WikiTagUtils::getWikipediaInfo($tag_label_normalized, null, $ignore_wikipedia_info);
    89             }
   107             }
    90             
   108             
    91             $tag->setWikipediaInfo($wp_response);
   109             $tag->setWikipediaInfo($wp_response);
    92     
   110     
    93             // Save datas.
   111             // Save datas.
    96                 
   114                 
    97             $wikipedia_revision_id = $wp_response['revision_id'];
   115             $wikipedia_revision_id = $wp_response['revision_id'];
    98     
   116     
    99         }
   117         }
   100         elseif($tag!=null && $tag->getWikipediaPageId()!=null) {
   118         elseif($tag!=null && $tag->getWikipediaPageId()!=null) {
   101             $wp_response = WikiTagUtils::getWikipediaInfo(null, $tag->getWikipediaPageId());
   119             $wp_response = WikiTagUtils::getWikipediaInfo(null, $tag->getWikipediaPageId(), $ignore_wikipedia_info);
   102             $wikipedia_revision_id = $wp_response['revision_id'];
   120             $wikipedia_revision_id = $wp_response['revision_id'];
   103         }
   121         }
   104         else {
   122         else {
   105             $wikipedia_revision_id = null;
   123             $wikipedia_revision_id = null;
   106         }
   124         }