1 | <?php |
||
2 | /****************************************************************************** |
||
3 | * Wikipedia Account Creation Assistance tool * |
||
4 | * ACC Development Team. Please see team.json for a list of contributors. * |
||
5 | * * |
||
6 | * This is free and unencumbered software released into the public domain. * |
||
7 | * Please see LICENSE.md for the full licencing statement. * |
||
8 | ******************************************************************************/ |
||
9 | |||
10 | namespace Waca\DataObjects; |
||
11 | |||
12 | use Exception; |
||
13 | use PDO; |
||
14 | use Waca\DataObject; |
||
15 | use Waca\Exceptions\ApplicationLogicException; |
||
16 | use Waca\Exceptions\OptimisticLockFailedException; |
||
17 | use Waca\PdoDatabase; |
||
18 | |||
19 | /** |
||
20 | * Ban data object |
||
21 | */ |
||
22 | class RequestData extends DataObject |
||
23 | { |
||
24 | const TYPE_IPV4 = 'ipv4'; |
||
25 | const TYPE_IPV6 = 'ipv6'; |
||
26 | const TYPE_EMAIL = 'email'; |
||
27 | const TYPE_USERAGENT = 'useragent'; |
||
28 | const TYPE_CLIENTHINT = 'clienthint'; |
||
29 | |||
30 | /** @var int */ |
||
31 | private $request; |
||
32 | /** @var string */ |
||
33 | private $type; |
||
34 | /** @var string|null */ |
||
35 | private $name; |
||
36 | /** @var string */ |
||
37 | private $value; |
||
38 | |||
39 | public static function getForRequest(int $requestId, PdoDatabase $database, ?string $type = null) |
||
40 | { |
||
41 | $statement = $database->prepare(<<<SQL |
||
42 | SELECT * FROM requestdata |
||
43 | WHERE request = :request AND type LIKE COALESCE(:type, '%'); |
||
44 | SQL |
||
45 | ); |
||
46 | |||
47 | $statement->bindValue(":request", $requestId); |
||
48 | $statement->bindValue(":type", $type); |
||
49 | |||
50 | $statement->execute(); |
||
51 | |||
52 | $result = array(); |
||
53 | /** @var RequestData $v */ |
||
54 | foreach ($statement->fetchAll(PDO::FETCH_CLASS, get_called_class()) as $v) { |
||
55 | $v->setDatabase($database); |
||
56 | $result[] = $v; |
||
57 | } |
||
58 | |||
59 | return $result; |
||
60 | } |
||
61 | |||
62 | |||
63 | /** |
||
64 | * @throws Exception |
||
65 | */ |
||
66 | public function save() |
||
67 | { |
||
68 | if ($this->isNew()) { |
||
69 | // insert |
||
70 | $statement = $this->dbObject->prepare(<<<SQL |
||
71 | INSERT INTO `requestdata` (request, type, name, value) |
||
72 | VALUES (:request, :type, :name, :value); |
||
73 | SQL |
||
74 | ); |
||
75 | |||
76 | $statement->bindValue(":request", $this->request); |
||
77 | $statement->bindValue(":type", $this->type); |
||
78 | $statement->bindValue(":name", $this->name); |
||
79 | $statement->bindValue(":value", $this->value); |
||
80 | |||
81 | if ($statement->execute()) { |
||
82 | $this->id = (int)$this->dbObject->lastInsertId(); |
||
83 | } |
||
84 | else { |
||
85 | throw new Exception($statement->errorInfo()); |
||
0 ignored issues
–
show
Bug
introduced
by
![]() |
|||
86 | } |
||
87 | } |
||
88 | else { |
||
89 | // update |
||
90 | throw new ApplicationLogicException('Updates to RequestData are not supported.'); |
||
91 | } |
||
92 | } |
||
93 | |||
94 | /** |
||
95 | * @return int |
||
96 | */ |
||
97 | public function getRequest(): int |
||
98 | { |
||
99 | return $this->request; |
||
100 | } |
||
101 | |||
102 | /** |
||
103 | * @param int $request |
||
104 | */ |
||
105 | public function setRequest(int $request): void |
||
106 | { |
||
107 | $this->request = $request; |
||
108 | } |
||
109 | |||
110 | /** |
||
111 | * @return string |
||
112 | */ |
||
113 | public function getType(): string |
||
114 | { |
||
115 | return $this->type; |
||
116 | } |
||
117 | |||
118 | /** |
||
119 | * @param string $type |
||
120 | */ |
||
121 | public function setType(string $type): void |
||
122 | { |
||
123 | $this->type = $type; |
||
124 | } |
||
125 | |||
126 | /** |
||
127 | * @return string|null |
||
128 | */ |
||
129 | public function getName(): ?string |
||
130 | { |
||
131 | return $this->name; |
||
132 | } |
||
133 | |||
134 | /** |
||
135 | * @param string|null $name |
||
136 | */ |
||
137 | public function setName(?string $name): void |
||
138 | { |
||
139 | $this->name = $name; |
||
140 | } |
||
141 | |||
142 | /** |
||
143 | * @return string |
||
144 | */ |
||
145 | public function getValue(): string |
||
146 | { |
||
147 | return $this->value; |
||
148 | } |
||
149 | |||
150 | /** |
||
151 | * @param string $value |
||
152 | */ |
||
153 | public function setValue(string $value): void |
||
154 | { |
||
155 | $this->value = $value; |
||
156 | } |
||
157 | } |
||
158 |