File: AIAssistant.php

package info (click to toggle)
matomo 5.8.0-1
  • links: PTS, VCS
  • area: main
  • in suites: sid
  • size: 95,068 kB
  • sloc: php: 289,425; xml: 127,249; javascript: 112,130; python: 202; sh: 178; makefile: 20; sql: 10
file content (221 lines) | stat: -rw-r--r-- 6,208 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
<?php

/**
 * Matomo - free/libre analytics platform
 *
 * @link    https://matomo.org
 * @license https://www.gnu.org/licenses/gpl-3.0.html GPL v3 or later
 */

namespace Piwik\Plugins\Referrers;

use Piwik\Cache;
use Piwik\Common;
use Piwik\Config;
use Piwik\Option;
use Piwik\Piwik;
use Piwik\SettingsPiwik;
use Piwik\Singleton;

/**
 * Contains methods to access AI assistant definition data.
 */
class AIAssistant extends Singleton
{
    public const OPTION_STORAGE_NAME = 'AIAssistantDefinitions';

    /** @var string location of definition file (relative to PIWIK_INCLUDE_PATH) */
    public const DEFINITION_FILE = '/vendor/matomo/searchengine-and-social-list/AIAssistants.yml';

    /** @var null|array<string, string> */
    protected $definitionList = null;

    /**
     * Returns list of AI assistants by URL
     *
     * @return array<string, string>
     */
    public function getDefinitions(): array
    {
        $cache = Cache::getEagerCache();
        $cacheId = 'AIAssistant-' . self::OPTION_STORAGE_NAME;

        if ($cache->contains($cacheId)) {
            /** @var array<string, string> $list */
            $list = $cache->fetch($cacheId);
        } else {
            $list = $this->loadDefinitions();
            $cache->save($cacheId, $list);
        }

        return $list;
    }

    /**
     * @return array<string, string>
     */
    private function loadDefinitions(): array
    {
        if ($this->definitionList === null) {
            $referrerDefinitionSyncOpt = Config::getInstance()->General['enable_referrer_definition_syncs'];

            if ($referrerDefinitionSyncOpt == 1) {
                $this->loadRemoteDefinitions();
            } else {
                $this->loadLocalYmlData();
            }
        }

        Piwik::postEvent('Referrer.addAIAssistantUrls', [&$this->definitionList]);

        return $this->definitionList ?? [];
    }

    /**
     * Loads definitions sourced from remote yaml with a local fallback
     */
    private function loadRemoteDefinitions(): void
    {
        // Read first from the auto-updated list in database
        $list = Option::get(self::OPTION_STORAGE_NAME);

        if ($list && SettingsPiwik::isInternetEnabled()) {
            $list = Common::safe_unserialize(base64_decode($list));
            if (!empty($list) && is_array($list)) {
                $this->definitionList = $list;
            }
        } else {
            // Fallback to reading the bundled list
            $this->loadLocalYmlData();
            Option::set(self::OPTION_STORAGE_NAME, base64_encode(serialize($this->definitionList)));
        }
    }

    /**
     * Loads the definition data from the local definitions file
     */
    private function loadLocalYmlData(): void
    {
        $yml = file_get_contents(PIWIK_INCLUDE_PATH . self::DEFINITION_FILE);
        if ($yml !== false) {
            $this->definitionList = $this->loadYmlData($yml);
        }
    }

    /**
     * Parses the given YML string and caches the resulting definitions
     *
     * @return null|array<string, string>
     */
    public function loadYmlData(string $yml): ?array
    {
        $ais = \Spyc::YAMLLoadString($yml);

        if (is_array($ais)) {
            $this->definitionList = $this->transformData($ais);
        }

        return $this->definitionList;
    }

    /**
     * @param array<string, string[]> $ais
     * @return array<string, string>
     */
    protected function transformData(array $ais): array
    {
        $urlToName = [];
        foreach ($ais as $name => $urls) {
            if (empty($urls) || !is_array($urls)) {
                continue;
            }

            foreach ($urls as $url) {
                $urlToName[$url] = $name;
            }
        }
        return $urlToName;
    }

    /**
     * Returns true if a URL belongs to an AI assistant, false if otherwise.
     *
     * @param string $url The URL to check.
     * @param string|null $aiAssistantName The name of the AI assistant to check for, or false to check for any.
     */
    public function isAIAssistantUrl(string $url, ?string $aiAssistantName = null): bool
    {
        foreach ($this->getDefinitions() as $domain => $name) {
            if (preg_match('#(^|[\.\/])' . preg_quote($domain) . '(\/|$)#', $url) && ($aiAssistantName === null || $name === $aiAssistantName)) {
                return true;
            }
        }

        return false;
    }


    /**
     * Gets AI assistant name from URL.
     */
    public function getAIAssistantFromDomain(string $url): string
    {
        foreach ($this->getDefinitions() as $domain => $name) {
            if (preg_match('#(^|[\.\/])' . preg_quote($domain) . '(\/|$)#', $url)) {
                return $name;
            }
        }

        return Piwik::translate('General_Unknown');
    }

    /**
     * Returns the main url of the AI assistant the given url matches
     */
    public function getMainUrl(string $url): string
    {
        $ai  = $this->getAIAssistantFromDomain($url);
        foreach ($this->getDefinitions() as $domain => $name) {
            if ($name === $ai) {
                return $domain;
            }
        }
        return $url;
    }

    /**
     * Returns the main url of the given AI assistant
     */
    public function getMainUrlFromName(string $aiAssistant): ?string
    {
        foreach ($this->getDefinitions() as $domain => $name) {
            if ($name === $aiAssistant) {
                return $domain;
            }
        }
        return null;
    }


    /**
     * Return AI assistant logo path by URL
     *
     * @see plugins/Morpheus/icons/dist/aiAssistants/
     */
    public function getLogoFromUrl(string $url): string
    {
        $ai = $this->getAIAssistantFromDomain($url);
        $ais = $this->getDefinitions();

        $filePattern = 'plugins/Morpheus/icons/dist/aiAssistants/%s.png';

        $aiDomains = array_keys($ais, $ai);
        foreach ($aiDomains as $domain) {
            if (file_exists(PIWIK_INCLUDE_PATH . '/' . sprintf($filePattern, $domain))) {
                return sprintf($filePattern, $domain);
            }
        }

        return sprintf($filePattern, 'xx');
    }
}