<commit_before><?php
namespace Concrete\Core\Page\Stack;

use Area;
use Concrete\Core\Multilingual\Page\Section\Section;
use GlobalArea;
use CacheLocal;
use Config;
use Loader;
use Page;
use PageType;

/**
 * Class Stack
 *
 * @package Concrete\Core\Page\Stack
 */
class Stack extends Page
{

    const ST_TYPE_USER_ADDED = 0;
    const ST_TYPE_GLOBAL_AREA = 20;

    /**
     * @param string $type
     * @return int
     */
    public static function mapImportTextToType($type)
    {
        switch ($type) {
            case 'global_area':
                return static::ST_TYPE_GLOBAL_AREA;
                break;
            default:
                return static::ST_TYPE_USER_ADDED;
                break;
        }
    }

    /**
     * @param $stackName
     * @return Stack
     */
    public static function getOrCreateGlobalArea($stackName)
    {
        $stack = static::getByName($stackName);
        if (!$stack) {
            $stack = static::addStack($stackName, static::ST_TYPE_GLOBAL_AREA);
        }
        return $stack;
    }

    /**
     * @param string $stackName
     * @param string $cvID
     * @return Page
     */
    public static function getByName($stackName, $cvID = 'RECENT')
    {
        $c = \Page::getCurrentPage();
        $identifier = sprintf('/stack/name/%s/%s', $stackName, $c->getCollectionID());
        $cache = \Core::make('cache/request');
        $item = $cache->getItem($identifier);
        if (!$item->isMiss()) {
            $cID = $item->get();
        } else {
            $item->lock();
            $db = Loader::db();
            $ms = false;
            if (\Core::make('multilingual/detector')->isEnabled()) {
                $ms = Section::getBySectionOfSite($c);
                if (!is_object($ms)) {
                    $ms = static::getPreferredSection();
                }
            }

            if (is_object($ms)) {
                $cID = $db->GetOne('select cID from Stacks where stName = ? and stMultilingualSection = ?', array($stackName, $ms->getCollectionID()));
            } else {
                $cID = $db->GetOne('select cID from Stacks where stName = ?', array($stackName));
            }
            $item->set($cID);
        }

        return static::getByID($cID, $cvID);
    }

    /**
     * @param int    $cID
     * @param string $cvID
     * @return bool|Page
     */
    public static function getByID($cID, $cvID = 'RECENT')
    {
        $db = Loader::db();
        $c = parent::getByID($cID, $cvID, 'Stack');

        if (static::isValidStack($c)) {
            return $c;
        }<commit_msg>Don't access current page methods if there's no current page<commit_after><?php
namespace Concrete\Core\Page\Stack;

use Area;
use Concrete\Core\Multilingual\Page\Section\Section;
use GlobalArea;
use CacheLocal;
use Config;
use Loader;
use Page;
use PageType;

/**
 * Class Stack
 *
 * @package Concrete\Core\Page\Stack
 */
class Stack extends Page
{

    const ST_TYPE_USER_ADDED = 0;
    const ST_TYPE_GLOBAL_AREA = 20;

    /**
     * @param string $type
     * @return int
     */
    public static function mapImportTextToType($type)
    {
        switch ($type) {
            case 'global_area':
                return static::ST_TYPE_GLOBAL_AREA;
                break;
            default:
                return static::ST_TYPE_USER_ADDED;
                break;
        }
    }

    /**
     * @param $stackName
     * @return Stack
     */
    public static function getOrCreateGlobalArea($stackName)
    {
        $stack = static::getByName($stackName);
        if (!$stack) {
            $stack = static::addStack($stackName, static::ST_TYPE_GLOBAL_AREA);
        }
        return $stack;
    }

    /**
     * @param string $stackName
     * @param string $cvID
     * @return Page
     */
    public static function getByName($stackName, $cvID = 'RECENT')
    {
        $c = \Page::getCurrentPage();
        if (is_object($c) && (!$c->isError())) {
            $identifier = sprintf('/stack/name/%s/%s', $stackName, $c->getCollectionID());
            $cache = \Core::make('cache/request');
            $item = $cache->getItem($identifier);
            if (!$item->isMiss()) {
                $cID = $item->get();
            } else {
                $item->lock();
                $db = Loader::db();
                $ms = false;
                if (\Core::make('multilingual/detector')->isEnabled()) {
                    $ms = Section::getBySectionOfSite($c);
                    if (!is_object($ms)) {
                        $ms = static::getPreferredSection();
                    }
                }
    
                if (is_object($ms)) {
                    $cID = $db->GetOne('select cID from Stacks where stName = ? and stMultilingualSection = ?', array($stackName, $ms->getCollectionID()));
                } else {
                    $cID = $db->GetOne('select cID from Stacks where stName = ?', array($stackName));
                }
                $item->set($cID);
            }
        } else {
            $cID = Loader::db()->GetOne('select cID from Stacks where stName = ?', array($stackName));
        }
        return $cID ? static::getByID($cID, $cvID) : false;
    }

    /**
     * @param int    $cID
     * @param string $cvID
     * @return bool|Page
     */
    public static function getByID($cID, $cvID = 'RECENT')
    {
        $db = Loader::db();
        $c = parent::getByID($cID, $cvID, 'Stack');

        if (static::isValidStack($c)) {
            return $c;
        }
{'fraction_non_alphanumeric': 0.14490388103010518, 'fraction_numerical': 0.001088139281828074, 'mean_word_length': 1.7924050632911392, 'pattern_counts': {'":': 0, '<': 5, '<?xml version=': 0, '>': 25, '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': '509781', 'n_tokens_mistral': 1686, 'n_tokens_neox': 1651, 'n_words': 347}