# HG changeset patch # User ymh # Date 1320852313 -3600 # Node ID d2fba1e3b94b958bf762c43dc0bcd8184437675a # Parent 7496254cfead550d419f8fc3e2a95ac77abd723a correction of reorder tag (including the javascript) diff -r 7496254cfead -r d2fba1e3b94b Command/ReorderTagsCommand.php --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/Command/ReorderTagsCommand.php Wed Nov 09 16:25:13 2011 +0100 @@ -0,0 +1,88 @@ + + * + * For the full copyright and license information, please view the LICENSE + * file that was distributed with this source code. + */ + +namespace IRI\Bundle\WikiTagBundle\Command; + +use Symfony\Bundle\FrameworkBundle\Command\ContainerAwareCommand; +use Symfony\Component\Console\Input\InputArgument; +use Symfony\Component\Console\Input\InputInterface; +use Symfony\Component\Console\Input\InputOption; +use Symfony\Component\Console\Output\OutputInterface; + +class ReorderTagsCommand extends ContainerAwareCommand +{ + + protected function configure() + { + parent::configure(); + + $this + ->setName('wikitag:reorder-tags') + ->setDescription('Reorder tags') + ->addOption("force","f",InputOption::VALUE_NONE, "Force all reorder"); + } + + + protected function execute(InputInterface $input, OutputInterface $output) + { + $doctrine = $this->getContainer()->get('doctrine'); + $configuration = $doctrine->getConnection()->getConfiguration(); + $configuration->setSQLLogger(null); + + $force = $input->getOption('force'); + + if($force) + { + $query = $doctrine->getEntityManager()->createQuery("SELECT doc from WikiTagBundle:Document doc"); + $querycount = $doctrine->getEntityManager()->createQuery("SELECT count(doc) from WikiTagBundle:Document doc"); + } + else + { + $query = $doctrine->getEntityManager()->createQuery("SELECT doc from WikiTagBundle:Document doc WHERE doc.manualOrder = FALSE"); + $querycount = $doctrine->getEntityManager()->createQuery("SELECT count(doc) from WikiTagBundle:Document doc WHERE doc.manualOrder = FALSE"); + } + + $total = $querycount->getSingleScalarResult(); + $search_service = $this->getContainer()->get('wiki_tag.search'); + + $done = 0; + $iterable = $query->iterate(); + $todetach = array(); + while (($row = $iterable->next()) !== false) + { + $done++; + $doc = $row[0]; + + $output->writeln("Process doc id ".$doc->getId()." $done/$total ".strval(intval(floatval($done)/floatval($total)*100.0))."%"); + + $doc->setManualOrder(false); + $doctrine->getEntityManager()->persist($doc); + + $search_service->reorderTagsForDocument($doc); + $todetach[] = $doc; + + + if($done%100 == 0) + { + $doctrine->getEntityManager()->flush(); + $doctrine->getEntityManager()->clear(); + /*foreach($todetach as $obj) + { + $doctrine->getEntityManager()->detach($obj); + }*/ + $todetach = array(); + $output->writeln("memory : ".strval(memory_get_usage(true))); + } + + } + + } + +} diff -r 7496254cfead -r d2fba1e3b94b Controller/WikiTagController.php --- a/Controller/WikiTagController.php Mon Nov 07 17:25:39 2011 +0100 +++ b/Controller/WikiTagController.php Wed Nov 09 16:25:13 2011 +0100 @@ -171,7 +171,7 @@ $dt->setTag($tag); $dt->setWikipediaRevisionId($revision_id); // - // HERE QUERY TO GET A INDEX_NOTE/SCORE for the tag. Here is python code : + // TODO: HERE QUERY TO GET A INDEX_NOTE/SCORE for the tag. Here is python code : //kwargs = {DJANGO_ID + "__exact": unicode(ds_id)} //results = SearchQuerySet().filter(title=tag_label).filter_or(description=tag_label).filter(**kwargs) //if len(results) > 0: @@ -195,8 +195,18 @@ */ public function reorderTagDocumentAction() { - $id_Doc = $this->getRequest()->request->get('wikitag_document_id'); - $res = $this->getDoctrine()->getRepository('WikiTagBundle:Document')->search(null,'test', null); + $id_doc = $this->getRequest()->request->get('wikitag_document_id'); + $res = $this->getDoctrine()->getRepository('WikiTagBundle:Document'); + $doc = $res->findOneByExternalId($id_doc); + $doc->setManualOrder(false); + $this->getDoctrine()->getEntityManager()->persist($doc); + + $search_service = $this->get('wiki_tag.search'); + + $search_service->reorderTagsForDocument($doc); + + $this->getDoctrine()->getEntityManager()->flush(); + return $this->renderDocTags($id_doc); } @@ -247,6 +257,7 @@ $new_DT->setOriginalOrder($new_order); $new_DT->setTagOrder($new_order); $new_DT->setWikipediaRevisionId($revision_id); + //TODO ; calculate score $em->persist($new_DT); $em->flush(); } diff -r 7496254cfead -r d2fba1e3b94b Entity/DocumentRepository.php --- a/Entity/DocumentRepository.php Mon Nov 07 17:25:39 2011 +0100 +++ b/Entity/DocumentRepository.php Wed Nov 09 16:25:13 2011 +0100 @@ -6,6 +6,7 @@ use IRI\Bundle\WikiTagBundle\Entity\Document; use Doctrine\ORM\Query\ResultSetMapping; use \ReflectionClass; +use Doctrine\ORM\AbstractQuery; /** * DocumentRepository @@ -215,31 +216,86 @@ */ function search(array $values, array $conditions=NULL) { -/* $em = $this->getEntityManager(); + $em = $this->getEntityManager(); $rsm = new ResultSetMapping(); - $rsm->addEntityResult("IRI/Bundle/WikiTagBundle/Entity/Document", "d"); + $rsm->addEntityResult("IRI\Bundle\WikiTagBundle\Entity\Document", "d"); $rsm->addFieldResult("d", "id", "id"); - $rsm->addFieldResult("d", "external_id", "externalId"); - $rsm->addScalarResult("d", "score"); + $rsm->addScalarResult("score", "score"); + $rsm->addMetaResult("d", "external_id", "externalId"); + - $score = ""; + $score = array(); + $i = 0; foreach ($values as $fielddef) { - + $i++; $columns = $fielddef["columns"]; $value = $fielddef["value"]; - $value = isset($fielddef["weight"])?$fielddef["weight"]:1.0; + $weight = isset($fielddef["weight"])?$fielddef["weight"]:1.0; - $score = "(MATCH($columns) AGAINST (:value))*:weight"; - $parameters[] = + $score[] = "(MATCH($columns) AGAINST (:value_$i))*:weight_$i"; + $parameters["value_$i"] = $value; + $parameters["weight_$i"] = $weight; } + $score_def = "(".implode("+", $score).")"; - $query = $em->createNativeQuery("SELECT d.id, d.external_id, MATCH(title) AGAINST('$value') FROM wikitag_document d WHERE MATCH(title) AGAINST('$value') > 0", $rsm); + $conditions_str = ""; - $res = $query->getScalarResult(); + if(!is_null($conditions)) + { + $conditions_array = array(); + $i = 0; + foreach ($conditions as $field => $conddef) + { + $i++; + if(is_array($conddef) && isset($conddef['operator'])) + { + $operator = $conddef["operator"]; + $values = $conddef["value"]; + } + elseif(is_array($conddef)) + { + $operator = "IN"; + $values = $conddef; + } + else + { + $operator = "="; + $values = $conddef; + } + + if($operator === "IN") + { + $in_parameters = array(); + for ($j = 0; $j < count($values); $j++) { + $parameters["cond_val_$i_$j"] = $values[$j]; + $in_parameters[] = ":cond_val_$i_$j"; + } + $cond = "($field IN (".implode(",",$in_parameters)."))"; + } + else + { + $cond = "($field $operator :cond_val_$i)"; + $parameters["cond_val_$i"] = $values; + } + $conditions_array[] = $cond; + } + + if(count($conditions_array) > 0) + { + $conditions_str = " AND ".implode(" AND ", $conditions_array); + } + + } - return $res;*/ + $query = $em->createNativeQuery("SELECT d.id, d.external_id, $score_def AS score FROM wikitag_document d WHERE $score_def > 0 $conditions_str ORDER BY score DESC", $rsm); + + $query->setParameters($parameters); + + $res = $query->getResult(); + + return $res; } diff -r 7496254cfead -r d2fba1e3b94b Resources/config/doctrine/DocumentTag.orm.yml --- a/Resources/config/doctrine/DocumentTag.orm.yml Mon Nov 07 17:25:39 2011 +0100 +++ b/Resources/config/doctrine/DocumentTag.orm.yml Wed Nov 09 16:25:13 2011 +0100 @@ -32,6 +32,7 @@ document: targetEntity: Document inversedBy: tags + cascade: ["detach"] joinColumn: name: document_id referencedColumnName: id diff -r 7496254cfead -r d2fba1e3b94b Resources/public/js/wikiTag.js --- a/Resources/public/js/wikiTag.js Mon Nov 07 17:25:39 2011 +0100 +++ b/Resources/public/js/wikiTag.js Wed Nov 09 16:25:13 2011 +0100 @@ -332,7 +332,7 @@ wikitag_document_id:$('#wikitag_document_id').val() }, success: function(msg, textStatus, XMLHttpRequest) { - $('#wikittag_table_container').html(msg); + $('#wikitag_table_container').html(msg); wikitag_init_tags_events(); // And scroll to the bottom $("html").animate({ scrollTop: $(document).height() }, 500); diff -r 7496254cfead -r d2fba1e3b94b Search/Search.php --- a/Search/Search.php Mon Nov 07 17:25:39 2011 +0100 +++ b/Search/Search.php Wed Nov 09 16:25:13 2011 +0100 @@ -14,16 +14,104 @@ 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; + } + + /** + * 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'); + $res = $doctrine->getRepository('WikiTagBundle:Document'); + $tags_score = array(); + foreach($document->getTags() as $tag) + { + $label = $tag->getTag()->getLabel(); + + // + $fields = $this->getContainer()->getParameter("wiki_tag.fields"); + + $fieldquery = array(); + foreach ($fields as $fieldname => $fielddef) { + $columns = "$fieldname"; + $value = $label; + if(isset($fielddef['weight'])) + { + $weight = $fielddef['weight']; + } + else + { + $weight = 1.0; + } + $fieldquery[] = array("columns"=>$columns, "value"=>$value, "weight"=>$weight); + } + + $score_res = $res->search($fieldquery, 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) + { + $qb = $this->getDoctrine()->getEntityManager()->createQueryBuilder(); + $qb->select('t', 'COUNT( dt.id ) AS nb_docs'); + $qb->from('WikiTagBundle:Tag','t'); + $qb->leftJoin('t.documents', 'dt', 'WITH', 't = dt.tag'); + $qb->addGroupBy('t.id'); + $qb->addOrderBy('nb_docs','DESC'); + + } + } \ No newline at end of file