# HG changeset patch # User ymh # Date 1323179592 -3600 # Node ID 186c4121c7b369ccb6acc0d3f793e009bb3c585a # Parent e854d8cb376c06f323d593dcf2889be076af10e7 add service tp copy tag from one document to another diff -r e854d8cb376c -r 186c4121c7b3 Command/SyncDocumentsCommand.php --- a/Command/SyncDocumentsCommand.php Mon Nov 28 15:58:34 2011 +0100 +++ b/Command/SyncDocumentsCommand.php Tue Dec 06 14:53:12 2011 +0100 @@ -152,6 +152,7 @@ private function execute_docs(InputInterface $input, OutputInterface $output) { + $class = $this->getContainer()->getParameter('wiki_tag.document_class'); $all = $input->getOption('all'); $doctrine = $this->getContainer()->get('doctrine'); $docrep = $doctrine->getRepository('WikiTagBundle:Document'); diff -r e854d8cb376c -r 186c4121c7b3 Entity/DocumentRepository.php --- a/Entity/DocumentRepository.php Mon Nov 28 15:58:34 2011 +0100 +++ b/Entity/DocumentRepository.php Tue Dec 06 14:53:12 2011 +0100 @@ -365,5 +365,28 @@ return $res; } + + /** + * Copy the tahg lst from one document instance to another + * @param IRI\Bundle\WikitagBundle\Model\DocumentInterface $src_doc + * @param IRI\Bundle\WikitagBundle\Model\DocumentInterface $tgt_doc + */ + public function copyTags($src_doc, $tgt_doc) + { + //remove the previous tags + foreach ($tgt_doc->getTags() as $doctag) { + $this->getEntityManager()->remove($doctag); + } + + // add the new ones + foreach ($src_doc->getTags() as $doctag) { + $new_doctag = clone $doctag; + $new_doctag->setDocument($tgt_doc); + $this->getEntityManager()->persist($new_doctag); + } + + $tgt_doc->setManualOrder(false); + $this->getEntityManager()->persist($tgt_doc); + } } \ No newline at end of file diff -r e854d8cb376c -r 186c4121c7b3 Entity/DocumentTagRepository.php --- a/Entity/DocumentTagRepository.php Mon Nov 28 15:58:34 2011 +0100 +++ b/Entity/DocumentTagRepository.php Tue Dec 06 14:53:12 2011 +0100 @@ -95,5 +95,5 @@ return null; } } - + } \ No newline at end of file diff -r e854d8cb376c -r 186c4121c7b3 Listener/DocumentListener.php --- a/Listener/DocumentListener.php Mon Nov 28 15:58:34 2011 +0100 +++ b/Listener/DocumentListener.php Tue Dec 06 14:53:12 2011 +0100 @@ -117,7 +117,7 @@ $class = $this->getContainer()->getParameter("wiki_tag.document_class"); if (is_a($entity, $class)) { - $logger->debug("treating document : " . $entity->getTitle()); + $logger->debug("treating document : " . $entity->getId()); $this->container->get('doctrine')->getRepository("WikiTagBundle:Document")->writeDocument($entity, $this->getContainer()->getParameter("wiki_tag.document_id_column"), $this->getContainer()->getParameter("wiki_tag.fields")); } elseif (is_a($entity, "IRI\Bundle\WikiTagBundle\Model\DocumentTagInterface")) diff -r e854d8cb376c -r 186c4121c7b3 Model/DocumentInterface.php --- a/Model/DocumentInterface.php Mon Nov 28 15:58:34 2011 +0100 +++ b/Model/DocumentInterface.php Tue Dec 06 14:53:12 2011 +0100 @@ -51,6 +51,13 @@ function getManualOrder(); /** + * Get the list of tags + * + * @return array of IRI\Bundle\WikiTagBundle\Model\DocumentTagInterface + */ + function getTags(); + + /** * Get tagsStr * * @return string @@ -63,5 +70,5 @@ * @param $tagsStr */ function setTagsStr($tagsStr); - + } diff -r e854d8cb376c -r 186c4121c7b3 Model/DocumentTag.php --- a/Model/DocumentTag.php Mon Nov 28 15:58:34 2011 +0100 +++ b/Model/DocumentTag.php Tue Dec 06 14:53:12 2011 +0100 @@ -189,4 +189,6 @@ return $WIKIPEDIA_VERSION_PERMALINK_TEMPLATE.$this->wikipediaRevisionId; } + + } \ No newline at end of file diff -r e854d8cb376c -r 186c4121c7b3 Model/DocumentTagInterface.php --- a/Model/DocumentTagInterface.php Mon Nov 28 15:58:34 2011 +0100 +++ b/Model/DocumentTagInterface.php Tue Dec 06 14:53:12 2011 +0100 @@ -102,5 +102,6 @@ * @return object */ function getDocument(); + } \ No newline at end of file diff -r e854d8cb376c -r 186c4121c7b3 Resources/config/services.yml --- a/Resources/config/services.yml Mon Nov 28 15:58:34 2011 +0100 +++ b/Resources/config/services.yml Tue Dec 06 14:53:12 2011 +0100 @@ -3,7 +3,8 @@ wiki_tag.document_listener.class: IRI\Bundle\WikiTagBundle\Listener\DocumentListener wiki_tag.wiki_tag_document_listener.class: IRI\Bundle\WikiTagBundle\Listener\WikiTagDocumentListener wiki_tag.shema_utils.class: IRI\Bundle\WikiTagBundle\Utils\SchemaUtils - wiki_tag.search_class: IRI\Bundle\WikiTagBundle\Search\Search + wiki_tag.search_service_class: IRI\Bundle\WikiTagBundle\Services\SearchService + wiki_tag.document_service_class: IRI\Bundle\WikiTagBundle\Services\DocumentService services: @@ -25,9 +26,14 @@ arguments: [@service_container] wiki_tag.search: - class: %wiki_tag.search_class% + class: %wiki_tag.search_service_class% arguments: [@service_container] + wiki_tag.document: + class: %wiki_tag.document_service_class% + arguments: [@service_container] + + # wiki_tag.example: # class: %wiki_tag.example.class% # arguments: [@service_id, "plain_value", %parameter%] diff -r e854d8cb376c -r 186c4121c7b3 Search/Search.php --- a/Search/Search.php Mon Nov 28 15:58:34 2011 +0100 +++ /dev/null Thu Jan 01 00:00:00 1970 +0000 @@ -1,140 +0,0 @@ - - * - * For the full copyright and license information, please view the LICENSE - * file that was distributed with this source code. - */ -namespace IRI\Bundle\WikiTagBundle\Search; - -use Symfony\Component\DependencyInjection\ContainerAware; -use Symfony\Component\DependencyInjection\ContainerInterface; - -class Search extends ContainerAware -{ - /** - * Get the container associated with this service. - * @return ContainerInterface - */ - public function getContainer() - { - return $this->container; - } - - /** - * Public constructor with container as parameter for contruct injection. - * @param ContainerInterface $container - */ - public function __construct(ContainerInterface $container) - { - $this->setContainer($container); - } - - private $doctrine; - - public function getDoctrine() - { - if(is_null($this->doctrine)) - { - $this->doctrine = $this->getContainer()->get('doctrine'); - } - return $this->doctrine; - } - /** - * - * Enter description here ... - * @param string $value - * @param array $conditions - * @param array $fields - */ - public function search($value, array $conditions, array $fields=null) - { - if(is_null($fields)) - { - $fields = $this->getContainer()->getParameter("wiki_tag.fields"); - } - $doctrine = $this->getContainer()->get('doctrine'); - $res = $doctrine->getRepository('WikiTagBundle:Document'); - $fieldquery = array(); - foreach ($fields as $fieldname => $fielddef) { - if(isset($fielddef['weight'])) - { - $weight = $fielddef['weight']; - } - else - { - $weight = 1.0; - } - $fieldquery[] = array("columns"=>$fieldname, "value"=>$value, "weight"=>$weight); - } - - $score_res = $res->search($fieldquery, $conditions); - - return $score_res; - } - - /** - * Service to reorder the tags using their notes in the index search - * @param IRI\Bundle\WikiTagBundle\Model\DocumentInterface $document - */ - public function reorderTagsForDocument($document) - { - $doctrine = $this->getContainer()->get('doctrine'); - - $tags_score = array(); - - foreach($document->getTags() as $tag) - { - $label = $tag->getTag()->getLabel(); - - $score_res = $this->search($label, array("id"=>$document->getId())); - - if(count($score_res)>0) - { - $score = floatval($score_res[0]['score']); - } - else - { - $score = 0.0; - } - $tags_score[] = array($score,$tag); - } - // sort tags based on score - $i=1; - usort($tags_score, function($a, $b) { - return $a[0]<$b[0]?1:-1; - }); - - foreach($tags_score as $item) - { - $tag = $item[1]; - $tag->setTagOrder($i++); - $tag->setIndexNote($item[0]); - $doctrine->getEntityManager()->persist($tag); - } - - } - - public function getTagCloud($max_tags) - { - $rep = $this->getDoctrine()->getRepository('WikiTagBundle:Tag'); - return $rep->getTagCloud($max_tags); - } - - public function completion($seed) - { - $rep = $this->getDoctrine()->getRepository('WikiTagBundle:Tag'); - - $res = array(); - foreach ($rep->getCompletion($seed) as $value) { - $res[] = $value['label']; - } - - return $res; - - } - - -} \ No newline at end of file diff -r e854d8cb376c -r 186c4121c7b3 Services/DocumentService.php --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/Services/DocumentService.php Tue Dec 06 14:53:12 2011 +0100 @@ -0,0 +1,81 @@ + + * + * For the full copyright and license information, please view the LICENSE + * file that was distributed with this source code. + */ +namespace IRI\Bundle\WikiTagBundle\Services; + +use Symfony\Component\DependencyInjection\ContainerAware; +use Symfony\Component\DependencyInjection\ContainerInterface; + +class DocumentService extends ContainerAware +{ + /** + * Get the container associated with this service. + * @return ContainerInterface + */ + public function getContainer() + { + return $this->container; + } + + /** + * Public constructor with container as parameter for contruct injection. + * @param ContainerInterface $container + */ + public function __construct(ContainerInterface $container) + { + $this->setContainer($container); + } + + private $doctrine; + + public function getDoctrine() + { + if(is_null($this->doctrine)) + { + $this->doctrine = $this->getContainer()->get('doctrine'); + } + return $this->doctrine; + } + + + /** + * Copy the list of tags of one document to another. + * + * @param mixed $id_doc_src the source document id + * @param mixed $id_doc_tgt the target document id + */ + public function copyTags($id_doc_src, $id_doc_tgt) + { + + $doctrine = $this->getDoctrine(); + $em = $doctrine->getEntityManager(); + + $doc_rep = $em->getRepository("WikiTagBundle:Document"); + + $src_doc = $doc_rep->findOneByExternalId($id_doc_src); + if(is_null($src_doc)) + { + throw new Exception("cloneTags: no source doc"); + } + + $tgt_doc = $doc_rep->findOneByExternalId($id_doc_tgt); + if(is_null($tgt_doc)) + { + throw new Exception("cloneTags: no target doc"); + } + + + $doc_rep->copyTags($src_doc, $tgt_doc); + + $em->flush(); + + } + + +} \ No newline at end of file diff -r e854d8cb376c -r 186c4121c7b3 Services/SearchService.php --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/Services/SearchService.php Tue Dec 06 14:53:12 2011 +0100 @@ -0,0 +1,140 @@ + + * + * For the full copyright and license information, please view the LICENSE + * file that was distributed with this source code. + */ +namespace IRI\Bundle\WikiTagBundle\Services; + +use Symfony\Component\DependencyInjection\ContainerAware; +use Symfony\Component\DependencyInjection\ContainerInterface; + +class SearchService extends ContainerAware +{ + /** + * Get the container associated with this service. + * @return ContainerInterface + */ + public function getContainer() + { + return $this->container; + } + + /** + * Public constructor with container as parameter for contruct injection. + * @param ContainerInterface $container + */ + public function __construct(ContainerInterface $container) + { + $this->setContainer($container); + } + + private $doctrine; + + public function getDoctrine() + { + if(is_null($this->doctrine)) + { + $this->doctrine = $this->getContainer()->get('doctrine'); + } + return $this->doctrine; + } + /** + * + * Enter description here ... + * @param string $value + * @param array $conditions + * @param array $fields + */ + public function search($value, array $conditions, array $fields=null) + { + if(is_null($fields)) + { + $fields = $this->getContainer()->getParameter("wiki_tag.fields"); + } + $doctrine = $this->getContainer()->get('doctrine'); + $res = $doctrine->getRepository('WikiTagBundle:Document'); + $fieldquery = array(); + foreach ($fields as $fieldname => $fielddef) { + if(isset($fielddef['weight'])) + { + $weight = $fielddef['weight']; + } + else + { + $weight = 1.0; + } + $fieldquery[] = array("columns"=>$fieldname, "value"=>$value, "weight"=>$weight); + } + + $score_res = $res->search($fieldquery, $conditions); + + return $score_res; + } + + /** + * Service to reorder the tags using their notes in the index search + * @param IRI\Bundle\WikiTagBundle\Model\DocumentInterface $document + */ + public function reorderTagsForDocument($document) + { + $doctrine = $this->getContainer()->get('doctrine'); + + $tags_score = array(); + + foreach($document->getTags() as $tag) + { + $label = $tag->getTag()->getLabel(); + + $score_res = $this->search($label, array("id"=>$document->getId())); + + if(count($score_res)>0) + { + $score = floatval($score_res[0]['score']); + } + else + { + $score = 0.0; + } + $tags_score[] = array($score,$tag); + } + // sort tags based on score + $i=1; + usort($tags_score, function($a, $b) { + return $a[0]<$b[0]?1:-1; + }); + + foreach($tags_score as $item) + { + $tag = $item[1]; + $tag->setTagOrder($i++); + $tag->setIndexNote($item[0]); + $doctrine->getEntityManager()->persist($tag); + } + + } + + public function getTagCloud($max_tags) + { + $rep = $this->getDoctrine()->getRepository('WikiTagBundle:Tag'); + return $rep->getTagCloud($max_tags); + } + + public function completion($seed) + { + $rep = $this->getDoctrine()->getRepository('WikiTagBundle:Tag'); + + $res = array(); + foreach ($rep->getCompletion($seed) as $value) { + $res[] = $value['label']; + } + + return $res; + + } + + +} \ No newline at end of file diff -r e854d8cb376c -r 186c4121c7b3 Tests/Search/SearchServiceTest.php --- a/Tests/Search/SearchServiceTest.php Mon Nov 28 15:58:34 2011 +0100 +++ /dev/null Thu Jan 01 00:00:00 1970 +0000 @@ -1,59 +0,0 @@ - - * - * For the full copyright and license information, please view the LICENSE - * file that was distributed with this source code. - */ - -namespace IRI\Bundle\WikiTagBundle\Tests\Services; - -require_once(__DIR__ . "/../../../../../../../app/AppKernel.php"); - -class SearchServiceTest extends \PHPUnit_Framework_TestCase -{ - - protected $_container; - - public function __construct() - { - $kernel = new \AppKernel("test", true); - $kernel->boot(); - $this->_container = $kernel->getContainer(); - parent::__construct(); - } - - protected function get($service) - { - return $this->_container->get($service); - } - - - public function testTagCloud() - { - - $search_service = $this->get("wiki_tag.search"); - - $result = $search_service->getTagCloud(30); - - $this->assertNotNull($result, "tag cloud should not be null"); - $this->assertLessThanOrEqual(30, count($result)); - } - - - public function testCompletion() - { - - $search_service = $this->get("wiki_tag.search"); - - $result = $search_service->completion("fra"); - - $this->assertNotNull($result, "tag cloud should not be null"); - $this->assertGreaterThanOrEqual(1, count($result)); - } - - -} - diff -r e854d8cb376c -r 186c4121c7b3 Tests/Services/DocumentServiceTest.php --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/Tests/Services/DocumentServiceTest.php Tue Dec 06 14:53:12 2011 +0100 @@ -0,0 +1,47 @@ + + * + * For the full copyright and license information, please view the LICENSE + * file that was distributed with this source code. + */ + +namespace IRI\Bundle\WikiTagBundle\Tests\Services; + +require_once(__DIR__ . "/../../../../../../../app/AppKernel.php"); + +class DocumentServiceTest extends \PHPUnit_Framework_TestCase +{ + + protected $_container; + + public function __construct() + { + $kernel = new \AppKernel("test", true); + $kernel->boot(); + $this->_container = $kernel->getContainer(); + parent::__construct(); + } + + protected function get($service) + { + return $this->_container->get($service); + } + + + public function testCopyTags() + { + + $search_service = $this->get("wiki_tag.document"); + + // TODO : get doc 1 + // TODO : get doc 2 + // call copyTags + + } + + +} + diff -r e854d8cb376c -r 186c4121c7b3 Tests/Services/SearchServiceTest.php --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/Tests/Services/SearchServiceTest.php Tue Dec 06 14:53:12 2011 +0100 @@ -0,0 +1,59 @@ + + * + * For the full copyright and license information, please view the LICENSE + * file that was distributed with this source code. + */ + +namespace IRI\Bundle\WikiTagBundle\Tests\Services; + +require_once(__DIR__ . "/../../../../../../../app/AppKernel.php"); + +class SearchServiceTest extends \PHPUnit_Framework_TestCase +{ + + protected $_container; + + public function __construct() + { + $kernel = new \AppKernel("test", true); + $kernel->boot(); + $this->_container = $kernel->getContainer(); + parent::__construct(); + } + + protected function get($service) + { + return $this->_container->get($service); + } + + + public function testTagCloud() + { + + $search_service = $this->get("wiki_tag.search"); + + $result = $search_service->getTagCloud(30); + + $this->assertNotNull($result, "tag cloud should not be null"); + $this->assertLessThanOrEqual(30, count($result)); + } + + + public function testCompletion() + { + + $search_service = $this->get("wiki_tag.search"); + + $result = $search_service->completion("fra"); + + $this->assertNotNull($result, "tag cloud should not be null"); + $this->assertGreaterThanOrEqual(1, count($result)); + } + + +} +