1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Padosoft\Laravel\Google\StructuredDataTestingTool; |
4
|
|
|
|
5
|
|
|
use Config; |
6
|
|
|
use Illuminate\Console\Command; |
7
|
|
|
|
8
|
|
|
class GoogleStructuredDataTestTool extends Command |
9
|
|
|
{ |
10
|
|
|
/** |
11
|
|
|
* The name and signature of the console command. |
12
|
|
|
* |
13
|
|
|
* @var string |
14
|
|
|
*/ |
15
|
|
|
protected $signature = 'google-markup:test |
16
|
|
|
{path? : path where find url.txt, if you want to test url direct, you can use directly url that start with http} |
17
|
|
|
{--M|mail= : If you want send result to email} |
18
|
|
|
{--N|nomailok=false : True if you want send result to email only for alarm, false is default} |
19
|
|
|
{--w|whitelist= : If you want exclude from alarm some url, divide by ","} |
20
|
|
|
{verbosity=false : If you want more verbosity log} |
21
|
|
|
'; |
22
|
|
|
|
23
|
|
|
/** |
24
|
|
|
* The console command description. |
25
|
|
|
* |
26
|
|
|
* @var string |
27
|
|
|
*/ |
28
|
|
|
protected $description = <<<EOF |
29
|
|
|
The <info>google-markup:test</info> command looks in url.txt file in the given path |
30
|
|
|
and foreach url find in this file, check against google structured data testing tool API: |
31
|
|
|
<info>php artisan google-markup:test</info> |
32
|
|
|
If you omit path argument, command look into url defined in app config. |
33
|
|
|
You can also pass an url in the path as an argument: |
34
|
|
|
<info>php artisan google-markup:test https://www.padosoft.com</info> |
35
|
|
|
By default, the command displays the result in console, but you can also |
36
|
|
|
send an html email by using the <info>--mail</info> option: |
37
|
|
|
<info>php artisan /path/to/my/url.txt [email protected]</info> |
38
|
|
|
If you want to receive an email only for alarm (find markup errors) using the <info>--nomailok</info> option: |
39
|
|
|
<info>php artisan /path/to/my/url.txt [email protected] --nomailok=true</info> |
40
|
|
|
If you want to exclude some url from alert using the <info>--whitelist</info> option: |
41
|
|
|
<info>php artisan /path/to/my/url.txt --whitelist=https://www.padosoft.com,https://blog.padosoft.com</info> |
42
|
|
|
If you want more verbosity log append --verbosity=true |
43
|
|
|
EOF; |
44
|
|
|
|
45
|
|
|
/** |
46
|
|
|
* @var array |
47
|
|
|
*/ |
48
|
|
|
protected $headersTableConsole = ['name', 'type', 'errors', 'warnings', 'isOk']; |
49
|
|
|
|
50
|
|
|
/** |
51
|
|
|
* @var array |
52
|
|
|
*/ |
53
|
|
|
protected $tableEntities = []; |
54
|
|
|
|
55
|
|
|
/** |
56
|
|
|
* Create a new command instance. |
57
|
|
|
* |
58
|
|
|
*/ |
59
|
|
|
public function __construct() |
60
|
|
|
{ |
61
|
|
|
parent::__construct(); |
62
|
|
|
} |
63
|
|
|
|
64
|
|
|
/** |
65
|
|
|
* Execute the console command. |
66
|
|
|
* |
67
|
|
|
* @return mixed |
68
|
|
|
*/ |
69
|
|
|
public function handle() |
70
|
|
|
{ |
71
|
|
|
$this->hardWork($this->argument(), $this->option()); |
72
|
|
|
} |
73
|
|
|
|
74
|
|
|
/** |
75
|
|
|
* @param $argument |
76
|
|
|
* @param $option |
77
|
|
|
*/ |
78
|
|
|
private function hardWork($argument, $option) |
79
|
|
|
{ |
80
|
|
|
$path = $argument['path']; |
81
|
|
|
$this->line('path: <info>' . $path . '</info>'); |
82
|
|
|
$this->line('Check url/file...'); |
83
|
|
|
$urls = $this->findUrls($path, $this); |
84
|
|
|
|
85
|
|
|
$this->tableEntities = []; |
86
|
|
|
$tuttoOk = true; |
87
|
|
|
$numUrl = 0; |
88
|
|
|
|
89
|
|
|
$whitelist = FileHelper::adjustPath($option['whitelist']); |
90
|
|
|
|
91
|
|
|
foreach ($urls as $url) { |
92
|
|
|
|
93
|
|
|
$this->line("Analyzing <info>" . ($numUrl + 1) . "</info> of <info>" . count($urls) . "</info>"); |
94
|
|
|
|
95
|
|
|
if(!$this->checkStructuredData($url, $whitelist)){ |
96
|
|
|
$tuttoOk = false; |
97
|
|
|
} |
98
|
|
|
|
99
|
|
|
$numUrl++; |
100
|
|
|
//Add a sleep so google doesn't think we are spam. thanks to mwight4. |
101
|
|
|
sleep(random_int(30,90)); |
102
|
|
|
} |
103
|
|
|
|
104
|
|
|
$this->notifyResult($option['mail'], $option['nomailok'], $tuttoOk); |
105
|
|
|
|
106
|
|
|
} |
107
|
|
|
|
108
|
|
|
/** |
109
|
|
|
* @param $mail |
110
|
|
|
* @param $nomailok |
111
|
|
|
* @param boolean $tuttoOk |
112
|
|
|
*/ |
113
|
|
|
private function notifyResult($mail, $nomailok, $tuttoOk) |
114
|
|
|
{ |
115
|
|
|
//print to console |
116
|
|
|
$this->table($this->headersTableConsole, $this->tableEntities); |
117
|
|
|
|
118
|
|
|
//send email |
119
|
|
|
if (!$tuttoOk || $nomailok == '' || strtolower($nomailok) != 'true') { |
120
|
|
|
$this->sendEmail($mail, $tuttoOk); |
121
|
|
|
} |
122
|
|
|
|
123
|
|
|
$this->notify($tuttoOk); |
124
|
|
|
} |
125
|
|
|
|
126
|
|
|
/** |
127
|
|
|
* @param boolean $result |
128
|
|
|
*/ |
129
|
|
|
private function notify($result) |
130
|
|
|
{ |
131
|
|
|
if ($result) { |
132
|
|
|
$this->notifyOK(); |
133
|
|
|
return; |
134
|
|
|
} |
135
|
|
|
|
136
|
|
|
$this->notifyKO(); |
137
|
|
|
} |
138
|
|
|
|
139
|
|
|
/** |
140
|
|
|
* |
141
|
|
|
*/ |
142
|
|
|
private function notifyOK() |
143
|
|
|
{ |
144
|
|
|
$esito = Config::get('laravel-google-structured-data-testing-tool.mailSubjectSuccess'); |
145
|
|
|
$this->line('<info>'.$esito.'</info>'); |
146
|
|
|
$this->line(''); |
147
|
|
|
} |
148
|
|
|
|
149
|
|
|
/** |
150
|
|
|
* |
151
|
|
|
*/ |
152
|
|
|
private function notifyKO() |
153
|
|
|
{ |
154
|
|
|
$esito = Config::get('laravel-google-structured-data-testing-tool.mailSubjetcAlarm'); |
155
|
|
|
$this->error($esito); |
156
|
|
|
$this->line(''); |
157
|
|
|
} |
158
|
|
|
|
159
|
|
|
/** |
160
|
|
|
* @param $mail |
161
|
|
|
* @param boolean $tuttoOk |
162
|
|
|
*/ |
163
|
|
|
private function sendEmail($mail, $tuttoOk) |
164
|
|
|
{ |
165
|
|
|
if ($mail != '') { |
166
|
|
|
$email = new MailHelper($this); |
167
|
|
|
$email->sendEmail($tuttoOk, $mail, $this->tableEntities); |
168
|
|
|
} |
169
|
|
|
} |
170
|
|
|
|
171
|
|
|
/** |
172
|
|
|
* |
173
|
|
|
* @param $url |
174
|
|
|
* @return array with a valid url or empty array |
175
|
|
|
*/ |
176
|
|
|
private function getUrl($url) |
177
|
|
|
{ |
178
|
|
|
$urls = array(); |
179
|
|
|
|
180
|
|
|
if (isUrl($url)) { |
181
|
|
|
$urls[] = $url; |
182
|
|
|
} |
183
|
|
|
|
184
|
|
|
return $urls; |
185
|
|
|
} |
186
|
|
|
|
187
|
|
|
/** |
188
|
|
|
* |
189
|
|
|
* @param $path |
190
|
|
|
* @param \Illuminate\Console\Command $cmd |
191
|
|
|
* @return array of valid url or empty array |
192
|
|
|
*/ |
193
|
|
|
private function getUrlsByPath($path, \Illuminate\Console\Command $cmd) |
194
|
|
|
{ |
195
|
|
|
$urls = array(); |
196
|
|
|
|
197
|
|
|
if (!file_exists($path)) { |
198
|
|
|
return $urls; |
199
|
|
|
} |
200
|
|
|
|
201
|
|
|
$cmd->line('Find file ' . $path); |
202
|
|
|
|
203
|
|
|
$urls_tmp = explode("\n", file_get_contents($path)); |
204
|
|
|
$cmd->line('Found <info>' . count($urls_tmp). '</info> entries in file.'); |
205
|
|
|
|
206
|
|
|
return array_filter($urls_tmp, function ($url) use ($cmd) { |
207
|
|
|
if (isUrl(trim($url))) { |
208
|
|
|
return true; |
209
|
|
|
} else { |
210
|
|
|
$cmd->error('ERROR: url \'' . trim($url) . '\' is NOT a valid url!'); |
211
|
|
|
return false; |
212
|
|
|
} |
213
|
|
|
}); |
214
|
|
|
} |
215
|
|
|
|
216
|
|
|
/** |
217
|
|
|
* |
218
|
|
|
* @param $path |
219
|
|
|
* @param \Illuminate\Console\Command $cmd |
220
|
|
|
* @return array of valid url |
221
|
|
|
*/ |
222
|
|
|
private function findUrls($path, \Illuminate\Console\Command $cmd) |
223
|
|
|
{ |
224
|
|
|
$urls = $this->getUrl($path); |
225
|
|
|
if (count($urls) > 0) { |
226
|
|
|
$cmd->line('Find url: <comment>' . $path . '</comment>'); |
227
|
|
|
return $urls; |
228
|
|
|
} |
229
|
|
|
|
230
|
|
|
$urls = $this->getUrlsByPath($path, $cmd); |
231
|
|
|
if (count($urls) > 0) { |
232
|
|
|
$cmd->line('Find <info>' . count($urls) . '</info> valid url in ' . $path); |
233
|
|
|
return $urls; |
234
|
|
|
} |
235
|
|
|
|
236
|
|
|
$cmd->error('File url ' . $path . ' not found and not a valid url!'); |
237
|
|
|
return $urls; |
238
|
|
|
} |
239
|
|
|
|
240
|
|
|
/** |
241
|
|
|
* @param $url |
242
|
|
|
* @param $whitelist |
243
|
|
|
* @return bool |
244
|
|
|
*/ |
245
|
|
|
private function checkStructuredData($url, $whitelist) |
246
|
|
|
{ |
247
|
|
|
$this->line("Analizing: $url ..."); |
248
|
|
|
|
249
|
|
|
$gtest = new GoogleMarkupHelper($this, $whitelist); |
250
|
|
|
$response = $gtest->checkUrl($url); |
251
|
|
|
|
252
|
|
|
if (($response === null) || !is_array($response)) { |
253
|
|
|
$this->error("Error! Response not vaild or null."); |
254
|
|
|
$this->line("Response:"); |
255
|
|
|
$this->line(get_var_dump_output($response)); |
256
|
|
|
return false; |
257
|
|
|
} |
258
|
|
|
if (count($response) == 0) { |
259
|
|
|
return false; |
260
|
|
|
} |
261
|
|
|
|
262
|
|
|
$tuttoOk = $gtest->checkResponse($response); |
263
|
|
|
|
264
|
|
|
$this->tableEntities = array_merge($this->tableEntities, $gtest->tableEntities); |
265
|
|
|
|
266
|
|
|
return $tuttoOk; |
267
|
|
|
} |
268
|
|
|
|
269
|
|
|
} |
270
|
|
|
|
271
|
|
|
|