<commit_before><?php
/**
 * Nooku Framework - http://www.nooku.org
 *
 * @copyright	Copyright (C) 2011 - 2013 <NAME> and Timble CVBA. (http://www.timble.net)
 * @license		GNU GPLv3 <http://www.gnu.org/licenses/gpl.html>
 * @link		git://git.assembla.com/nooku-framework.git for the canonical source repository
 */

use Nooku\Library;

/**
 * Pageable Database Behavior
 *
 * @author  <NAME> <http://nooku.assembla.com/profile/arunasmazeika>
 * @package Component\Articles
 */
class ArticlesDatabaseBehaviorPageable extends Library\DatabaseBehaviorAbstract
{
    protected $_constraints;

    protected $_pages;

    /**
     * @var int The ID of the user to get accessible pages from.
     */
    protected $_user;

    public function __construct(Library\ObjectConfig $config)
    {
        parent::__construct($config);

        $this->_user = $this->getObject('com:users.model.users')->id($config->user)->getRow();
    }

    protected function _beforeSelect(Library\DatabaseContext $context)
    {
        $this->_filterByPages($context);
    }

    protected function _filterByPages(Library\DatabaseContext $context)
    {
        $base_where = '';

        foreach ($context->query->where as $where) {
            $base_where .= ' ' . $where['combination'] . ' ' . $where['condition'];
        }

        if (!empty($base_where)) {
            $base_where = ' AND ' . $base_where;
        }

        $constraints = $this->_getConstraints();

        if ($categories = $constraints['categories'])
        {
            $context->query->where('categories.categories_category_id IN :categories')
                ->bind(array('categories' => $categories));
        }

        if ($parents = $constraints['category_parents'])
        {
            $context->query->where('(categories.parent_id IN :parents' . $base_where . ')', 'OR')
                ->bind(array('parents' => $parents));
        }

        if ($articles = $constraints['articles'])
        {
            $context->query->where('(tbl.articles_article_id IN :articles' . $base_where . ')', 'OR')
                ->bind(array('articles' => $articles));
        }
    }

    protected function _getConstraints()
    {
        if (!$this->_constraints) {

            $constraints = array('categories' => array(), 'articles' => array(), 'category_parents' => array());

            if ($pages = $this->_getPages())
            {
                foreach ($pages as $page)
                {
                    $link = $page->getLink();

                    // Particular case ... all articles from all categories.
                    if ($page->link_url == 'option=com_articles&view=articles') {
                        $constraints['categories'][] = 0;
                    }

                    if (isset($link->query['category']))
                    {
                        // Assume view=articles
                        if ($link->query['view'] != 'categories') {
                            $constraints['categories'][] = (int) $link->query['category'];
                        } else {
                            $constraints['category_parents'][] = (int) $link->query['category'];
                        }
                    }

                    if (($link->query['view'] == 'article') && isset($link->query['id'])) {
                        $constraints['articles'][] = $link->query['id'];
                    }
                }

                // No filtering by category.
                if (in_array(0, $constraints['categories'])) {
                    $constraints['categories'] = array();
                }
            }

            $this->_constraints = $constraints;
        }

        return $this->_constraints;
    }

    protected function _getPages()
    {
        if (!$this->_pages)
        {
            $user = $this->_user;

            $needles = array(
                'users_group_id' => array_merge(array(0), $user->getGroups()),
                'component_name' => 'com_'.$this->getMixer()->getIdentifier()->package);

            if ($user->guest) {
                $needles['access'] = 0;
            }

            $pages = $this->getObject('com:pages.model.pages')
                           ->application('site')
                           ->published(true)
                           ->getRowset()->find($needles);

            $this->_pages = $pages;
        }

        return $this->_pages;
    }
<commit_msg>User user.provider to load a user object.<commit_after><?php
/**
 * Nooku Framework - http://www.nooku.org
 *
 * @copyright	Copyright (C) 2011 - 2013 <NAME> and Timble CVBA. (http://www.timble.net)
 * @license		GNU GPLv3 <http://www.gnu.org/licenses/gpl.html>
 * @link		git://git.assembla.com/nooku-framework.git for the canonical source repository
 */

use Nooku\Library;

/**
 * Pageable Database Behavior
 *
 * @author  <NAME> <http://nooku.assembla.com/profile/arunasmazeika>
 * @package Component\Articles
 */
class ArticlesDatabaseBehaviorPageable extends Library\DatabaseBehaviorAbstract
{
    protected $_constraints;

