From be6ce076022949dad0b61c81f5a10287a23cb5c5 Mon Sep 17 00:00:00 2001 From: randytate Date: Sat, 28 Feb 2015 23:08:43 -0700 Subject: [PATCH] Add counter of 30x redirects. --- URLResolver.php | 20 +++++++++++++++++++- 1 file changed, 19 insertions(+), 1 deletion(-) diff --git a/URLResolver.php b/URLResolver.php index e828bfb..02d9730 100644 --- a/URLResolver.php +++ b/URLResolver.php @@ -109,6 +109,8 @@ public function resolveURL($url) { $url_is_open_graph = false; $url_is_canonical = false; + $url_includes_redirect = false; + $url_redirect_30x_count = 0; $url_results = array(); for ($i = 0; $i < $this->max_redirects; $i++) { @@ -128,6 +130,9 @@ public function resolveURL($url) { # Don't allow it to overwrite a true value determined from markup with a false value... if (!$url_result->isOpenGraphURL()) { $url_result->isOpenGraphURL($url_is_open_graph); } if (!$url_result->isCanonicalURL()) { $url_result->isCanonicalURL($url_is_canonical); } + + # Propogate the 30x count + $url_result->redirect30xCount($url_redirect_30x_count); # Also print a short status line regarding the URL once it is fetched if ($this->is_debug) { @@ -188,6 +193,7 @@ public function resolveURL($url) { $url = $next_url; $url_is_open_graph = $url_result->redirectTargetIsOpenGraphURL(); $url_is_canonical = $url_result->redirectTargetIsCanonicalURL(); + $url_redirect_30x_count = $url_result->redirect30xCount(); } return $this->resolveURLResults($url_results); @@ -560,6 +566,8 @@ class URLResolverResult { private $redirect_is_open_graph = false; private $redirect_is_canonical = false; + private $redirect_30x_count = 0; + private $failed = false; private $error = false; private $error_message = ''; @@ -580,7 +588,11 @@ public function setHTTPStatusCode($status) { $this->status = $status; } public function hasSuccessHTTPStatus() { return ($this->status == 200); } # Returns _true_ if the [HTTP status code] for the resolved URL is 301 or 302. - public function hasRedirectHTTPStatus() { return ($this->status == 301 || $this->status == 302 || $this->status == 303); } + public function hasRedirectHTTPStatus() { + $isRedirect = ($this->status == 301 || $this->status == 302 || $this->status == 303); + $this->redirect30xCount( $isRedirect ); + return $isRedirect; + } # Returns the value of the Content-Type [HTTP header] for the resolved URL. # If header not provided, _null_ is returned. Examples: text/html, image/jpeg, ... @@ -614,6 +626,12 @@ public function isStartingURL($value=null) { return $this->is_starting_point; } + # Returns number of 30x redirects we've been through + public function redirect30xCount($value=null) { + if (isset($value)) { $this->redirect_30x_count += $value; } + return $this->redirect_30x_count; + } + # Returns true if an error occurred while resolving the URL. # If this returns false, $url_result is guaranteed to have a status code. public function didErrorOccur() {