1
|
|
|
<?php |
2
|
|
|
/****************************************************************************** |
3
|
|
|
* Wikipedia Account Creation Assistance tool * |
4
|
|
|
* * |
5
|
|
|
* All code in this file is released into the public domain by the ACC * |
6
|
|
|
* Development Team. Please see team.json for a list of contributors. * |
7
|
|
|
******************************************************************************/ |
8
|
|
|
|
9
|
|
|
namespace Waca\DataObjects; |
10
|
|
|
|
11
|
|
|
use Exception; |
12
|
|
|
use Waca\DataObject; |
13
|
|
|
use Waca\Exceptions\OptimisticLockFailedException; |
14
|
|
|
use Waca\PdoDatabase; |
15
|
|
|
|
16
|
|
|
class UserPreference extends DataObject |
17
|
|
|
{ |
18
|
|
|
|
19
|
|
|
/** @var int */ |
20
|
|
|
private $user; |
21
|
|
|
|
22
|
|
|
/** @var ?int */ |
23
|
|
|
private $domain; |
24
|
|
|
|
25
|
|
|
/** @var string */ |
26
|
|
|
private $preference; |
27
|
|
|
|
28
|
|
|
/** @var ?mixed */ |
29
|
|
|
private $value; |
30
|
|
|
|
31
|
|
|
public static function getLocalPreference(PdoDatabase $database, int $user, string $preference, int $domain) { |
32
|
|
|
$statement = $database->prepare('SELECT * FROM userpreference WHERE preference = :preference AND USER = :user AND domain = :domain'); |
33
|
|
|
$statement->execute([ |
34
|
|
|
':user' => $user, |
35
|
|
|
':preference' => $preference, |
36
|
|
|
':domain' => $domain |
37
|
|
|
]); |
38
|
|
|
|
39
|
|
|
$resultObject = $statement->fetchObject(get_called_class()); |
40
|
|
|
|
41
|
|
|
if ($resultObject !== false) { |
42
|
|
|
$resultObject->setDatabase($database); |
43
|
|
|
} |
44
|
|
|
|
45
|
|
|
return $resultObject; |
46
|
|
|
} |
47
|
|
|
|
48
|
|
|
public static function getGlobalPreference(PdoDatabase $database, int $user, string $preference) { |
49
|
|
|
$statement = $database->prepare('SELECT * FROM userpreference WHERE preference = :preference AND USER = :user AND domain IS NULL'); |
50
|
|
|
$statement->execute([ |
51
|
|
|
':user' => $user, |
52
|
|
|
':preference' => $preference |
53
|
|
|
]); |
54
|
|
|
|
55
|
|
|
$resultObject = $statement->fetchObject(get_called_class()); |
56
|
|
|
|
57
|
|
|
if ($resultObject !== false) { |
58
|
|
|
$resultObject->setDatabase($database); |
59
|
|
|
} |
60
|
|
|
|
61
|
|
|
return $resultObject; |
62
|
|
|
} |
63
|
|
|
|
64
|
|
|
/** |
65
|
|
|
* @inheritDoc |
66
|
|
|
* @throws Exception |
67
|
|
|
*/ |
68
|
|
|
public function save() |
69
|
|
|
{ |
70
|
|
|
if($this->isNew()) { |
71
|
|
|
// insert |
72
|
|
|
$statement = $this->dbObject->prepare(<<<SQL |
73
|
|
|
INSERT INTO `userpreference` ( |
74
|
|
|
user, domain, preference, value |
75
|
|
|
) VALUES ( |
76
|
|
|
:user, :domain, :preference, :value |
77
|
|
|
); |
78
|
|
|
SQL |
79
|
|
|
); |
80
|
|
|
$statement->bindValue(":user", $this->user); |
81
|
|
|
$statement->bindValue(":domain", $this->domain); |
82
|
|
|
$statement->bindValue(":preference", $this->preference); |
83
|
|
|
$statement->bindValue(":value", $this->value); |
84
|
|
|
|
85
|
|
|
if ($statement->execute()) { |
86
|
|
|
$this->id = (int)$this->dbObject->lastInsertId(); |
87
|
|
|
} |
88
|
|
|
else { |
89
|
|
|
throw new Exception($statement->errorInfo()); |
|
|
|
|
90
|
|
|
} |
91
|
|
|
}else{ |
92
|
|
|
// update |
93
|
|
|
$statement = $this->dbObject->prepare(<<<SQL |
94
|
|
|
UPDATE `userpreference` SET |
95
|
|
|
value = :value, |
96
|
|
|
updateversion = updateversion + 1 |
97
|
|
|
WHERE id = :id AND updateversion = :updateversion; |
98
|
|
|
SQL |
99
|
|
|
); |
100
|
|
|
$statement->bindValue(":value", $this->value); |
101
|
|
|
|
102
|
|
|
$statement->bindValue(':id', $this->id); |
103
|
|
|
$statement->bindValue(':updateversion', $this->updateversion); |
104
|
|
|
|
105
|
|
|
if (!$statement->execute()) { |
106
|
|
|
throw new Exception($statement->errorInfo()); |
107
|
|
|
} |
108
|
|
|
|
109
|
|
|
if ($statement->rowCount() !== 1) { |
110
|
|
|
throw new OptimisticLockFailedException(); |
111
|
|
|
} |
112
|
|
|
|
113
|
|
|
$this->updateversion++; |
114
|
|
|
} |
115
|
|
|
} |
116
|
|
|
|
117
|
|
|
/** |
118
|
|
|
* @return int |
119
|
|
|
*/ |
120
|
|
|
public function getUser(): int |
121
|
|
|
{ |
122
|
|
|
return $this->user; |
123
|
|
|
} |
124
|
|
|
|
125
|
|
|
/** |
126
|
|
|
* @param int $user |
127
|
|
|
*/ |
128
|
|
|
public function setUser(int $user): void |
129
|
|
|
{ |
130
|
|
|
$this->user = $user; |
131
|
|
|
} |
132
|
|
|
|
133
|
|
|
/** |
134
|
|
|
* @return ?int |
135
|
|
|
*/ |
136
|
|
|
public function getDomain(): ?int |
137
|
|
|
{ |
138
|
|
|
return $this->domain; |
139
|
|
|
} |
140
|
|
|
|
141
|
|
|
/** |
142
|
|
|
* @param ?int $domain |
143
|
|
|
*/ |
144
|
|
|
public function setDomain(?int $domain): void |
145
|
|
|
{ |
146
|
|
|
$this->domain = $domain; |
147
|
|
|
} |
148
|
|
|
|
149
|
|
|
/** |
150
|
|
|
* @return string |
151
|
|
|
*/ |
152
|
|
|
public function getPreference(): string |
153
|
|
|
{ |
154
|
|
|
return $this->preference; |
155
|
|
|
} |
156
|
|
|
|
157
|
|
|
/** |
158
|
|
|
* @param string $preference |
159
|
|
|
*/ |
160
|
|
|
public function setPreference(string $preference): void |
161
|
|
|
{ |
162
|
|
|
$this->preference = $preference; |
163
|
|
|
} |
164
|
|
|
|
165
|
|
|
/** |
166
|
|
|
* @return mixed|null |
167
|
|
|
*/ |
168
|
|
|
public function getValue() |
169
|
|
|
{ |
170
|
|
|
return $this->value; |
171
|
|
|
} |
172
|
|
|
|
173
|
|
|
/** |
174
|
|
|
* @param mixed|null $value |
175
|
|
|
*/ |
176
|
|
|
public function setValue($value): void |
177
|
|
|
{ |
178
|
|
|
$this->value = $value; |
179
|
|
|
} |
180
|
|
|
} |
181
|
|
|
|