From 552ac8efc13da3d02dfdd2f095a5ddc040eb299c Mon Sep 17 00:00:00 2001 From: Nicolas Hohm Date: Sun, 14 Feb 2016 12:38:56 +0100 Subject: [PATCH] Implement content checker --- composer.json | 2 +- config.php.dist | 14 ++--- console | 2 + src/Checker/Content.php | 86 +++++++++++++++++++++++++++++++ src/Checker/TextOrHtmlPresent.php | 18 ------- 5 files changed, 97 insertions(+), 25 deletions(-) create mode 100644 src/Checker/Content.php delete mode 100644 src/Checker/TextOrHtmlPresent.php diff --git a/composer.json b/composer.json index bd90989..1c71498 100644 --- a/composer.json +++ b/composer.json @@ -1,5 +1,5 @@ { - "name": "nickel715/websiteMonitoring", + "name": "nickel715/website-monitoring", "description": "", "license": "MIT", "authors": [ diff --git a/config.php.dist b/config.php.dist index 3ebf2cf..b791d78 100644 --- a/config.php.dist +++ b/config.php.dist @@ -10,20 +10,22 @@ return [ [ 'url' => '', 'checker' => [ - 'page-exists' => [ + 'page-exists' => [ 'pages' => $privatePages, ], - 'robots-txt' => [ + 'robots-txt' => [ 'disallows' => $privatePages, ], - 'sitemap-blacklist' => [ + 'sitemap-blacklist' => [ 'path' => 'sitemap.xml', 'blacklist' => $privatePages, ], - 'text-or-html-present' => [ + 'content' => [ [ - 'page' => '/', - 'text' => 'copyright', + 'page' => '/', + 'text_present' => 'copyright', + 'html_present' => ['

', '

'], + 'text_not_present' => 'debug', ], ], ], diff --git a/console b/console index d97240c..c1ebc5b 100755 --- a/console +++ b/console @@ -4,6 +4,7 @@ require_once 'vendor/autoload.php'; use Interop\Container\ContainerInterface; +use WebsiteMonitoring\Checker\Content; use WebsiteMonitoring\Checker\PageExists; use WebsiteMonitoring\Checker\RobotsTxt; use WebsiteMonitoring\Checker\SitemapBlacklist; @@ -20,6 +21,7 @@ $serviceManager->setFactory(CheckerPluginManager::class, function (ContainerInte 'page-exists' => PageExists::class, 'robots-txt' => RobotsTxt::class, 'sitemap-blacklist' => SitemapBlacklist::class, + 'content' => Content::class, ], ]); }); diff --git a/src/Checker/Content.php b/src/Checker/Content.php new file mode 100644 index 0000000..fa99aa8 --- /dev/null +++ b/src/Checker/Content.php @@ -0,0 +1,86 @@ +getUrl() . $page['page']; + $response = (new Client($link))->send(); + if (!$response->isOk()) { + $failures = array_merge($failures, [ + 'Can not request the sitemap at ' . $link . '.', + 'The following error occurs: ' . $response->getStatusCode() . ' ' . $response->getReasonPhrase(), + ]); + } + $failures = array_merge($failures, $this->checkPage($response->getBody(), $page)); + } + return $failures; + } + + private function checkPage($html, array $pageConfig) + { + $failures = []; + $text = strip_tags(html_entity_decode($html)); + if (!empty($pageConfig['text_present'])) { + foreach ((array)$pageConfig['text_present'] as $item) { + strpos($text, $item) === false + && $failures[] = 'Can not find ' . $item . ' on page ' . $pageConfig['page']; + } + } + if (!empty($pageConfig['text_not_present'])) { + foreach ((array)$pageConfig['text_not_present'] as $item) { + strpos($text, $item) !== false + && $failures[] = 'Found ' . $item . ' on page ' . $pageConfig['page']; + } + } + if (!empty($pageConfig['html_present'])) { + foreach ((array)$pageConfig['html_present'] as $item) { + strpos($html, $item) === false + && $failures[] = 'Can not find ' . $item . ' on page ' . $pageConfig['page']; + } + } + if (!empty($pageConfig['html_not_present'])) { + foreach ((array)$pageConfig['html_not_present'] as $item) { + strpos($html, $item) !== false + && $failures[] = 'Found ' . $item . ' on page ' . $pageConfig['page']; + } + } + return $failures; + } + + public function parse(WebsiteConfig $config, array $checkerConfig = []) + { + $result = []; + + foreach ($checkerConfig as $page) { + if (empty($page['page'])) { + return 'Config error, no page defined'; + } + $result[] = 'Check page ' . $page['page']; + if (!empty($page['text_present'])) { + $result[] = 'For following text(s) are present: ' . implode(', ', (array)$page['text_present']); + } + if (!empty($page['text_not_present'])) { + $result[] = 'For following text(s) are not present: ' . implode(', ', (array)$page['text_not_present']); + } + if (!empty($page['html_present'])) { + $result[] = 'For following html are present: ' . implode(', ', (array)$page['html_present']); + } + if (!empty($page['html_not_present'])) { + $result[] = 'For following html are not present: ' . implode(', ', (array)$page['html_not_present']); + } + return $result; + } + } +} diff --git a/src/Checker/TextOrHtmlPresent.php b/src/Checker/TextOrHtmlPresent.php deleted file mode 100644 index b0ca6b8..0000000 --- a/src/Checker/TextOrHtmlPresent.php +++ /dev/null @@ -1,18 +0,0 @@ -