    protected $_pages;

    /**
     * @var int The ID of the user to get accessible pages from.
     */
    protected $_user;

    public function __construct(Library\ObjectConfig $config)
    {
        parent::__construct($config);

        $this->_user = $this->getObject('user.provider')->load($config->user);
    }

    protected function _beforeSelect(Library\DatabaseContext $context)
    {
        $this->_filterByPages($context);
    }

    protected function _filterByPages(Library\DatabaseContext $context)
    {
        $base_where = '';

        foreach ($context->query->where as $where) {
            $base_where .= ' ' . $where['combination'] . ' ' . $where['condition'];
        }

        if (!empty($base_where)) {
            $base_where = ' AND ' . $base_where;
        }

        $constraints = $this->_getConstraints();

        if ($categories = $constraints['categories'])
        {
            $context->query->where('categories.categories_category_id IN :categories')
                ->bind(array('categories' => $categories));
        }

        if ($parents = $constraints['category_parents'])
        {
            $context->query->where('(categories.parent_id IN :parents' . $base_where . ')', 'OR')
                ->bind(array('parents' => $parents));
        }

        if ($articles = $constraints['articles'])
        {
            $context->query->where('(tbl.articles_article_id IN :articles' . $base_where . ')', 'OR')
                ->bind(array('articles' => $articles));
        }
    }

    protected function _getConstraints()
    {
        if (!$this->_constraints) {

            $constraints = array('categories' => array(), 'articles' => array(), 'category_parents' => array());

            if ($pages = $this->_getPages())
            {
                foreach ($pages as $page)
                {
                    $link = $page->getLink();

                    // Particular case ... all articles from all categories.
                    if ($page->link_url == 'option=com_articles&view=articles') {
                        $constraints['categories'][] = 0;
                    }

                    if (isset($link->query['category']))
                    {
                        // Assume view=articles
                        if ($link->query['view'] != 'categories') {
                            $constraints['categories'][] = (int) $link->query['category'];
                        } else {
                            $constraints['category_parents'][] = (int) $link->query['category'];
                        }
                    }

                    if (($link->query['view'] == 'article') && isset($link->query['id'])) {
                        $constraints['articles'][] = $link->query['id'];
                    }
                }

                // No filtering by category.
                if (in_array(0, $constraints['categories'])) {
                    $constraints['categories'] = array();
                }
            }

            $this->_constraints = $constraints;
        }

        return $this->_constraints;
    }

    protected function _getPages()
    {
        if (!$this->_pages)
        {
            $user = $this->_user;

            $needles = array(
                'users_group_id' => array_merge(array(0), $user->getGroups()),
                'component_name' => 'com_'.$this->getMixer()->getIdentifier()->package);

            if (!$user->isAuthentic()) {
                $needles['access'] = 0;
            }

            $pages = $this->getObject('com:pages.model.pages')
                           ->application('site')
                           ->published(true)
                           ->getRowset()->find($needles);

            $this->_pages = $pages;
        }

        return $this->_pages;
    }

{'fraction_non_alphanumeric': 0.15423671443077966, 'fraction_numerical': 0.0029335439467448945, 'mean_word_length': 1.7032631899969504, 'pattern_counts': {'":': 0, '<': 13, '<?xml version=': 0, '>': 116, 'https://': 0, 'lorem ipsum': 0, 'www.': 6, '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': '6614699', 'n_tokens_mistral': 2453, 'n_tokens_neox': 2299, 'n_words': 424}