<commit_before><?php

namespace Backend\Modules\Pages\Domain\Page;

use Backend\Core\Engine\Model;
use Backend\Modules\Pages\Domain\ModuleExtra\ModuleExtra;
use Backend\Modules\Pages\Domain\ModuleExtra\ModuleExtraType;
use Backend\Modules\Pages\Domain\PageBlock\PageBlock;
use Backend\Modules\Pages\Engine\Model as BackendPagesModel;
use Common\Doctrine\Entity\Meta;
use Common\Locale;
use DateTime;
use Doctrine\Bundle\DoctrineBundle\Repository\ServiceEntityRepository;
use Doctrine\Common\Persistence\ManagerRegistry;
use Doctrine\ORM\AbstractQuery;
use Doctrine\ORM\Query\Expr\Join;
use Doctrine\ORM\QueryBuilder;
use ForkCMS\App\ForkController;
use Frontend\Core\Language\Language;

/**
 * @method Page|null find($id, $lockMode = null, $lockVersion = null)
 * @method Page|null findOneBy(array $criteria, array $orderBy = null)
 * @method Page[]    findAll()
 * @method Page[]    findBy(array $criteria, array $orderBy = null, $limit = null, $offset = null)
 */
class PageRepository extends ServiceEntityRepository
{
    public function __construct(ManagerRegistry $registry)
    {
        parent::__construct($registry, Page::class);
    }

    public function add(Page $page): void
    {
        $this->getEntityManager()->persist($page);
    }

    public function save(Page $page): void
    {
        $this->getEntityManager()->flush($page);
    }

    public function remove(Page $page): void
    {
        $this->getEntityManager()->remove($page);
        $this->getEntityManager()->flush($page);
    }

    public function archive(int $id, Locale $locale): void
    {
        $this
            ->createQueryBuilder('p')
            ->set('p.status', Status::archive())
            ->where('p.id = :id')
            ->andWhere('p.locale = :locale')
            ->setParameters(
                [
                    'id' => $id,
                    'locale' => $locale,
                ]
            )
            ->getQuery()
            ->execute();
    }

    public function deleteByRevisionIds(array $ids): void
    {
        $qb = $this->createQueryBuilder('p');
        $qb
            ->delete()
            ->where($qb->expr()->in('p.revisionId', ':ids'))
            ->setParameter('ids', $ids)
            ->getQuery()
            ->execute();

        $this->getEntityManager()->clear(PageBlock::class);
        $this->getEntityManager()->clear(Page::class);
    }

    public function deleteByIdAndUserIdAndStatusAndLocale(
        int $id,
        int $userId,
        Status $status,
        Locale $locale
    ): void {
        $this
            ->createQueryBuilder('p')
            ->delete()
            ->where('p.id = :id')
            ->andWhere('p.userId = :userId')
            ->andWhere('p.status = :status')
            ->andWhere('p.locale = :locale')
            ->setParameters(
                [
                    'id' => $id,
                    'userId' => $userId,
                    'status' => $status,
                    'locale' => $locale,
                ]
            )
            ->getQuery()
            ->execute();

        $this->getEntityManager()->clear(PageBlock::class);
        $this->getEntityManager()->clear(Page::class);
    }

    public function get(int $id, int $revisionId = null, Locale $locale = null): array
    {
        $qb = $this->buildGetQuery($id, $revisionId, $locale);

        // @todo This wil not be necessary when we can return the entities instead of arrays
        $results = $qb->getQuery()->getScalarResult();
        foreach ($results as &$result) {
            $result = $this->processFields($result);
        }

        return $results;
    }

    public function getOne(int $id, int $revisionId = null, Locale $locale = null): ?array
    {
        $qb = $this->buildGetQuery($id, $revisionId, $locale);
        $qb->setMaxResults(1);

        // @todo This wil not be necessary when we can return the entities instead of arrays
        $results = $qb->getQuery()->getScalarResult();

        if (count($results) === 0) {
            return null;
        }

        return $this->processFields($results[0]);
    }

