Completed
Push — master ( c332a3...b7b580 )
by Sam
02:32
created

AutomatedEditsHelper::getEditsSummary()   B

Complexity

Conditions 5
Paths 5

Size

Total Lines 40
Code Lines 25

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 40
rs 8.439
c 0
b 0
f 0
cc 5
eloc 25
nc 5
nop 1
1
<?php
2
3
namespace AppBundle\Helper;
4
5
use Doctrine\DBAL\Connection;
6
use Psr\Cache\CacheItemPoolInterface;
7
use Symfony\Component\Config\Definition\Exception\Exception;
8
use Symfony\Component\HttpKernel\Exception\NotFoundHttpException;
9
use Symfony\Component\DependencyInjection\ContainerInterface;
10
use Symfony\Component\VarDumper\VarDumper;
11
12
class AutomatedEditsHelper
13
{
14
15
    /** @var ContainerInterface */
16
    private $container;
17
18
    /** @var string[] The list of tools that are considered reverting. */
19
    public $revertTools = [
20
        'Generic rollback',
21
        'Undo',
22
        'Pending changes revert',
23
        'Huggle',
24
        'STiki',
25
        'Igloo',
26
        'WikiPatroller',
27
        'Twinkle revert',
28
        'Bot revert'
29
    ];
30
31
    /** @var string[] The list of tool names and their regexes. */
32
    protected $tools;
33
34
    public function __construct(ContainerInterface $container)
35
    {
36
        $this->container = $container;
37
    }
38
39
    /**
40
     * Was the edit (semi-)automated, based on the edit summary?
41
     * @param  string  $summary Edit summary
42
     * @return boolean          Yes or no
43
     */
44 View Code Duplication
    public function isAutomated($summary)
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
45
    {
46
        foreach ($this->getTools() as $tool => $regex) {
47
            if (preg_match("/$regex/", $summary)) {
48
                return true;
49
            }
50
        }
51
52
        return false;
53
    }
54
55
    /**
56
     * Was the edit a revert, based on the edit summary?
57
     * @param  string $summary Edit summary
58
     * @return boolean         Yes or no
59
     */
60 View Code Duplication
    public function isRevert($summary)
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
61
    {
62
        foreach ($this->revertTools as $tool) {
63
            $regex = $this->getTools()[$tool];
64
65
            if (preg_match("/$regex/", $summary)) {
66
                return true;
67
            }
68
        }
69
70
        return false;
71
    }
72
73
    /**
74
     * Get the name of the tool that matched the given edit summary
75
     * @param  string $summary Edit summary
76
     * @return string|boolean  Name of tool, or false if nothing was found
77
     */
78 View Code Duplication
    public function getTool($summary)
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
79
    {
80
        foreach ($this->getTools() as $tool => $regex) {
81
            if (preg_match("/$regex/", $summary)) {
82
                return $tool;
83
            }
84
        }
85
86
        return false;
87
    }
88
89
    /**
90
     * Get the list of automated tools
91
     * @return array Associative array of 'tool name' => 'regex'
92
     */
93
    public function getTools()
94
    {
95
        if (is_array($this->tools)) {
96
            return $this->tools;
97
        }
98
        $this->tools = call_user_func_array(
0 ignored issues
show
Documentation Bug introduced by
It seems like call_user_func_array('ar...ter('automated_tools')) of type * is incompatible with the declared type array<integer,string> of property $tools.

Our type inference engine has found an assignment to a property that is incompatible with the declared type of that property.

Either this assignment is in error or the assigned type should be added to the documentation/type hint for that property..

Loading history...
99
            'array_merge',
100
            $this->container->getParameter("automated_tools")
101
        );
102
        return $this->tools;
103
    }
104
105
    /**
106
     * Get a summary of automated edits made by the given user in their last 1000 edits.
107
     * Will cache the result for 10 minutes.
108
     * @param integer $userId The user ID.
109
     * @return integer[] Array of edit counts, keyed by all tool names from
110
     * app/config/semi_automated.yml
111
     */
112
    public function getEditsSummary($userId)
113
    {
114
        // Set up cache.
115
        /** @var CacheItemPoolInterface $cache */
116
        $cache = $this->container->get('cache.app');
117
        $cacheItem = $cache->getItem('automatedEdits.'.$userId);
118
        if ($cacheItem->isHit()) {
119
            return $cacheItem->get();
120
        }
121
122
        // Get the most recent 1000 edit summaries.
123
        /** @var Connection $replicas */
124
        $replicas = $this->container->get('doctrine')->getManager('replicas')->getConnection();
125
        /** @var LabsHelper $labsHelper */
126
        $labsHelper = $this->container->get('app.labs_helper');
127
        $sql = "SELECT rev_comment FROM ".$labsHelper->getTable('revision')
128
               ." WHERE rev_user=:userId ORDER BY rev_timestamp DESC LIMIT 1000";
129
        $resultQuery = $replicas->prepare($sql);
130
        $resultQuery->bindParam("userId", $userId);
131
        $resultQuery->execute();
132
        $results = $resultQuery->fetchAll();
133
        $out = [];
134
        foreach ($results as $result) {
135
            $toolName = $this->getTool($result['rev_comment']);
136
            if ($toolName) {
137
                if (!isset($out[$toolName])) {
138
                    $out[$toolName] = 0;
139
                }
140
                $out[$toolName]++;
141
            }
142
        }
143
        arsort($out);
144
145
        // Cache for 10 minutes.
146
        $cacheItem->expiresAfter(new \DateInterval('PT10M'));
147
        $cacheItem->set($out);
148
        $cache->save($cacheItem);
149
150
        return $out;
151
    }
152
}
153