Code Coverage |
||||||||||
Lines |
Functions and Methods |
Classes and Traits |
||||||||
Total | |
72.22% |
13 / 18 |
|
40.00% |
2 / 5 |
CRAP | |
0.00% |
0 / 1 |
EventBusWeightedTagSerializer | |
72.22% |
13 / 18 |
|
40.00% |
2 / 5 |
6.77 | |
0.00% |
0 / 1 |
__construct | |
0.00% |
0 / 3 |
|
0.00% |
0 / 1 |
2 | |||
getStream | |
0.00% |
0 / 1 |
|
0.00% |
0 / 1 |
2 | |||
toArray | |
100.00% |
12 / 12 |
|
100.00% |
1 / 1 |
2 | |||
toSetEvent | |
100.00% |
1 / 1 |
|
100.00% |
1 / 1 |
1 | |||
toClearEvent | |
0.00% |
0 / 1 |
|
0.00% |
0 / 1 |
2 |
1 | <?php |
2 | |
3 | namespace CirrusSearch; |
4 | |
5 | use MediaWiki\Extension\EventBus\Serializers\EventSerializer; |
6 | use MediaWiki\Extension\EventBus\Serializers\MediaWiki\PageEntitySerializer; |
7 | use MediaWiki\WikiMap\WikiMap; |
8 | use WikiPage; |
9 | |
10 | class EventBusWeightedTagSerializer { |
11 | public const SCHEMA = '/development/cirrussearch/page_weighted_tags_change/1.0.0'; |
12 | public const STREAM = 'mediawiki.cirrussearch.page_weighted_tags_change.rc0'; |
13 | |
14 | private EventSerializer $eventSerializer; |
15 | private PageEntitySerializer $pageEntitySerializer; |
16 | private string $stream; |
17 | |
18 | /** |
19 | * @param EventSerializer $eventSerializer |
20 | * @param PageEntitySerializer $pageEntitySerializer |
21 | * @param string $stream |
22 | */ |
23 | public function __construct( |
24 | EventSerializer $eventSerializer, |
25 | PageEntitySerializer $pageEntitySerializer, |
26 | string $stream = self::STREAM |
27 | ) { |
28 | $this->eventSerializer = $eventSerializer; |
29 | $this->pageEntitySerializer = $pageEntitySerializer; |
30 | $this->stream = $stream; |
31 | } |
32 | |
33 | /** |
34 | * @return string |
35 | */ |
36 | public function getStream(): string { |
37 | return $this->stream; |
38 | } |
39 | |
40 | /** |
41 | * @param WikiPage $wikiPage page to tag |
42 | * @param array $weightedTags `weighted_tags` payload, see schema |
43 | * @param bool|null $revBased `rev_based` flag |
44 | * @param string|null $dt event timestamp |
45 | * |
46 | * @return array encoded event |
47 | * @see self::SCHEMA |
48 | */ |
49 | public function toArray( WikiPage $wikiPage, array $weightedTags, ?bool $revBased = null, ?string $dt = null ): array { |
50 | $uri = $this->pageEntitySerializer->canonicalPageURL( $wikiPage ); |
51 | $page = $this->pageEntitySerializer->toArray( $wikiPage ); |
52 | return $this->eventSerializer->createEvent( |
53 | self::SCHEMA, |
54 | $this->stream, |
55 | $uri, |
56 | array_merge( $revBased === null ? [] : [ 'rev_based' => $revBased ], [ |
57 | 'dt' => EventSerializer::timestampToDt( $dt ), |
58 | 'wiki_id' => WikiMap::getCurrentWikiId(), |
59 | 'page' => $page, |
60 | 'weighted_tags' => $weightedTags |
61 | ] ) ); |
62 | } |
63 | |
64 | /** |
65 | * @param WikiPage $wikiPage |
66 | * @param array[] $set prefix => [ [ 'tag' => string ] | [ 'tag' => string, 'score' => int ] ] map |
67 | * @param bool|null $revBased `rev_based` flag |
68 | * @param string|null $dt event timestamp |
69 | * @return array |
70 | */ |
71 | public function toSetEvent( WikiPage $wikiPage, array $set, ?bool $revBased = null, ?string $dt = null ): array { |
72 | return $this->toArray( $wikiPage, [ 'set' => $set ], $revBased, $dt ); |
73 | } |
74 | |
75 | /** |
76 | * @param WikiPage $wikiPage |
77 | * @param string[] $clear prefixes to be cleared |
78 | * @param bool|null $revBased `rev_based` flag |
79 | * @param string|null $dt event timestamp |
80 | * @return array |
81 | */ |
82 | public function toClearEvent( WikiPage $wikiPage, array $clear, ?bool $revBased = null, ?string $dt = null ): array { |
83 | return $this->toArray( $wikiPage, [ 'clear' => $clear ], $revBased, $dt ); |
84 | } |
85 | } |