Passed
Push — master ( e6ce91...429907 )
by MusikAnimal
04:46
created

AdminScore::prepareData()   B

Complexity

Conditions 6
Paths 9

Size

Total Lines 38
Code Lines 26

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 42

Importance

Changes 0
Metric Value
cc 6
eloc 26
nc 9
nop 0
dl 0
loc 38
ccs 0
cts 31
cp 0
crap 42
rs 8.8817
c 0
b 0
f 0
1
<?php
2
/**
3
 * This file contains only the AdminScore class.
4
 */
5
6
namespace Xtools;
7
8
use DateTime;
9
10
/**
11
 * An AdminScore provides scores of logged actions and on-wiki activity made by a user,
12
 * to measure if they would be suitable as an administrator.
13
 */
14
class AdminScore extends Model
15
{
16
    /**
17
     * @var array Multipliers (may need review). This currently is dynamic, but should be a constant.
18
     */
19
    private $multipliers = [
20
        'account-age-mult' => 1.25,
21
        'edit-count-mult' => 1.25,
22
        'user-page-mult' => 0.1,
23
        'patrols-mult' => 1,
24
        'blocks-mult' => 1.4,
25
        'afd-mult' => 1.15,
26
        'recent-activity-mult' => 0.9,
27
        'aiv-mult' => 1.15,
28
        'edit-summaries-mult' => 0.8,
29
        'namespaces-mult' => 1.0,
30
        'pages-created-live-mult' => 1.4,
31
        'pages-created-deleted-mult' => 1.4,
32
        'rpp-mult' => 1.15,
33
        'user-rights-mult' => 0.75,
34
    ];
35
36
    /** @var Project The project. */
37
    protected $project;
38
39
    /** @var User The user. */
40
    protected $user;
41
42
    /** @var array The scoring results. */
43
    protected $scores;
44
45
    /** @var int The total of all scores. */
46
    protected $total;
47
48
    /**
49
     * AdminScore constructor.
50
     * @param Project $project
51
     * @param User $user
52
     */
53
    public function __construct(Project $project, User $user)
54
    {
55
        $this->project = $project;
56
        $this->user = $user;
57
    }
58
59
    /**
60
     * Get the scoring results.
61
     * @return array See AdminScoreRepository::getData() for the list of keys.
62
     */
63
    public function getScores()
64
    {
65
        if (isset($this->scores)) {
66
            return $this->scores;
67
        }
68
        $this->prepareData();
69
        return $this->scores;
70
    }
71
72
    /**
73
     * Get the total score.
74
     * @return int
75
     */
76
    public function getTotal()
77
    {
78
        if (isset($this->total)) {
79
            return $this->total;
80
        }
81
        $this->prepareData();
82
        return $this->total;
83
    }
84
85
    /**
86
     * Set the scoring results on class properties $scores and $total.
87
     */
88
    public function prepareData()
89
    {
90
        $data = $this->getRepository()->fetchData($this->project, $this->user);
91
        $this->total = 0;
92
        $this->scores = [];
93
94
        foreach ($data as $row) {
95
            $key = $row['source'];
96
            $value = $row['value'];
97
98
            // WMF Replica databases are returning binary control characters
99
            // This is specifically shown with WikiData.
100
            // More details: T197165
101
            $value = str_replace("\x00", "", $value);
102
103
            if ($key === 'account-age') {
104
                if ($value == null) {
105
                    $value = 0;
106
                } else {
107
                    $now = new DateTime();
108
                    $date = new DateTime($value);
109
                    $diff = $date->diff($now);
110
                    $formula = 365 * (int)$diff->format('%y') + 30 *
111
                        (int)$diff->format('%m') + (int)$diff->format('%d');
112
                    if ($formula < 365) {
113
                        $this->multipliers["account-age-mult"] = 0;
114
                    }
115
                    $value = $formula;
116
                }
117
            }
118
119
            $multiplierKey = $row['source'] . '-mult';
120
            $multiplier = isset($this->multipliers[$multiplierKey]) ? $this->multipliers[$multiplierKey] : 1;
121
            $score = max(min($value * $multiplier, 100), -100);
122
            $this->scores[$key]['mult'] = $multiplier;
123
            $this->scores[$key]['value'] = $value;
124
            $this->scores[$key]['score'] = $score;
125
            $this->total += $score;
126
        }
127
    }
128
}
129