update Piwik to version 2.16 (fixes #91)

This commit is contained in:
oliver 2016-04-10 18:55:57 +02:00
commit d885a4baa9
5833 changed files with 418860 additions and 226988 deletions

View file

@ -0,0 +1,65 @@
<?php
/**
* Piwik - free/libre analytics platform
*
* @link http://piwik.org
* @license http://www.gnu.org/licenses/gpl-3.0.html GPL v3 or later
*/
namespace Piwik\Plugins\SEO\Metric;
use Piwik\Container\StaticContainer;
use Piwik\Metrics\Formatter;
use Piwik\Piwik;
/**
* Aggregates metrics from several providers.
*/
class Aggregator implements MetricsProvider
{
/**
* @var MetricsProvider[]
*/
private $providers;
public function __construct()
{
$this->providers = $this->getProviders();
}
public function getMetrics($domain)
{
$metrics = array();
foreach ($this->providers as $provider) {
$metrics = array_merge($metrics, $provider->getMetrics($domain));
}
return $metrics;
}
/**
* @return MetricsProvider[]
*/
private function getProviders()
{
$container = StaticContainer::getContainer();
$providers = array(
$container->get('Piwik\Plugins\SEO\Metric\Google'),
$container->get('Piwik\Plugins\SEO\Metric\Bing'),
$container->get('Piwik\Plugins\SEO\Metric\Alexa'),
$container->get('Piwik\Plugins\SEO\Metric\DomainAge'),
$container->get('Piwik\Plugins\SEO\Metric\Dmoz'),
);
/**
* Use this event to register new SEO metrics providers.
*
* @param array $providers Contains an array of Piwik\Plugins\SEO\Metric\MetricsProvider instances.
*/
Piwik::postEvent('SEO.getMetricsProviders', array(&$providers));
return $providers;
}
}

View file

@ -0,0 +1,53 @@
<?php
/**
* Piwik - free/libre analytics platform
*
* @link http://piwik.org
* @license http://www.gnu.org/licenses/gpl-3.0.html GPL v3 or later
*/
namespace Piwik\Plugins\SEO\Metric;
use Piwik\Http;
use Piwik\NumberFormatter;
use Piwik\Plugins\Referrers\SearchEngine;
use Psr\Log\LoggerInterface;
/**
* Retrieves the Alexa rank.
*/
class Alexa implements MetricsProvider
{
const URL = 'http://data.alexa.com/data?cli=10&url=';
const LINK = 'http://www.alexa.com/siteinfo/';
/**
* @var LoggerInterface
*/
private $logger;
public function __construct(LoggerInterface $logger)
{
$this->logger = $logger;
}
public function getMetrics($domain)
{
try {
$response = Http::sendHttpRequest(self::URL . urlencode($domain), $timeout = 10, @$_SERVER['HTTP_USER_AGENT']);
$xml = @simplexml_load_string($response);
$value = $xml ? NumberFormatter::getInstance()->formatNumber((int)$xml->SD->POPULARITY['TEXT']) : null;
} catch (\Exception $e) {
$this->logger->warning('Error while getting Alexa SEO stats: {message}', array('message' => $e->getMessage()));
$value = null;
}
$logo = SearchEngine::getInstance()->getLogoFromUrl('http://alexa.com');
$link = self::LINK . urlencode($domain);
return array(
new Metric('alexa', 'SEO_AlexaRank', $value, $logo, $link)
);
}
}

View file

@ -0,0 +1,56 @@
<?php
/**
* Piwik - free/libre analytics platform
*
* @link http://piwik.org
* @license http://www.gnu.org/licenses/gpl-3.0.html GPL v3 or later
*/
namespace Piwik\Plugins\SEO\Metric;
use Piwik\Http;
use Piwik\NumberFormatter;
use Piwik\Plugins\Referrers\SearchEngine;
use Psr\Log\LoggerInterface;
/**
* Fetches the number of pages indexed in Bing.
*/
class Bing implements MetricsProvider
{
const URL = 'http://www.bing.com/search?mkt=en-US&q=site%3A';
/**
* @var LoggerInterface
*/
private $logger;
public function __construct(LoggerInterface $logger)
{
$this->logger = $logger;
}
public function getMetrics($domain)
{
$url = self::URL . urlencode($domain);
try {
$response = str_replace('&nbsp;', ' ', 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]));
} else {
$pageCount = 0;
}
} catch (\Exception $e) {
$this->logger->warning('Error while getting Bing SEO stats: {message}', array('message' => $e->getMessage()));
$pageCount = null;
}
$logo = SearchEngine::getInstance()->getLogoFromUrl('http://bing.com');
return array(
new Metric('bing-index', 'SEO_Bing_IndexedPages', $pageCount, $logo, null, null, 'General_Pages')
);
}
}

