Services/SearchService.php
author ymh <ymh.work@gmail.com>
Mon, 19 Dec 2011 17:50:04 +0100
changeset 65 ba6b8e38d90e
parent 57 186c4121c7b3
child 68 e7384fb35f7a
permissions -rwxr-xr-x
Correct event mamangement in hdabo_sf
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
57
186c4121c7b3 add service tp copy tag from one document to another
ymh <ymh.work@gmail.com>
parents:
diff changeset
     1
<?php
186c4121c7b3 add service tp copy tag from one document to another
ymh <ymh.work@gmail.com>
parents:
diff changeset
     2
/*
186c4121c7b3 add service tp copy tag from one document to another
ymh <ymh.work@gmail.com>
parents:
diff changeset
     3
 * This file is part of the WikiTagBundle package.
186c4121c7b3 add service tp copy tag from one document to another
ymh <ymh.work@gmail.com>
parents:
diff changeset
     4
 *
186c4121c7b3 add service tp copy tag from one document to another
ymh <ymh.work@gmail.com>
parents:
diff changeset
     5
 * (c) IRI <http://www.iri.centrepompidou.fr/>
186c4121c7b3 add service tp copy tag from one document to another
ymh <ymh.work@gmail.com>
parents:
diff changeset
     6
 *
186c4121c7b3 add service tp copy tag from one document to another
ymh <ymh.work@gmail.com>
parents:
diff changeset
     7
 * For the full copyright and license information, please view the LICENSE
186c4121c7b3 add service tp copy tag from one document to another
ymh <ymh.work@gmail.com>
parents:
diff changeset
     8
 * file that was distributed with this source code.
186c4121c7b3 add service tp copy tag from one document to another
ymh <ymh.work@gmail.com>
parents:
diff changeset
     9
 */
186c4121c7b3 add service tp copy tag from one document to another
ymh <ymh.work@gmail.com>
parents:
diff changeset
    10
namespace IRI\Bundle\WikiTagBundle\Services;
186c4121c7b3 add service tp copy tag from one document to another
ymh <ymh.work@gmail.com>
parents:
diff changeset
    11
 
186c4121c7b3 add service tp copy tag from one document to another
ymh <ymh.work@gmail.com>
parents:
diff changeset
    12
use Symfony\Component\DependencyInjection\ContainerAware;
186c4121c7b3 add service tp copy tag from one document to another
ymh <ymh.work@gmail.com>
parents:
diff changeset
    13
use Symfony\Component\DependencyInjection\ContainerInterface;
186c4121c7b3 add service tp copy tag from one document to another
ymh <ymh.work@gmail.com>
parents:
diff changeset
    14
186c4121c7b3 add service tp copy tag from one document to another
ymh <ymh.work@gmail.com>
parents:
diff changeset
    15
