|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
namespace whm\Smoke\Rules\Xml\Sitemap; |
|
4
|
|
|
|
|
5
|
|
|
use phm\HttpWebdriverClient\Http\Response\DomAwareResponse; |
|
6
|
|
|
use Psr\Http\Message\ResponseInterface; |
|
7
|
|
|
use whm\Smoke\Rules\StandardRule; |
|
8
|
|
|
use whm\Smoke\Rules\ValidationFailedException; |
|
9
|
|
|
|
|
10
|
|
|
/** |
|
11
|
|
|
* This rule checks if a sitemap.xml file is valid. |
|
12
|
|
|
*/ |
|
13
|
|
|
class ValidRule extends StandardRule |
|
14
|
|
|
{ |
|
15
|
|
|
const SCHEMA = 'schema.xsd'; |
|
16
|
|
|
const NON_STRICT_SCHEMA = 'nonStrictSchema.xsd'; |
|
17
|
|
|
const INDEX = 'siteindex.xsd'; |
|
18
|
|
|
|
|
19
|
|
|
private $strictMode; |
|
20
|
|
|
private $debug; |
|
21
|
|
|
|
|
22
|
|
|
// protected $contentTypes = array('text/xml', 'application/xml'); |
|
23
|
|
|
|
|
24
|
|
|
public function init($strictMode = true, $debug = false) |
|
25
|
|
|
{ |
|
26
|
|
|
$this->debug = $debug; |
|
27
|
|
|
$this->strictMode = $strictMode; |
|
28
|
|
|
} |
|
29
|
|
|
|
|
30
|
|
|
private function getSchema($isIndex) |
|
31
|
|
|
{ |
|
32
|
|
|
if ($isIndex) { |
|
33
|
|
|
return __DIR__ . '/' . self::INDEX; |
|
34
|
|
|
} |
|
35
|
|
|
|
|
36
|
|
|
if ($this->strictMode) { |
|
37
|
|
|
return __DIR__ . '/' . self::SCHEMA; |
|
38
|
|
|
} else { |
|
39
|
|
|
return __DIR__ . '/' . self::NON_STRICT_SCHEMA; |
|
40
|
|
|
} |
|
41
|
|
|
} |
|
42
|
|
|
|
|
43
|
|
|
/** |
|
44
|
|
|
* @param $body |
|
45
|
|
|
* @param $filename |
|
46
|
|
|
* @param bool $isIndex |
|
47
|
|
|
* @throws ValidationFailedException |
|
48
|
|
|
*/ |
|
49
|
|
|
private function validateBody($body, $filename, $isIndex = true) |
|
50
|
|
|
{ |
|
51
|
|
|
$dom = new \DOMDocument(); |
|
52
|
|
|
@$dom->loadXML($body); |
|
|
|
|
|
|
53
|
|
|
|
|
54
|
|
|
$valid = @$dom->schemaValidate($this->getSchema($isIndex)); |
|
55
|
|
|
|
|
56
|
|
|
if (!$valid) { |
|
57
|
|
|
$lastError = libxml_get_last_error(); |
|
58
|
|
|
throw new ValidationFailedException( |
|
59
|
|
|
'The given sitemap file (' . $filename . ') did not validate against the sitemap schema (last error: ' . str_replace("\n", '', $lastError->message) . ').'); |
|
60
|
|
|
} |
|
61
|
|
|
} |
|
62
|
|
|
|
|
63
|
|
|
/** |
|
64
|
|
|
* @param ResponseInterface $response |
|
65
|
|
|
* @throws ValidationFailedException |
|
66
|
|
|
*/ |
|
67
|
|
|
protected function doValidation(ResponseInterface $response) |
|
68
|
|
|
{ |
|
69
|
|
|
$contentType = $response->getHeader('content-type'); |
|
70
|
|
|
|
|
71
|
|
|
if ($response instanceof DomAwareResponse) { |
|
72
|
|
|
$body = (string)$response->getHtmlBody(); |
|
73
|
|
|
} else { |
|
74
|
|
|
$body = (string)$response->getBody(); |
|
75
|
|
|
} |
|
76
|
|
|
|
|
77
|
|
|
if (is_array($contentType) && $contentType[0] === "application/gzip") { |
|
78
|
|
|
$body = gzdecode($response->getBody()); |
|
79
|
|
|
} |
|
80
|
|
|
|
|
81
|
|
|
// sitemapindex or urlset |
|
82
|
|
|
if (preg_match('/<sitemapindex/', $body)) { |
|
83
|
|
|
$this->validateBody($body, (string)$response->getUri()); |
|
|
|
|
|
|
84
|
|
|
} elseif (preg_match('/<urlset/', $body)) { |
|
85
|
|
|
$this->validateBody($body, (string)$response->getUri(), false); |
|
|
|
|
|
|
86
|
|
|
} else { |
|
87
|
|
|
throw new ValidationFailedException('The given document is not a valid sitemap. Nether sitemapindex nor urlset element was found. '); |
|
88
|
|
|
} |
|
89
|
|
|
} |
|
90
|
|
|
} |
|
91
|
|
|
|
If you suppress an error, we recommend checking for the error condition explicitly: