Code Coverage |
||||||||||
Lines |
Functions and Methods |
Classes and Traits |
||||||||
Total | |
0.00% |
0 / 24 |
|
0.00% |
0 / 2 |
CRAP | |
0.00% |
0 / 1 |
AbstractTopicInsertListener | |
0.00% |
0 / 24 |
|
0.00% |
0 / 2 |
110 | |
0.00% |
0 / 1 |
onAfterInsertExpectedChange | n/a |
0 / 0 |
n/a |
0 / 0 |
0 | |||||
onAfterInsert | |
0.00% |
0 / 15 |
|
0.00% |
0 / 1 |
42 | |||
getUsersToSubscribe | |
0.00% |
0 / 9 |
|
0.00% |
0 / 1 |
20 |
1 | <?php |
2 | |
3 | namespace Flow\Data\Listener; |
4 | |
5 | use Flow\Container; |
6 | use Flow\Exception\InvalidDataException; |
7 | use Flow\FlowActions; |
8 | use Flow\Model\PostRevision; |
9 | use Flow\Model\Workflow; |
10 | use Flow\WatchedTopicItems; |
11 | use MediaWiki\User\User; |
12 | |
13 | /** |
14 | * Auto-watch topics when the user performs one of the actions specified |
15 | * in the constructor. |
16 | */ |
17 | abstract class AbstractTopicInsertListener extends AbstractListener { |
18 | /** |
19 | * @param string $changeType |
20 | * @param Workflow $workflow |
21 | */ |
22 | abstract protected function onAfterInsertExpectedChange( $changeType, Workflow $workflow ); |
23 | |
24 | public function onAfterInsert( $object, array $row, array $metadata ) { |
25 | if ( !$object instanceof PostRevision ) { |
26 | wfWarn( __METHOD__ . ': Object is no PostRevision instance' ); |
27 | return; |
28 | } |
29 | |
30 | if ( !isset( $metadata['workflow'] ) ) { |
31 | wfWarn( __METHOD__ . ': Missing required metadata: workflow' ); |
32 | return; |
33 | } |
34 | $workflow = $metadata['workflow']; |
35 | if ( !$workflow instanceof Workflow ) { |
36 | throw new InvalidDataException( 'Workflow metadata is not Workflow instance' ); |
37 | } |
38 | |
39 | if ( $workflow->getType() !== 'topic' ) { |
40 | wfWarn( __METHOD__ . ': Expected "topic" workflow but received "' . $workflow->getType() . '"' ); |
41 | return; |
42 | } |
43 | |
44 | if ( !$workflow->getArticleTitle() ) { |
45 | return; |
46 | } |
47 | |
48 | $this->onAfterInsertExpectedChange( $row['rev_change_type'], $metadata['workflow'] ); |
49 | } |
50 | |
51 | /** |
52 | * Returns an array of user ids to subscribe to the title. |
53 | * |
54 | * @param string $changeType |
55 | * @param string $watchType Key of the corresponding 'watch' array in FlowActions.php |
56 | * @param WatchedTopicItems[] $params Params to feed to callback function that will return |
57 | * an array of users to subscribe |
58 | * @return User[] |
59 | */ |
60 | public static function getUsersToSubscribe( $changeType, $watchType, array $params = [] ) { |
61 | /** @var FlowActions $actions */ |
62 | $actions = Container::get( 'flow_actions' ); |
63 | |
64 | // Find users defined for this action, in FlowActions.php |
65 | try { |
66 | $users = $actions->getValue( $changeType, 'watch', $watchType ); |
67 | } catch ( \Exception $e ) { |
68 | return []; |
69 | } |
70 | |
71 | // Null will be returned if nothing is defined for this changeType |
72 | if ( !$users ) { |
73 | return []; |
74 | } |
75 | |
76 | // Some actions may have more complex logic to determine watching users |
77 | if ( is_callable( $users ) ) { |
78 | $users = $users( ...$params ); |
79 | } |
80 | |
81 | return $users; |
82 | } |
83 | } |