MCPcopy Create free account
hub / github.com/Rello/analytics / readData

Method readData

lib/Datasource/Regex.php:67–124  ·  view source on GitHub ↗

* Read the Data * @param $option * @return array available options of the data source */

($option)

Source from the content-addressed store, hash-verified

65 * @return array available options of the data source
66 */
67 public function readData($option): array
68 {
69 $regex = isset($option['regex']) ? htmlspecialchars_decode($option['regex'], ENT_NOQUOTES) : '';
70 $url = isset($option['url']) ? htmlspecialchars_decode($option['url'], ENT_NOQUOTES) : '';
71 $response = $this->httpClient->request($url, 'GET', ['User-Agent' => 'Nextcloud Analytics']);
72 if ($response['error'] !== null) {
73 return [
74 'header' => ['', 'Dimension2', 'Count'],
75 'dimensions' => ['', 'Dimension2'],
76 'data' => [],
77 'error' => $response['error'],
78 ];
79 }
80
81 // Fetch HTML through Nextcloud's SSRF-aware client.
82 $curlResult = $response['body'];
83 $httpCode = $response['status'];
84
85 $header = ['', 'Dimension2', 'Count'];
86
87 // Early return on HTTP error
88 if ($httpCode < 200 || $httpCode >= 300) {
89 return [
90 'header' => $header,
91 'dimensions' => ['', 'Dimension2'],
92 'data' => [],
93 'error' => 'HTTP response code: ' . $httpCode,
94 'rawData' => $curlResult,
95 'URL' => $url,
96 ];
97 }
98
99 // Apply regex and build data
100 preg_match_all($regex, $curlResult, $matches);
101
102 $dimensions = $matches['dimension'] ?? [];
103 $values = $matches['value'] ?? [];
104 $limit = isset($option['limit']) && (int)$option['limit'] > 0 ? (int)$option['limit'] : count($dimensions);
105
106 // Use array_map for efficient data construction
107 $data = [];
108 for ($i = 0; $i < min($limit, count($dimensions), count($values)); $i++) {
109 $data[] = [
110 $option['name'] ?? '',
111 $dimensions[$i],
112 $values[$i]
113 ];
114 }
115
116 return [
117 'header' => $header,
118 'dimensions' => array_slice($header, 0, count($header) - 1),
119 'data' => $data,
120 'error' => 0,
121 'rawData' => $curlResult,
122 'URL' => $url,
123 ];
124 }

Callers 2

readMethod · 0.45

Calls 1

requestMethod · 0.45

Tested by 1