Code Coverage
 
Lines
Functions and Methods
Classes and Traits
Total
0.00% covered (danger)
0.00%
0 / 33
0.00% covered (danger)
0.00%
0 / 4
CRAP
0.00% covered (danger)
0.00%
0 / 1
ModerationLogger
0.00% covered (danger)
0.00%
0 / 33
0.00% covered (danger)
0.00%
0 / 4
110
0.00% covered (danger)
0.00%
0 / 1
 __construct
0.00% covered (danger)
0.00%
0 / 1
0.00% covered (danger)
0.00%
0 / 1
2
 canLog
0.00% covered (danger)
0.00%
0 / 1
0.00% covered (danger)
0.00%
0 / 1
2
 log
0.00% covered (danger)
0.00%
0 / 27
0.00% covered (danger)
0.00%
0 / 1
42
 getLogType
0.00% covered (danger)
0.00%
0 / 4
0.00% covered (danger)
0.00%
0 / 1
6
1<?php
2
3namespace Flow\Log;
4
5use Closure;
6use Flow\Container;
7use Flow\FlowActions;
8use Flow\Model\PostRevision;
9use Flow\Model\UUID;
10use Flow\Model\Workflow;
11use ManualLogEntry;
12use MediaWiki\Title\Title;
13
14class ModerationLogger {
15    /**
16     * @var FlowActions
17     */
18    protected $actions;
19
20    /**
21     * @param FlowActions $actions
22     */
23    public function __construct( FlowActions $actions ) {
24        $this->actions = $actions;
25    }
26
27    /**
28     * Check if an action should be logged (= if a log_type is set)
29     *
30     * @param PostRevision $post
31     * @param string $action
32     * @return bool
33     */
34    public function canLog( PostRevision $post, $action ) {
35        return (bool)$this->getLogType( $post, $action );
36    }
37
38    /**
39     * Adds a moderation activity item to the log under the appropriate action
40     *
41     * @param PostRevision $post
42     * @param string $action The action we'll be logging
43     * @param string $reason Comment, reason for the moderation
44     * @param UUID $workflowId Workflow being worked on
45     * @return int|null The id of the newly inserted log entry
46     */
47    public function log( PostRevision $post, $action, $reason, UUID $workflowId ) {
48        if ( !$this->canLog( $post, $action ) ) {
49            return null;
50        }
51
52        $params = [
53            'topicId' => $workflowId->getAlphadecimal(),
54        ];
55        if ( !$post->isTopicTitle() ) {
56            $params['postId'] = $post->getPostId()->getAlphadecimal();
57        }
58
59        $logType = $this->getLogType( $post, $action );
60
61        // reasonably likely this is already loaded in-process and just returns that object
62        /** @var Workflow $workflow */
63        $workflow = Container::get( 'storage.workflow' )->get( $workflowId );
64        if ( $workflow ) {
65            $title = $workflow->getArticleTitle();
66        } else {
67            $title = false;
68        }
69        $error = false;
70        if ( !$title ) {
71            // We dont want to fail logging due to this, so repoint it at Main_Page which
72            // will probably be noticed, also log it below once we know the logId
73            $title = Title::newMainPage();
74            $error = true;
75        }
76
77        // insert logging entry
78        $logEntry = new ManualLogEntry( $logType, "flow-$action" );
79        $logEntry->setTarget( $title );
80        $logEntry->setPerformer( $post->getUserTuple()->createUser() );
81        $logEntry->setParameters( $params );
82        $logEntry->setComment( $reason );
83        $logEntry->setTimestamp( $post->getModerationTimestamp() );
84
85        $logId = $logEntry->insert();
86
87        if ( $error ) {
88            wfDebugLog( 'Flow', __METHOD__ . ': Could not map workflowId to workflow object for ' .
89                $workflowId->getAlphadecimal() . " log entry $logId defaulted to Main_Page" );
90        }
91
92        return $logId;
93    }
94
95    /**
96     * @param PostRevision $post
97     * @param string $action
98     * @return string
99     */
100    public function getLogType( PostRevision $post, $action ) {
101        $logType = $this->actions->getValue( $action, 'log_type' );
102        if ( $logType instanceof Closure ) {
103            $logType = $logType( $post, $this );
104        }
105
106        return $logType;
107    }
108}