Welcome to mirror list, hosted at ThFree Co, Russian Federation.

API.php « SEO « plugins - github.com/matomo-org/matomo.git - Unnamed repository; edit this file 'description' to name the repository.
summaryrefslogtreecommitdiff
blob: 3d6ad91b054b87517ea08f1e99f40501b1814810 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
<?php
/**
 * Piwik - free/libre analytics platform
 *
 * @link https://matomo.org
 * @license http://www.gnu.org/licenses/gpl-3.0.html GPL v3 or later
 */

namespace Piwik\Plugins\SEO;

use Piwik\DataTable;
use Piwik\Piwik;
use Piwik\Plugins\SEO\Metric\Aggregator;
use Piwik\Plugins\SEO\Metric\Metric;
use Piwik\Plugins\SEO\Metric\ProviderCache;
use Piwik\Url;

/**
 * @see plugins/Referrers/functions.php
 * @method static API getInstance()
 */
require_once PIWIK_INCLUDE_PATH . '/plugins/Referrers/functions.php';

/**
 * The SEO API lets you access a list of SEO metrics for the specified URL: Google PageRank, Google/Bing indexed pages
 * Alexa Rank and age of the Domain name.
 *
 * @method static API getInstance()
 */
class API extends \Piwik\Plugin\API
{
    /**
     * Returns SEO statistics for a URL.
     *
     * @param string $url URL to request SEO stats for
     * @return DataTable
     */
    public function getRank($url)
    {
        Piwik::checkUserHasSomeViewAccess();

        $metricProvider = new ProviderCache(new Aggregator());
        $domain = Url::getHostFromUrl($url);
        $metrics = $metricProvider->getMetrics($domain);

        return $this->toDataTable($metrics);
    }

    /**
     * @param Metric[] $metrics
     * @return DataTable
     */
    private function toDataTable(array $metrics)
    {
        $translated = array();

        foreach ($metrics as $metric) {
            if (!$metric instanceof Metric) {
                continue;
            }

            $label = Piwik::translate($metric->getName());
            $translated[$label] = array(
                'id'           => $metric->getId(),
                'rank'         => $metric->getValue(),
                'logo'         => $metric->getLogo(),
                'logo_link'    => $metric->getLogoLink(),
                'logo_tooltip' => Piwik::translate($metric->getLogoTooltip()),
                'rank_suffix'  => Piwik::translate($metric->getValueSuffix()),
            );
        }

        return DataTable::makeFromIndexedArray($translated);
    }
}