Entity/TagRepository.php
changeset 42 0e57c730bb18
parent 34 21fab44f46fe
child 50 e967654e90cb
child 58 87bf6ec8af90
--- a/Entity/TagRepository.php	Fri Nov 18 17:54:30 2011 +0100
+++ b/Entity/TagRepository.php	Fri Nov 25 18:55:42 2011 +0100
@@ -3,6 +3,8 @@
 namespace IRI\Bundle\WikiTagBundle\Entity;
 
 use Doctrine\ORM\EntityRepository;
+use IRI\Bundle\WikiTagBundle\Utils\WikiTagUtils;
+use IRI\Bundle\WikiTagBundle\Entity\Tag;
 
 /**
  * TagRepository
@@ -41,4 +43,90 @@
         return $query->getResult();
         
     }
+    
+    /**
+    * Get or create tag. Returns an array(tag:WikiTagTag, revision_id=int, created:Boolean)
+    * @param $tag_label
+    * @param $doctrine
+    * @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))
+    */
+    public function getOrCreateTag($tag_label)
+    {
+        $tag_label_normalized = WikiTagUtils::normalizeTag($tag_label);
+        // We get the wikipedia references for the tag_label
+        // We get or create the tag object
+        $tags = $this->findBy(array('normalizedLabel' => $tag_label_normalized));
+        $tag = null;
+        foreach ($tags as $t) {
+            if($tag==null || $t->getUrlStatus()!=Tag::$TAG_URL_STATUS_DICT['null_result']) {
+                $tag = $t;
+                if($t->getUrlStatus()!=Tag::$TAG_URL_STATUS_DICT['null_result']) {
+                    break;
+                }
+            }
+        }
+        $wp_request_done = false;
+        if($tag==null) {
+            $tag = new Tag();
+            $tag->setLabel($tag_label_normalized);
+            $tag->setOriginalLabel($tag_label);
+            $tag->setNormalizedLabel($tag_label_normalized);
+            $created = true;
+        }
+        else {
+            $created = false;
+            $match_exists = false;
+            // Even if a tag with the normalised label exists, IF this tag is not wikipedia semantised,
+            // we search if a wikipedia semantised version exists in the base
+            foreach ($tags as $t) {
+                if($t->getUrlStatus()==Tag::$TAG_URL_STATUS_DICT['match']) {
+                    $tag = $t;
+                    $match_exists = true;
+                    break;
+                }
+            }
+            if($match_exists==false) {
+                $wp_response = WikiTagUtils::getWikipediaInfo($tag_label_normalized);
+                $status = $wp_response['status'];
+                if($status==Tag::$TAG_URL_STATUS_DICT['match']) {
+                    $tag = new Tag();
+                    $tag->setLabel($tag_label_normalized);
+                    $tag->setOriginalLabel($tag_label);
+                    $tag->setNormalizedLabel($tag_label_normalized);
+                    $created = true;
+                    $wp_request_done = true;
+                }
+            }
+        }
+    
+        // We request Wikipedia if the tag is created
+        if($created==true) {
+            
+            if($wp_request_done==false) {
+                $wp_response = WikiTagUtils::getWikipediaInfo($tag_label_normalized);
+            }
+            
+            $tag->setWikipediaInfo($wp_response);
+    
+            // Save datas.
+            $em = $this->getEntityManager();
+            $em->persist($tag);
+            $em->flush();
+                
+            $wikipedia_revision_id = $wp_response['revision_id'];
+    
+        }
+        else if($tag!=null && $tag->getWikipediaPageId()!=null) {
+            
+            $wp_response = WikiTagUtils::getWikipediaInfo(null, $tag->getWikipediaPageId());
+            $wikipedia_revision_id = $wp_response['revision_id'];
+        }
+        else {
+            $wikipedia_revision_id = null;
+        }
+    
+        return array($tag, $wikipedia_revision_id, $created);//, $wpReponse);
+    }
+    
+    
 }
\ No newline at end of file