PDF rausgenommen

This commit is contained in:
aschwarz
2023-01-23 11:03:31 +01:00
parent 82d562a322
commit a6523903eb
28078 changed files with 4247552 additions and 2 deletions

View File

@ -0,0 +1,64 @@
<?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'),
);
/**
* 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,69 @@
<?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 Psr\Log\LoggerInterface;
/**
* Retrieves the Alexa rank.
*/
class Alexa implements MetricsProvider
{
const URL = 'https://data.alexa.com/data?cli=10&url=';
const URL_FALLBACK = 'https://www.alexa.com/minisiteinfo/';
const LINK = 'https://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 = $this->tryFallbackMethod($domain);
}
$logo = "plugins/Morpheus/icons/dist/SEO/alexa.com.png";
$link = self::LINK . urlencode($domain);
return array(
new Metric('alexa', 'SEO_AlexaRank', $value, $logo, $link)
);
}
private function tryFallbackMethod($domain)
{
try {
$response = Http::sendHttpRequest(self::URL_FALLBACK . urlencode($domain), $timeout = 10, @$_SERVER['HTTP_USER_AGENT']);
$dom = new \DomDocument();
$dom->loadHTML($response);
$nodes = (new \DomXPath($dom))->query("//div[contains(@class, 'data')]");
if (isset($nodes[0]->nodeValue)) {
$globalRanking = (int) str_replace(array(',', '.'), '', $nodes[0]->nodeValue);
return NumberFormatter::getInstance()->formatNumber($globalRanking);
}
} catch (\Exception $e) {
$this->logger->warning('Error while getting Alexa SEO stats via fallback method: {message}', array('message' => $e->getMessage()));
}
return null;
}
}

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 Psr\Log\LoggerInterface;
/**
* Fetches the number of pages indexed in Bing.
*/
class Bing implements MetricsProvider
{
const URL = 'https://www.bing.com/search?setlang=en-US&rdr=1&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']));
$response = str_replace('&#160;', '', $response); // number uses nbsp as thousand seperator
if (preg_match('#([0-9,\.]+) results#i', $response, $p)) {
$pageCount = NumberFormatter::getInstance()->formatNumber((int)str_replace(array(',', '.'), '', $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 = "plugins/Morpheus/icons/dist/SEO/bing.com.png";
return array(
new Metric('bing-index', 'SEO_Bing_IndexedPages', $pageCount, $logo, null, null, 'General_Pages')
);
}
}

View File

@ -0,0 +1,139 @@
<?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)
{
$domain = str_replace('www.', '', $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/Morpheus/icons/dist/SEO/whois.png')
);
}
/**
* Returns the domain age archive.org lists for the current url
*
* @param string $domain
* @return int
*/
private function getAgeArchiveOrg($domain)
{
$response = $this->getUrl('https://archive.org/wayback/available?timestamp=19900101&url=' . urlencode($domain));
$data = json_decode($response, true);
if (empty($data["archived_snapshots"]["closest"]["timestamp"])) {
return 0;
}
return strtotime($data["archived_snapshots"]["closest"]["timestamp"]);
}
/**
* Returns the domain age who.is lists for the current url
*
* @param string $domain
* @return int
*/
private function getAgeWhoIs($domain)
{
$data = $this->getUrl('https://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]));
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)
{
$data = $this->getUrl('https://www.whois.com/whois/' . urlencode($domain));
preg_match('#(?:Creation Date|Created On|created|Registration Date):\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 $this->getHttpResponse($url);
} catch (\Exception $e) {
$this->logger->warning('Error while getting SEO stats (domain age): {message}', array('message' => $e->getMessage()));
return '';
}
}
private function getHttpResponse($url)
{
return str_replace('&nbsp;', ' ', Http::sendHttpRequest($url, $timeout = 10, @$_SERVER['HTTP_USER_AGENT']));
}
}

View File

@ -0,0 +1,64 @@
<?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 Psr\Log\LoggerInterface;
/**
* Retrieves Google PageRank.
*/
class Google implements MetricsProvider
{
const SEARCH_URL = 'https://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);
$logo = "plugins/Morpheus/icons/dist/SEO/google.com.png";
return array(
new Metric('google-index', 'SEO_Google_IndexedPages', $pageCount, $logo, null, null, 'General_Pages'),
);
}
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(array(',', '.'), '', $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;
}
}
}

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;
}
}