ScanController.php 1.99 KB
<?php

namespace app\commands;

use yii;
use yii\console\Controller;
use yii\db\Expression;
use yii\helpers\Console;
use app\components\Collection;
use app\models\Host;

/**
 * Парсинг запрещенных ресурсов в РФ.
 */
class ScanController extends Controller
{
    public $file = '';

    public function beforeAction($action)
    {
        if (!parent::beforeAction($action)) {
            return false;
        }
        return true;
    }

    public function options($actionID)
    {
        return ['file'];
    }

    public function optionAliases()
    {
        return ['f' => 'file'];
    }

    /**
     * Парсинг запрещенных ресурсов в РФ.
     */
    public function actionIndex()
    {
        $hosts = Collection::getHostsByCsv(Yii::$app->params['csv_url']);
        $csv_date = date("Y-m-d H:i:s",strtotime($hosts['csv_date']));
        foreach ($hosts['data'] as $host)
            {
                $host_utf8 = utf8_encode($host);
                $res = Yii::$app->db->createCommand();
                if ( Host::find()->where(['domain' =>$host_utf8])->count() )
                    $res->update(
                        '{{%host}}',
                        [
                            'csv_date' => $csv_date],
                            [
                                'domain' => $host_utf8
                            ]
                    );
                        else
                    $res->insert(
                        '{{%host}}',
                        [
                            'domain' =>$host_utf8,
                            'created_at' => new Expression('NOW()'),
                            'csv_date' => $csv_date,
                        ]
                    );
                $res->execute();
            }

        echo "\n".$this->ansiFormat('Количество полученных хостов', Console::FG_CYAN) . ' => ' . $this->ansiFormat(count($hosts['data']), Console::BOLD, Console::FG_GREEN) . "\n";
    }

}