Code Coverage
 
Lines
Functions and Methods
Classes and Traits
Total
0.00% covered (danger)
0.00%
0 / 25
0.00% covered (danger)
0.00%
0 / 2
CRAP
0.00% covered (danger)
0.00%
0 / 1
AbstractTopicInsertListener
0.00% covered (danger)
0.00%
0 / 25
0.00% covered (danger)
0.00%
0 / 2
110
0.00% covered (danger)
0.00%
0 / 1
 onAfterInsertExpectedChange
n/a
0 / 0
n/a
0 / 0
0
 onAfterInsert
0.00% covered (danger)
0.00%
0 / 16
0.00% covered (danger)
0.00%
0 / 1
42
 getUsersToSubscribe
0.00% covered (danger)
0.00%
0 / 9
0.00% covered (danger)
0.00%
0 / 1
20
1<?php
2
3namespace Flow\Data\Listener;
4
5use Flow\Container;
6use Flow\Exception\InvalidDataException;
7use Flow\FlowActions;
8use Flow\Model\PostRevision;
9use Flow\Model\Workflow;
10use Flow\WatchedTopicItems;
11use MediaWiki\User\User;
12
13/**
14 * Auto-watch topics when the user performs one of the actions specified
15 * in the constructor.
16 */
17abstract 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        /** @var $title Title */
45        $title = $workflow->getArticleTitle();
46        if ( !$title ) {
47            return;
48        }
49
50        $this->onAfterInsertExpectedChange( $row['rev_change_type'], $metadata['workflow'] );
51    }
52
53    /**
54     * Returns an array of user ids to subscribe to the title.
55     *
56     * @param string $changeType
57     * @param string $watchType Key of the corresponding 'watch' array in FlowActions.php
58     * @param WatchedTopicItems[] $params Params to feed to callback function that will return
59     *   an array of users to subscribe
60     * @return User[]
61     */
62    public static function getUsersToSubscribe( $changeType, $watchType, array $params = [] ) {
63        /** @var FlowActions $actions */
64        $actions = Container::get( 'flow_actions' );
65
66        // Find users defined for this action, in FlowActions.php
67        try {
68            $users = $actions->getValue( $changeType, 'watch', $watchType );
69        } catch ( \Exception $e ) {
70            return [];
71        }
72
73        // Null will be returned if nothing is defined for this changeType
74        if ( !$users ) {
75            return [];
76        }
77
78        // Some actions may have more complex logic to determine watching users
79        if ( is_callable( $users ) ) {
80            $users = $users( ...$params );
81        }
82
83        return $users;
84    }
85}