1
|
|
|
<?php |
2
|
|
|
namespace SEOstats\Services; |
3
|
|
|
|
4
|
|
|
/** |
5
|
|
|
* SEOstats extension for Sistrix data. |
6
|
|
|
* |
7
|
|
|
* @package SEOstats |
8
|
|
|
* @author Stephan Schmitz <[email protected]> |
9
|
|
|
* @copyright Copyright (c) 2010 - present Stephan Schmitz |
10
|
|
|
* @license http://eyecatchup.mit-license.org/ MIT License |
11
|
|
|
* @updated 2013/08/14 |
12
|
|
|
*/ |
13
|
|
|
|
14
|
|
|
use SEOstats\Common\SEOstatsException as E; |
15
|
|
|
use SEOstats\SEOstats as SEOstats; |
16
|
|
|
use SEOstats\Config as Config; |
17
|
|
|
use SEOstats\Helper as Helper; |
18
|
|
|
|
19
|
|
|
class Sistrix extends SEOstats |
20
|
|
|
{ |
21
|
3 |
|
public static function getDBs() |
22
|
|
|
{ |
23
|
|
|
return array( |
24
|
3 |
|
'de', // de – Germany |
25
|
3 |
|
'at', // at – Austria |
26
|
3 |
|
'ch', // ch – Switzerland |
27
|
3 |
|
'us', // us – USA |
28
|
3 |
|
'uk', // uk – England |
29
|
3 |
|
'es', // es – Spain |
30
|
3 |
|
'fr', // fr – France |
31
|
3 |
|
'it', // it – Italy |
32
|
3 |
|
); |
33
|
|
|
} |
34
|
|
|
|
35
|
|
|
/** |
36
|
|
|
* Returns the Sistrix visibility index |
37
|
|
|
* |
38
|
|
|
* @access public |
39
|
|
|
* @param url string The URL to check. |
40
|
|
|
* @return integer Returns the Sistrix visibility index. |
41
|
|
|
* @link http://www.sistrix.com/blog/870-sistrix-visibilityindex.html |
42
|
|
|
*/ |
43
|
2 |
View Code Duplication |
public static function getVisibilityIndex($url = false) |
|
|
|
|
44
|
|
|
{ |
45
|
2 |
|
$url = parent::getUrl($url); |
|
|
|
|
46
|
2 |
|
$domain = Helper\Url::parseHost($url); |
47
|
2 |
|
$dataUrl = sprintf(Config\Services::SISTRIX_VI_URL, urlencode($domain)); |
48
|
|
|
|
49
|
2 |
|
$html = static::_getPage($dataUrl); |
50
|
2 |
|
@preg_match_all('#<h3>(.*?)<\/h3>#si', $html, $matches); |
|
|
|
|
51
|
|
|
|
52
|
2 |
|
return isset($matches[1][0]) ? $matches[1][0] : parent::noDataDefaultValue(); |
|
|
|
|
53
|
|
|
} |
54
|
|
|
|
55
|
|
|
/** |
56
|
|
|
* Returns the Sistrix visibility index by using the SISTRIX API |
57
|
|
|
* |
58
|
|
|
* @access public |
59
|
|
|
* @param url string The URL to check. |
60
|
|
|
* @return integer Returns the Sistrix visibility index. |
61
|
|
|
* @link http://www.sistrix.com/blog/870-sistrix-visibilityindex.html |
62
|
|
|
*/ |
63
|
3 |
|
public static function getVisibilityIndexByApi($url = false, $db = false) |
64
|
|
|
{ |
65
|
3 |
|
self::guardApiKey(); |
66
|
3 |
|
self::guardApiCredits(); |
67
|
|
|
|
68
|
3 |
|
$url = parent::getUrl($url); |
|
|
|
|
69
|
3 |
|
$domain = static::getDomainFromUrl($url); |
70
|
3 |
|
$database = static::getValidDatabase($db); |
71
|
|
|
|
72
|
3 |
|
$dataUrl = sprintf(Config\Services::SISTRIX_API_VI_URL, Config\ApiKeys::getSistrixApiAccessKey(), urlencode($domain), $database); |
73
|
|
|
|
74
|
3 |
|
$json = static::_getPage($dataUrl); |
75
|
|
|
|
76
|
3 |
|
if(empty($json)) { |
77
|
1 |
|
return parent::noDataDefaultValue(); |
|
|
|
|
78
|
|
|
} |
79
|
|
|
|
80
|
2 |
|
$json_decoded = (Helper\Json::decode($json, true)); |
81
|
2 |
View Code Duplication |
if (!isset($json_decoded['answer'][0]['sichtbarkeitsindex'][0]['value'])) { |
|
|
|
|
82
|
1 |
|
return parent::noDataDefaultValue(); |
|
|
|
|
83
|
|
|
} |
84
|
1 |
|
return $json_decoded['answer'][0]['sichtbarkeitsindex'][0]['value']; |
85
|
|
|
} |
86
|
|
|
|
87
|
10 |
|
public static function getApiCredits() |
88
|
|
|
{ |
89
|
10 |
|
self::guardApiKey(); |
90
|
|
|
|
91
|
10 |
|
$dataUrl = sprintf(Config\Services::SISTRIX_API_CREDITS_URL, Config\ApiKeys::getSistrixApiAccessKey()); |
92
|
10 |
|
$json = static::_getPage($dataUrl); |
93
|
|
|
|
94
|
10 |
|
if(empty($json)) { |
95
|
2 |
|
return parent::noDataDefaultValue(); |
|
|
|
|
96
|
|
|
} |
97
|
|
|
|
98
|
8 |
|
$json_decoded = (Helper\Json::decode($json, true)); |
99
|
8 |
View Code Duplication |
if (!isset($json_decoded['answer'][0]['credits'][0]['value'])) { |
|
|
|
|
100
|
6 |
|
return parent::noDataDefaultValue(); |
|
|
|
|
101
|
|
|
} |
102
|
2 |
|
return $json_decoded['answer'][0]['credits'][0]['value']; |
103
|
|
|
} |
104
|
|
|
|
105
|
5 |
|
public static function checkApiCredits() |
106
|
|
|
{ |
107
|
5 |
|
return static::getApiCredits() > 0; |
108
|
|
|
} |
109
|
|
|
|
110
|
13 |
|
protected static function guardApiKey() |
111
|
|
|
{ |
112
|
13 |
|
if(!static::hasApiKey()) { |
113
|
|
|
self::exc('In order to use the SISTRIX API, you must obtain and set an ' . |
114
|
|
|
'API key first (see SEOstats\Config\ApiKeys.php).' . PHP_EOL); |
115
|
|
|
} |
116
|
13 |
|
} |
117
|
|
|
|
118
|
|
|
protected static function hasApiKey() |
119
|
|
|
{ |
120
|
|
|
if ('' == Config\ApiKeys::getSistrixApiAccessKey()) { |
121
|
|
|
return false; |
122
|
|
|
} |
123
|
|
|
|
124
|
|
|
return true; |
125
|
|
|
} |
126
|
|
|
|
127
|
3 |
|
protected static function guardApiCredits() |
128
|
|
|
{ |
129
|
3 |
|
if(!static::checkApiCredits()) { |
130
|
|
|
self::exc('Not enough API credits.'.PHP_EOL); |
131
|
|
|
} |
132
|
3 |
|
} |
133
|
|
|
|
134
|
3 |
|
private static function checkDatabase($db) |
135
|
|
|
{ |
136
|
3 |
|
return !in_array($db, self::getDBs()) ? false : $db; |
137
|
|
|
} |
138
|
|
|
|
139
|
3 |
|
protected static function getDomainFromUrl($url) |
140
|
|
|
{ |
141
|
3 |
|
$url = parent::getUrl($url); |
|
|
|
|
142
|
3 |
|
$domain = Helper\Url::parseHost($url); |
143
|
3 |
|
static::guardDomainIsValid($domain); |
144
|
|
|
|
145
|
3 |
|
return $domain; |
146
|
|
|
} |
147
|
|
|
|
148
|
3 |
|
protected static function getValidDatabase($db) |
149
|
|
|
{ |
150
|
3 |
|
$db = ($db == false) ? Config\DefaultSettings::SISTRIX_DB : $db; |
151
|
|
|
|
152
|
3 |
|
$database = self::checkDatabase($db); |
153
|
3 |
|
static::guardDatabaseIsValid($database); |
154
|
|
|
|
155
|
3 |
|
return $database; |
156
|
|
|
} |
157
|
|
|
|
158
|
3 |
|
protected static function guardDatabaseIsValid($database) |
159
|
|
|
{ |
160
|
3 |
|
if (false === $database) { |
161
|
|
|
self::exc('db'); |
162
|
|
|
} |
163
|
3 |
|
} |
164
|
|
|
|
165
|
3 |
|
protected static function guardDomainIsValid($domain) |
166
|
|
|
{ |
167
|
3 |
|
if (false == $domain) { |
168
|
|
|
self::exc('Invalid domain name.'); |
169
|
|
|
} |
170
|
3 |
|
} |
171
|
|
|
|
172
|
|
View Code Duplication |
private static function exc($err) |
|
|
|
|
173
|
|
|
{ |
174
|
|
|
$e = ($err == 'db') |
175
|
|
|
? "Invalid database. Choose one of: " . |
176
|
|
|
substr( implode(", ", self::getDBs()), 0, -2) |
177
|
|
|
: $err; |
178
|
|
|
throw new E($e); |
179
|
|
|
} |
180
|
|
|
} |
181
|
|
|
|
Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.
You can also find more detailed suggestions in the “Code” section of your repository.