    public function getLatestForApi(int $id, Locale $locale = null): ?array
    {
        $qb = $this->buildGetQuery($id, null, $locale);
        $qb
            ->select('p, m')
            ->andWhere('p.status = :active')
            ->setParameter('active', Status::active())
            ->addOrderBy('p.revisionId', 'DESC')
            ->setMaxResults(1);

        $results = $qb->getQuery()->getArrayResult();

        if (count($results) === 0) {
            return null;
        }

        $result = [];

        foreach ($results[0] as $pageItemKey => $pageItemValue) {
            $result[$pageItemKey] = $pageItemValue;
        }

        $result['meta'] = $results[1];

        // Unserialize data
        if (array_key_exists('data', $results)) {
            $result['data'] = unserialize($result['data'], ['allowed_classes' => false]);
        }

        $qb = $this
            ->createQueryBuilder('p')
            ->select('b')
            ->innerJoin('p.blocks', 'b')
            ->where('p.id = :pageId')
            ->andWhere('p.revisionId = :revisionId')
            ->andWhere('p.locale = :locale')
            ->orderBy('b.sequence', 'ASC');

        $qb->setParameters(
            [
                'pageId' => $id,
                'revisionId' => $result['revisionId'],
                'locale' => $locale,
            ]
        );

        $blocks = $qb
            ->getQuery()
            ->getArrayResult();

        $result['blocks'] = $blocks;

        $qb = $this
            ->getEntityManager()
            ->createQueryBuilder()
            ->select('e')
            ->from(ModuleExtra::class, 'e')<commit_msg>Fix pages api<commit_after><?php

namespace Backend\Modules\Pages\Domain\Page;

use Backend\Core\Engine\Model;
use Backend\Modules\Pages\Domain\ModuleExtra\ModuleExtra;
use Backend\Modules\Pages\Domain\ModuleExtra\ModuleExtraType;
use Backend\Modules\Pages\Domain\PageBlock\PageBlock;
use Backend\Modules\Pages\Engine\Model as BackendPagesModel;
use Common\Doctrine\Entity\Meta;
use Common\Locale;
use DateTime;
use Doctrine\Bundle\DoctrineBundle\Repository\ServiceEntityRepository;
use Doctrine\Common\Persistence\ManagerRegistry;
use Doctrine\ORM\AbstractQuery;
use Doctrine\ORM\Query\Expr\Join;
use Doctrine\ORM\QueryBuilder;
use ForkCMS\App\ForkController;
use Frontend\Core\Language\Language;

/**
 * @method Page|null find($id, $lockMode = null, $lockVersion = null)
 * @method Page|null findOneBy(array $criteria, array $orderBy = null)
 * @method Page[]    findAll()
 * @method Page[]    findBy(array $criteria, array $orderBy = null, $limit = null, $offset = null)
 */
class PageRepository extends ServiceEntityRepository
{
    public function __construct(ManagerRegistry $registry)
    {
        parent::__construct($registry, Page::class);
    }

    public function add(Page $page): void
    {
        $this->getEntityManager()->persist($page);
    }

    public function save(Page $page): void
    {
        $this->getEntityManager()->flush($page);
    }

    public function remove(Page $page): void
    {
        $this->getEntityManager()->remove($page);
        $this->getEntityManager()->flush($page);
    }

    public function archive(int $id, Locale $locale): void
    {
        $this
            ->createQueryBuilder('p')
            ->set('p.status', Status::archive())
            ->where('p.id = :id')
            ->andWhere('p.locale = :locale')
            ->setParameters(
                [
                    'id' => $id,
                    'locale' => $locale,
                ]
            )
            ->getQuery()
            ->execute();
    }

    public function deleteByRevisionIds(array $ids): void
    {
        $qb = $this->createQueryBuilder('p');
        $qb
            ->delete()
            ->where($qb->expr()->in('p.revisionId', ':ids'))
            ->setParameter('ids', $ids)
            ->getQuery()
            ->execute();

        $this->getEntityManager()->clear(PageBlock::class);
        $this->getEntityManager()->clear(Page::class);
    }

    public function deleteByIdAndUserIdAndStatusAndLocale(
        int $id,
        int $userId,
        Status $status,
        Locale $locale
    ): void {
        $this
            ->createQueryBuilder('p')
            ->delete()
            ->where('p.id = :id')
            ->andWhere('p.userId = :userId')
            ->andWhere('p.status = :status')
            ->andWhere('p.locale = :locale')
            ->setParameters(
                [
                    'id' => $id,
                    'userId' => $userId,
                    'status' => $status,
                    'locale' => $locale,
                ]
            )
            ->getQuery()
            ->execute();

        $this->getEntityManager()->clear(PageBlock::class);
        $this->getEntityManager()->clear(Page::class);
    }

    public function get(int $id, int $revisionId = null, Locale $locale = null): array
    {
        $qb = $this->buildGetQuery($id, $revisionId, $locale);

        // @todo This wil not be necessary when we can return the entities instead of arrays
        $results = $qb->getQuery()->getScalarResult();
        foreach ($results as &$result) {
            $result = $this->processFields($result);
        }

        return $results;
    }

    public function getOne(int $id, int $revisionId = null, Locale $locale = null): ?array
    {
        $qb = $this->buildGetQuery($id, $revisionId, $locale);
        $qb->setMaxResults(1);

        // @todo This wil not be necessary when we can return the entities instead of arrays
        $results = $qb->getQuery()->getScalarResult();

        if (count($results) === 0) {
            return null;
        }

        return $this->processFields($results[0]);
    }

    public function getLatestForApi(int $id, Locale $locale = null): ?array
    {
        $qb = $this->buildGetQuery($id, null, $locale);
        $qb
            ->select('p, m')
            ->andWhere('p.status = :active')
            ->setParameter('active', Status::active())
            ->addOrderBy('p.revisionId', 'DESC')
            ->setMaxResults(1);

        $results = $qb->getQuery()->getArrayResult();

        if (count($results) === 0) {
            return null;
        }

        $result = [];

        foreach ($results[0] as $pageItemKey => $pageItemValue) {
            $result[$pageItemKey] = $pageItemValue;
        }

        // Unserialize data
        if (array_key_exists('data', $results)) {
            $result['data'] = unserialize($result['data'], ['allowed_classes' => false]);
        }

        $qb = $this
            ->getEntityManager()->createQueryBuilder()
            ->select('b')
            ->from(PageBlock::class, 'b')
            ->innerJoin('b.page', 'p')
            ->where('p.id = :pageId')
            ->andWhere('p.revisionId = :revisionId')
            ->andWhere('p.locale = :locale')
            ->orderBy('b.sequence', 'ASC');

        $qb->setParameters(
            [
                'pageId' => $id,
                'revisionId' => $result['revisionId'],
                'locale' => $locale,
            ]
        );

        $blocks = $qb
            ->getQuery()
            ->getArrayResult();

        $result['blocks'] = $blocks;

        $qb = $this
            ->getEntityManager()
            ->createQueryBuilder()
            ->select('e')
            ->from(ModuleExtra::class, 'e')
{'fraction_non_alphanumeric': 0.15427976758303702, 'fraction_numerical': 0.0011273957158962795, 'mean_word_length': 1.9961028838659391, 'pattern_counts': {'":': 0, '<': 5, '<?xml version=': 0, '>': 169, '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': '4889405', 'n_tokens_mistral': 3348, 'n_tokens_neox': 3274, 'n_words': 659}