diff options
author | Thomas Steur <tsteur@users.noreply.github.com> | 2016-10-30 23:37:58 +0300 |
---|---|---|
committer | GitHub <noreply@github.com> | 2016-10-30 23:37:58 +0300 |
commit | 3018d078a9b302abbf78e2c8129b66979fc5bb21 (patch) | |
tree | fa931f63c779fe7ba1a3a2d996537f4102a111e4 | |
parent | 570bc14616884b40025277e63cabc424be480d29 (diff) | |
parent | 77c213a61bc43dd93b5661929586b56e8765382c (diff) |
Merge pull request #10393 from pebosi/fix-seo-metrics
Update SEO Metrics
-rw-r--r-- | plugins/SEO/Metric/Bing.php | 6 | ||||
-rw-r--r-- | plugins/SEO/Metric/Dmoz.php | 2 | ||||
-rw-r--r-- | plugins/SEO/Metric/DomainAge.php | 15 | ||||
-rw-r--r-- | plugins/SEO/Metric/Google.php | 111 |
4 files changed, 12 insertions, 122 deletions
diff --git a/plugins/SEO/Metric/Bing.php b/plugins/SEO/Metric/Bing.php index 71553ffc6e..b0805548d4 100644 --- a/plugins/SEO/Metric/Bing.php +++ b/plugins/SEO/Metric/Bing.php @@ -18,7 +18,7 @@ use Psr\Log\LoggerInterface; */ class Bing implements MetricsProvider { - const URL = 'http://www.bing.com/search?mkt=en-US&q=site%3A'; + const URL = 'http://www.bing.com/search?setlang=en-US&rdr=1&q=site%3A'; /** * @var LoggerInterface @@ -37,8 +37,8 @@ class Bing implements MetricsProvider try { $response = str_replace(' ', ' ', Http::sendHttpRequest($url, $timeout = 10, @$_SERVER['HTTP_USER_AGENT'])); - if (preg_match('#([0-9\,]+) results#i', $response, $p)) { - $pageCount = NumberFormatter::getInstance()->formatNumber((int)str_replace(',', '', $p[1])); + if (preg_match('#([0-9,\.]+) results#i', $response, $p)) { + $pageCount = NumberFormatter::getInstance()->formatNumber((int)str_replace(array(',', '.'), '', $p[1])); } else { $pageCount = 0; } diff --git a/plugins/SEO/Metric/Dmoz.php b/plugins/SEO/Metric/Dmoz.php index c7b8860a89..05e8e1a0e9 100644 --- a/plugins/SEO/Metric/Dmoz.php +++ b/plugins/SEO/Metric/Dmoz.php @@ -38,7 +38,7 @@ class Dmoz implements MetricsProvider try { $response = Http::sendHttpRequest(self::URL . urlencode($domain), $timeout = 10, @$_SERVER['HTTP_USER_AGENT']); - preg_match('#DMOZ Sites[^\(]+\([0-9]-[0-9]+ of ([0-9]+)\)#', $response, $p); + preg_match('#[0-9] - [0-9]+ of ([0-9]+)#', $response, $p); if (!empty($p[1])) { $value = NumberFormatter::getInstance()->formatNumber((int)$p[1]); } else { diff --git a/plugins/SEO/Metric/DomainAge.php b/plugins/SEO/Metric/DomainAge.php index a059b49572..836011b12e 100644 --- a/plugins/SEO/Metric/DomainAge.php +++ b/plugins/SEO/Metric/DomainAge.php @@ -35,6 +35,8 @@ class DomainAge implements MetricsProvider public function getMetrics($domain) { + $domain = str_replace('www.', '', $domain); + $ages = array(); $age = $this->getAgeArchiveOrg($domain); @@ -72,9 +74,8 @@ class DomainAge implements MetricsProvider */ private function getAgeArchiveOrg($domain) { - $url = str_replace('www.', '', $domain); - $data = $this->getUrl('http://wayback.archive.org/web/*/' . urlencode($url)); - preg_match('#<a href=\"([^>]*)' . preg_quote($url) . '/\">([^<]*)<\/a>#', $data, $p); + $data = $this->getUrl('http://wayback.archive.org/web/*/' . urlencode($domain)); + preg_match('#<a href=\"([^>]*)' . preg_quote($domain) . '/\">([^<]*)<\/a>#', $data, $p); if (!empty($p[2])) { $value = strtotime($p[2]); if ($value === false) { @@ -93,9 +94,7 @@ class DomainAge implements MetricsProvider */ private function getAgeWhoIs($domain) { - $url = preg_replace('/^www\./', '', $domain); - $url = 'http://www.who.is/whois/' . urlencode($url); - $data = $this->getUrl($url); + $data = $this->getUrl('http://www.who.is/whois/' . urlencode($domain)); preg_match('#(?:Creation Date|Created On|created|Registered on)\.*:\s*([ \ta-z0-9\/\-:\.]+)#si', $data, $p); if (!empty($p[1])) { $value = strtotime(trim($p[1])); @@ -115,9 +114,7 @@ class DomainAge implements MetricsProvider */ private function getAgeWhoisCom($domain) { - $url = preg_replace('/^www\./', '', $domain); - $url = 'http://www.whois.com/whois/' . urlencode($url); - $data = $this->getUrl($url); + $data = $this->getUrl('http://www.whois.com/whois/' . urlencode($domain)); preg_match('#(?:Creation Date|Created On|created):\s*([ \ta-z0-9\/\-:\.]+)#si', $data, $p); if (!empty($p[1])) { $value = strtotime(trim($p[1])); diff --git a/plugins/SEO/Metric/Google.php b/plugins/SEO/Metric/Google.php index 7abd82b371..4247ec138f 100644 --- a/plugins/SEO/Metric/Google.php +++ b/plugins/SEO/Metric/Google.php @@ -36,13 +36,11 @@ class Google implements MetricsProvider public function getMetrics($domain) { $pageCount = $this->fetchIndexedPagesCount($domain); - $pageRank = $this->fetchPageRank($domain); $logo = SearchEngine::getInstance()->getLogoFromUrl('http://google.com'); return array( new Metric('google-index', 'SEO_Google_IndexedPages', $pageCount, $logo, null, null, 'General_Pages'), - new Metric('pagerank', 'Google PageRank', $pageRank, $logo, null, null, '/10'), ); } @@ -53,8 +51,8 @@ class Google implements MetricsProvider try { $response = str_replace(' ', ' ', Http::sendHttpRequest($url, $timeout = 10, @$_SERVER['HTTP_USER_AGENT'])); - if (preg_match('#([0-9\,]+) results#i', $response, $p)) { - return NumberFormatter::getInstance()->formatNumber((int)str_replace(',', '', $p[1])); + if (preg_match('#([0-9,\.]+) results#i', $response, $p)) { + return NumberFormatter::getInstance()->formatNumber((int)str_replace(array(',', '.'), '', $p[1])); } else { return 0; } @@ -64,109 +62,4 @@ class Google implements MetricsProvider } } - public function fetchPageRank($domain) - { - $chwrite = $this->checkHash($this->hashURL($domain)); - - $url = "http://toolbarqueries.google.com/tbr?client=navclient-auto&ch=" . $chwrite . "&features=Rank&q=info:" . $domain . "&num=100&filter=0"; - - try { - $response = Http::sendHttpRequest($url, $timeout = 10, @$_SERVER['HTTP_USER_AGENT']); - - preg_match('#Rank_[0-9]:[0-9]:([0-9]+){1,}#si', $response, $p); - - return isset($p[1]) ? $p[1] : null; - } catch (\Exception $e) { - $this->logger->warning('Error while getting Google PageRank for SEO stats: {message}', array('message' => $e->getMessage())); - return null; - } - } - - /** - * Generate a hash for a url - * - * @param string $string - * @return int - */ - private function hashURL($string) - { - $Check1 = $this->strToNum($string, 0x1505, 0x21); - $Check2 = $this->strToNum($string, 0, 0x1003F); - - $Check1 >>= 2; - $Check1 = (($Check1 >> 4) & 0x3FFFFC0) | ($Check1 & 0x3F); - $Check1 = (($Check1 >> 4) & 0x3FFC00) | ($Check1 & 0x3FF); - $Check1 = (($Check1 >> 4) & 0x3C000) | ($Check1 & 0x3FFF); - - $T1 = (((($Check1 & 0x3C0) << 4) | ($Check1 & 0x3C)) << 2) | ($Check2 & 0xF0F); - $T2 = (((($Check1 & 0xFFFFC000) << 4) | ($Check1 & 0x3C00)) << 0xA) | ($Check2 & 0xF0F0000); - - return ($T1 | $T2); - } - - /** - * Generate a checksum for the hash string - * - * @param int $hashnum - * @return string - */ - private function checkHash($hashnum) - { - $CheckByte = 0; - $Flag = 0; - - $HashStr = sprintf('%u', $hashnum); - $length = strlen($HashStr); - - for ($i = $length - 1; $i >= 0; $i--) { - $Re = $HashStr{$i}; - if (1 === ($Flag % 2)) { - $Re += $Re; - $Re = (int)($Re / 10) + ($Re % 10); - } - $CheckByte += $Re; - $Flag++; - } - - $CheckByte %= 10; - if (0 !== $CheckByte) { - $CheckByte = 10 - $CheckByte; - if (1 === ($Flag % 2)) { - if (1 === ($CheckByte % 2)) { - $CheckByte += 9; - } - $CheckByte >>= 1; - } - } - - return '7' . $CheckByte . $HashStr; - } - - /** - * Convert numeric string to int - * - * @param string $Str - * @param int $Check - * @param int $Magic - * @return int - */ - private function strToNum($Str, $Check, $Magic) - { - $Int32Unit = 4294967296; // 2^32 - - $length = strlen($Str); - for ($i = 0; $i < $length; $i++) { - $Check *= $Magic; - // If the float is beyond the boundaries of integer (usually +/- 2.15e+9 = 2^31), - // the result of converting to integer is undefined - // refer to http://www.php.net/manual/en/language.types.integer.php - if ($Check >= $Int32Unit) { - $Check = ($Check - $Int32Unit * (int)($Check / $Int32Unit)); - //if the check less than -2^31 - $Check = ($Check < -2147483648) ? ($Check + $Int32Unit) : $Check; - } - $Check += ord($Str{$i}); - } - return $Check; - } } |