Code Coverage |
||||||||||
Lines |
Functions and Methods |
Classes and Traits |
||||||||
Total | |
94.44% |
17 / 18 |
|
66.67% |
2 / 3 |
CRAP | |
0.00% |
0 / 1 |
EventBusRCFeedFormatter | |
94.44% |
17 / 18 |
|
66.67% |
2 / 3 |
7.01 | |
0.00% |
0 / 1 |
removeNulls | |
87.50% |
7 / 8 |
|
0.00% |
0 / 1 |
5.05 | |||
getLine | |
100.00% |
9 / 9 |
|
100.00% |
1 / 1 |
1 | |||
formatArray | |
100.00% |
1 / 1 |
|
100.00% |
1 / 1 |
1 |
1 | <?php |
2 | |
3 | namespace MediaWiki\Extension\EventBus\Adapters\RCFeed; |
4 | |
5 | use MediaWiki\Extension\EventBus\EventBus; |
6 | use MediaWiki\MediaWikiServices; |
7 | use MediaWiki\RCFeed\MachineReadableRCFeedFormatter; |
8 | use RecentChange; |
9 | |
10 | /** |
11 | * Augments the RecentChange object for use with the EventBus service, and then |
12 | * formats it into a JSON string. |
13 | * |
14 | * @extends MachineReadableRCFeedFormatter |
15 | */ |
16 | class EventBusRCFeedFormatter extends MachineReadableRCFeedFormatter { |
17 | /** |
18 | * Stream name to which this event belongs. |
19 | */ |
20 | public const STREAM = 'mediawiki.recentchange'; |
21 | |
22 | /** |
23 | * Removes properties which values are 'null' from the event. |
24 | * Will modify the original event passed in |
25 | * |
26 | * @param array $event the event to modify. |
27 | * @return array |
28 | */ |
29 | private static function removeNulls( $event ) { |
30 | if ( !is_array( $event ) ) { |
31 | return $event; |
32 | } |
33 | foreach ( $event as $key => $value ) { |
34 | if ( $value === null ) { |
35 | unset( $event[$key] ); |
36 | } elseif ( is_array( $value ) ) { |
37 | $event[$key] = self::removeNulls( $value ); |
38 | } |
39 | } |
40 | |
41 | return $event; |
42 | } |
43 | |
44 | /** |
45 | * Calls MachineReadableRCFeedFormatter's getLine(), augments |
46 | * the returned object so that it is suitable for POSTing to |
47 | * the EventBus service, and then returns those events |
48 | * serialized (AKA formatted) as a JSON string by calling |
49 | * EventBus serializeEvents(). |
50 | * |
51 | * @inheritDoc |
52 | * @suppress PhanTypeMismatchArgument |
53 | */ |
54 | public function getLine( array $feed, RecentChange $rc, $actionComment ) { |
55 | $attrs = parent::getLine( $feed, $rc, $actionComment ); |
56 | |
57 | $eventFactory = EventBus::getInstanceForStream( self::STREAM )->getFactory(); |
58 | $eventFactory->setCommentFormatter( MediaWikiServices::getInstance()->getCommentFormatter() ); |
59 | $event = $eventFactory->createRecentChangeEvent( |
60 | self::STREAM, |
61 | $rc->getTitle(), |
62 | $attrs |
63 | ); |
64 | |
65 | return EventBus::serializeEvents( [ self::removeNulls( $event ) ] ); |
66 | } |
67 | |
68 | /** |
69 | * Here, formatArray is implemented to just return the same |
70 | * event it is given. Since parent::getLine() calls this, |
71 | * and we need to augment the $event after it is returned from |
72 | * parent::getLine, we don't actually want to serialize (AKA format) |
73 | * the event at this time. This class' getLine function will |
74 | * serialize/format the event after it has augmented the |
75 | * event returned here. |
76 | * |
77 | * @inheritDoc |
78 | * @suppress PhanTypeMismatchReturnProbablyReal |
79 | */ |
80 | protected function formatArray( array $event ) { |
81 | return $event; |
82 | } |
83 | } |