Utils/WikiTagUtils.php
changeset 42 0e57c730bb18
parent 32 38dcd2db04e4
child 43 54f204bceb28
--- a/Utils/WikiTagUtils.php	Fri Nov 18 17:54:30 2011 +0100
+++ b/Utils/WikiTagUtils.php	Fri Nov 25 18:55:42 2011 +0100
@@ -13,106 +13,6 @@
     
     
     /**
-     * Get or create tag. Returns an array(tag:WikiTagTag, revision_id=int, created:Boolean)
-     */
-    
-    /**
-     *
-     * Enter description here ...
-     * @param unknown_type $tag_label
-     * @param unknown_type $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 static function getOrCreateTag($tag_label, $doctrine)
-    {
-        $tag_label_normalized = WikiTagUtils::normalizeTag($tag_label);
-        // We get the wikipedia references for the tag_label
-        // We get or create the tag object
-        $tags = $doctrine->getRepository('WikiTagBundle:Tag')->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);
-            }
-            $new_label = $wp_response['new_label'];
-            $status = $wp_response['status'];
-            $url = $wp_response['wikipedia_url'];
-            $pageid = $wp_response['pageid'];
-            $dbpedia_uri = $wp_response["dbpedia_uri"];
-            $wikipedia_revision_id = $wp_response['revision_id'];
-            
-            # We save the datas
-            if($new_label!=null){
-                $tag->setLabel($new_label);
-            }
-            if($status!=null){
-                $tag->setUrlStatus($status);
-            }
-            $tag->setWikipediaUrl($url);
-            $tag->setWikipediaPageId($pageid);
-            $tag->setDbpediaUri($dbpedia_uri);
-            
-            // Save datas.
-            $em = $doctrine->getEntityManager();
-            $em->persist($tag);
-            $em->flush();
-            
-        }
-        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);
-    }
-    
-    /**
      * Cleans the tag label
      */
     public static function normalizeTag($tag_label)
@@ -181,7 +81,12 @@
             $status = Tag::$TAG_URL_STATUS_DICT["match"];
         }
         // In redirection, we have to get more datas by adding redirects=true to the params
-        if($status==Tag::$TAG_URL_STATUS_DICT["redirection"]){
+        $alternative_label = null;
+        $alternative_url = null;
+        $alternative_pageid = null;
+        if($status==Tag::$TAG_URL_STATUS_DICT["redirection"])
+        {
+            //TODO: add alternative label
             $params['redirects'] = "true";
             $ar = WikiTagUtils::requestWikipedia($params);
             $res = $ar[0];
@@ -192,6 +97,9 @@
             }
             // get first result
             $page = reset($pages);
+            $alternative_label = array_key_exists('title', $page) ? $page['title'] : null;
+            $alternative_url = array_key_exists('fullurl', $page) ? $page['fullurl'] : null;
+            $alternative_pageid = array_key_exists('pageid', $page) ? $page['pageid'] : null;
         }
         
         $revision_id = $page['lastrevid'];
@@ -214,7 +122,17 @@
             $dbpedia_uri = WikiTagUtils::getDbpediaUri($english_label);
         }
         
-        $wp_response = array('new_label'=>$new_label, 'status'=>$status, 'wikipedia_url'=>$url, 'pageid'=>$pageid, 'dbpedia_uri'=>$dbpedia_uri, 'revision_id'=>$revision_id, 'response'=>$original_response);
+        $wp_response = array(
+            'new_label'=>$new_label,
+        	'alternative_label'=>$alternative_label,
+        	'status'=>$status,
+        	'wikipedia_url'=>$url,
+            'wikipedia_alternative_url'=>$alternative_url,
+        	'pageid'=>$pageid,
+        	'alternative_pageid'=>$alternative_pageid,
+        	'dbpedia_uri'=>$dbpedia_uri,
+        	'revision_id'=>$revision_id,
+        	'response'=>$original_response);
         //return $url." <br/>RES =  ".$res/*." <br/>DUMP =  ".var_dump($pages)*/." <br/>COUNT =  ".count($pages)." <br/>page =  ".var_dump($page);
         return $wp_response;
     }