diff --git a/README.md b/README.md index 8575c73..96827c4 100644 --- a/README.md +++ b/README.md @@ -83,3 +83,29 @@ SilverStripe\ExternalLinks\Tasks\CheckExternalLinksTask: When upgrading from 1.x to 2.x (SilverStripe 3.x to 4.x) you will need to be aware of the following API changes: * Configuration property `CheckExternalLinksTask.IgnoreCodes` renamed to `CheckExternalLinksTask.ignore_codes` + * Configuration property `CheckExternalLinksTask.FollowLocation` and `BypassCache` renamed to `follow_location` and `bypass_cache` + +## Follow 301 redirects + +You may want to follow a redirected URL a example of this would be redirecting from http to https +can give you a false poitive as the http code of 301 will be returned which will be classed +as a working link. + +To allow redirects to be followed setup the following config in your config.yml + +```yaml +# Follow 301 redirects +SilverStripe\ExternalLinks\Tasks\CurlLinkChecker: + follow_location: 1 +``` + +## Bypass cache + +By default the task will attempt to cache any results the cache can be bypassed with the +following config in config.yml. + +```yaml +# Bypass SS_Cache +SilverStripe\ExternalLinks\Tasks\CurlLinkChecker:: + bypass_cache: 1 +``` diff --git a/src/Tasks/CurlLinkChecker.php b/src/Tasks/CurlLinkChecker.php index a09eb1e..79c82c5 100644 --- a/src/Tasks/CurlLinkChecker.php +++ b/src/Tasks/CurlLinkChecker.php @@ -3,6 +3,7 @@ namespace SilverStripe\ExternalLinks\Tasks; use Psr\SimpleCache\CacheInterface; +use SilverStripe\Core\Config\Configurable; use SilverStripe\Core\Injector\Injector; /** @@ -10,6 +11,25 @@ use SilverStripe\Core\Injector\Injector; */ class CurlLinkChecker implements LinkChecker { + use Configurable; + + /** + * If we want to follow redirects a 301 http code for example + * Set via YAML file + * + * @config + * @var boolean + */ + private static $follow_location = false; + + /** + * If we want to bypass the cache + * Set via YAML file + * + * @config + * @var boolean + */ + private static $bypass_cache = false; /** * Return cache @@ -34,6 +54,13 @@ class CurlLinkChecker implements LinkChecker return null; } + if (!$this->config()->get('bypass_cache')) { + // Check if we have a cached result + $cacheKey = md5($href); + $result = $this->getCache()->load($cacheKey); + if($result !== false) return $result; + } + // Check if we have a cached result $cacheKey = md5($href); $result = $this->getCache()->get($cacheKey, false); @@ -44,14 +71,19 @@ class CurlLinkChecker implements LinkChecker // No cached result so just request $handle = curl_init($href); curl_setopt($handle, CURLOPT_RETURNTRANSFER, true); + if ($this->config()->get('follow_location')) { + curl_setopt($handle, CURLOPT_FOLLOWLOCATION, TRUE); + } curl_setopt($handle, CURLOPT_CONNECTTIMEOUT, 5); curl_setopt($handle, CURLOPT_TIMEOUT, 10); curl_exec($handle); $httpCode = curl_getinfo($handle, CURLINFO_HTTP_CODE); curl_close($handle); - // Cache result - $this->getCache()->set($cacheKey, $httpCode); + if (!$this->config()->get('bypass_cache')) { + // Cache result + $this->getCache()->save($httpCode, $cacheKey); + } return $httpCode; } }