<commit_before><?php

namespace ZEDx\Utils;

use Carbon\Carbon;
use Exception;
use File;
use Modules;
use Themes;
use Widgets;
use ZEDx\Core;
use ZEDx\Models\Package;
use Zipper;

class Updater
{
    protected $is_console;
    protected $latestJson = [];
    protected $updateJson = [];
    protected $packageType;
    protected $packageNamespace;
    protected $packageVersion;
    protected $eventId;
    protected $eventProgress;
    protected $logData = [];
    protected $storageUpdateFolder;
    protected $initEventMessage = false;

    public function __construct()
    {
        $this->logData = [];
    }

    public function setPackageType($type)
    {
        $this->packageType = $type;

        return $this;
    }

    public function setPackageNamespace($namespace)
    {
        $this->packageNamespace = $namespace;
    }

    public function setPackageVersion($version)
    {
        $this->packageVersion = $version;
    }

    public function getPackageVersion()
    {
        return $this->packageVersion;
    }

    public function isLatest($force = false)
    {
        $list = $this->getUpdatesList($force);

        return empty($list['core'])
            && empty($list['theme'])
            && empty($list['module'])
            && empty($list['widget']);
    }

    public function isLatestPackage($type, $namespace)
    {
        $list = $this->getUpdatesList();

        return (bool) !array_get($list, $type.'.'.$namespace, false);
    }

    /**
     * Get all updates that should be updated.
     *
     * @return mixed
     */
    public function getUpdatesList($force = false)
    {
        static $result;

        if ($result) {
            return $result;
        }

        $result = [
            'core'    => [],
            'theme'   => [],
            'widget'  => [],
            'module'  => [],
        ];

        $setting = setting();

        if ($force || ($setting->api_checked_at && $setting->api_checked_at->diffInHours() >= 12)) {
            $this->updateVersionsFromApi();
        }

        $packages = Package::all();
        $componentsVersions = $this->getPackagesVersions();

        foreach ($packages as $package) {
            if (array_get($componentsVersions, $package->type.'.'.$package->namespace) == $package->api_version) {
                continue;
            }

            $result[$package->type][$package->namespace] = [
                'namespace' => $package->namespace,
                'date'      => $package->date,
                'version'   => $package->api_version,
            ];
        }

        return $result;
    }

    protected function updateVersionsFromApi()
    {
        $list = $this->getPackagesVersions();

        foreach ($list as $type => $packages) {
            foreach ($packages as $package => $version) {
                $this->setPackageType($type);
                $this->setPackageNamespace($package);

                $latest = $this->getLatest();

                $model = Package::firstOrNew([
                    'type'      => $type,
                    'namespace' => $package,
                ]);

                if ($latest == null) {
                    $model->delete();
                    continue;
                }


                $model->api_version = $latest->version;
                $model->date = Carbon::createFromFormat('F d Y H:i:s', $latest->date);

                $model->save();
            }
        }

        $setting = setting();

        $setting->api_checked_at = Carbon::now();
        $setting->save();
    }

