<commit_before><?php

namespace whm\Smoke\Rules\Xml\Sitemap;

use phm\HttpWebdriverClient\Http\Response\DomAwareResponse;
use Psr\Http\Message\ResponseInterface;
use whm\Smoke\Rules\StandardRule;
use whm\Smoke\Rules\ValidationFailedException;

/**
 * This rule checks if a sitemap.xml file is valid.
 */
class ValidRule extends StandardRule
{
    const SCHEMA = 'schema.xsd';
    const NON_STRICT_SCHEMA = 'nonStrictSchema.xsd';
    const INDEX = 'siteindex.xsd';

    private $strictMode;
    private $debug;

    private $gzipContentTypes = [
        'application/x-gzip',
        'application/gzip'
    ];

    // protected $contentTypes = array('text/xml', 'application/xml');

    public function init($strictMode = true, $debug = false)
    {
        $this->debug = $debug;
        $this->strictMode = $strictMode;
    }

    private function getSchema($isIndex)
    {
        if ($isIndex) {
            return __DIR__ . '/' . self::INDEX;
        }

        if ($this->strictMode) {
            return __DIR__ . '/' . self::SCHEMA;
        } else {
            return __DIR__ . '/' . self::NON_STRICT_SCHEMA;
        }
    }

    /**
     * @param $body
     * @param $filename
     * @param bool $isIndex
     * @throws ValidationFailedException
     */
    private function validateBody($body, $filename, $isIndex = true)
    {
        $dom = new \DOMDocument();
        @$dom->loadXML($body);

        $valid = @$dom->schemaValidate($this->getSchema($isIndex));

        if (!$valid) {
            $lastError = libxml_get_last_error();
            throw new ValidationFailedException(
                'The given sitemap file (' . $filename . ') did not validate against the sitemap schema (last error: ' . str_replace("\n", '', $lastError->message) . ').');
        }
    }

    /**
     * @param ResponseInterface $response
     * @throws ValidationFailedException
     */
    protected function doValidation(ResponseInterface $response)
    {
        $contentType = $response->getHeader('content-type');

        if ($response instanceof DomAwareResponse) {
            $body = (string)$response->getHtmlBody();
        } else {
            $body = (string)$response->getBody();
        }

        if (is_array($contentType) && in_array(strtolower($contentType[0]), $this->gzipContentTypes)) {
            $body = gzdecode($response->getBody());
        }

        // sitemapindex or urlset
        if (preg_match('/<sitemapindex/', $body)) {
            $this->validateBody($body, (string)$response->getUri());
        } elseif (preg_match('/<urlset/', $body)) {
            $this->validateBody($body, (string)$response->getUri(), false);
        } else {
            throw new ValidationFailedException('The given document is not a valid sitemap. Nether sitemapindex nor urlset element was found. ');
        }
    }
}<commit_msg>-> CheckResult<commit_after><?php

namespace whm\Smoke\Rules\Xml\Sitemap;

use phm\HttpWebdriverClient\Http\Response\DomAwareResponse;
use Psr\Http\Message\ResponseInterface;
use whm\Smoke\Rules\CheckResult;
use whm\Smoke\Rules\StandardRule;
use whm\Smoke\Rules\ValidationFailedException;

/**
 * This rule checks if a sitemap.xml file is valid.
 */
class ValidRule extends StandardRule
{
    const SCHEMA = 'schema.xsd';
    const NON_STRICT_SCHEMA = 'nonStrictSchema.xsd';
    const INDEX = 'siteindex.xsd';

    private $strictMode;
    private $debug;

    private $gzipContentTypes = [
        'application/x-gzip',
        'application/gzip'
    ];

    // protected $contentTypes = array('text/xml', 'application/xml');

    public function init($strictMode = true, $debug = false)
    {
        $this->debug = $debug;
        $this->strictMode = $strictMode;
    }

    private function getSchema($isIndex)
    {
        if ($isIndex) {
            return __DIR__ . '/' . self::INDEX;
        }

        if ($this->strictMode) {
            return __DIR__ . '/' . self::SCHEMA;
        } else {
            return __DIR__ . '/' . self::NON_STRICT_SCHEMA;
        }
    }

    /**
     * @param $body
     * @param $filename
     * @param bool $isIndex
     * @throws ValidationFailedException
     */
    private function validateBody($body, $filename, $isIndex = true)
    {
        $dom = new \DOMDocument();
        @$dom->loadXML($body);

        $valid = @$dom->schemaValidate($this->getSchema($isIndex));

        if (!$valid) {
            $lastError = libxml_get_last_error();
            $message = 'The given sitemap file (' . $filename . ') did not validate against the sitemap schema (last error: ' . str_replace("\n", '', $lastError->message) . ').';
            return new CheckResult(CheckResult::STATUS_FAILURE, $message);
        } else {
            $message = 'The given sitemap file (' . $filename . ') is valid.';
            return new CheckResult(CheckResult::STATUS_FAILURE, $message);

        }
    }

    /**
     * @param ResponseInterface $response
     * @throws ValidationFailedException
     */
    protected function doValidation(ResponseInterface $response)
    {
        $contentType = $response->getHeader('content-type');

        if ($response instanceof DomAwareResponse) {
            $body = (string)$response->getHtmlBody();
        } else {
            $body = (string)$response->getBody();
        }

        if (is_array($contentType) && in_array(strtolower($contentType[0]), $this->gzipContentTypes)) {
            $body = gzdecode($response->getBody());
        }

        // sitemapindex or urlset
        if (preg_match('/<sitemapindex/', $body)) {
            return $this->validateBody($body, (string)$response->getUri(), true);
        } elseif (preg_match('/<urlset/', $body)) {
            return $this->validateBody($body, (string)$response->getUri(), false);
        } else {
            throw new ValidationFailedException('The given document is not a valid sitemap. Nether sitemapindex nor urlset element was found. ');
        }
    }
}
{'fraction_non_alphanumeric': 0.13920645595158038, 'fraction_numerical': 0.0003362474781439139, 'mean_word_length': 2.458720930232558, 'pattern_counts': {'":': 0, '<': 9, '<?xml version=': 0, '>': 36, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 8}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '2630450', 'n_tokens_mistral': 1777, 'n_tokens_neox': 1742, 'n_words': 369}