Entity/TagRepository.php
changeset 67 989d9e117586
parent 65 ba6b8e38d90e
child 68 e7384fb35f7a
--- a/Entity/TagRepository.php	Mon Dec 19 18:15:42 2011 +0100
+++ b/Entity/TagRepository.php	Mon Dec 26 22:53:50 2011 +0100
@@ -45,21 +45,14 @@
         
     }
     
-    /**
-    * 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)
+    private function findSemantizedTag($entityList, $normalized_label)
     {
-        $tag_label_normalized = WikiTagUtils::normalizeTag($tag_label);
-        // We get the wikipedia references for the tag_label
-        // We get or create the tag object
-        // TODO: search also in work unit ?
-        $tags = $this->findBy(array('normalizedLabel' => $tag_label_normalized));
         $tag = null;
-        foreach ($tags as $t) {
+        foreach ($entityList as $entity) {
+            if(!is_null($normalized_label) && (!is_a($entity, "\IRI\Bundle\WikiTagBundle\Model\TagInterface") || $normalized_label !== $entity->getNormalizedLabel())) {
+                continue;
+            }
+            $t = $entity;
             if($tag==null
             || $tag->getUrlStatus() === Tag::$TAG_URL_STATUS_DICT['unsemantized']
             || ($tag->getUrlStatus() === Tag::$TAG_URL_STATUS_DICT['null_result'] && $t->getUrlStatus() !== Tag::$TAG_URL_STATUS_DICT['unsemantized'])) {
@@ -69,6 +62,31 @@
                 }
             }
         }
+        
+        return $tag;
+        
+    }
+    
+    /**
+    * 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, $ignore_wikipedia_info=false)
+    {
+        $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 = $this->findSemantizedTag($tags, null);
+        $uow = $this->getEntityManager()->getUnitOfWork();
+        if(is_null($tag)) {
+            $tag = $this->findSemantizedTag($uow->getScheduledEntityInsertions(), $tag_label_normalized);
+        }
+        if(is_null($tag)) {
+            $tag = $this->findSemantizedTag($uow->getScheduledEntityUpdates(), $tag_label_normalized);
+        }
         $wp_request_done = false;
         if($tag==null) {
             $tag = new Tag();
@@ -85,7 +103,7 @@
         if($created==true || $tag->getUrlStatus()===Tag::$TAG_URL_STATUS_DICT['null_result']) {
             
             if($wp_request_done==false) {
-                $wp_response = WikiTagUtils::getWikipediaInfo($tag_label_normalized);
+                $wp_response = WikiTagUtils::getWikipediaInfo($tag_label_normalized, null, $ignore_wikipedia_info);
             }
             
             $tag->setWikipediaInfo($wp_response);
@@ -98,7 +116,7 @@
     
         }
         elseif($tag!=null && $tag->getWikipediaPageId()!=null) {
-            $wp_response = WikiTagUtils::getWikipediaInfo(null, $tag->getWikipediaPageId());
+            $wp_response = WikiTagUtils::getWikipediaInfo(null, $tag->getWikipediaPageId(), $ignore_wikipedia_info);
             $wikipedia_revision_id = $wp_response['revision_id'];
         }
         else {