Entity/TagRepository.php
author ymh <ymh.work@gmail.com>
Tue, 21 Feb 2012 15:15:22 +0100
changeset 74 901463f9b11c
parent 68 e7384fb35f7a
child 75 ca2a145e67f3
permissions -rwxr-xr-x
add headers for public repository release
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
2
13f43f53d0ba first implementation
ymh <ymh.work@gmail.com>
parents:
diff changeset
     1
<?php
74
901463f9b11c add headers for public repository release
ymh <ymh.work@gmail.com>
parents: 68
diff changeset
     2
/*
901463f9b11c add headers for public repository release
ymh <ymh.work@gmail.com>
parents: 68
diff changeset
     3
 * This file is part of the WikiTagBundle package.
901463f9b11c add headers for public repository release
ymh <ymh.work@gmail.com>
parents: 68
diff changeset
     4
 *
901463f9b11c add headers for public repository release
ymh <ymh.work@gmail.com>
parents: 68
diff changeset
     5
 * (c) IRI <http://www.iri.centrepompidou.fr/>
901463f9b11c add headers for public repository release
ymh <ymh.work@gmail.com>
parents: 68
diff changeset
     6
 *
901463f9b11c add headers for public repository release
ymh <ymh.work@gmail.com>
parents: 68
diff changeset
     7
 * For the full copyright and license information, please view the LICENSE
901463f9b11c add headers for public repository release
ymh <ymh.work@gmail.com>
parents: 68
diff changeset
     8
 * file that was distributed with this source code.
901463f9b11c add headers for public repository release
ymh <ymh.work@gmail.com>
parents: 68
diff changeset
     9
 */
2
13f43f53d0ba first implementation
ymh <ymh.work@gmail.com>
parents:
diff changeset
    10
13f43f53d0ba first implementation
ymh <ymh.work@gmail.com>
parents:
diff changeset
    11
namespace IRI\Bundle\WikiTagBundle\Entity;
13f43f53d0ba first implementation
ymh <ymh.work@gmail.com>
parents:
diff changeset
    12
13f43f53d0ba first implementation
ymh <ymh.work@gmail.com>
parents:
diff changeset
    13
use Doctrine\ORM\EntityRepository;
42
0e57c730bb18 Documentation and add alternative wp url and label + migrations
ymh <ymh.work@gmail.com>
parents: 34
diff changeset
    14
use IRI\Bundle\WikiTagBundle\Utils\WikiTagUtils;
0e57c730bb18 Documentation and add alternative wp url and label + migrations
ymh <ymh.work@gmail.com>
parents: 34
diff changeset
    15
use IRI\Bundle\WikiTagBundle\Entity\Tag;
2
13f43f53d0ba first implementation
ymh <ymh.work@gmail.com>
parents:
diff changeset
    16
13f43f53d0ba first implementation
ymh <ymh.work@gmail.com>
parents:
diff changeset
    17
/**
13f43f53d0ba first implementation
ymh <ymh.work@gmail.com>
parents:
diff changeset
    18
 * TagRepository
13f43f53d0ba first implementation
ymh <ymh.work@gmail.com>
parents:
diff changeset
    19
 *
13f43f53d0ba first implementation
ymh <ymh.work@gmail.com>
parents:
diff changeset
    20
 * This class was generated by the Doctrine ORM. Add your own custom
13f43f53d0ba first implementation
ymh <ymh.work@gmail.com>
parents:
diff changeset
    21
 * repository methods below.
13f43f53d0ba first implementation
ymh <ymh.work@gmail.com>
parents:
diff changeset
    22
 */
13f43f53d0ba first implementation
ymh <ymh.work@gmail.com>
parents:
diff changeset
    23
