<commit_before><?php

namespace AlexisLefebvre\Bundle\AsyncTweetsBundle\Entity;

use Doctrine\ORM\EntityRepository;

/**
 * TweetRepository
 *
 * This class was generated by the Doctrine ORM. Add your own custom
 * repository methods below.
 */
class TweetRepository extends EntityRepository
{
    private $nbTweets = 10;
    
    public function getWithUsers($page = 1)
    {
        $firstResult = (($page - 1) * $this->nbTweets);
                
        $qb = $this->createQueryBuilder('t');
        
        $query = $qb
            ->select('t, user')
            ->innerJoin('t.user', 'user')
            
            ->addSelect('rt')
            ->leftJoin('t.retweeted_status', 'rt')
            
            ->addSelect('rt_user')
            ->leftJoin('rt.user', 'rt_user')
            
            // Ignore tweets that were only retweeted
            ->where(
                $qb->expr()->eq('t.in_timeline', 'true')
            )
            
            ->orderBy('t.id', 'DESC')
            
            ->setFirstResult($firstResult)
            ->setMaxResults($this->nbTweets)
        ;
        
        return $query->getQuery()->getResult();
    }
    
    public function getWithUsersAndMedias($firstTweetId = null)
    {
        $qb = $this->createQueryBuilder('t');
        
        $query = $qb
            ->select('t, user, medias')
            ->innerJoin('t.user', 'user')
            ->leftJoin('t.medias', 'medias')
            
            ->addSelect('rt')
            ->leftJoin('t.retweeted_status', 'rt')
            
            ->addSelect('rt_user')
            ->leftJoin('rt.user', 'rt_user')
            
            // Ignore tweets that were only retweeted
            ->where(
                $qb->expr()->eq('t.in_timeline', 'true')
            )
            
            ->orderBy('t.id', 'ASC')
            
            ->setFirstResult(0)
            ->setMaxResults($this->nbTweets)
        ;
        
        if (! is_null($firstTweetId))
        {
            $query = $query->andWhere(
                $qb->expr()->gte('t.id', $firstTweetId)
            );
        }
        
        return $query->getQuery()->getResult();
    }
    
    /**
     * @param string $condition
     * @param string $order
     */
    private function getTweetId($condition, $order, $tweetId)
    {
        $qb = $this->createQueryBuilder('t')
            ->select('t.id')
            
            ->where('t.id '.$condition.' :tweetId')
            ->setParameter(':tweetId', $tweetId)
            
            ->orderBy('t.id', $order)
            
            ->setFirstResult($this->nbTweets - 1)
            ->setMaxResults(1)
        ;
        
        $result = $qb->getQuery()->getOneOrNullResult();
        
        return($this->getIdOrNull($result));
    }
    
    private function getIdOrNull($result)
    {
        return(is_array($result) ? $result['id'] : null);
    }
    
    public function getPreviousAndNextTweetId($tweetId)
    {
        return(array(
            $this->getTweetId('<', 'DESC', $tweetId),
            $this->getTweetId('>', 'ASC', $tweetId)
        ));
    }
    
    public function countPendingTweets($lastTweetId = null)
    {
        $qb = $this->createQueryBuilder('t');
        
        $query = $qb
            ->add('select', $qb->expr()->count('t.id'))
            // Ignore tweets that were only retweeted
            ->where(
                $qb->expr()->eq('t.in_timeline', 'true')
            )
        ;
        
        if (! is_null($lastTweetId))
        {
            $query = $query->andWhere(
                $qb->expr()->gte('t.id', $lastTweetId)
            );
        }
        
        # return result of "COUNT()" query
        return $query->getQuery()->getSingleScalarResult();
    }
    
    public function getLastTweet()
    {
        $qb = $this->createQueryBuilder('t')
            ->addOrderBy('t.id', 'DESC')
            ->setFirstResult(0)
            ->setMaxResults(1)
        ;
        
        return $qb->getQuery()->getOneOrNullResult();
    }
    
    private function getTweetsLessThanId($tweetId)
    {
        $qb = $this->createQueryBuilder('t')
            ->select('t, m')
            ->leftJoin('t.medias', 'm')
            ->where('t.id < :tweetId')
            ->setParameter(':tweetId', $tweetId)
            
            ->orderBy('t.id', 'DESC')
        ;
        
        return($qb->getQuery()->getResult());
    }
    
    /**
     * Remove Media not associated to any Tweet
     */
    private function removeOrphanMedias(Media $media)
    {
        if (count($media->getTweets()) == 0)
        {
            $this->_em->remove($media);
        }
    }
    
    public function deleteTweetsLessThanId($tweetId)
    {
        $count = 0;
        
        foreach ($this->getTweetsLessThanId($tweetId) as $tweet)
        {
            foreach ($tweet->getMedias() as $media)
            {
                $tweet->removeMedia($media);
                $this->removeOrphanMedias($media);
            }
            
            $this->_em->remove($tweet);
            
            $count++;
        }
        
        $this->_em->flush();
        
        return($count);
    }
}<commit_msg>Fixes #9: fixed bug with "Delete old tweets" button<commit_after><?php

namespace AlexisLefebvre\Bundle\AsyncTweetsBundle\Entity;

use Doctrine\ORM\EntityRepository;

/**
 * TweetRepository
 *
 * This class was generated by the Doctrine ORM. Add your own custom
 * repository methods below.
 */
class TweetRepository extends EntityRepository
{
    private $nbTweets = 10;
    
