ScanController.php
6.82 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
<?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;
use yii\db\Query;
/**
* Парсинг запрещенных ресурсов в РФ.
*/
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()
{
$rec = ['insert'=>0, 'update'=>0];
$csv_row = 0;
$csv_skip_row = 0;
echo PHP_EOL.$this->ansiFormat('Download file: '.Yii::$app->params['csv_url'], Console::FG_CYAN);
$csv_file = Collection::download_file(Yii::$app->params['csv_url']);
if ($csv_file!=='')
{
echo PHP_EOL.$this->ansiFormat('Start parse file: '.$csv_file, Console::FG_CYAN);
$csv_handle = fopen($csv_file ,'r');
if ($csv_handle !== false)
{
$csv_datatime = '';
while (($csv_data = fgetcsv($csv_handle, 1024, ';')) !== false)
{
$csv_row++;
if ($csv_row===1)
$csv_datatime = date(
"Y-m-d H:i:s",
strtotime(
str_replace('Updated: ', '', $csv_data[0])
)
);
elseif (isset($csv_data[1])===true)
{
$host = str_replace(
'*.',
'',
utf8_encode($csv_data[1])
);
$host_level = count( explode('.', $host) );
if ($host_level === 2)
{
$db_request = Yii::$app->db->createCommand();
if ($db_request)
{
$host_bd = (new Query())
->select('`csv_date`')
->from('{{%host}}')
->where('domain=:host', array(':host'=>$host))
->limit(1)
->one();
if ( $host_bd === false )
{
$db_request->insert(
'{{%host}}',
[
'domain' =>$host,
'created_at' => new Expression('NOW()'),
'csv_date' => $csv_datatime,
]
)->execute();
$rec['insert']++;
}
elseif (isset($host_bd['csv_date']) && $host_bd['csv_date'] !== $csv_datatime)
{
$db_request->update(
'{{%host}}',
[
'csv_date' => $csv_datatime
],
[
'domain' => $host
]
)->execute();
$rec['update']++;
}
unset($host_bd);
}
else
echo PHP_EOL.$this->ansiFormat('! Error create DB request');
unset($db_request);
}
else
$csv_skip_row++;
unset($host_level);
unset($host);
}
else
$csv_skip_row++;
}
unset($csv_data);
unset($csv_datatime);
fclose($csv_handle);
unlink($csv_file);
}
else
echo PHP_EOL.$this->ansiFormat('! Not file open for read');
unset($csv_handle);
}
else
echo PHP_EOL.$this->ansiFormat('! Not file download');
unset($csv_file);
echo PHP_EOL.$this->ansiFormat('Count of rows processed: ', Console::FG_CYAN). $this->ansiFormat($csv_row, Console::BOLD, Console::FG_GREEN). ' Skiped rows: '.$this->ansiFormat($csv_skip_row, Console::BOLD, Console::FG_RED).' (Insert: '.$this->ansiFormat($rec['insert'], Console::BOLD, Console::FG_BLUE).' Update:'.$this->ansiFormat($rec['update'], Console::BOLD, Console::FG_BLUE).')';
echo PHP_EOL;
unset($rec);
unset($csv_skip_row);
unset($csv_row);
}
}