Skip to content

Commit

Permalink
Implement content checker
Browse files Browse the repository at this point in the history
  • Loading branch information
nicolashohm committed Feb 14, 2016
1 parent b11ec90 commit 552ac8e
Show file tree
Hide file tree
Showing 5 changed files with 97 additions and 25 deletions.
2 changes: 1 addition & 1 deletion composer.json
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
{
"name": "nickel715/websiteMonitoring",
"name": "nickel715/website-monitoring",
"description": "",
"license": "MIT",
"authors": [
Expand Down
14 changes: 8 additions & 6 deletions config.php.dist
Original file line number Diff line number Diff line change
Expand Up @@ -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' => ['<h1>', '<h2>'],
'text_not_present' => 'debug',
],
],
],
Expand Down
2 changes: 2 additions & 0 deletions console
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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,
],
]);
});
Expand Down
86 changes: 86 additions & 0 deletions src/Checker/Content.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,86 @@
<?php

namespace WebsiteMonitoring\Checker;

use WebsiteMonitoring\WebsiteConfig;
use Zend\Http\Client;

class Content implements CheckerInterface
{
public function check(WebsiteConfig $config, array $checkerConfig = [])
{
$failures = [];
foreach ($checkerConfig as $page) {
if (empty($page['page'])) {
$failures[] = 'Config error, no page defined';
continue;
}
$link = $config->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;
}
}
}
18 changes: 0 additions & 18 deletions src/Checker/TextOrHtmlPresent.php

This file was deleted.

0 comments on commit 552ac8e

Please sign in to comment.