add reindex action
authorymh <ymh.work@gmail.com>
Wed, 14 Apr 2010 16:20:20 +0200
changeset 47 f736ee27a189
parent 46 19805b5868d6
child 48 2c84c98aacb6
add reindex action
engine/sonyengine/mosatags.sonyengine.war
web/thdProject/apps/backend/modules/film/actions/actions.class.php
web/thdProject/lib/model/doctrine/ThdFilm.class.php
web/thdProject/plugins/iriSonyenginePlugin/lib/client/SonyengineClient.php
web/thdProject/plugins/iriSonyenginePlugin/lib/iriSonyengineEventListener.class.php
Binary file engine/sonyengine/mosatags.sonyengine.war has changed
--- a/web/thdProject/apps/backend/modules/film/actions/actions.class.php	Wed Apr 14 16:15:54 2010 +0200
+++ b/web/thdProject/apps/backend/modules/film/actions/actions.class.php	Wed Apr 14 16:20:20 2010 +0200
@@ -14,20 +14,46 @@
 class filmActions extends autoFilmActions
 {
 	public function executeListReindex(sfWebRequest $request) {
+
 		sfContext::getInstance()->getLogger()->info("FilmAction : listReindex");
 		
 		//empty solr
-		//empty sonyengine
+		$solr = uvmcSolrServicesManager::getInstance()->getService();
+        $solr->deleteByQuery('*:*');
+        
+        // reset sony engine
+        $dispatcher = sfContext::getInstance()->getEventDispatcher();
+        $dispatcher->notify(new sfEvent("", 'iri_sonyengine.reset', array('retrain' => true)));
+		
+		
+		$films_array = array();
+		$tags_array = array();
 		
 		// get all films
-		// add film to film array for sonyengine
-		// for each film get all segments
-		// get all tags
-		// add to film array for sony engine
-		// solr index film
+		$films = Doctrine_Query::create()
+		  ->select("f.*")
+		  ->from('ThdFilm f')
+		  ->execute();
+		  
+		sfContext::getInstance()->getLogger()->info("Query films : " . print_r($films->count(),true));
+		  
+		foreach ($films as $film) {
+			$films_array[] = $film;
+			
+			$tags = $film->getTagsArray();
+			foreach ($tags as $tag_name) {
+				$tags_array[] = array('segment_id'=>$film->uniqueid,'name'=>$tag_name);
+			}			
+			$dispatcher->notify(new sfEvent($this, 'uvmc_solr.add_document_to_collection', array('object'=>$film, 'commit'=>false)));
+		}
 
-		// add all films to sonyengine
-		// add all tags to sony engine
+		sfContext::getInstance()->getLogger()->info("Query films : films array " . print_r($films->count(),true));
+		
+		$dispatcher->notify(new sfEvent($this, 'iri_sonyengine.segment_create', array('object'=>$films_array, 'retrain'=>true)));
+		$dispatcher->notify(new sfEvent($this, 'iri_sonyengine.tag_add', array('object'=>$tags_array, 'retrain'=>true)));
+		
+        $dispatcher->notify(new sfEvent($this, 'uvmc_solr.add_collection', array('commit'=>false)));
+		$dispatcher->notify(new sfEvent($this, 'uvmc_solr.commit', array()));
 		
 		$this->redirect("@thd_film_film");
 	}
--- a/web/thdProject/lib/model/doctrine/ThdFilm.class.php	Wed Apr 14 16:15:54 2010 +0200
+++ b/web/thdProject/lib/model/doctrine/ThdFilm.class.php	Wed Apr 14 16:20:20 2010 +0200
@@ -5,21 +5,33 @@
  */
 class ThdFilm extends BaseThdFilm
 {
-
+    public function getSonyengineFields()
+    {
+    	$fields = array('id' => $this->getUniqueid());
+    	
+    	return $fields;
+    }
+    
+    private function getSolrTextValue($original_value) {
+        return $original_value==null?"":strval($original_value);
+    }
+    
     public function getSolrDocumentFields()
     {
         // keys of this array are fields' name of solr's schema.xml
         $type = "ThdFilm";
-        $fields = array('type'           => $type,
-                        'id'             => $this->getId(),
-                        'title'          => array('value' => $this->getTitle(), 'boost' => 1.0),
-                        'desc'           => array('value' => $this->getPitch(), 'boost' => 1.0),
-                        'directors'      => array('value' => $this->getDirectors(), 'boost' => 1.0),
-                        'actors'         => array('value' => $this->getActors(), 'boost' => 1.0),
-                        'original_title' => array('value' => $this->getOriginal_title(), 'boost' => 1.0),
-                        'uniqueid'       => array('value' => $this->getUniqueid(), 'boost' => 1.0),
+        $fields = array('type'           => $this->getSolrTextValue($type),
+                        'id'             => $this->getSolrTextValue($this->getUniqueid()),
+                        'title'          => array('value' => $this->getSolrTextValue($this->getTitle()), 'boost' => 1.0),
+                        'desc'           => array('value' => $this->getSolrTextValue($this->getPitch()), 'boost' => 1.0),
+                        'directors'      => array('value' => $this->getSolrTextValue($this->getDirectors()), 'boost' => 1.0),
+                        'actors'         => array('value' => $this->getSolrTextValue($this->getActors()), 'boost' => 1.0),
+                        'original_title' => array('value' => $this->getSolrTextValue($this->getOriginal_title()), 'boost' => 1.0),
+                        'uniqueid'       => array('value' => $this->getSolrTextValue($this->getUniqueid()), 'boost' => 1.0),
+                        'tags'           => array('value' => $this->getSolrTextValue($this->getTags()), 'boost' => 1.0),
         );
 
+        sfContext::getInstance()->getLogger()->info("getSolrDocumentFields : " . print_r($fields,true));
         return $fields;
     }
 
@@ -44,5 +56,36 @@
     public function getProductionCountriesArray() {
       return $this->decodeJsonData('production_countries');
     }
+    
+    public function getTags() {
+    	$tags = "";
+    	$q = Doctrine_Query::create()
+    	   ->select('s.*')
+    	   ->from('ThdSegment s')
+    	   ->leftJoin('s.ThdVideo v')
+    	   ->leftJoin('v.ThdFilm f')
+    	   ->where('f.id = ?', $this->id);
+        
+    	$segments = $q->fetchArray();
+    	
+    	foreach($segments as $segment) {
+    		$tags .= $segment['tags'].",";
+    	}
+    	
+    	return rtrim($tags,',');
+    }
+    
+    public function getTagsArray() {
+    	
+    	$tags = $this->getTags();
+    	$res = array();
+    	
+    	foreach (explode(',',$tags) as $tag_name) {
+    		$res[] = trim($tag_name);
+    	}
+    	
+    	return $res;
+    }
+    
 
 }
--- a/web/thdProject/plugins/iriSonyenginePlugin/lib/client/SonyengineClient.php	Wed Apr 14 16:15:54 2010 +0200
+++ b/web/thdProject/plugins/iriSonyenginePlugin/lib/client/SonyengineClient.php	Wed Apr 14 16:20:20 2010 +0200
@@ -16,7 +16,7 @@
 	}
 	
 	public function __construct($host, $port, $path) {
-		$path = rtrim($path,"/");
+		$path = trim($path,'/');
 		$this->base_url = "http://$host:$port/$path";
 	}
 	
@@ -25,15 +25,20 @@
 		$path = ltrim($path,"/");
 		
 		$ch = curl_init();
-		$url = "$this->base_url/$path"
+		$url = rtrim($this->base_url,'/')."/".$path;
+		
+		sfContext::getInstance()->getLogger()->info("SonyengineClient.send - url : ".$url);
+		
 		curl_setopt($ch,CURLOPT_URL,$url);
 		curl_setopt($ch,CURLOPT_RETURNTRANSFER,1);
 		
 		if($method == self::POST_METHOD) {
 			
-			$field_string = "";
+			$fields_string = "";
 			
-			foreach($fields as $key=>$value) { $fields_string .= $key.'='.urlencode($value).'&'; }
+			foreach($fields as $key=>$value) {
+				$fields_string .= $key.'='.urlencode($value).'&';
+			}
 			$fields_string = rtrim($fields_string,'&');
 			
 			curl_setopt($ch,CURLOPT_POST,1);
@@ -79,7 +84,8 @@
 		
 		$result = array();
 		
-		$doc = DOMDocument::loadXML($res);
+		$doc = new DOMDocument();
+		$doc->loadXML($res);
 		$xpath = new DOMXPath($doc);
 		
 		$nodes = $xpath->query("/sonyengine/neighbor");
@@ -91,13 +97,24 @@
 		return $result;
 	}
 	
+	private function processXmlResponse($res, $node, $count) {
+        
+		$doc = new DOMDocument();
+		$doc->loadXML($res);
+        $xpath = new DOMXPath($doc);
+        
+        $nodes = $xpath->query("//*/".$node);
+		
+        return ($nodes->length == $count);
+	}
+	
 	public function engineReset() {
 		$path = "engine/reset";
 		
 		$res = $this->send($path,self::POST_METHOD,array());
 
 		if($res != "ok") {
-			throw new Exception('Can not reset engine');
+			throw new Exception('Can not reset engine : ' . $res);
 		}
 		
 	}
@@ -113,24 +130,27 @@
 		if(is_array($movieId)) {
 			$path = "segment/createall";
 			$doc = new DOMDocument();
-			$doc->appendChild($doc->createElement("sonyengine"));
+			$root = $doc->createElement("sonyengine");
+			$doc->appendChild($root);
 			
 			foreach($movieId as $id) {
 				$elem = $doc->createElement("segment");
 				$elem->setAttribute("id",$id);
-				$doc->appendChild($elem);
+				$root->appendChild($elem);
 			}
 			$fields = array("xml"=>$doc->saveXML(), "retrain"=>$retrain);
+			$count = count($movieId);
 		}
 		else {
 			$path = "segment/create";
 			$fields = array("id"=>$movieId, "retrain"=>$retrain);
+			$count = 1;
 		}
 		
 		$res = $this->send($path, self::POST_METHOD, $fields);
 		
-		if($res != "ok") {
-			throw new Exception('Can not create segment');
+		if(! $this->processXmlResponse($res, "movieFragment", $count)) {
+			throw new Exception('Can not create segment ' . $res);
 		}		
 		
 	}
@@ -144,7 +164,7 @@
 		else
 			$retrain = "false";
 		
-		if(is_array($array) && (0 !== count(array_diff_key($array, array_keys(array_keys($array)))) || count($array)==0)) {
+		if(is_array($array)) {
 			$tags = $array;
 		}
 		else {
@@ -156,19 +176,20 @@
 
 		
 		$doc = new DOMDocument();
-		$doc->appendChild($doc->createElement("sonyengine"));
+		$root = $doc->createElement("sonyengine");
+		$doc->appendChild($root);
 		foreach($tags as $tag) {
 			$elem = $doc->createElement("tag");
 			$elem->setAttribute("name",$tag["name"]);
 			$elem->setAttribute("segment",$tag["segment"]);
-			$doc->appendChild($elem);
+			$root->appendChild($elem);
 		}
 		$fields = array("xml"=>$doc->saveXML(), "retrain"=>$retrain);
 		
 		$res = $this->send($path, self::POST_METHOD, $fields);
 		
-		if($res != "ok") {
-			throw new Exception('Can not add tags');
+		if(!$this->processXmlResponse($res,"tag",count($tags))) {
+			throw new Exception('Can not add tags : '. print_r($fields,true)."\n==========\n". print_r($tags,true)."\n==========\n".$res);
 		}		
 
 	}
--- a/web/thdProject/plugins/iriSonyenginePlugin/lib/iriSonyengineEventListener.class.php	Wed Apr 14 16:15:54 2010 +0200
+++ b/web/thdProject/plugins/iriSonyenginePlugin/lib/iriSonyengineEventListener.class.php	Wed Apr 14 16:20:20 2010 +0200
@@ -37,6 +37,8 @@
 		
 		$client = SonyengineClient::getInstance();
 		$obj = $event['object'];
+		
+		$retrain = isset($event['retrain'])?$event['retrain']:true;
 
 		$objs = null;
 		
@@ -50,11 +52,22 @@
 		
 		$array = array();
 		foreach($obj as $obj_instance) {
-			$fields = $obj_instance->getSonyengineFields();
-			$array[] = $fields["id"];
+
+			if(is_array($obj_instance)) {
+                $fields = $obj_instance;
+            }
+            elseif(method_exists($obj_instance,'getSonyengineFields')) {
+			    $fields = $obj_instance->getSonyengineFields();
+            }
+            
+            if(isset($fields) && isset($fields['id'])) {
+			    $array[] = $fields["id"];
+            }
 		}
-			
-		$client->segmentCreate($array, true);
+		
+		if(count($array) > 0) {
+		    $client->segmentCreate($array, $retrain);
+		}
 	}
 	
 	static function listenToTagAdd(sfEvent $event) {
@@ -62,6 +75,8 @@
 		$client = SonyengineClient::getInstance();
 		$obj = $event['object'];
 
+        $retrain = isset($event['retrain'])?$event['retrain']:true;
+				
 		$objs = null;
 		
 		if(is_array($obj)) {
@@ -74,10 +89,21 @@
 		
 		$array = array();
 		foreach($obj as $obj_instance) {
-			$fields = $obj_instance->getSonyengineFields();
-			$array[] = array("segment"=>$fields["segment_id"],"name"=>$fields["name"]);
+			
+			if(is_array($obj_instance)) {
+			    $fields = $obj_instance;
+			}
+			elseif(method_exists($obj_instance,'getSonyengineFields')) {
+				$fields = $obj_instance->getSonyengineFields();
+			}
+			
+			if(count($fields)>0) {
+				$array[] = array("segment"=>$fields["segment_id"],"name"=>$fields["name"]);
+			}
 		}
 		
-		$client->tagAdd($array, true);
+		if(count($array) > 0) {
+		  $client->tagAdd($array, $retrain);
+		}
 	}
 }
\ No newline at end of file