Code Coverage
 
Lines
Functions and Methods
Classes and Traits
Total
93.62% covered (success)
93.62%
44 / 47
66.67% covered (warning)
66.67%
2 / 3
CRAP
0.00% covered (danger)
0.00%
0 / 1
EventBusWeightedTagsUpdater
93.62% covered (success)
93.62%
44 / 47
66.67% covered (warning)
66.67%
2 / 3
11.03
0.00% covered (danger)
0.00%
0 / 1
 __construct
0.00% covered (danger)
0.00%
0 / 3
0.00% covered (danger)
0.00%
0 / 1
2
 resetWeightedTags
100.00% covered (success)
100.00%
18 / 18
100.00% covered (success)
100.00%
1 / 1
4
 updateWeightedTags
100.00% covered (success)
100.00%
26 / 26
100.00% covered (success)
100.00%
1 / 1
6
1<?php
2
3namespace CirrusSearch;
4
5use CirrusSearch\Extra\MultiList\MultiListBuilder;
6use Exception;
7use MediaWiki\Extension\EventBus\EventBusFactory;
8use MediaWiki\Page\ProperPageIdentity;
9use MediaWiki\Page\WikiPageFactory;
10
11class EventBusWeightedTagsUpdater implements WeightedTagsUpdater {
12
13    private EventBusFactory $eventBusFactory;
14    private EventBusWeightedTagSerializer $eventSerializer;
15    private WikiPageFactory $wikiPageFactory;
16
17    /**
18     * @param EventBusFactory $eventBusFactory
19     * @param EventBusWeightedTagSerializer $eventSerializer
20     * @param WikiPageFactory $wikiPageFactory
21     */
22    public function __construct(
23        EventBusFactory $eventBusFactory,
24        EventBusWeightedTagSerializer $eventSerializer,
25        WikiPageFactory $wikiPageFactory
26    ) {
27        $this->eventBusFactory = $eventBusFactory;
28        $this->eventSerializer = $eventSerializer;
29        $this->wikiPageFactory = $wikiPageFactory;
30    }
31
32    /**
33     * @inheritDoc
34     */
35    public function resetWeightedTags(
36        ProperPageIdentity $page,
37        array $tagPrefixes,
38        ?string $trigger = null
39    ): void {
40        $event = $this->eventSerializer->toClearEvent(
41            $this->wikiPageFactory->newFromTitle( $page ),
42            $tagPrefixes,
43            $trigger === 'revision' ? true : null
44        );
45        try {
46            $outcome = $this->eventBusFactory->getInstanceForStream( EventBusWeightedTagSerializer::STREAM )->send(
47                $event
48            );
49        } catch ( Exception $e ) {
50            throw new WeightedTagsException(
51                "Failed to send reset: {msg}",
52                [ 'msg' => $e->getMessage() ], $e
53            );
54        }
55        if ( $outcome !== true ) {
56            throw new WeightedTagsException(
57                "Failed to send reset: {msg}",
58                [ 'msg' => var_export( $outcome, true ) ]
59            );
60        }
61    }
62
63    /**
64     * @inheritDoc
65     */
66    public function updateWeightedTags(
67        ProperPageIdentity $page,
68        string $tagPrefix,
69        ?array $tagWeights = null,
70        ?string $trigger = null
71    ): void {
72        $weightedTags = MultiListBuilder::buildWeightedTags( $tagPrefix, $tagWeights );
73
74        $set = array_reduce( $weightedTags, static function ( $set, $weightedTag ) {
75            if ( !isset( $set[$weightedTag->getPrefix()] ) ) {
76                $set[$weightedTag->getPrefix()] = [];
77            }
78            $mappedWeightedTag = [ 'tag' => $weightedTag->getName() ];
79            $weight = $weightedTag->getWeight();
80            if ( $weight !== null ) {
81                $mappedWeightedTag['score'] = $weight / 1000;
82            }
83            $set[$weightedTag->getPrefix()][] = $mappedWeightedTag;
84            return $set;
85        }, [] );
86
87        $event = $this->eventSerializer->toSetEvent(
88            $this->wikiPageFactory->newFromTitle( $page ), $set, $trigger === 'revision' ? true : null );
89
90        try {
91            $outcome = $this->eventBusFactory->getInstanceForStream( EventBusWeightedTagSerializer::STREAM )->send(
92                $event
93            );
94        } catch ( Exception $e ) {
95            throw new WeightedTagsException(
96                "Failed to send update: {msg}",
97                [ 'msg' => $e->getMessage() ], $e
98            );
99        }
100        if ( $outcome !== true ) {
101            throw new WeightedTagsException(
102                "Failed to send update: {msg}",
103                [ 'msg' => var_export( $outcome, true ) ]
104            );
105        }
106    }
107
108}