    public function getWithUsers($page = 1)
    {
        $firstResult = (($page - 1) * $this->nbTweets);
                
        $qb = $this->createQueryBuilder('t');
        
        $query = $qb
            ->select('t, user')
            ->innerJoin('t.user', 'user')
            
            ->addSelect('rt')
            ->leftJoin('t.retweeted_status', 'rt')
            
            ->addSelect('rt_user')
            ->leftJoin('rt.user', 'rt_user')
            
            // Ignore tweets that were only retweeted
            ->where(
                $qb->expr()->eq('t.in_timeline', 'true')
            )
            
            ->orderBy('t.id', 'DESC')
            
            ->setFirstResult($firstResult)
            ->setMaxResults($this->nbTweets)
        ;
        
        return $query->getQuery()->getResult();
    }
    
    public function getWithUsersAndMedias($firstTweetId = null)
    {
        $qb = $this->createQueryBuilder('t');
        
        $query = $qb
            ->select('t, user, medias')
            ->innerJoin('t.user', 'user')
            ->leftJoin('t.medias', 'medias')
            
            ->addSelect('rt')
            ->leftJoin('t.retweeted_status', 'rt')
            
            ->addSelect('rt_user')
            ->leftJoin('rt.user', 'rt_user')
            
            // Ignore tweets that were only retweeted
            ->where(
                $qb->expr()->eq('t.in_timeline', 'true')
            )
            
            ->orderBy('t.id', 'ASC')
            
            ->setFirstResult(0)
            ->setMaxResults($this->nbTweets)
        ;
        
        if (! is_null($firstTweetId))
        {
            $query = $query->andWhere(
                $qb->expr()->gte('t.id', $firstTweetId)
            );
        }
        
        return $query->getQuery()->getResult();
    }
    
    /**
     * @param string $condition
     * @param string $order
     */
    private function getTweetId($condition, $order, $tweetId)
    {
        $qb = $this->createQueryBuilder('t')
            ->select('t.id')
            
            ->where('t.id '.$condition.' :tweetId')
            ->setParameter(':tweetId', $tweetId)
            
            ->andWhere('t.in_timeline = true')
            
            ->orderBy('t.id', $order)
            
            ->setFirstResult($this->nbTweets - 1)
            ->setMaxResults(1)
        ;
        
        $result = $qb->getQuery()->getOneOrNullResult();
        
        return($this->getIdOrNull($result));
    }
    
    private function getIdOrNull($result)
    {
        return(is_array($result) ? $result['id'] : null);
    }
    
    public function getPreviousAndNextTweetId($tweetId)
    {
        return(array(
            $this->getTweetId('<', 'DESC', $tweetId),
            $this->getTweetId('>', 'ASC', $tweetId)
        ));
    }
    
    public function countPendingTweets($lastTweetId = null)
    {
        $qb = $this->createQueryBuilder('t');
        
        $query = $qb
            ->add('select', $qb->expr()->count('t.id'))
            // Ignore tweets that were only retweeted
            ->where(
                $qb->expr()->eq('t.in_timeline', 'true')
            )
        ;
        
        if (! is_null($lastTweetId))
        {
            $query = $query->andWhere(
                $qb->expr()->gte('t.id', $lastTweetId)
            );
        }
        
        # return result of "COUNT()" query
        return $query->getQuery()->getSingleScalarResult();
    }
    
    public function getLastTweet()
    {
        $qb = $this->createQueryBuilder('t')
            ->addOrderBy('t.id', 'DESC')
            ->setFirstResult(0)
            ->setMaxResults(1)
        ;
        
        return $qb->getQuery()->getOneOrNullResult();
    }
    
    private function getTweetsLessThanId($tweetId)
    {
        $qb = $this->createQueryBuilder('t')
            ->select('t, m')
            ->leftJoin('t.medias', 'm')
            
            ->where('t.id < :tweetId')
            ->setParameter(':tweetId', $tweetId)
            
            // Ignore retweeted tweets (it would break foreign keys)
            //  http://stackoverflow.com/questions/15087933/how-to-do-left-join-in-doctrine/15088250#15088250
            ->leftJoin(
                'AsyncTweetsBundle:Tweet',
                't2',
                'WITH',
                't.id = t2.retweeted_status'
            )
            ->andWhere('(t2.retweeted_status IS NULL)')
            
            ->orderBy('t.id', 'DESC')
        ;
        
        return($qb->getQuery()->getResult());
    }
    
    /**
     * Remove Media not associated to any Tweet
     */
    private function removeOrphanMedias(Media $media)
    {
        if (count($media->getTweets()) == 0)
        {
            $this->_em->remove($media);
        }
    }
    
    public function deleteTweetsLessThanId($tweetId)
    {
        $count = 0;
        
        foreach ($this->getTweetsLessThanId($tweetId) as $tweet)
        {
            foreach ($tweet->getMedias() as $media)
            {
                $tweet->removeMedia($media);
                $this->removeOrphanMedias($media);
            }
            
            // Ignore tweets that were only retweeted
            if ($tweet->getInTimeline())
            {
                $count++;
            }
            
            $this->_em->remove($tweet);
        }
        
        $this->_em->flush();
        
        return($count);
    }
}
{'fraction_non_alphanumeric': 0.15091958168049044, 'fraction_numerical': 0.004507753335737469, 'mean_word_length': 1.4167755991285402, 'pattern_counts': {'":': 0, '<': 9, '<?xml version=': 0, '>': 183, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '5771259', 'n_tokens_mistral': 3369, 'n_tokens_neox': 3311, 'n_words': 522}