<commit_before><?php

namespace PDS\StoryBundle\Entity;

use Doctrine\ORM\EntityRepository;
use Doctrine\ORM\NoResultException;
use Doctrine\ORM\QueryBuilder;

use PDS\StoryBundle\Entity\Story;
use PDS\StoryBundle\Entity\Time;
use PDS\StoryBundle\Entity\Country;
use PDS\StoryBundle\Entity\Tag;
use PDS\UserBundle\Entity\User;

class StoryRepository extends EntityRepository
{

    public function top($limit = 10)
    {
        return $this->getStories($this->generateTopQueryBuilder(), $limit);
    }

    public function byTime(Time $time)
    {
        return $this->getFilteredBy($time);
    }

    public function byTeller(User $user)
    {
        return $this->getFilteredBy($user);
    }

    public function byLocation(Country $country)
    {
        return $this->getFilteredBy($country);
    }

    public function byTopic(Tag $tag)
    {
        $storiesWithTag = $this->getEntityManager()
            ->getRepository('PDSStoryBundle:Tag')
            ->createQueryBuilder("t")
            ->select("tg.resourceId")
            ->leftJoin("t.tagging", "tg")
            ->where("tg.resourceType = 'story'")
            ->andWhere("t.id = ?1")
            ->setParameter(1, $tag->getId())
            ->getDQL();

        $queryBuilder = $this->generateTopQueryBuilder();
        return $this->getStories(
            $queryBuilder->where(
                $queryBuilder->expr()->in("s.id", $storiesWithTag)
            )
                ->setParameter(1, $tag->getId())
        );
    }


    public function related(Story $story)
    {
        return $this->top();
    }

    public function search($q)
    {
        return $this->top();
    }

    private function getStories(QueryBuilder $queryBuilder, $limit = 10)
    {
        $query = $queryBuilder->getQuery();
        $query->setMaxResults($limit);
        $r = $query->getResult();
        $res = array();
        foreach ($r as $rr) {
            $res[] = $rr[0];
        }
        return $res;
    }

    /**
     * @return \Doctrine\ORM\QueryBuilder
     */
    private function generateTopQueryBuilder()
    {
        return $this
            ->createQueryBuilder('s')
            ->select('s')
            ->addSelect("SUM(v.value)/COUNT(v.id) as rat")
            ->leftJoin("s.Votes", "v")
            ->groupBy("s.id")
            ->orderBy("rat", "desc");
    }


    private function getFilteredBy($relatedEntity)
    {
        $class = explode("\\", get_class($relatedEntity));
        return $this->getStories(
            $this->generateTopQueryBuilder()
                ->leftJoin("s." . end($class), "r")
                ->where("r.id = ?1")
                ->setParameter(1, $relatedEntity->getId())
        );
    }

}<commit_msg>search by title<commit_after><?php

namespace PDS\StoryBundle\Entity;

use Doctrine\ORM\EntityRepository;
use Doctrine\ORM\NoResultException;
use Doctrine\ORM\QueryBuilder;

use PDS\StoryBundle\Entity\Story;
use PDS\StoryBundle\Entity\Time;
use PDS\StoryBundle\Entity\Country;
use PDS\StoryBundle\Entity\Tag;
use PDS\UserBundle\Entity\User;

class StoryRepository extends EntityRepository
{

    public function top($limit = 10)
    {
        return $this->getStories($this->generateTopQueryBuilder(), $limit);
    }

    public function byTime(Time $time)
    {
        return $this->getFilteredBy($time);
    }

    public function byTeller(User $user)
    {
        return $this->getFilteredBy($user);
    }

    public function byLocation(Country $country)
    {
        return $this->getFilteredBy($country);
    }

    public function byTopic(Tag $tag)
    {
        $storiesWithTag = $this->getEntityManager()
            ->getRepository('PDSStoryBundle:Tag')
            ->createQueryBuilder("t")
            ->select("tg.resourceId")
            ->leftJoin("t.tagging", "tg")
            ->where("tg.resourceType = 'story'")
            ->andWhere("t.id = ?1")
            ->setParameter(1, $tag->getId())
            ->getDQL();

        $queryBuilder = $this->generateTopQueryBuilder();
        return $this->getStories(
            $queryBuilder->where(
                $queryBuilder->expr()->in("s.id", $storiesWithTag)
            )
                ->setParameter(1, $tag->getId())
        );
    }


    public function related(Story $story)
    {
        return $this->top();
    }

    public function search($q)
    {
        $queryBuilder = $this->generateTopQueryBuilder();
        return $this->getStories(
            $queryBuilder
                ->where($queryBuilder->expr()->like("s.title", "'%$q%'"))
        );
    }

    private function getStories(QueryBuilder $queryBuilder, $limit = 10)
    {
        $query = $queryBuilder->getQuery();
        $query->setMaxResults($limit);
        $r = $query->getResult();
        $res = array();
        foreach ($r as $rr) {
            $res[] = $rr[0];
        }
        return $res;
    }

    /**
     * @return \Doctrine\ORM\QueryBuilder
     */
    private function generateTopQueryBuilder()
    {
        return $this
            ->createQueryBuilder('s')
            ->select('s')
            ->addSelect("SUM(v.value)/COUNT(v.id) as rat")
            ->leftJoin("s.Votes", "v")
            ->groupBy("s.id")
            ->orderBy("rat", "desc");
    }


    private function getFilteredBy($relatedEntity)
    {
        $class = explode("\\", get_class($relatedEntity));
        return $this->getStories(
            $this->generateTopQueryBuilder()
                ->leftJoin("s." . end($class), "r")
                ->where("r.id = ?1")
                ->setParameter(1, $relatedEntity->getId())
        );
    }

}
{'fraction_non_alphanumeric': 0.15407908799429995, 'fraction_numerical': 0.003562522265764161, 'mean_word_length': 2.086860912589335, 'pattern_counts': {'":': 0, '<': 5, '<?xml version=': 0, '>': 85, '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': '2227341', 'n_tokens_mistral': 1715, 'n_tokens_neox': 1658, 'n_words': 287}