This repository has been archived by the owner on Jun 30, 2020. It is now read-only.
forked from bopoda/robots-txt-parser
-
Notifications
You must be signed in to change notification settings - Fork 0
/
RobotsTxtParser.php
244 lines (206 loc) · 5.46 KB
/
RobotsTxtParser.php
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
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
<?php
/**
* Class for parsing robots.txt files
*
* @author Eugene Yurkevich (bopodaa@gmail.com)
*
* Useful links and materials about robots.txt crawling
* @link https://developers.google.com/webmasters/control-crawl-index/docs/robots_txt
* @link https://help.yandex.com/webmaster/controlling-robot/robots-txt.xml
*/
class RobotsTxtParser
{
// default encoding
const DEFAULT_ENCODING = 'UTF-8';
// directives
const DIRECTIVE_NOINDEX = 'noindex';
const DIRECTIVE_ALLOW = 'allow';
const DIRECTIVE_DISALLOW = 'disallow';
const DIRECTIVE_HOST = 'host';
const DIRECTIVE_SITEMAP = 'sitemap';
const DIRECTIVE_USERAGENT = 'user-agent';
const DIRECTIVE_CRAWL_DELAY = 'crawl-delay';
const DIRECTIVE_CLEAN_PARAM = 'clean-param';
//default user-agent
const USER_AGENT_ALL = '*';
/**
* @var string $content Original robots.txt content
*/
private $content = '';
/**
* @var array $rules Rules with all parsed directives by all user-agents
*/
private $rules = [];
/**
* @var string $currentDirective Current directive
*/
private $currentDirective;
/**
* @var string $userAgent Current user-agent
*/
private $userAgent;
/**
* @param string $content Robots.txt content
* @param string $encoding Encoding
* @return RobotsTxtParser
*/
public function __construct($content, $encoding = self::DEFAULT_ENCODING)
{
// convert encoding
$encoding = !empty($encoding) ? $encoding : mb_detect_encoding($content, mb_detect_order(), false);
if ($encoding == "UTF-8") {
$content = mb_convert_encoding($content, 'UTF-8', 'UTF-8');
}
// set content
$this->content = iconv(mb_detect_encoding($content, mb_detect_order(), false), "UTF-8//IGNORE", $content);
$this->prepareRules();
}
/**
* Get rules by specific bot (user-agent)
* Use $userAgent = NULL to get all rules for all user-agents grouped by user-agent. User-agents will return in lower case.
* Use $userAgent = '*' to get common rules.
* Use $userAgent = 'YandexBot' to get rules for user-agent 'YandexBot'.
*
* @param string $userAgent
* @return array
*/
public function getRules($userAgent = NULL)
{
if (is_null($userAgent)) {
//return all rules
return $this->rules;
}
else {
$userAgent = mb_strtolower($userAgent);
if (isset($this->rules[$userAgent])) {
return $this->rules[$userAgent];
}
else {
return [];
}
}
}
/**
* Get sitemaps links.
* Sitemap always relates to all user-agents and return in rules with user-agent "*"
*
* @return array
*/
public function getSitemaps()
{
$rules = $this->getRules(self::USER_AGENT_ALL);
if (!empty($rules[self::DIRECTIVE_SITEMAP])) {
return $rules[self::DIRECTIVE_SITEMAP];
}
return [];
}
/**
* Return original robots.txt content
*
* @return string
*/
public function getContent()
{
return $this->content;
}
/**
* Return array of supported directives
*
* @return array
*/
private function getAllowedDirectives()
{
return [
self::DIRECTIVE_NOINDEX,
self::DIRECTIVE_ALLOW,
self::DIRECTIVE_DISALLOW,
self::DIRECTIVE_HOST,
self::DIRECTIVE_SITEMAP,
self::DIRECTIVE_USERAGENT,
self::DIRECTIVE_CRAWL_DELAY,
self::DIRECTIVE_CLEAN_PARAM,
];
}
/**
* Parse rules
*
* @return void
*/
private function prepareRules()
{
$rows = explode(PHP_EOL, $this->content);
foreach ($rows as $row) {
$row = preg_replace('/#.*/', '', $row);
$parts = explode(':', $row, 2);
if (count($parts) < 2) {
continue;
}
$directive = trim(strtolower($parts[0]));
$value = trim($parts[1]);
$this->handleDirective($directive, $value);
}
$this->removeDublicates();
}
private function removeDublicates()
{
foreach ($this->rules as $userAgent => $rules) {
foreach ($this->rules[$userAgent] as $directive => $value) {
if (is_array($this->rules[$userAgent][$directive])) {
$this->rules[$userAgent][$directive] = array_values(array_unique($this->rules[$userAgent][$directive]));
}
}
}
}
/**
* Handle directive with value
* Assign value to directive
*
* @param string $directive
* @param string $value
*/
private function handleDirective($directive, $value)
{
if (!in_array($directive, $this->getAllowedDirectives())) {
return;
}
switch ($directive) {
case self::DIRECTIVE_USERAGENT:
$this->currentDirective = $directive;
$this->userAgent = strtolower($value);
if (!isset($this->rules[$this->userAgent])) {
$this->rules[$this->userAgent] = [];
}
break;
case self::DIRECTIVE_DISALLOW:
$this->currentDirective = $directive;
if ($this->userAgent && $value) {
$this->rules[$this->userAgent][self::DIRECTIVE_DISALLOW][] = $value;
}
break;
case self::DIRECTIVE_CRAWL_DELAY:
$this->currentDirective = $directive;
if ($this->userAgent && $value) {
$this->rules[$this->userAgent][self::DIRECTIVE_CRAWL_DELAY] = (double) $value;
}
break;
case self::DIRECTIVE_SITEMAP:
$this->currentDirective = $directive;
if ($value) {
$this->rules[self::USER_AGENT_ALL][self::DIRECTIVE_SITEMAP][] = $value;
}
break;
case self::DIRECTIVE_HOST:
$this->currentDirective = $directive;
if ($value && empty($this->rules[self::USER_AGENT_ALL][self::DIRECTIVE_HOST])) {
$this->rules[self::USER_AGENT_ALL][self::DIRECTIVE_HOST] = $value;
}
break;
default:
$this->currentDirective = $directive;
if (!empty($this->userAgent)) {
$this->rules[$this->userAgent][$this->currentDirective][] = $value;
}
break;
}
}
}