    /**
     * Get all packages versions.
     *
     * @return mixed
     */
    public function getPackagesVersions()
    {
        $components = [
            'core'    => [],
            'theme'   => [],
            'widget'  => [],
            'module'  => [],
        ];

        $components['core']['ZEDx'] = Core::VERSION;

        foreach (Themes::all() as $theme) {
            $components['theme'][$theme['manifest']['name']] = $theme['manifest']['version'];
        }

        foreach (Widgets::noType()->all(null, true) as $namespace => $widget) {
            $components['widget'][$namespace] = $widget->version;
        }

        foreach (Modules::all() as $module) {
            $components['module'][$module->name] = $module->version;<commit_msg>fix widgets list<commit_after><?php

namespace ZEDx\Utils;

use Carbon\Carbon;
use Exception;
use File;
use Modules;
use Themes;
use Widgets;
use ZEDx\Core;
use ZEDx\Models\Package;
use Zipper;

class Updater
{
    protected $is_console;
    protected $latestJson = [];
    protected $updateJson = [];
    protected $packageType;
    protected $packageNamespace;
    protected $packageVersion;
    protected $eventId;
    protected $eventProgress;
    protected $logData = [];
    protected $storageUpdateFolder;
    protected $initEventMessage = false;

    public function __construct()
    {
        $this->logData = [];
    }

    public function setPackageType($type)
    {
        $this->packageType = $type;

        return $this;
    }

    public function setPackageNamespace($namespace)
    {
        $this->packageNamespace = $namespace;
    }

    public function setPackageVersion($version)
    {
        $this->packageVersion = $version;
    }

    public function getPackageVersion()
    {
        return $this->packageVersion;
    }

    public function isLatest($force = false)
    {
        $list = $this->getUpdatesList($force);

        return empty($list['core'])
            && empty($list['theme'])
            && empty($list['module'])
            && empty($list['widget']);
    }

    public function isLatestPackage($type, $namespace)
    {
        $list = $this->getUpdatesList();

        return (bool) !array_get($list, $type.'.'.$namespace, false);
    }

    /**
     * Get all updates that should be updated.
     *
     * @return mixed
     */
    public function getUpdatesList($force = false)
    {
        static $result;

        if ($result) {
            return $result;
        }

        $result = [
            'core'    => [],
            'theme'   => [],
            'widget'  => [],
            'module'  => [],
        ];

        $setting = setting();

        if ($force || ($setting->api_checked_at && $setting->api_checked_at->diffInHours() >= 12)) {
            $this->updateVersionsFromApi();
        }

        $packages = Package::all();
        $componentsVersions = $this->getPackagesVersions();

        foreach ($packages as $package) {
            if (array_get($componentsVersions, $package->type.'.'.$package->namespace) == $package->api_version) {
                continue;
            }

            $result[$package->type][$package->namespace] = [
                'namespace' => $package->namespace,
                'date'      => $package->date,
                'version'   => $package->api_version,
            ];
        }

        return $result;
    }

    protected function updateVersionsFromApi()
    {
        $list = $this->getPackagesVersions();

        foreach ($list as $type => $packages) {
            foreach ($packages as $package => $version) {
                $this->setPackageType($type);
                $this->setPackageNamespace($package);

                $latest = $this->getLatest();

                $model = Package::firstOrNew([
                    'type'      => $type,
                    'namespace' => $package,
                ]);

                if ($latest == null) {
                    $model->delete();
                    continue;
                }


                $model->api_version = $latest->version;
                $model->date = Carbon::createFromFormat('F d Y H:i:s', $latest->date);

                $model->save();
            }
        }

        $setting = setting();

        $setting->api_checked_at = Carbon::now();
        $setting->save();
    }

    /**
     * Get all packages versions.
     *
     * @return mixed
     */
    public function getPackagesVersions()
    {
        $components = [
            'core'    => [],
            'theme'   => [],
            'widget'  => [],
            'module'  => [],
        ];

        $components['core']['ZEDx'] = Core::VERSION;

        foreach (Themes::all() as $theme) {
            $components['theme'][$theme['manifest']['name']] = $theme['manifest']['version'];
        }

        foreach (Widgets::noType()->noFilter()->all(null, true) as $namespace => $widget) {
            $components['widget'][$namespace] = $widget->get('version');
        }

        foreach (Modules::all() as $module) {
            $components['module'][$module->name] = $module->version;
{'fraction_non_alphanumeric': 0.1516270482344796, 'fraction_numerical': 0.00046157396722824835, 'mean_word_length': 1.8176202860858257, 'pattern_counts': {'":': 0, '<': 5, '<?xml version=': 0, '>': 110, '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': '3061061', 'n_tokens_mistral': 2439, 'n_tokens_neox': 2339, 'n_words': 457}