1
|
|
|
<?php |
2
|
|
|
/** |
3
|
|
|
* Data Analytics |
4
|
|
|
* |
5
|
|
|
* This file is licensed under the Affero General Public License version 3 or |
6
|
|
|
* later. See the LICENSE.md file. |
7
|
|
|
* |
8
|
|
|
* @author Marcel Scherello <[email protected]> |
9
|
|
|
* @copyright 2020 Marcel Scherello |
10
|
|
|
*/ |
11
|
|
|
|
12
|
|
|
namespace OCA\Analytics\Datasource; |
13
|
|
|
|
14
|
|
|
use OCP\ILogger; |
15
|
|
|
|
16
|
|
|
class GithubService |
17
|
|
|
{ |
18
|
|
|
private $logger; |
19
|
|
|
|
20
|
|
|
public function __construct( |
21
|
|
|
ILogger $logger |
22
|
|
|
) |
23
|
|
|
{ |
24
|
|
|
$this->logger = $logger; |
25
|
|
|
} |
26
|
|
|
|
27
|
|
|
/** |
28
|
|
|
* Get the items for the selected category |
29
|
|
|
* |
30
|
|
|
* @NoAdminRequired |
31
|
|
|
* @param array $option |
32
|
|
|
* @return array |
33
|
|
|
*/ |
34
|
|
|
public function read($option) |
35
|
|
|
{ |
36
|
|
|
if (isset($option['link'])) $string = 'https://api.github.com/repos/' . $option['link'] . '/releases'; |
37
|
|
|
else $string = 'https://api.github.com/repos/' . $option['user'] . '/' . $option['repository'] . '/releases'; |
38
|
|
|
|
39
|
|
|
$ch = curl_init(); |
40
|
|
|
if ($ch != false) { |
41
|
|
|
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, FALSE); |
42
|
|
|
curl_setopt($ch, CURLOPT_HEADER, false); |
43
|
|
|
curl_setopt($ch, CURLOPT_FOLLOWLOCATION, true); |
44
|
|
|
curl_setopt($ch, CURLOPT_URL, $string); |
45
|
|
|
curl_setopt($ch, CURLOPT_REFERER, $string); |
46
|
|
|
curl_setopt($ch, CURLOPT_RETURNTRANSFER, TRUE); |
47
|
|
|
curl_setopt($ch, CURLOPT_USERAGENT, 'Mozilla/5.0 (Windows; U; Windows NT 5.1; en-US; rv:1.8.1.13) Gecko/20080311 Firefox/2.0.0.13'); |
48
|
|
|
$curlResult = curl_exec($ch); |
49
|
|
|
curl_close($ch); |
50
|
|
|
} else { |
51
|
|
|
$curlResult = ''; |
52
|
|
|
} |
53
|
|
|
|
54
|
|
|
$json = json_decode($curlResult, true); |
55
|
|
|
$i = 0; |
56
|
|
|
|
57
|
|
|
$data = array(); |
58
|
|
|
foreach ($json as $item) { |
59
|
|
|
if (isset($option['limit'])) { |
60
|
|
|
if ($i === (int)$option['limit']) break; |
61
|
|
|
} |
62
|
|
|
$nc_value = 0; |
63
|
|
|
foreach ($item['assets'] as $asset) { |
64
|
|
|
if (substr($asset['name'], -2) == 'gz') $nc_value = $asset['download_count']; |
65
|
|
|
} |
66
|
|
|
array_push($data, [$item['tag_name'], $this->floatvalue($nc_value)]); |
67
|
|
|
$i++; |
68
|
|
|
} |
69
|
|
|
|
70
|
|
|
$header = array(); |
71
|
|
|
$header[0] = 'Version'; |
72
|
|
|
$header[1] = 'Count'; |
73
|
|
|
|
74
|
|
|
return [ |
75
|
|
|
'header' => $header, |
76
|
|
|
'data' => $data, |
77
|
|
|
'error' => 0, |
78
|
|
|
]; |
79
|
|
|
} |
80
|
|
|
|
81
|
|
|
/** |
82
|
|
|
* template for options & settings |
83
|
|
|
* |
84
|
|
|
* @NoAdminRequired |
85
|
|
|
* @return array |
86
|
|
|
*/ |
87
|
|
|
public function getTemplate() |
88
|
|
|
{ |
89
|
|
|
$template = array(); |
90
|
|
|
array_push($template, ['id' => 'user', 'name' => 'GitHub Username', 'placeholder' => 'GitHub user']); |
91
|
|
|
array_push($template, ['id' => 'repository', 'name' => 'Repository', 'placeholder' => 'GitHub repository']); |
92
|
|
|
array_push($template, ['id' => 'limit', 'name' => 'Limit', 'placeholder' => 'Number of records']); |
93
|
|
|
array_push($template, ['id' => 'timestamp', 'name' => 'Timestamp of dataload', 'placeholder' => 'true/false']); |
94
|
|
|
return $template; |
95
|
|
|
} |
96
|
|
|
|
97
|
|
|
private function floatvalue($val) |
98
|
|
|
{ |
99
|
|
|
$val = str_replace(",", ".", $val); |
100
|
|
|
$val = preg_replace('/\.(?=.*\.)/', '', $val); |
101
|
|
|
$val = preg_replace('/[^0-9-.]+/', '', $val); |
102
|
|
|
if (is_numeric($val)) { |
103
|
|
|
return floatval($val); |
104
|
|
|
} else { |
105
|
|
|
return false; |
106
|
|
|
} |
107
|
|
|
} |
108
|
|
|
} |