Code Coverage |
||||||||||
Lines |
Functions and Methods |
Classes and Traits |
||||||||
Total | |
46.67% |
14 / 30 |
|
75.00% |
3 / 4 |
CRAP | |
0.00% |
0 / 1 |
EventSerializer | |
46.67% |
14 / 30 |
|
75.00% |
3 / 4 |
21.29 | |
0.00% |
0 / 1 |
__construct | |
100.00% |
3 / 3 |
|
100.00% |
1 / 1 |
1 | |||
timestampToDt | |
100.00% |
1 / 1 |
|
100.00% |
1 / 1 |
1 | |||
createEvent | |
0.00% |
0 / 16 |
|
0.00% |
0 / 1 |
30 | |||
createMeta | |
100.00% |
10 / 10 |
|
100.00% |
1 / 1 |
2 |
1 | <?php |
2 | /** |
3 | * This program is free software; you can redistribute it and/or modify |
4 | * it under the terms of the GNU General Public License as published by |
5 | * the Free Software Foundation; either version 2 of the License, or |
6 | * (at your option) any later version. |
7 | * |
8 | * This program is distributed in the hope that it will be useful, |
9 | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
10 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
11 | * GNU General Public License for more details. |
12 | * |
13 | * You should have received a copy of the GNU General Public License along |
14 | * with this program; if not, write to the Free Software Foundation, Inc., |
15 | * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. |
16 | * http://www.gnu.org/copyleft/gpl.html |
17 | * |
18 | * @file |
19 | * @author Andrew Otto <otto@wikimedia.org> |
20 | */ |
21 | namespace MediaWiki\Extension\EventBus\Serializers; |
22 | |
23 | use MediaWiki\Config\Config; |
24 | use MediaWiki\Http\Telemetry; |
25 | use MediaWiki\WikiMap\WikiMap; |
26 | use Wikimedia\UUID\GlobalIdGenerator; |
27 | |
28 | /** |
29 | * EventSerializer should be used to create an event array suitable |
30 | * for producing to WMF's Event Platform, usually via EventGate. |
31 | */ |
32 | class EventSerializer { |
33 | /** |
34 | * @var Config |
35 | */ |
36 | private Config $mainConfig; |
37 | /** |
38 | * @var GlobalIdGenerator |
39 | */ |
40 | private GlobalIdGenerator $globalIdGenerator; |
41 | |
42 | private Telemetry $telemetry; |
43 | |
44 | /** |
45 | * @param Config $mainConfig |
46 | * @param GlobalIdGenerator $globalIdGenerator |
47 | * @param Telemetry $telemetry |
48 | */ |
49 | public function __construct( |
50 | Config $mainConfig, |
51 | GlobalIdGenerator $globalIdGenerator, |
52 | Telemetry $telemetry |
53 | ) { |
54 | $this->mainConfig = $mainConfig; |
55 | $this->globalIdGenerator = $globalIdGenerator; |
56 | $this->telemetry = $telemetry; |
57 | } |
58 | |
59 | /** |
60 | * Format a timestamp for a date-time attribute in an event in ISO_8601 format. |
61 | * |
62 | * @param string|null $timestamp Timestamp, in a format supported by wfTimestamp(), or null for current timestamp. |
63 | * @return string |
64 | */ |
65 | public static function timestampToDt( ?string $timestamp = null ): string { |
66 | return wfTimestamp( TS_ISO_8601, $timestamp ); |
67 | } |
68 | |
69 | /** |
70 | * Adds a meta subobject to $eventAttrs based on uri and stream. |
71 | * |
72 | * @param string $schema |
73 | * Schema URI for '$schema' field. |
74 | * |
75 | * @param string $stream |
76 | * Name of stream for meta.stream field. |
77 | * |
78 | * @param string $uri |
79 | * Uri of this entity for meta.uri field. |
80 | * |
81 | * @param array $eventAttrs |
82 | * Additional event attributes to include. |
83 | * |
84 | * @param string|null $wikiId |
85 | * wikiId to use for meta.domain. If null ServerName will be used. |
86 | * |
87 | * @param bool|string|null $ingestionTimestamp |
88 | * If true, meta.dt will be set to the current timestamp. |
89 | * If a string, meta.dt will be set to value of timestampToDt($timestampToDt). |
90 | * If null, meta.dt will not be set. |
91 | * It is preferred to leave this value at null, |
92 | * as EventBus produces through EventGate, which will handle setting meta.dt |
93 | * to its ingestion time. |
94 | * See: https://phabricator.wikimedia.org/T267648 |
95 | * |
96 | * @return array $eventAttrs + $schema + meta sub object |
97 | */ |
98 | public function createEvent( |
99 | string $schema, |
100 | string $stream, |
101 | string $uri, |
102 | array $eventAttrs, |
103 | ?string $wikiId = null, |
104 | $ingestionTimestamp = null |
105 | ): array { |
106 | // If $wikiId is provided, and we can get a $wikiRef, then use $wikiRef->getDisplayName(). |
107 | // Else just use ServerName. |
108 | $domain = $this->mainConfig->get( 'ServerName' ); |
109 | $wikiRef = $wikiId ? WikiMap::getWiki( $wikiId ) : null; |
110 | if ( $wikiRef ) { |
111 | $domain = $wikiRef->getDisplayName(); |
112 | } |
113 | |
114 | $metaDt = null; |
115 | if ( $ingestionTimestamp === true ) { |
116 | $metaDt = self::timestampToDt(); |
117 | } elseif ( $ingestionTimestamp !== false ) { |
118 | $metaDt = self::timestampToDt( $ingestionTimestamp ); |
119 | } |
120 | |
121 | return array_merge( |
122 | $eventAttrs, |
123 | [ |
124 | '$schema' => $schema, |
125 | 'meta' => $this->createMeta( $stream, $uri, $domain, $metaDt ) |
126 | ] |
127 | ); |
128 | } |
129 | |
130 | /** |
131 | * Creates the meta subobject that should be common for all events. |
132 | * |
133 | * @param string $stream |
134 | * @param string $uri |
135 | * @param string $domain |
136 | * @param string|null $dt |
137 | * If this is null, 'dt' will not be set. |
138 | * @return array |
139 | */ |
140 | private function createMeta( |
141 | string $stream, |
142 | string $uri, |
143 | string $domain, |
144 | ?string $dt = null |
145 | ): array { |
146 | $metaAttrs = [ |
147 | 'stream' => $stream, |
148 | 'uri' => $uri, |
149 | 'id' => $this->globalIdGenerator->newUUIDv4(), |
150 | 'request_id' => $this->telemetry->getRequestId(), |
151 | 'domain' => $domain, |
152 | ]; |
153 | |
154 | if ( $dt !== null ) { |
155 | $metaAttrs['dt'] = $dt; |
156 | } |
157 | |
158 | return $metaAttrs; |
159 | } |
160 | } |