class TagRepository extends EntityRepository
13f43f53d0ba first implementation
ymh <ymh.work@gmail.com>
parents:
diff changeset
    24
{
34
21fab44f46fe add event on tag changed
ymh <ymh.work@gmail.com>
parents: 2
diff changeset
    25
    public function getTagCloud($max_tags)
21fab44f46fe add event on tag changed
ymh <ymh.work@gmail.com>
parents: 2
diff changeset
    26
    {
21fab44f46fe add event on tag changed
ymh <ymh.work@gmail.com>
parents: 2
diff changeset
    27
        $qb = $this->getEntityManager()->createQueryBuilder();
21fab44f46fe add event on tag changed
ymh <ymh.work@gmail.com>
parents: 2
diff changeset
    28
        $qb->select('t.id', 't.label', 'COUNT( dt.id ) AS nb_docs');
21fab44f46fe add event on tag changed
ymh <ymh.work@gmail.com>
parents: 2
diff changeset
    29
        $qb->from('WikiTagBundle:Tag','t');
21fab44f46fe add event on tag changed
ymh <ymh.work@gmail.com>
parents: 2
diff changeset
    30
        $qb->leftJoin('t.documents', 'dt', 'WITH', 't = dt.tag');
21fab44f46fe add event on tag changed
ymh <ymh.work@gmail.com>
parents: 2
diff changeset
    31
        $qb->addGroupBy('t.id');
21fab44f46fe add event on tag changed
ymh <ymh.work@gmail.com>
parents: 2
diff changeset
    32
        $qb->addOrderBy('nb_docs','DESC');
21fab44f46fe add event on tag changed
ymh <ymh.work@gmail.com>
parents: 2
diff changeset
    33
        $qb->setMaxResults($max_tags);
21fab44f46fe add event on tag changed
ymh <ymh.work@gmail.com>
parents: 2
diff changeset
    34
        
21fab44f46fe add event on tag changed
ymh <ymh.work@gmail.com>
parents: 2
diff changeset
    35
        $query = $qb->getQuery();
21fab44f46fe add event on tag changed
ymh <ymh.work@gmail.com>
parents: 2
diff changeset
    36
        return $query->getResult();
21fab44f46fe add event on tag changed
ymh <ymh.work@gmail.com>
parents: 2
diff changeset
    37
    }
21fab44f46fe add event on tag changed
ymh <ymh.work@gmail.com>
parents: 2
diff changeset
    38
    
68
e7384fb35f7a improve search test and documentation
ymh <ymh.work@gmail.com>
parents: 67
diff changeset
    39
    /**
e7384fb35f7a improve search test and documentation
ymh <ymh.work@gmail.com>
parents: 67
diff changeset
    40
     * returns a list of tags label containing the given seed.
e7384fb35f7a improve search test and documentation
ymh <ymh.work@gmail.com>
parents: 67
diff changeset
    41
     * The seed is either at the beggining, the end of the label or at the beggining of a word.
e7384fb35f7a improve search test and documentation
ymh <ymh.work@gmail.com>
parents: 67
diff changeset
    42
     * @param string $seed
e7384fb35f7a improve search test and documentation
ymh <ymh.work@gmail.com>
parents: 67
diff changeset
    43
     */
34
21fab44f46fe add event on tag changed
ymh <ymh.work@gmail.com>
parents: 2
diff changeset
    44
    public function getCompletion($seed)
21fab44f46fe add event on tag changed
ymh <ymh.work@gmail.com>
parents: 2
diff changeset
    45
    {
21fab44f46fe add event on tag changed
ymh <ymh.work@gmail.com>
parents: 2
diff changeset
    46
        $qb = $this->getEntityManager()->createQueryBuilder();
65
ba6b8e38d90e Correct event mamangement in hdabo_sf
ymh <ymh.work@gmail.com>
parents: 63
diff changeset
    47
        $qb->select('DISTINCT t.label');
34
21fab44f46fe add event on tag changed
ymh <ymh.work@gmail.com>
parents: 2
diff changeset
    48
        $qb->from('WikiTagBundle:Tag','t');
21fab44f46fe add event on tag changed
ymh <ymh.work@gmail.com>
parents: 2
diff changeset
    49
        $qb->where($qb->expr()->orx(
63
774ba82dca59 add tests and fixtures
ymh <ymh.work@gmail.com>
parents: 60
diff changeset
    50
            $qb->expr()->like('t.label',$qb->expr()->literal(addcslashes(mysql_real_escape_string($seed),"%_")."%")),
34
21fab44f46fe add event on tag changed
ymh <ymh.work@gmail.com>
parents: 2
diff changeset
    51
            $qb->expr()->like('t.label',$qb->expr()->literal("%".addcslashes(mysql_real_escape_string($seed),"%_"))),
21fab44f46fe add event on tag changed
ymh <ymh.work@gmail.com>
parents: 2
diff changeset
    52
            $qb->expr()->like('t.label',$qb->expr()->literal("% ".addcslashes(mysql_real_escape_string($seed),"%_")."%"))
21fab44f46fe add event on tag changed
ymh <ymh.work@gmail.com>
parents: 2
diff changeset
    53
        ));
21fab44f46fe add event on tag changed
ymh <ymh.work@gmail.com>
parents: 2
diff changeset
    54
        
21fab44f46fe add event on tag changed
ymh <ymh.work@gmail.com>
parents: 2
diff changeset
    55
        $query = $qb->getQuery();
21fab44f46fe add event on tag changed
ymh <ymh.work@gmail.com>
parents: 2
diff changeset
    56
        
21fab44f46fe add event on tag changed
ymh <ymh.work@gmail.com>
parents: 2
diff changeset
    57
        return $query->getResult();
21fab44f46fe add event on tag changed
ymh <ymh.work@gmail.com>
parents: 2
diff changeset
    58
        
21fab44f46fe add event on tag changed
ymh <ymh.work@gmail.com>
parents: 2
diff changeset
    59
    }
42
0e57c730bb18 Documentation and add alternative wp url and label + migrations
ymh <ymh.work@gmail.com>
parents: 34
diff changeset
    60
    
67
989d9e117586 correct bugs on database opt
ymh <ymh.work@gmail.com>
parents: 65
diff changeset
    61
    private function findSemantizedTag($entityList, $normalized_label)
42
0e57c730bb18 Documentation and add alternative wp url and label + migrations
ymh <ymh.work@gmail.com>
parents: 34
diff changeset
    62
    {
0e57c730bb18 Documentation and add alternative wp url and label + migrations
ymh <ymh.work@gmail.com>
parents: 34
diff changeset
    63
        $tag = null;
67
989d9e117586 correct bugs on database opt
ymh <ymh.work@gmail.com>
parents: 65
diff changeset
    64
        foreach ($entityList as $entity) {
989d9e117586 correct bugs on database opt
ymh <ymh.work@gmail.com>
parents: 65
diff changeset
    65
            if(!is_null($normalized_label) && (!is_a($entity, "\IRI\Bundle\WikiTagBundle\Model\TagInterface") || $normalized_label !== $entity->getNormalizedLabel())) {
989d9e117586 correct bugs on database opt
ymh <ymh.work@gmail.com>
parents: 65
diff changeset
    66
                continue;
989d9e117586 correct bugs on database opt
ymh <ymh.work@gmail.com>
parents: 65
diff changeset
    67
            }
989d9e117586 correct bugs on database opt
ymh <ymh.work@gmail.com>
parents: 65
diff changeset
    68
            $t = $entity;
58
87bf6ec8af90 add unsemantized status
ymh <ymh.work@gmail.com>
parents: 42
diff changeset
    69
            if($tag==null
87bf6ec8af90 add unsemantized status
ymh <ymh.work@gmail.com>
parents: 42
diff changeset
    70
            || $tag->getUrlStatus() === Tag::$TAG_URL_STATUS_DICT['unsemantized']
87bf6ec8af90 add unsemantized status
ymh <ymh.work@gmail.com>
parents: 42
diff changeset
    71
            || ($tag->getUrlStatus() === Tag::$TAG_URL_STATUS_DICT['null_result'] && $t->getUrlStatus() !== Tag::$TAG_URL_STATUS_DICT['unsemantized'])) {
42
0e57c730bb18 Documentation and add alternative wp url and label + migrations
ymh <ymh.work@gmail.com>
parents: 34
diff changeset
    72
                $tag = $t;
58
87bf6ec8af90 add unsemantized status
ymh <ymh.work@gmail.com>
parents: 42
diff changeset
    73
                if($tag->getUrlStatus()!=Tag::$TAG_URL_STATUS_DICT['unsemantized'] && $tag->getUrlStatus()!=Tag::$TAG_URL_STATUS_DICT['null_result']) {
42
0e57c730bb18 Documentation and add alternative wp url and label + migrations
ymh <ymh.work@gmail.com>
parents: 34
diff changeset
    74
                    break;
0e57c730bb18 Documentation and add alternative wp url and label + migrations
ymh <ymh.work@gmail.com>
parents: 34
diff changeset
    75
                }
0e57c730bb18 Documentation and add alternative wp url and label + migrations
ymh <ymh.work@gmail.com>
parents: 34
diff changeset
    76
            }
0e57c730bb18 Documentation and add alternative wp url and label + migrations
ymh <ymh.work@gmail.com>
parents: 34
diff changeset
    77
        }
67
989d9e117586 correct bugs on database opt
ymh <ymh.work@gmail.com>
parents: 65
diff changeset
    78
        
989d9e117586 correct bugs on database opt
ymh <ymh.work@gmail.com>
parents: 65
diff changeset
    79
        return $tag;
989d9e117586 correct bugs on database opt
ymh <ymh.work@gmail.com>
parents: 65
diff changeset
    80
        
989d9e117586 correct bugs on database opt
ymh <ymh.work@gmail.com>
parents: 65
diff changeset
    81
    }
989d9e117586 correct bugs on database opt
ymh <ymh.work@gmail.com>
parents: 65
diff changeset
    82
    
989d9e117586 correct bugs on database opt
ymh <ymh.work@gmail.com>
parents: 65
diff changeset
    83
    /**
989d9e117586 correct bugs on database opt
ymh <ymh.work@gmail.com>
parents: 65
diff changeset
    84
    * Get or create tag. Returns an array(tag:WikiTagTag, revision_id=int, created:Boolean)
989d9e117586 correct bugs on database opt
ymh <ymh.work@gmail.com>
parents: 65
diff changeset
    85
    * @param $tag_label
989d9e117586 correct bugs on database opt
ymh <ymh.work@gmail.com>
parents: 65
diff changeset
    86
    * @param $doctrine
989d9e117586 correct bugs on database opt
ymh <ymh.work@gmail.com>
parents: 65
diff changeset
    87
    * @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))
989d9e117586 correct bugs on database opt
ymh <ymh.work@gmail.com>
parents: 65
diff changeset
    88
    */
68
e7384fb35f7a improve search test and documentation
ymh <ymh.work@gmail.com>
parents: 67
diff changeset
    89
    public function getOrCreateTag($tag_label, $ignore_wikipedia_info=false, $logger=null)
67
989d9e117586 correct bugs on database opt
ymh <ymh.work@gmail.com>
parents: 65
diff changeset
    90
    {
989d9e117586 correct bugs on database opt
ymh <ymh.work@gmail.com>
parents: 65
diff changeset
    91
        $tag_label_normalized = WikiTagUtils::normalizeTag($tag_label);
989d9e117586 correct bugs on database opt
ymh <ymh.work@gmail.com>
parents: 65
diff changeset
    92
        // We get the wikipedia references for the tag_label
989d9e117586 correct bugs on database opt
ymh <ymh.work@gmail.com>
parents: 65
diff changeset
    93
        // We get or create the tag object
989d9e117586 correct bugs on database opt
ymh <ymh.work@gmail.com>
parents: 65
diff changeset
    94
        $tags = $this->findBy(array('normalizedLabel' => $tag_label_normalized));
989d9e117586 correct bugs on database opt
ymh <ymh.work@gmail.com>
parents: 65
diff changeset
    95
        $tag = $this->findSemantizedTag($tags, null);
989d9e117586 correct bugs on database opt
ymh <ymh.work@gmail.com>
parents: 65
diff changeset
    96
        $uow = $this->getEntityManager()->getUnitOfWork();
989d9e117586 correct bugs on database opt
ymh <ymh.work@gmail.com>
parents: 65
diff changeset
    97
        if(is_null($tag)) {
989d9e117586 correct bugs on database opt
ymh <ymh.work@gmail.com>
parents: 65
diff changeset
    98
            $tag = $this->findSemantizedTag($uow->getScheduledEntityInsertions(), $tag_label_normalized);
989d9e117586 correct bugs on database opt
ymh <ymh.work@gmail.com>
parents: 65
diff changeset
    99
        }
989d9e117586 correct bugs on database opt
ymh <ymh.work@gmail.com>
parents: 65
diff changeset
   100
        if(is_null($tag)) {
989d9e117586 correct bugs on database opt
ymh <ymh.work@gmail.com>
parents: 65
diff changeset
   101
            $tag = $this->findSemantizedTag($uow->getScheduledEntityUpdates(), $tag_label_normalized);
989d9e117586 correct bugs on database opt
ymh <ymh.work@gmail.com>
parents: 65
diff changeset
   102
        }
42
0e57c730bb18 Documentation and add alternative wp url and label + migrations
ymh <ymh.work@gmail.com>
parents: 34
diff changeset
   103
        $wp_request_done = false;
0e57c730bb18 Documentation and add alternative wp url and label + migrations
ymh <ymh.work@gmail.com>
parents: 34
diff changeset
   104
        if($tag==null) {
0e57c730bb18 Documentation and add alternative wp url and label + migrations
ymh <ymh.work@gmail.com>
parents: 34
diff changeset
   105
            $tag = new Tag();
0e57c730bb18 Documentation and add alternative wp url and label + migrations
ymh <ymh.work@gmail.com>
parents: 34
diff changeset
   106
            $tag->setLabel($tag_label_normalized);
0e57c730bb18 Documentation and add alternative wp url and label + migrations
ymh <ymh.work@gmail.com>
parents: 34
diff changeset
   107
            $tag->setOriginalLabel($tag_label);
0e57c730bb18 Documentation and add alternative wp url and label + migrations
ymh <ymh.work@gmail.com>
parents: 34
diff changeset
   108
            $tag->setNormalizedLabel($tag_label_normalized);
0e57c730bb18 Documentation and add alternative wp url and label + migrations
ymh <ymh.work@gmail.com>
parents: 34
diff changeset
   109
            $created = true;
0e57c730bb18 Documentation and add alternative wp url and label + migrations
ymh <ymh.work@gmail.com>
parents: 34
diff changeset
   110
        }
0e57c730bb18 Documentation and add alternative wp url and label + migrations
ymh <ymh.work@gmail.com>
parents: 34
diff changeset
   111
        else {
0e57c730bb18 Documentation and add alternative wp url and label + migrations
ymh <ymh.work@gmail.com>
parents: 34
diff changeset
   112
            $created = false;
0e57c730bb18 Documentation and add alternative wp url and label + migrations
ymh <ymh.work@gmail.com>
parents: 34
diff changeset
   113
        }
0e57c730bb18 Documentation and add alternative wp url and label + migrations
ymh <ymh.work@gmail.com>
parents: 34
diff changeset
   114
    
58
87bf6ec8af90 add unsemantized status
ymh <ymh.work@gmail.com>
parents: 42
diff changeset
   115
        // We request Wikipedia if the tag is created or if this is a null result
87bf6ec8af90 add unsemantized status
ymh <ymh.work@gmail.com>
parents: 42
diff changeset
   116
        if($created==true || $tag->getUrlStatus()===Tag::$TAG_URL_STATUS_DICT['null_result']) {
42
0e57c730bb18 Documentation and add alternative wp url and label + migrations
ymh <ymh.work@gmail.com>
parents: 34
diff changeset
   117
            
0e57c730bb18 Documentation and add alternative wp url and label + migrations
ymh <ymh.work@gmail.com>
parents: 34
diff changeset
   118
            if($wp_request_done==false) {
68
e7384fb35f7a improve search test and documentation
ymh <ymh.work@gmail.com>
parents: 67
diff changeset
   119
                $wp_response = WikiTagUtils::getWikipediaInfo($tag_label_normalized, null, $ignore_wikipedia_info, $logger);
42
0e57c730bb18 Documentation and add alternative wp url and label + migrations
ymh <ymh.work@gmail.com>
parents: 34
diff changeset
   120
            }
0e57c730bb18 Documentation and add alternative wp url and label + migrations
ymh <ymh.work@gmail.com>
parents: 34
diff changeset
   121
            
0e57c730bb18 Documentation and add alternative wp url and label + migrations
ymh <ymh.work@gmail.com>
parents: 34
diff changeset
   122
            $tag->setWikipediaInfo($wp_response);
0e57c730bb18 Documentation and add alternative wp url and label + migrations
ymh <ymh.work@gmail.com>
parents: 34
diff changeset
   123
    
0e57c730bb18 Documentation and add alternative wp url and label + migrations
ymh <ymh.work@gmail.com>
parents: 34
diff changeset
   124
            // Save datas.
0e57c730bb18 Documentation and add alternative wp url and label + migrations
ymh <ymh.work@gmail.com>
parents: 34
diff changeset
   125
            $em = $this->getEntityManager();
0e57c730bb18 Documentation and add alternative wp url and label + migrations
ymh <ymh.work@gmail.com>
parents: 34
diff changeset
   126
            $em->persist($tag);
0e57c730bb18 Documentation and add alternative wp url and label + migrations
ymh <ymh.work@gmail.com>
parents: 34
diff changeset
   127
                
0e57c730bb18 Documentation and add alternative wp url and label + migrations
ymh <ymh.work@gmail.com>
parents: 34
diff changeset
   128
            $wikipedia_revision_id = $wp_response['revision_id'];
0e57c730bb18 Documentation and add alternative wp url and label + migrations
ymh <ymh.work@gmail.com>
parents: 34
diff changeset
   129
    
0e57c730bb18 Documentation and add alternative wp url and label + migrations
ymh <ymh.work@gmail.com>
parents: 34
diff changeset
   130
        }
60
984ba20c150b Remove unnecessary try/catch
ymh <ymh.work@gmail.com>
parents: 59
diff changeset
   131
        elseif($tag!=null && $tag->getWikipediaPageId()!=null) {
68
e7384fb35f7a improve search test and documentation
ymh <ymh.work@gmail.com>
parents: 67
diff changeset
   132
            $wp_response = WikiTagUtils::getWikipediaInfo(null, $tag->getWikipediaPageId(), $ignore_wikipedia_info, $logger);
42
0e57c730bb18 Documentation and add alternative wp url and label + migrations
ymh <ymh.work@gmail.com>
parents: 34
diff changeset
   133
            $wikipedia_revision_id = $wp_response['revision_id'];
0e57c730bb18 Documentation and add alternative wp url and label + migrations
ymh <ymh.work@gmail.com>
parents: 34
diff changeset
   134
        }
0e57c730bb18 Documentation and add alternative wp url and label + migrations
ymh <ymh.work@gmail.com>
parents: 34
diff changeset
   135
        else {
0e57c730bb18 Documentation and add alternative wp url and label + migrations
ymh <ymh.work@gmail.com>
parents: 34
diff changeset
   136
            $wikipedia_revision_id = null;
0e57c730bb18 Documentation and add alternative wp url and label + migrations
ymh <ymh.work@gmail.com>
parents: 34
diff changeset
   137
        }
0e57c730bb18 Documentation and add alternative wp url and label + migrations
ymh <ymh.work@gmail.com>
parents: 34
diff changeset
   138
    
0e57c730bb18 Documentation and add alternative wp url and label + migrations
ymh <ymh.work@gmail.com>
parents: 34
diff changeset
   139
        return array($tag, $wikipedia_revision_id, $created);//, $wpReponse);
0e57c730bb18 Documentation and add alternative wp url and label + migrations
ymh <ymh.work@gmail.com>
parents: 34
diff changeset
   140
    }
0e57c730bb18 Documentation and add alternative wp url and label + migrations
ymh <ymh.work@gmail.com>
parents: 34
diff changeset
   141
    
59
ymh <ymh.work@gmail.com>
parents: 50 58
diff changeset
   142
}