class SearchService extends ContainerAware
186c4121c7b3 add service tp copy tag from one document to another
ymh <ymh.work@gmail.com>
parents:
diff changeset
    16
{
186c4121c7b3 add service tp copy tag from one document to another
ymh <ymh.work@gmail.com>
parents:
diff changeset
    17
    /**
186c4121c7b3 add service tp copy tag from one document to another
ymh <ymh.work@gmail.com>
parents:
diff changeset
    18
     * Get the container associated with this service.
186c4121c7b3 add service tp copy tag from one document to another
ymh <ymh.work@gmail.com>
parents:
diff changeset
    19
     * @return ContainerInterface
186c4121c7b3 add service tp copy tag from one document to another
ymh <ymh.work@gmail.com>
parents:
diff changeset
    20
     */
186c4121c7b3 add service tp copy tag from one document to another
ymh <ymh.work@gmail.com>
parents:
diff changeset
    21
    public function getContainer()
186c4121c7b3 add service tp copy tag from one document to another
ymh <ymh.work@gmail.com>
parents:
diff changeset
    22
    {
186c4121c7b3 add service tp copy tag from one document to another
ymh <ymh.work@gmail.com>
parents:
diff changeset
    23
        return $this->container;
186c4121c7b3 add service tp copy tag from one document to another
ymh <ymh.work@gmail.com>
parents:
diff changeset
    24
    }
186c4121c7b3 add service tp copy tag from one document to another
ymh <ymh.work@gmail.com>
parents:
diff changeset
    25
    
186c4121c7b3 add service tp copy tag from one document to another
ymh <ymh.work@gmail.com>
parents:
diff changeset
    26
    /**
186c4121c7b3 add service tp copy tag from one document to another
ymh <ymh.work@gmail.com>
parents:
diff changeset
    27
     * Public constructor with container as parameter for contruct injection.
186c4121c7b3 add service tp copy tag from one document to another
ymh <ymh.work@gmail.com>
parents:
diff changeset
    28
     * @param ContainerInterface $container
186c4121c7b3 add service tp copy tag from one document to another
ymh <ymh.work@gmail.com>
parents:
diff changeset
    29
     */
186c4121c7b3 add service tp copy tag from one document to another
ymh <ymh.work@gmail.com>
parents:
diff changeset
    30
    public function __construct(ContainerInterface $container)
186c4121c7b3 add service tp copy tag from one document to another
ymh <ymh.work@gmail.com>
parents:
diff changeset
    31
    {
186c4121c7b3 add service tp copy tag from one document to another
ymh <ymh.work@gmail.com>
parents:
diff changeset
    32
        $this->setContainer($container);
186c4121c7b3 add service tp copy tag from one document to another
ymh <ymh.work@gmail.com>
parents:
diff changeset
    33
    }
186c4121c7b3 add service tp copy tag from one document to another
ymh <ymh.work@gmail.com>
parents:
diff changeset
    34
    
186c4121c7b3 add service tp copy tag from one document to another
ymh <ymh.work@gmail.com>
parents:
diff changeset
    35
    private $doctrine;
186c4121c7b3 add service tp copy tag from one document to another
ymh <ymh.work@gmail.com>
parents:
diff changeset
    36
    
186c4121c7b3 add service tp copy tag from one document to another
ymh <ymh.work@gmail.com>
parents:
diff changeset
    37
    public function getDoctrine()
186c4121c7b3 add service tp copy tag from one document to another
ymh <ymh.work@gmail.com>
parents:
diff changeset
    38
    {
186c4121c7b3 add service tp copy tag from one document to another
ymh <ymh.work@gmail.com>
parents:
diff changeset
    39
        if(is_null($this->doctrine))
186c4121c7b3 add service tp copy tag from one document to another
ymh <ymh.work@gmail.com>
parents:
diff changeset
    40
        {
186c4121c7b3 add service tp copy tag from one document to another
ymh <ymh.work@gmail.com>
parents:
diff changeset
    41
            $this->doctrine = $this->getContainer()->get('doctrine');
186c4121c7b3 add service tp copy tag from one document to another
ymh <ymh.work@gmail.com>
parents:
diff changeset
    42
        }
186c4121c7b3 add service tp copy tag from one document to another
ymh <ymh.work@gmail.com>
parents:
diff changeset
    43
        return $this->doctrine;
186c4121c7b3 add service tp copy tag from one document to another
ymh <ymh.work@gmail.com>
parents:
diff changeset
    44
    }
186c4121c7b3 add service tp copy tag from one document to another
ymh <ymh.work@gmail.com>
parents:
diff changeset
    45
    /**
186c4121c7b3 add service tp copy tag from one document to another
ymh <ymh.work@gmail.com>
parents:
diff changeset
    46
     *
186c4121c7b3 add service tp copy tag from one document to another
ymh <ymh.work@gmail.com>
parents:
diff changeset
    47
     * Enter description here ...
186c4121c7b3 add service tp copy tag from one document to another
ymh <ymh.work@gmail.com>
parents:
diff changeset
    48
     * @param string $value
186c4121c7b3 add service tp copy tag from one document to another
ymh <ymh.work@gmail.com>
parents:
diff changeset
    49
     * @param array $conditions
186c4121c7b3 add service tp copy tag from one document to another
ymh <ymh.work@gmail.com>
parents:
diff changeset
    50
     * @param array $fields
186c4121c7b3 add service tp copy tag from one document to another
ymh <ymh.work@gmail.com>
parents:
diff changeset
    51
     */
186c4121c7b3 add service tp copy tag from one document to another
ymh <ymh.work@gmail.com>
parents:
diff changeset
    52
    public function search($value, array $conditions, array $fields=null)
186c4121c7b3 add service tp copy tag from one document to another
ymh <ymh.work@gmail.com>
parents:
diff changeset
    53
    {
186c4121c7b3 add service tp copy tag from one document to another
ymh <ymh.work@gmail.com>
parents:
diff changeset
    54
        if(is_null($fields))
186c4121c7b3 add service tp copy tag from one document to another
ymh <ymh.work@gmail.com>
parents:
diff changeset
    55
        {
186c4121c7b3 add service tp copy tag from one document to another
ymh <ymh.work@gmail.com>
parents:
diff changeset
    56
            $fields = $this->getContainer()->getParameter("wiki_tag.fields");
186c4121c7b3 add service tp copy tag from one document to another
ymh <ymh.work@gmail.com>
parents:
diff changeset
    57
        }
186c4121c7b3 add service tp copy tag from one document to another
ymh <ymh.work@gmail.com>
parents:
diff changeset
    58
        $doctrine = $this->getContainer()->get('doctrine');
186c4121c7b3 add service tp copy tag from one document to another
ymh <ymh.work@gmail.com>
parents:
diff changeset
    59
        $res = $doctrine->getRepository('WikiTagBundle:Document');
186c4121c7b3 add service tp copy tag from one document to another
ymh <ymh.work@gmail.com>
parents:
diff changeset
    60
        $fieldquery = array();
186c4121c7b3 add service tp copy tag from one document to another
ymh <ymh.work@gmail.com>
parents:
diff changeset
    61
        foreach ($fields as $fieldname => $fielddef) {
186c4121c7b3 add service tp copy tag from one document to another
ymh <ymh.work@gmail.com>
parents:
diff changeset
    62
            if(isset($fielddef['weight']))
186c4121c7b3 add service tp copy tag from one document to another
ymh <ymh.work@gmail.com>
parents:
diff changeset
    63
            {
186c4121c7b3 add service tp copy tag from one document to another
ymh <ymh.work@gmail.com>
parents:
diff changeset
    64
                $weight = $fielddef['weight'];
186c4121c7b3 add service tp copy tag from one document to another
ymh <ymh.work@gmail.com>
parents:
diff changeset
    65
            }
186c4121c7b3 add service tp copy tag from one document to another
ymh <ymh.work@gmail.com>
parents:
diff changeset
    66
            else
186c4121c7b3 add service tp copy tag from one document to another
ymh <ymh.work@gmail.com>
parents:
diff changeset
    67
            {
186c4121c7b3 add service tp copy tag from one document to another
ymh <ymh.work@gmail.com>
parents:
diff changeset
    68
                $weight = 1.0;
186c4121c7b3 add service tp copy tag from one document to another
ymh <ymh.work@gmail.com>
parents:
diff changeset
    69
            }
186c4121c7b3 add service tp copy tag from one document to another
ymh <ymh.work@gmail.com>
parents:
diff changeset
    70
            $fieldquery[] = array("columns"=>$fieldname, "value"=>$value, "weight"=>$weight);
186c4121c7b3 add service tp copy tag from one document to another
ymh <ymh.work@gmail.com>
parents:
diff changeset
    71
        }
186c4121c7b3 add service tp copy tag from one document to another
ymh <ymh.work@gmail.com>
parents:
diff changeset
    72
        
186c4121c7b3 add service tp copy tag from one document to another
ymh <ymh.work@gmail.com>
parents:
diff changeset
    73
        $score_res = $res->search($fieldquery, $conditions);
186c4121c7b3 add service tp copy tag from one document to another
ymh <ymh.work@gmail.com>
parents:
diff changeset
    74
        
186c4121c7b3 add service tp copy tag from one document to another
ymh <ymh.work@gmail.com>
parents:
diff changeset
    75
        return $score_res;
186c4121c7b3 add service tp copy tag from one document to another
ymh <ymh.work@gmail.com>
parents:
diff changeset
    76
    }
186c4121c7b3 add service tp copy tag from one document to another
ymh <ymh.work@gmail.com>
parents:
diff changeset
    77
    
186c4121c7b3 add service tp copy tag from one document to another
ymh <ymh.work@gmail.com>
parents:
diff changeset
    78
    /**
186c4121c7b3 add service tp copy tag from one document to another
ymh <ymh.work@gmail.com>
parents:
diff changeset
    79
     * Service to reorder the tags using their notes in the index search
186c4121c7b3 add service tp copy tag from one document to another
ymh <ymh.work@gmail.com>
parents:
diff changeset
    80
     * @param IRI\Bundle\WikiTagBundle\Model\DocumentInterface $document
186c4121c7b3 add service tp copy tag from one document to another
ymh <ymh.work@gmail.com>
parents:
diff changeset
    81
     */
186c4121c7b3 add service tp copy tag from one document to another
ymh <ymh.work@gmail.com>
parents:
diff changeset
    82
    public function reorderTagsForDocument($document)
186c4121c7b3 add service tp copy tag from one document to another
ymh <ymh.work@gmail.com>
parents:
diff changeset
    83
    {
186c4121c7b3 add service tp copy tag from one document to another
ymh <ymh.work@gmail.com>
parents:
diff changeset
    84
        $doctrine = $this->getContainer()->get('doctrine');
186c4121c7b3 add service tp copy tag from one document to another
ymh <ymh.work@gmail.com>
parents:
diff changeset
    85
        
186c4121c7b3 add service tp copy tag from one document to another
ymh <ymh.work@gmail.com>
parents:
diff changeset
    86
        $tags_score = array();
186c4121c7b3 add service tp copy tag from one document to another
ymh <ymh.work@gmail.com>
parents:
diff changeset
    87
        
186c4121c7b3 add service tp copy tag from one document to another
ymh <ymh.work@gmail.com>
parents:
diff changeset
    88
        foreach($document->getTags() as $tag)
186c4121c7b3 add service tp copy tag from one document to another
ymh <ymh.work@gmail.com>
parents:
diff changeset
    89
        {
186c4121c7b3 add service tp copy tag from one document to another
ymh <ymh.work@gmail.com>
parents:
diff changeset
    90
            $label = $tag->getTag()->getLabel();
186c4121c7b3 add service tp copy tag from one document to another
ymh <ymh.work@gmail.com>
parents:
diff changeset
    91
            
186c4121c7b3 add service tp copy tag from one document to another
ymh <ymh.work@gmail.com>
parents:
diff changeset
    92
            $score_res = $this->search($label, array("id"=>$document->getId()));
186c4121c7b3 add service tp copy tag from one document to another
ymh <ymh.work@gmail.com>
parents:
diff changeset
    93
            
186c4121c7b3 add service tp copy tag from one document to another
ymh <ymh.work@gmail.com>
parents:
diff changeset
    94
            if(count($score_res)>0)
186c4121c7b3 add service tp copy tag from one document to another
ymh <ymh.work@gmail.com>
parents:
diff changeset
    95
            {
186c4121c7b3 add service tp copy tag from one document to another
ymh <ymh.work@gmail.com>
parents:
diff changeset
    96
                $score = floatval($score_res[0]['score']);
186c4121c7b3 add service tp copy tag from one document to another
ymh <ymh.work@gmail.com>
parents:
diff changeset
    97
            }
186c4121c7b3 add service tp copy tag from one document to another
ymh <ymh.work@gmail.com>
parents:
diff changeset
    98
            else
186c4121c7b3 add service tp copy tag from one document to another
ymh <ymh.work@gmail.com>
parents:
diff changeset
    99
            {
186c4121c7b3 add service tp copy tag from one document to another
ymh <ymh.work@gmail.com>
parents:
diff changeset
   100
                $score = 0.0;
186c4121c7b3 add service tp copy tag from one document to another
ymh <ymh.work@gmail.com>
parents:
diff changeset
   101
            }
186c4121c7b3 add service tp copy tag from one document to another
ymh <ymh.work@gmail.com>
parents:
diff changeset
   102
            $tags_score[] = array($score,$tag);
186c4121c7b3 add service tp copy tag from one document to another
ymh <ymh.work@gmail.com>
parents:
diff changeset
   103
        }
186c4121c7b3 add service tp copy tag from one document to another
ymh <ymh.work@gmail.com>
parents:
diff changeset
   104
        // sort tags based on score
186c4121c7b3 add service tp copy tag from one document to another
ymh <ymh.work@gmail.com>
parents:
diff changeset
   105
        $i=1;
186c4121c7b3 add service tp copy tag from one document to another
ymh <ymh.work@gmail.com>
parents:
diff changeset
   106
        usort($tags_score, function($a, $b) {
186c4121c7b3 add service tp copy tag from one document to another
ymh <ymh.work@gmail.com>
parents:
diff changeset
   107
            return $a[0]<$b[0]?1:-1;
186c4121c7b3 add service tp copy tag from one document to another
ymh <ymh.work@gmail.com>
parents:
diff changeset
   108
        });
186c4121c7b3 add service tp copy tag from one document to another
ymh <ymh.work@gmail.com>
parents:
diff changeset
   109
186c4121c7b3 add service tp copy tag from one document to another
ymh <ymh.work@gmail.com>
parents:
diff changeset
   110
        foreach($tags_score as $item)
186c4121c7b3 add service tp copy tag from one document to another
ymh <ymh.work@gmail.com>
parents:
diff changeset
   111
        {
186c4121c7b3 add service tp copy tag from one document to another
ymh <ymh.work@gmail.com>
parents:
diff changeset
   112
            $tag = $item[1];
186c4121c7b3 add service tp copy tag from one document to another
ymh <ymh.work@gmail.com>
parents:
diff changeset
   113
            $tag->setTagOrder($i++);
186c4121c7b3 add service tp copy tag from one document to another
ymh <ymh.work@gmail.com>
parents:
diff changeset
   114
            $tag->setIndexNote($item[0]);
186c4121c7b3 add service tp copy tag from one document to another
ymh <ymh.work@gmail.com>
parents:
diff changeset
   115
            $doctrine->getEntityManager()->persist($tag);
186c4121c7b3 add service tp copy tag from one document to another
ymh <ymh.work@gmail.com>
parents:
diff changeset
   116
        }
186c4121c7b3 add service tp copy tag from one document to another
ymh <ymh.work@gmail.com>
parents:
diff changeset
   117
        
186c4121c7b3 add service tp copy tag from one document to another
ymh <ymh.work@gmail.com>
parents:
diff changeset
   118
    }
186c4121c7b3 add service tp copy tag from one document to another
ymh <ymh.work@gmail.com>
parents:
diff changeset
   119
    
186c4121c7b3 add service tp copy tag from one document to another
ymh <ymh.work@gmail.com>
parents:
diff changeset
   120
    public function getTagCloud($max_tags)
186c4121c7b3 add service tp copy tag from one document to another
ymh <ymh.work@gmail.com>
parents:
diff changeset
   121
    {
186c4121c7b3 add service tp copy tag from one document to another
ymh <ymh.work@gmail.com>
parents:
diff changeset
   122
        $rep = $this->getDoctrine()->getRepository('WikiTagBundle:Tag');
186c4121c7b3 add service tp copy tag from one document to another
ymh <ymh.work@gmail.com>
parents:
diff changeset
   123
        return $rep->getTagCloud($max_tags);
186c4121c7b3 add service tp copy tag from one document to another
ymh <ymh.work@gmail.com>
parents:
diff changeset
   124
    }
186c4121c7b3 add service tp copy tag from one document to another
ymh <ymh.work@gmail.com>
parents:
diff changeset
   125
    
186c4121c7b3 add service tp copy tag from one document to another
ymh <ymh.work@gmail.com>
parents:
diff changeset
   126
    public function completion($seed)
186c4121c7b3 add service tp copy tag from one document to another
ymh <ymh.work@gmail.com>
parents:
diff changeset
   127
    {
186c4121c7b3 add service tp copy tag from one document to another
ymh <ymh.work@gmail.com>
parents:
diff changeset
   128
        $rep = $this->getDoctrine()->getRepository('WikiTagBundle:Tag');
186c4121c7b3 add service tp copy tag from one document to another
ymh <ymh.work@gmail.com>
parents:
diff changeset
   129
        
186c4121c7b3 add service tp copy tag from one document to another
ymh <ymh.work@gmail.com>
parents:
diff changeset
   130
        $res = array();
186c4121c7b3 add service tp copy tag from one document to another
ymh <ymh.work@gmail.com>
parents:
diff changeset
   131
        foreach ($rep->getCompletion($seed) as $value) {
186c4121c7b3 add service tp copy tag from one document to another
ymh <ymh.work@gmail.com>
parents:
diff changeset
   132
            $res[] = $value['label'];
186c4121c7b3 add service tp copy tag from one document to another
ymh <ymh.work@gmail.com>
parents:
diff changeset
   133
        }
186c4121c7b3 add service tp copy tag from one document to another
ymh <ymh.work@gmail.com>
parents:
diff changeset
   134
        
186c4121c7b3 add service tp copy tag from one document to another
ymh <ymh.work@gmail.com>
parents:
diff changeset
   135
        return $res;
186c4121c7b3 add service tp copy tag from one document to another
ymh <ymh.work@gmail.com>
parents:
diff changeset
   136
        
186c4121c7b3 add service tp copy tag from one document to another
ymh <ymh.work@gmail.com>
parents:
diff changeset
   137
    }
186c4121c7b3 add service tp copy tag from one document to another
ymh <ymh.work@gmail.com>
parents:
diff changeset
   138
    
186c4121c7b3 add service tp copy tag from one document to another
ymh <ymh.work@gmail.com>
parents:
diff changeset
   139
        
186c4121c7b3 add service tp copy tag from one document to another
ymh <ymh.work@gmail.com>
parents:
diff changeset
   140
}