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