<commit_before><?php

namespace DotPlant\EntityStructure\components;

use DevGroup\TagDependencyHelper\LazyCacheTrait;
use DevGroup\TagDependencyHelper\NamingHelper;
use DotPlant\EntityStructure\models\BaseStructure;
use DotPlant\EntityStructure\models\Entity;
use Faker\Provider\Base;
use yii;
use yii\base\Object;
use yii\helpers\ArrayHelper;
use yii\web\Request;
use yii\web\UrlManager;
use yii\web\UrlRuleInterface;

class StructureUrlRule extends Object implements UrlRuleInterface
{
    const ROUTE = 'universal/show';
    const MAIN_PAGE_URL = '~mainpage~';

    protected $allowedUrlParams = ['page', 'sort'];

    /**
     * Parses the given request and returns the corresponding route and parameters.
     *
     * @param UrlManager $manager the URL manager
     * @param Request $request the request component
     *
     * @return array|boolean the parsing result. The route and the parameters are returned as an array.
     * If false, it means this rule cannot be used to parse this path info.
     */
    public function parseRequest($manager, $request)
    {
        // @todo: Add caching
        $pathInfo = $request->getPathInfo();
        if ($pathInfo === '') {
            $pathInfo = self::MAIN_PAGE_URL;
        }
        $cacheKey = "StructureUrlCache:$pathInfo";
        $cached = Yii::$app->cache->get($cacheKey);
        if ($cached === false) {
            $parts = explode('/', preg_replace('#/+#', '/', $pathInfo));
            /** @var BaseStructure $structure */
            $structure = null;
            $lastStructure = null;
            $route = self::ROUTE;
            $routeParams = [];
            foreach ($parts as $index => $slug) {
                $key = "parseRequest:$slug:" . Yii::$app->multilingual->context_id . ":" . ($structure === null ? 'null' : $structure['id']);
                $structureCached = Yii::$app->cache->get(md5($key));
                if ($structureCached === false) {
                    $structure = BaseStructure::find()
                        ->select(['id', 'entity_id'])
                        ->where(
                            [
                                // @todo: Use SQL-index
                                BaseStructure::getTranslationTableName() . '.slug' => $slug,
                                BaseStructure::getTranslationTableName() . '.is_active' => true,
                                'context_id' => [Yii::$app->multilingual->context_id, null],
                                'is_deleted' => false,
                                'parent_id' => $structure === null ? null : $structure['id'],
                            ]
                        )
                        ->asArray(true)
                        ->limit(1)
                        ->one();

                    if ($structure !== null) {
                        Yii::$app->cache->set(
                            $key,
                            $structure,
                            86400,
                            new yii\caching\TagDependency([
                                'tags' => NamingHelper::getObjectTag(BaseStructure::class, $structure['id'])
                            ])
                        );
                    }
                } else {
                    $structure = $structureCached;
                }
                if ($structure === null) {
                    if (
                        $lastStructure === null
                        || ($entity = Entity::loadModel($lastStructure['entity_id'])) === null
                        || count($slugs = array_slice($parts, $index)) < 1
                    ) {<commit_msg>parsing fix<commit_after><?php

namespace DotPlant\EntityStructure\components;

use DevGroup\TagDependencyHelper\LazyCacheTrait;
use DevGroup\TagDependencyHelper\NamingHelper;
use DotPlant\EntityStructure\models\BaseStructure;
use DotPlant\EntityStructure\models\Entity;
use Faker\Provider\Base;
use yii;
use yii\base\Object;
use yii\helpers\ArrayHelper;
use yii\web\Request;
use yii\web\UrlManager;
use yii\web\UrlRuleInterface;

class StructureUrlRule extends Object implements UrlRuleInterface
{
    const ROUTE = 'universal/show';
    const MAIN_PAGE_URL = '~mainpage~';

    protected $allowedUrlParams = ['page', 'sort'];

    /**
     * Parses the given request and returns the corresponding route and parameters.
     *
     * @param UrlManager $manager the URL manager
     * @param Request $request the request component
     *
     * @return array|boolean the parsing result. The route and the parameters are returned as an array.
     * If false, it means this rule cannot be used to parse this path info.
     */
    public function parseRequest($manager, $request)
    {
        // @todo: Add caching
        $pathInfo = $request->getPathInfo();
        if ($pathInfo === '') {
            $pathInfo = self::MAIN_PAGE_URL;
        }
        $cacheKey = "StructureUrlCache:$pathInfo";
        $cached = Yii::$app->cache->get($cacheKey);
        if ($cached === false) {
            $parts = explode('/', preg_replace('#/+#', '/', $pathInfo));
            /** @var BaseStructure $structure */
            $structure = null;
            $lastStructure = null;
            $route = self::ROUTE;
            $routeParams = [];
            foreach ($parts as $index => $slug) {
                $key = "parseRequest:$slug:" . Yii::$app->multilingual->context_id . ":" . ($structure === null ? 'null' : $structure['id']);
                $structureCached = Yii::$app->cache->get(md5($key));
                if ($structureCached === false) {
                    $structure = BaseStructure::find()
                        ->select(['id', 'entity_id'])
                        ->where( // @todo: Use SQL-index
                            [
                                'and',
                                [BaseStructure::getTranslationTableName() . '.slug' => $slug],
                                [BaseStructure::getTranslationTableName() . '.is_active' => true],
                                [
                                    'or',
                                    ['context_id' => Yii::$app->multilingual->context_id],
                                    ['context_id' => null],
                                ],
                                ['is_deleted' => false],
                                ['parent_id' => $structure === null ? null : $structure['id']],
                            ]
                        )
                        ->asArray(true)
                        ->limit(1)
                        ->one();

                    if ($structure !== null) {
                        Yii::$app->cache->set(
                            $key,
                            $structure,
                            86400,
                            new yii\caching\TagDependency([
                                'tags' => NamingHelper::getObjectTag(BaseStructure::class, $structure['id'])
                            ])
                        );
                    }
                } else {
                    $structure = $structureCached;
                }
                if ($structure === null) {
                    if (
                        $lastStructure === null
                        || ($entity = Entity::loadModel($lastStructure['entity_id'])) === null
                        || count($slugs = array_slice($parts, $index)) < 1
                    ) {
{'fraction_non_alphanumeric': 0.12513440860215053, 'fraction_numerical': 0.002150537634408602, 'mean_word_length': 1.4096502590673574, 'pattern_counts': {'":': 2, '<': 7, '<?xml version=': 0, '>': 50, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 4, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '4023235', 'n_tokens_mistral': 1883, 'n_tokens_neox': 1806, 'n_words': 404}