View file

@ -0,0 +1,63 @@
<?php
/**
* Piwik - free/libre analytics platform
*
* @link http://piwik.org
* @license http://www.gnu.org/licenses/gpl-3.0.html GPL v3 or later
*/
namespace Piwik\Plugins\SEO\Metric;
use Piwik\Http;
use Piwik\NumberFormatter;
use Piwik\Plugins\Referrers\SearchEngine;
use Psr\Log\LoggerInterface;
/**
* Retrieves the number of Dmoz.org entries.
*/
class Dmoz implements MetricsProvider
{
const URL = 'http://www.dmoz.org/search?q=';
/**
* @var LoggerInterface
*/
private $logger;
/**
* @param LoggerInterface $logger
*/
public function __construct(LoggerInterface $logger)
{
$this->logger = $logger;
}
public function getMetrics($domain)
{
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);
if (!empty($p[1])) {
$value = NumberFormatter::getInstance()->formatNumber((int)$p[1]);
} else {
$value = 0;
}
// Add DMOZ only if > 0 entries found
if ($value == 0) {
return array();
}
} catch (\Exception $e) {
$this->logger->warning('Error while getting Dmoz SEO stats: {message}', array('message' => $e->getMessage()));
$value = null;
}
$logo = SearchEngine::getInstance()->getLogoFromUrl('http://dmoz.org');
return array(
new Metric('dmoz', 'SEO_Dmoz', $value, $logo)
);
}
}

View file

@ -0,0 +1,141 @@
<?php
/**
* Piwik - free/libre analytics platform
*
* @link http://piwik.org
* @license http://www.gnu.org/licenses/gpl-3.0.html GPL v3 or later
*/
namespace Piwik\Plugins\SEO\Metric;
use Piwik\Http;
use Piwik\Metrics\Formatter;
use Psr\Log\LoggerInterface;
/**
* Fetches the domain age using archive.org, who.is and whois.com.
*/
class DomainAge implements MetricsProvider
{
/**
* @var Formatter
*/
private $formatter;
/**
* @var LoggerInterface
*/
private $logger;
public function __construct(Formatter $formatter, LoggerInterface $logger)
{
$this->formatter = $formatter;
$this->logger = $logger;
}
public function getMetrics($domain)
{
$ages = array();
$age = $this->getAgeArchiveOrg($domain);
if ($age > 0) {
$ages[] = $age;
}
$age = $this->getAgeWhoIs($domain);
if ($age > 0) {
$ages[] = $age;
}
$age = $this->getAgeWhoisCom($domain);
if ($age > 0) {
$ages[] = $age;
}
if (count($ages) > 0) {
$value = min($ages);
$value = $this->formatter->getPrettyTimeFromSeconds(time() - $value, true);
} else {
$value = null;
}
return array(
new Metric('domain-age', 'SEO_DomainAge', $value, 'plugins/SEO/images/whois.png')
);
}
/**
* Returns the domain age archive.org lists for the current url
*
* @param string $domain
* @return int
*/
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);
if (!empty($p[2])) {
$value = strtotime($p[2]);
if ($value === false) {
return 0;
}
return $value;
}
return 0;
}
/**
* Returns the domain age who.is lists for the current url
*
* @param string $domain
* @return int
*/
private function getAgeWhoIs($domain)
{
$url = preg_replace('/^www\./', '', $domain);
$url = 'http://www.who.is/whois/' . urlencode($url);
$data = $this->getUrl($url);
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]));
if ($value === false) {
return 0;
}
return $value;
}
return 0;
}
/**
* Returns the domain age whois.com lists for the current url
*
* @param string $domain
* @return int
*/
private function getAgeWhoisCom($domain)
{
$url = preg_replace('/^www\./', '', $domain);
$url = 'http://www.whois.com/whois/' . urlencode($url);
$data = $this->getUrl($url);
preg_match('#(?:Creation Date|Created On|created):\s*([ \ta-z0-9\/\-:\.]+)#si', $data, $p);
if (!empty($p[1])) {
$value = strtotime(trim($p[1]));
if ($value === false) {
return 0;
}
return $value;
}
return 0;
}
private function getUrl($url)
{
try {
return str_replace('&nbsp;', ' ', Http::sendHttpRequest($url, $timeout = 10, @$_SERVER['HTTP_USER_AGENT']));
} catch (\Exception $e) {
$this->logger->warning('Error while getting SEO stats (domain age): {message}', array('message' => $e->getMessage()));
return '';
}
}
}

View file

