Passed
Push — master ( 4aa385...802d29 )
by Marcel
02:44
created

Regex::backup()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 2
Code Lines 0

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
eloc 0
c 0
b 0
f 0
nc 1
nop 0
dl 0
loc 2
rs 10
1
<?php
2
/**
3
 * 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 2019-2022 Marcel Scherello
10
 */
11
12
namespace OCA\Analytics\Datasource;
13
14
use OCP\IL10N;
15
use Psr\Log\LoggerInterface;
16
17
class Regex implements IDatasource
18
{
19
    private $logger;
20
    private $l10n;
21
22
    public function __construct(
23
        IL10N $l10n,
24
        LoggerInterface $logger
25
    )
26
    {
27
        $this->l10n = $l10n;
28
        $this->logger = $logger;
29
    }
30
31
    /**
32
     * @return string Display Name of the datasource
33
     */
34
    public function getName(): string
35
    {
36
        return $this->l10n->t('HTML grabber');
37
    }
38
39
    /**
40
     * @return int digit unique datasource id
41
     */
42
    public function getId(): int
43
    {
44
        return 5;
45
    }
46
47
    /**
48
     * @return array available options of the datasoure
49
     */
50
    public function getTemplate(): array
51
    {
52
        $template = array();
53
        array_push($template, ['id' => 'url', 'name' => 'URL', 'placeholder' => 'url']);
54
        array_push($template, ['id' => 'name', 'name' => 'Data series description', 'placeholder' => 'optional']);
55
        array_push($template, ['id' => 'regex', 'name' => $this->l10n->t('valid regex'), 'placeholder' => '//']);
56
        array_push($template, ['id' => 'limit', 'name' => $this->l10n->t('Limit'), 'placeholder' => $this->l10n->t('Number of rows')]);
57
        array_push($template, ['id' => 'timestamp', 'name' => $this->l10n->t('Timestamp of data load'), 'placeholder' => $this->l10n->t('true/false'), 'type' => 'tf']);
58
        return $template;
59
    }
60
61
    /**
62
     * Read the Data
63
     * @param $option
64
     * @return array available options of the datasoure
65
     */
66
    public function readData($option): array
67
    {
68
        $regex = htmlspecialchars_decode($option['regex'], ENT_NOQUOTES);
69
        $url = htmlspecialchars_decode($option['url'], ENT_NOQUOTES);
70
71
        $context = stream_context_create(
72
            array(
73
                "http" => array(
74
                    "header" => "User-Agent: NextCloud Analytics APP"
75
                )
76
            )
77
        );
78
79
        $html = file_get_contents($url, false, $context);
80
        preg_match_all($regex, $html, $matches);
81
82
        $data = array();
83
        $count = count($matches['dimension']);
84
        for ($i = 0; $i < $count; $i++) {
85
            if (isset($option['limit'])) {
86
                if ($i === (int)$option['limit'] AND (int)$option['limit'] !== 0) break;
87
            }
88
            array_push($data, [$option['name'], $matches['dimension'][$i], $matches['value'][$i]]);
89
        }
90
91
        $header = array();
92
        $header[0] = '';
93
        $header[1] = 'Dimension2';
94
        $header[2] = 'Count';
95
96
        return [
97
            'header' => $header,
98
            'dimensions' => array_slice($header, 0, count($header) - 1),
99
            'data' => $data,
100
            'error' => 0,
101
            'rawData' => $html,
102
            'URL' => $url,
103
        ];
104
    }
105
}
106