Entity/TagRepository.php
author ymh <ymh.work@gmail.com>
Thu, 23 Feb 2012 23:02:07 +0100
changeset 75 ca2a145e67f3
parent 74 901463f9b11c
permissions -rwxr-xr-x
Completion service add the nb of doc + test

<?php
/*
 * This file is part of the WikiTagBundle package.
 *
 * (c) IRI <http://www.iri.centrepompidou.fr/>
 *
 * For the full copyright and license information, please view the LICENSE
 * file that was distributed with this source code.
 */

namespace IRI\Bundle\WikiTagBundle\Entity;

use Doctrine\ORM\EntityRepository;
use IRI\Bundle\WikiTagBundle\Utils\WikiTagUtils;
use IRI\Bundle\WikiTagBundle\Entity\Tag;

/**
 * TagRepository
 *
 * This class was generated by the Doctrine ORM. Add your own custom
 * repository methods below.
 */
class TagRepository extends EntityRepository
{
    public function getTagCloud($max_tags)
    {
        $qb = $this->getEntityManager()->createQueryBuilder();
        $qb->select('t.id', 't.label', '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');
        $qb->setMaxResults($max_tags);
        
        $query = $qb->getQuery();
        return $query->getResult();
    }
    
    /**
     * returns a list of tags label containing the given seed.
     * The seed is either at the beggining, the end of the label or at the beggining of a word.
     * @param string $seed
     */
    public function getCompletion($seed)
    {
        $qb = $this->getEntityManager()->createQueryBuilder();
        $qb->select('t.label', 'COUNT(dt.id) AS nb_docs');
        $qb->from('WikiTagBundle:Tag','t');
        $qb->leftJoin('t.documents', 'dt', 'WITH', 't = dt.tag');
        $qb->addGroupBy('t.label');
        $qb->where($qb->expr()->orx(
            $qb->expr()->like('t.label',$qb->expr()->literal(addcslashes(mysql_real_escape_string($seed),"%_")."%")),
            $qb->expr()->like('t.label',$qb->expr()->literal("%".addcslashes(mysql_real_escape_string($seed),"%_"))),
            $qb->expr()->like('t.label',$qb->expr()->literal("% ".addcslashes(mysql_real_escape_string($seed),"%_")."%"))
        ));
        
        $query = $qb->getQuery();
        
        return $query->getResult();
        
    }
    
    private function findSemantizedTag($entityList, $normalized_label)
    {
        $tag = null;
        foreach ($entityList as $entity) {
            if(!is_null($normalized_label) && (!is_a($entity, "\IRI\Bundle\WikiTagBundle\Model\TagInterface") || $normalized_label !== $entity->getNormalizedLabel())) {
                continue;
            }
            $t = $entity;
            if($tag==null
            || $tag->getUrlStatus() === Tag::$TAG_URL_STATUS_DICT['unsemantized']
            || ($tag->getUrlStatus() === Tag::$TAG_URL_STATUS_DICT['null_result'] && $t->getUrlStatus() !== Tag::$TAG_URL_STATUS_DICT['unsemantized'])) {
                $tag = $t;
                if($tag->getUrlStatus()!=Tag::$TAG_URL_STATUS_DICT['unsemantized'] && $tag->getUrlStatus()!=Tag::$TAG_URL_STATUS_DICT['null_result']) {
                    break;
                }
            }
        }
        
        return $tag;
        
    }
    
    /**
    * Get or create tag. Returns an array(tag:WikiTagTag, revision_id=int, created:Boolean)
    * @param $tag_label
    * @param $doctrine
    * @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))
    */
    public function getOrCreateTag($tag_label, $ignore_wikipedia_info=false, $logger=null)
    {
        $tag_label_normalized = WikiTagUtils::normalizeTag($tag_label);
        // We get the wikipedia references for the tag_label
        // We get or create the tag object
        $tags = $this->findBy(array('normalizedLabel' => $tag_label_normalized));
        $tag = $this->findSemantizedTag($tags, null);
        $uow = $this->getEntityManager()->getUnitOfWork();
        if(is_null($tag)) {
            $tag = $this->findSemantizedTag($uow->getScheduledEntityInsertions(), $tag_label_normalized);
        }
        if(is_null($tag)) {
            $tag = $this->findSemantizedTag($uow->getScheduledEntityUpdates(), $tag_label_normalized);
        }
        $wp_request_done = false;
        if($tag==null) {
            $tag = new Tag();
            $tag->setLabel($tag_label_normalized);
            $tag->setOriginalLabel($tag_label);
            $tag->setNormalizedLabel($tag_label_normalized);
            $created = true;
        }
        else {
            $created = false;
        }
    
        // We request Wikipedia if the tag is created or if this is a null result
        if($created==true || $tag->getUrlStatus()===Tag::$TAG_URL_STATUS_DICT['null_result']) {
            
            if($wp_request_done==false) {
                $wp_response = WikiTagUtils::getWikipediaInfo($tag_label_normalized, null, $ignore_wikipedia_info, $logger);
            }
            
            $tag->setWikipediaInfo($wp_response);
    
            // Save datas.
            $em = $this->getEntityManager();
            $em->persist($tag);
                
            $wikipedia_revision_id = $wp_response['revision_id'];
    
        }
        elseif($tag!=null && $tag->getWikipediaPageId()!=null) {
            $wp_response = WikiTagUtils::getWikipediaInfo(null, $tag->getWikipediaPageId(), $ignore_wikipedia_info, $logger);
            $wikipedia_revision_id = $wp_response['revision_id'];
        }
        else {
            $wikipedia_revision_id = null;
        }
    
        return array($tag, $wikipedia_revision_id, $created);//, $wpReponse);
    }
    
}