Issues (496)

lib/Datasource/Regex.php (2 issues)

Labels
Severity
1
<?php
2
/**
3
 * Analytics
4
 *
5
 * SPDX-FileCopyrightText: 2019-2022 Marcel Scherello
6
 * SPDX-License-Identifier: AGPL-3.0-or-later
7
 */
8
9
namespace OCA\Analytics\Datasource;
10
11
use OCP\IL10N;
0 ignored issues
show
The type OCP\IL10N was not found. Maybe you did not declare it correctly or list all dependencies?

The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g. excluded_paths: ["lib/*"], you can move it to the dependency path list as follows:

filter:
    dependency_paths: ["lib/*"]

For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths

Loading history...
12
use Psr\Log\LoggerInterface;
0 ignored issues
show
The type Psr\Log\LoggerInterface was not found. Maybe you did not declare it correctly or list all dependencies?

The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g. excluded_paths: ["lib/*"], you can move it to the dependency path list as follows:

filter:
    dependency_paths: ["lib/*"]

For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths

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