PluginProbe ʕ •ᴥ•ʔ
Matomo Analytics – Powerful, Privacy-First Insights for WordPress / trunk
Matomo Analytics – Powerful, Privacy-First Insights for WordPress vtrunk
5.11.1 5.11.0 5.10.2 5.10.1 trunk 1.0.2 1.0.3 1.0.4 1.0.5 1.0.6 1.1.0 1.1.1 1.1.2 1.1.3 1.2.0 1.3.0 1.3.1 1.3.2 4.0.0 4.0.1 4.0.2 4.0.3 4.0.4 4.1.0 4.1.1 4.1.2 4.1.3 4.10.0 4.11.0 4.12.0 4.13.0 4.13.2 4.13.3 4.13.4 4.13.5 4.14.0 4.14.1 4.14.2 4.15.0 4.15.1 4.15.2 4.15.3 4.2.0 4.3.0 4.3.1 4.4.1 4.4.2 4.5.0 4.6.0 5.0.1 5.0.2 5.0.3 5.0.4 5.0.5 5.0.6 5.0.7 5.0.8 5.1.0 5.1.1 5.1.2 5.1.3 5.1.4 5.1.5 5.1.6 5.1.7 5.10.0 5.2.0 5.2.1 5.2.2 5.3.0 5.3.1 5.3.2 5.3.3 5.6.0 5.6.1 5.7.0 5.7.1 5.8.0 5.8.1 5.8.2
matomo / app / core / Plugin / Segment.php
matomo / app / core / Plugin Last commit date
ConsoleCommand 1 month ago Dimension 1 month ago API.php 6 months ago AggregatedMetric.php 2 years ago ArchivedMetric.php 1 year ago Archiver.php 1 month ago Categories.php 2 years ago ComponentFactory.php 3 months ago ComputedMetric.php 1 year ago ConsoleCommand.php 1 month ago Controller.php 1 month ago ControllerAdmin.php 2 weeks ago Dependency.php 1 month ago LogTablesProvider.php 2 years ago Manager.php 1 month ago Menu.php 1 month ago MetadataLoader.php 1 month ago Metric.php 1 month ago PluginException.php 1 year ago ProcessedMetric.php 3 months ago ReleaseChannels.php 3 months ago Report.php 1 month ago ReportsProvider.php 2 years ago RequestProcessors.php 4 months ago Segment.php 3 months ago SettingsProvider.php 1 month ago Tasks.php 1 month ago ThemeStyles.php 2 weeks ago ViewDataTable.php 3 months ago Visualization.php 1 year ago WidgetsProvider.php 3 months ago
Segment.php
401 lines
1 <?php
2
3 /**
4 * Matomo - free/libre analytics platform
5 *
6 * @link https://matomo.org
7 * @license https://www.gnu.org/licenses/gpl-3.0.html GPL v3 or later
8 */
9 namespace Piwik\Plugin;
10
11 use Exception;
12 use Piwik\Columns\Dimension;
13 use Piwik\Development;
14 /**
15 * Creates a new segment that can be used for instance within the {@link \Piwik\Columns\Dimension::configureSegment()}
16 * method. Make sure to set at least the following values: {@link setName()}, {@link setSegment()},
17 * {@link setSqlSegment()}, {@link setType()} and {@link setCategory()}. If you are using a segment in the context of a
18 * dimension the type and the SQL segment is usually set for you automatically.
19 *
20 * Example:
21 * ```
22 $segment = new \Piwik\Plugin\Segment();
23 $segment->setType(\Piwik\Plugin\Segment::TYPE_DIMENSION);
24 $segment->setName('General_EntryKeyword');
25 $segment->setCategory('General_Visit');
26 $segment->setSegment('entryKeyword');
27 $segment->setSqlSegment('log_visit.entry_keyword');
28 $segment->setAcceptedValues('Any keywords people search for on your website such as "help" or "imprint"');
29 ```
30 * @api
31 * @since 2.5.0
32 */
33 class Segment
34 {
35 /**
36 * Segment type 'dimension'. Can be used along with {@link setType()}.
37 * @api
38 */
39 public const TYPE_DIMENSION = 'dimension';
40 /**
41 * Segment type 'metric'. Can be used along with {@link setType()}.
42 * @api
43 */
44 public const TYPE_METRIC = 'metric';
45 private $type;
46 private $category;
47 private $name;
48 private $segment;
49 private $sqlSegment;
50 private $sqlFilter;
51 private $sqlFilterValue;
52 private $acceptValues;
53 private $permission;
54 private $suggestedValuesCallback;
55 private $unionOfSegments;
56 private $isInternalSegment = \false;
57 private $suggestedValuesApi = '';
58 private $needsMostFrequentValues = \true;
59 /**
60 * If true, this segment will only be visible to a registered user (see API.getSegmentsMetadata).
61 *
62 * @var bool
63 */
64 private $requiresRegisteredUser = \false;
65 /**
66 * @ignore
67 */
68 public final function __construct()
69 {
70 $this->init();
71 }
72 /**
73 * @var Dimension
74 */
75 public $dimension;
76 /**
77 * Here you can initialize this segment and set any default values. It is called directly after the object is
78 * created.
79 * @api
80 */
81 protected function init()
82 {
83 }
84 /**
85 * Here you should explain which values are accepted/useful for your segment, for example:
86 * "1, 2, 3, etc." or "comcast.net, proxad.net, etc.". If the value needs any special encoding you should mention
87 * this as well. For example "Any URL including protocol. The URL must be URL encoded."
88 *
89 * @param string $acceptedValues
90 * @api
91 */
92 public function setAcceptedValues($acceptedValues)
93 {
94 $this->acceptValues = $acceptedValues;
95 }
96 /**
97 * Set (overwrite) the category this segment belongs to. It should be a translation key such as 'General_Actions'
98 * or 'General_Visit'.
99 * @param string $category
100 * @api
101 */
102 public function setCategory($category)
103 {
104 $this->category = $category;
105 }
106 /**
107 * Set (overwrite) the segment display name. This name will be visible in the API and the UI. It should be a
108 * translation key such as 'Actions_ColumnEntryPageTitle' or 'Resolution_ColumnResolution'.
109 * @param string $name
110 * @api
111 */
112 public function setName($name)
113 {
114 $this->name = $name;
115 }
116 /**
117 * Set (overwrite) the name of the segment. The name should be lower case first and has to be unique. The segment
118 * name defined here needs to be set in the URL to actually apply this segment. Eg if the segment is 'searches'
119 * you need to set "&segment=searches>0" in the UI.
120 * @param string $segment
121 * @api
122 */
123 public function setSegment($segment)
124 {
125 $this->segment = $segment;
126 $this->check();
127 }
128 /**
129 * Sometimes you want users to set values that differ from the way they are actually stored. For instance if you
130 * want to allow to filter by any URL than you might have to resolve this URL to an action id. Or a country name
131 * maybe has to be mapped to a 2 letter country code. You can do this by specifying either a callable such as
132 * `array('Classname', 'methodName')` or by passing a closure. There will be four values passed to the given closure
133 * or callable: `string $valueToMatch`, `string $segment` (see {@link setSegment()}), `string $matchType`
134 * (eg SegmentExpression::MATCH_EQUAL or any other match constant of this class) and `$segmentName`.
135 *
136 * If the closure returns NULL, then Piwik assumes the segment sub-string will not match any visitor.
137 *
138 * @param callable $sqlFilter
139 * @api
140 */
141 public function setSqlFilter($sqlFilter)
142 {
143 $this->sqlFilter = $sqlFilter;
144 }
145 /**
146 * Similar to {@link setSqlFilter()} you can map a given segment value to another value. For instance you could map
147 * "new" to 0, 'returning' to 1 and any other value to '2'. You can either define a callable or a closure. There
148 * will be only one value passed to the closure or callable which contains the value a user has set for this
149 * segment. This callback is called shortly before {@link setSqlFilter()}.
150 * @param string|array $sqlFilterValue
151 * @api
152 */
153 public function setSqlFilterValue($sqlFilterValue)
154 {
155 $this->sqlFilterValue = $sqlFilterValue;
156 }
157 /**
158 * Defines to which column in the MySQL database the segment belongs: 'mytablename.mycolumnname'. Eg
159 * 'log_visit.idsite'. When a segment is applied the given or filtered value will be compared with this column.
160 *
161 * @param string $sqlSegment
162 * @api
163 */
164 public function setSqlSegment($sqlSegment)
165 {
166 $this->sqlSegment = $sqlSegment;
167 $this->check();
168 }
169 /**
170 * Set a list of segments that should be used instead of fetching the values from a single column.
171 * All set segments will be applied via an OR operator.
172 *
173 * @param array $segments
174 * @api
175 */
176 public function setUnionOfSegments($segments)
177 {
178 $this->unionOfSegments = $segments;
179 $this->check();
180 }
181 /**
182 * @return array
183 * @ignore
184 */
185 public function getUnionOfSegments()
186 {
187 return $this->unionOfSegments;
188 }
189 /**
190 * @return string
191 * @ignore
192 */
193 public function getSqlSegment()
194 {
195 return $this->sqlSegment;
196 }
197 /**
198 * @return string
199 * @ignore
200 */
201 public function getSqlFilterValue()
202 {
203 return $this->sqlFilterValue;
204 }
205 /**
206 * @return string
207 * @ignore
208 */
209 public function getAcceptValues()
210 {
211 return $this->acceptValues;
212 }
213 /**
214 * @return string
215 * @ignore
216 */
217 public function getSqlFilter()
218 {
219 return $this->sqlFilter;
220 }
221 /**
222 * Set (overwrite) the type of this segment which is usually either a 'dimension' or a 'metric'.
223 * @param string $type See constants TYPE_*
224 * @api
225 */
226 public function setType($type)
227 {
228 $this->type = $type;
229 }
230 /**
231 * @return string
232 * @ignore
233 */
234 public function getType()
235 {
236 return $this->type;
237 }
238 /**
239 * @return string
240 * @ignore
241 */
242 public function getName()
243 {
244 return $this->name;
245 }
246 /**
247 * @return string
248 * @ignore
249 */
250 public function getCategoryId()
251 {
252 return $this->category;
253 }
254 /**
255 * Returns the name of this segment as it should appear in segment expressions.
256 *
257 * @return string
258 */
259 public function getSegment()
260 {
261 return $this->segment;
262 }
263 /**
264 * @return string
265 * @ignore
266 */
267 public function getSuggestedValuesCallback()
268 {
269 return $this->suggestedValuesCallback;
270 }
271 /**
272 * Set callback which will be executed when user will call for suggested values for segment.
273 *
274 * @param callable $suggestedValuesCallback
275 */
276 public function setSuggestedValuesCallback($suggestedValuesCallback)
277 {
278 $this->suggestedValuesCallback = $suggestedValuesCallback;
279 }
280 /**
281 * @return string
282 * @ignore
283 */
284 public function getSuggestedValuesApi()
285 {
286 return $this->suggestedValuesApi;
287 }
288 /**
289 * Set callback which will be executed when user will call for suggested values for segment.
290 *
291 * @param string $suggestedValuesApi
292 */
293 public function setSuggestedValuesApi($suggestedValuesApi)
294 {
295 if (!empty($suggestedValuesApi) && is_string($suggestedValuesApi)) {
296 if (Development::isEnabled() && strpos($suggestedValuesApi, '.get') === \false) {
297 throw new Exception('Invalid suggested values API defined, expecting ".get" to be present.');
298 }
299 } else {
300 $suggestedValuesApi = '';
301 }
302 $this->suggestedValuesApi = $suggestedValuesApi;
303 }
304 public function setNeedsMostFrequentValues(bool $value)
305 {
306 $this->needsMostFrequentValues = $value;
307 }
308 /**
309 * You can restrict the access to this segment by passing a boolean `false`. For instance if you want to make
310 * a certain segment only available to users having super user access you could do the following:
311 * `$segment->setPermission(Piwik::hasUserSuperUserAccess());`
312 * @param bool $permission
313 * @api
314 */
315 public function setPermission($permission)
316 {
317 $this->permission = $permission;
318 }
319 /**
320 * @return array
321 * @ignore
322 */
323 public function toArray()
324 {
325 $segment = array('type' => $this->type, 'category' => $this->category, 'name' => $this->name, 'segment' => $this->segment, 'sqlSegment' => $this->sqlSegment, 'needsMostFrequentValues' => $this->needsMostFrequentValues);
326 if (!empty($this->unionOfSegments)) {
327 $segment['unionOfSegments'] = $this->unionOfSegments;
328 }
329 if (!empty($this->sqlFilter)) {
330 $segment['sqlFilter'] = $this->sqlFilter;
331 }
332 if (!empty($this->sqlFilterValue)) {
333 $segment['sqlFilterValue'] = $this->sqlFilterValue;
334 }
335 if (!empty($this->acceptValues)) {
336 $segment['acceptedValues'] = $this->acceptValues;
337 }
338 if (isset($this->permission)) {
339 $segment['permission'] = $this->permission;
340 }
341 if (is_callable($this->suggestedValuesCallback)) {
342 $segment['suggestedValuesCallback'] = $this->suggestedValuesCallback;
343 }
344 if (is_string($this->suggestedValuesApi) && !empty($this->suggestedValuesApi)) {
345 $segment['suggestedValuesApi'] = $this->suggestedValuesApi;
346 }
347 return $segment;
348 }
349 /**
350 * Returns true if this segment should only be visible to registered users (see API.getSegmentsMetadata),
351 * false if it should always be visible to any user (even the anonymous user).
352 *
353 * @return boolean
354 * @ignore
355 */
356 public function isRequiresRegisteredUser()
357 {
358 return $this->requiresRegisteredUser;
359 }
360 /**
361 * Sets whether the segment should only be visible to registered users. If set to false it will be even visible to
362 * the anonymous user
363 *
364 * @param boolean $requiresRegisteredUser
365 * @ignore
366 */
367 public function setRequiresRegisteredUser($requiresRegisteredUser)
368 {
369 $this->requiresRegisteredUser = $requiresRegisteredUser;
370 }
371 /**
372 * Sets whether the segment is for internal use only and should not be visible in the UI or in API metadata output.
373 * These types of segments are, for example, used in unions for other segments, but have no value to users.
374 *
375 * @param bool $value
376 */
377 public function setIsInternal($value)
378 {
379 $this->isInternalSegment = $value;
380 }
381 /**
382 * Gets whether the segment is for internal use only and should not be visible in the UI or in API metadata output.
383 * These types of segments are, for example, used in unions for other segments, but have no value to users.
384 *
385 * @return bool
386 */
387 public function isInternal()
388 {
389 return $this->isInternalSegment;
390 }
391 private function check()
392 {
393 if ($this->sqlSegment && $this->unionOfSegments) {
394 throw new Exception(sprintf('Union of segments and SQL segment is set for segment "%s", use only one of them', $this->name));
395 }
396 if ($this->segment && $this->unionOfSegments && in_array($this->segment, $this->unionOfSegments, \true)) {
397 throw new Exception(sprintf('The segment %s contains a union segment to itself', $this->name));
398 }
399 }
400 }
401