@ -0,0 +1,172 @@
<?php
/**
* Piwik - free/libre analytics platform
*
* @link http://piwik.org
* @license http://www.gnu.org/licenses/gpl-3.0.html GPL v3 or later
*/
namespace Piwik\Plugins\SEO\Metric;
use Piwik\Http;
use Piwik\NumberFormatter;
use Piwik\Plugins\Referrers\SearchEngine;
use Psr\Log\LoggerInterface;
/**
* Retrieves Google PageRank.
*/
class Google implements MetricsProvider
{
const SEARCH_URL = 'http://www.google.com/search?hl=en&q=site%3A';
/**
* @var LoggerInterface
*/
private $logger;
/**
* @param LoggerInterface $logger
*/
public function __construct(LoggerInterface $logger)
{
$this->logger = $logger;
}
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'),
);
}
public function fetchIndexedPagesCount($domain)
{
$url = self::SEARCH_URL . urlencode($domain);
try {
$response = str_replace('&nbsp;', ' ', 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]));
} else {
return 0;
}
} catch (\Exception $e) {
$this->logger->warning('Error while getting Google search SEO stats: {message}', array('message' => $e->getMessage()));
return null;
}
}
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;
}
}

View file

@ -0,0 +1,145 @@
<?php
/**
* Piwik - free/libre analytics platform
*
* @link http://piwik.org
* @license http://www.gnu.org/licenses/gpl-3.0.html GPL v3 or later
*/
namespace Piwik\Plugins\SEO\Metric;
/**
* Describes a SEO metric.
*/
class Metric
{
/**
* @var string
*/
private $id;
/**
* @var string
*/
private $name;
/**
* @var string
*/
private $value;
/**
* @var string
*/
private $logo;
/**
* @var string|null
*/
private $logoLink;
/**
* @var string|null
*/
private $logoTooltip;
/**
* @var string|null
*/
private $valueSuffix;
/**
* @param string $id
* @param string $name Can be a string or a translation ID.
* @param string $value Rank value.
* @param string $logo URL to a logo.
* @param string|null $logoLink
* @param string|null $logoTooltip
* @param string|null $valueSuffix
*/
public function __construct($id, $name, $value, $logo, $logoLink = null, $logoTooltip = null, $valueSuffix = null)
{
$this->id = $id;
$this->name = $name;
$this->value = $value;
$this->logo = $logo;
$this->logoLink = $logoLink;
$this->logoTooltip = $logoTooltip;
$this->valueSuffix = $valueSuffix;
}
/**
* @return string
*/
public function getId()
{
return $this->id;
}
/**
* @return string
*/
public function getName()
{
return $this->name;
}
/**
* @return string
*/
public function getValue()
{
return $this->value;
}
/**
* @return string
*/
public function getLogo()
{
return $this->logo;
}
/**
* @return string|null
*/
public function getLogoLink()
{
return $this->logoLink;
}
/**
* @return string|null
*/
public function getLogoTooltip()
{
return $this->logoTooltip;
}
/**
* @return null|string
*/
public function getValueSuffix()
{
return $this->valueSuffix;
}
/**
* Allows the class to be serialized with var_export (in the cache).
*
* @param array $array
* @return Metric
*/
public static function __set_state($array)
{
return new self(
$array['id'],
$array['name'],
$array['value'],
$array['logo'],
$array['logoLink'],
$array['logoTooltip'],
$array['valueSuffix']
);
}
}

View file

@ -0,0 +1,21 @@
<?php
/**
* Piwik - free/libre analytics platform
*
* @link http://piwik.org
* @license http://www.gnu.org/licenses/gpl-3.0.html GPL v3 or later
*/
namespace Piwik\Plugins\SEO\Metric;
/**
* Provides SEO metrics for a domain.
*/
interface MetricsProvider
{
/**
* @param string $domain
* @return Metric[]
*/
public function getMetrics($domain);
}

View file

@ -0,0 +1,48 @@
<?php
/**
* Piwik - free/libre analytics platform
*
* @link http://piwik.org
* @license http://www.gnu.org/licenses/gpl-3.0.html GPL v3 or later
*/
namespace Piwik\Plugins\SEO\Metric;
use Piwik\Cache;
/**
* Caches another provider.
*/
class ProviderCache implements MetricsProvider
{
/**
* @var MetricsProvider
*/
private $provider;
/**
* @var Cache\Lazy
*/
private $cache;
public function __construct(MetricsProvider $provider)
{
$this->provider = $provider;
$this->cache = Cache::getLazyCache();
}
public function getMetrics($domain)
{
$cacheId = 'SEO_getRank_' . md5($domain);
$metrics = $this->cache->fetch($cacheId);
if (! is_array($metrics)) {
$metrics = $this->provider->getMetrics($domain);
$this->cache->save($cacheId, $metrics, 60 * 60 * 6);
}
return $metrics;
}
}