enwikipedia-acc /
waca
| 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\OptimisticLockFailedException; |
||||
| 16 | use Waca\PdoDatabase; |
||||
| 17 | |||||
| 18 | /** |
||||
| 19 | * Email template data object |
||||
| 20 | * |
||||
| 21 | * This is the close reasons thing. |
||||
| 22 | */ |
||||
| 23 | class EmailTemplate extends DataObject |
||||
| 24 | { |
||||
| 25 | const ACTION_CREATED = 'created'; |
||||
| 26 | const ACTION_NOT_CREATED = 'not created'; |
||||
| 27 | const ACTION_NONE = 'none'; |
||||
| 28 | const ACTION_DEFER = 'defer'; |
||||
| 29 | |||||
| 30 | /** @var string the name of the template */ |
||||
| 31 | private $name; |
||||
| 32 | private $text; |
||||
| 33 | /** @var string|null */ |
||||
| 34 | private $jsquestion; |
||||
| 35 | private $active = 1; |
||||
| 36 | private $preloadonly = 0; |
||||
| 37 | private $defaultaction = self::ACTION_NOT_CREATED; |
||||
| 38 | private $queue; |
||||
| 39 | /** @var int */ |
||||
| 40 | private $domain; |
||||
| 41 | |||||
| 42 | /** |
||||
| 43 | * Gets active non-preload templates |
||||
| 44 | * |
||||
| 45 | * @param string $defaultAction Default action to take (EmailTemplate::ACTION_CREATED or EmailTemplate::ACTION_NOT_CREATED) |
||||
| 46 | * @param PdoDatabase $database |
||||
| 47 | * @param int $domain |
||||
| 48 | * @param int|null $filter Template IDs to filter out |
||||
| 49 | * |
||||
| 50 | * @return array|false |
||||
| 51 | */ |
||||
| 52 | public static function getActiveNonpreloadTemplates($defaultAction, PdoDatabase $database, int $domain, ?int $filter = null) |
||||
| 53 | { |
||||
| 54 | $statement = $database->prepare(<<<SQL |
||||
| 55 | SELECT * FROM `emailtemplate` |
||||
| 56 | WHERE defaultaction = :forcreated AND active = 1 AND preloadonly = 0 AND (:skipFilter = 1 OR id <> :filter) AND domain = :domain; |
||||
| 57 | SQL |
||||
| 58 | ); |
||||
| 59 | $statement->bindValue(":forcreated", $defaultAction); |
||||
| 60 | $statement->bindValue(":filter", $filter); |
||||
| 61 | $statement->bindValue(":skipFilter", $filter === null ? 1 : 0); |
||||
| 62 | $statement->bindValue(":domain", $domain); |
||||
| 63 | |||||
| 64 | $statement->execute(); |
||||
| 65 | |||||
| 66 | $resultObject = $statement->fetchAll(PDO::FETCH_CLASS, get_called_class()); |
||||
| 67 | |||||
| 68 | /** @var EmailTemplate $t */ |
||||
| 69 | foreach ($resultObject as $t) { |
||||
| 70 | $t->setDatabase($database); |
||||
| 71 | } |
||||
| 72 | |||||
| 73 | return $resultObject; |
||||
| 74 | } |
||||
| 75 | |||||
| 76 | /** |
||||
| 77 | * Gets active non-preload and preload templates, optionally filtered by the default action. |
||||
| 78 | * |
||||
| 79 | * @param null|bool|string $defaultAction Default action to take (EmailTemplate::ACTION_CREATED, |
||||
| 80 | * EmailTemplate::ACTION_NOT_CREATED, or EmailTemplate::ACTION_NONE), or optionally null to |
||||
| 81 | * just get everything. |
||||
| 82 | * @param PdoDatabase $database |
||||
| 83 | * @param int $domain |
||||
| 84 | * |
||||
| 85 | * @return array|false |
||||
| 86 | */ |
||||
| 87 | public static function getAllActiveTemplates($defaultAction, PdoDatabase $database, int $domain) |
||||
| 88 | { |
||||
| 89 | if ($defaultAction === false) { |
||||
| 90 | $statement = $database->prepare( |
||||
| 91 | "SELECT * FROM `emailtemplate` WHERE defaultaction NOT IN ('created', 'not created') AND active = 1 AND domain = :domain;"); |
||||
| 92 | } |
||||
| 93 | elseif ($defaultAction === null) { |
||||
|
0 ignored issues
–
show
introduced
by
Loading history...
|
|||||
| 94 | $statement = $database->prepare("SELECT * FROM `emailtemplate` WHERE active = 1 AND domain = :domain;"); |
||||
| 95 | } |
||||
| 96 | else { |
||||
| 97 | $statement = $database->prepare("SELECT * FROM `emailtemplate` WHERE defaultaction = :forcreated AND active = 1 AND domain = :domain;"); |
||||
| 98 | $statement->bindValue(":forcreated", $defaultAction); |
||||
| 99 | } |
||||
| 100 | |||||
| 101 | $statement->bindValue(":domain", $domain); |
||||
| 102 | |||||
| 103 | $statement->execute(); |
||||
| 104 | |||||
| 105 | $resultObject = $statement->fetchAll(PDO::FETCH_CLASS, get_called_class()); |
||||
| 106 | |||||
| 107 | /** @var EmailTemplate $t */ |
||||
| 108 | foreach ($resultObject as $t) { |
||||
| 109 | $t->setDatabase($database); |
||||
| 110 | } |
||||
| 111 | |||||
| 112 | return $resultObject; |
||||
| 113 | } |
||||
| 114 | |||||
| 115 | /** |
||||
| 116 | * Gets all the inactive templates |
||||
| 117 | * |
||||
| 118 | * @param PdoDatabase $database |
||||
| 119 | * @param int $domain |
||||
| 120 | * |
||||
| 121 | * @return array |
||||
| 122 | */ |
||||
| 123 | public static function getAllInactiveTemplates(PdoDatabase $database, int $domain) |
||||
| 124 | { |
||||
| 125 | $statement = $database->prepare("SELECT * FROM `emailtemplate` WHERE active = 0 AND domain = :domain;"); |
||||
| 126 | $statement->execute([':domain' => $domain]); |
||||
| 127 | |||||
| 128 | $resultObject = $statement->fetchAll(PDO::FETCH_CLASS, get_called_class()); |
||||
| 129 | |||||
| 130 | /** @var EmailTemplate $t */ |
||||
| 131 | foreach ($resultObject as $t) { |
||||
| 132 | $t->setDatabase($database); |
||||
| 133 | } |
||||
| 134 | |||||
| 135 | return $resultObject; |
||||
| 136 | } |
||||
| 137 | |||||
| 138 | /** |
||||
| 139 | * @param string $name |
||||
| 140 | * @param PdoDatabase $database |
||||
| 141 | * @param int $domain |
||||
| 142 | * |
||||
| 143 | * @return EmailTemplate|false |
||||
| 144 | */ |
||||
| 145 | public static function getByName($name, PdoDatabase $database, int $domain) |
||||
| 146 | { |
||||
| 147 | $statement = $database->prepare("SELECT * FROM `emailtemplate` WHERE name = :name AND domain = :domain LIMIT 1;"); |
||||
| 148 | $statement->bindValue(":name", $name); |
||||
| 149 | $statement->bindValue(":domain", $domain); |
||||
| 150 | |||||
| 151 | $statement->execute(); |
||||
| 152 | |||||
| 153 | $resultObject = $statement->fetchObject(get_called_class()); |
||||
| 154 | |||||
| 155 | if ($resultObject != false) { |
||||
| 156 | $resultObject->setDatabase($database); |
||||
| 157 | } |
||||
| 158 | |||||
| 159 | return $resultObject; |
||||
| 160 | } |
||||
| 161 | |||||
| 162 | /** |
||||
| 163 | * @return EmailTemplate |
||||
| 164 | */ |
||||
| 165 | public static function getDroppedTemplate() |
||||
| 166 | { |
||||
| 167 | $t = new EmailTemplate(); |
||||
| 168 | $t->id = 0; |
||||
| 169 | $t->active = 1; |
||||
| 170 | $t->defaultaction = self::ACTION_NONE; |
||||
| 171 | $t->name = 'Dropped'; |
||||
| 172 | |||||
| 173 | return $t; |
||||
| 174 | } |
||||
| 175 | |||||
| 176 | /** |
||||
| 177 | * @throws Exception |
||||
| 178 | */ |
||||
| 179 | public function save() |
||||
| 180 | { |
||||
| 181 | if ($this->isNew()) { |
||||
| 182 | // insert |
||||
| 183 | $statement = $this->dbObject->prepare(<<<SQL |
||||
| 184 | INSERT INTO `emailtemplate` (name, text, jsquestion, defaultaction, active, preloadonly, queue, domain) |
||||
| 185 | VALUES (:name, :text, :jsquestion, :defaultaction, :active, :preloadonly, :queue, :domain); |
||||
| 186 | SQL |
||||
| 187 | ); |
||||
| 188 | $statement->bindValue(":name", $this->name); |
||||
| 189 | $statement->bindValue(":text", $this->text); |
||||
| 190 | $statement->bindValue(":jsquestion", $this->jsquestion); |
||||
| 191 | $statement->bindValue(":defaultaction", $this->defaultaction); |
||||
| 192 | $statement->bindValue(":active", $this->active); |
||||
| 193 | $statement->bindValue(":preloadonly", $this->preloadonly); |
||||
| 194 | $statement->bindValue(":queue", $this->queue); |
||||
| 195 | $statement->bindValue(":domain", $this->domain); |
||||
| 196 | |||||
| 197 | if ($statement->execute()) { |
||||
| 198 | $this->id = (int)$this->dbObject->lastInsertId(); |
||||
| 199 | } |
||||
| 200 | else { |
||||
| 201 | throw new Exception($statement->errorInfo()); |
||||
|
0 ignored issues
–
show
$statement->errorInfo() of type array is incompatible with the type string expected by parameter $message of Exception::__construct().
(
Ignorable by Annotation
)
If this is a false-positive, you can also ignore this issue in your code via the
Loading history...
|
|||||
| 202 | } |
||||
| 203 | } |
||||
| 204 | else { |
||||
| 205 | // update |
||||
| 206 | $statement = $this->dbObject->prepare(<<<SQL |
||||
| 207 | UPDATE `emailtemplate` |
||||
| 208 | SET name = :name, |
||||
| 209 | text = :text, |
||||
| 210 | jsquestion = :jsquestion, |
||||
| 211 | defaultaction = :defaultaction, |
||||
| 212 | active = :active, |
||||
| 213 | preloadonly = :preloadonly, |
||||
| 214 | queue = :queue, |
||||
| 215 | updateversion = updateversion + 1 |
||||
| 216 | WHERE id = :id AND updateversion = :updateversion; |
||||
| 217 | SQL |
||||
| 218 | ); |
||||
| 219 | $statement->bindValue(':id', $this->id); |
||||
| 220 | $statement->bindValue(':updateversion', $this->updateversion); |
||||
| 221 | |||||
| 222 | $statement->bindValue(':name', $this->name); |
||||
| 223 | $statement->bindValue(":text", $this->text); |
||||
| 224 | $statement->bindValue(":jsquestion", $this->jsquestion); |
||||
| 225 | $statement->bindValue(":defaultaction", $this->defaultaction); |
||||
| 226 | $statement->bindValue(":active", $this->active); |
||||
| 227 | $statement->bindValue(":preloadonly", $this->preloadonly); |
||||
| 228 | $statement->bindValue(":queue", $this->queue); |
||||
| 229 | |||||
| 230 | if (!$statement->execute()) { |
||||
| 231 | throw new Exception($statement->errorInfo()); |
||||
| 232 | } |
||||
| 233 | |||||
| 234 | if ($statement->rowCount() !== 1) { |
||||
| 235 | throw new OptimisticLockFailedException(); |
||||
| 236 | } |
||||
| 237 | |||||
| 238 | $this->updateversion++; |
||||
| 239 | } |
||||
| 240 | } |
||||
| 241 | |||||
| 242 | /** |
||||
| 243 | * Override delete() from DataObject |
||||
| 244 | */ |
||||
| 245 | public function delete() |
||||
| 246 | { |
||||
| 247 | throw new Exception("You shouldn't be doing that, you'll break logs."); |
||||
| 248 | } |
||||
| 249 | |||||
| 250 | /** |
||||
| 251 | * @return string |
||||
| 252 | */ |
||||
| 253 | public function getName() |
||||
| 254 | { |
||||
| 255 | return $this->name; |
||||
| 256 | } |
||||
| 257 | |||||
| 258 | /** |
||||
| 259 | * @param string $name |
||||
| 260 | */ |
||||
| 261 | public function setName($name) |
||||
| 262 | { |
||||
| 263 | $this->name = $name; |
||||
| 264 | } |
||||
| 265 | |||||
| 266 | /** |
||||
| 267 | * @return string |
||||
| 268 | */ |
||||
| 269 | public function getText() |
||||
| 270 | { |
||||
| 271 | return $this->text; |
||||
| 272 | } |
||||
| 273 | |||||
| 274 | /** |
||||
| 275 | * @param string $text |
||||
| 276 | */ |
||||
| 277 | public function setText($text) |
||||
| 278 | { |
||||
| 279 | $this->text = $text; |
||||
| 280 | } |
||||
| 281 | |||||
| 282 | /** |
||||
| 283 | * @return string|null |
||||
| 284 | */ |
||||
| 285 | public function getJsquestion() |
||||
| 286 | { |
||||
| 287 | return $this->jsquestion; |
||||
| 288 | } |
||||
| 289 | |||||
| 290 | /** |
||||
| 291 | * @param string $jsquestion |
||||
| 292 | */ |
||||
| 293 | public function setJsquestion($jsquestion) |
||||
| 294 | { |
||||
| 295 | $this->jsquestion = $jsquestion; |
||||
| 296 | } |
||||
| 297 | |||||
| 298 | /** |
||||
| 299 | * @return string |
||||
| 300 | */ |
||||
| 301 | public function getDefaultAction() |
||||
| 302 | { |
||||
| 303 | return $this->defaultaction; |
||||
| 304 | } |
||||
| 305 | |||||
| 306 | /** |
||||
| 307 | * @param string $defaultAction |
||||
| 308 | */ |
||||
| 309 | public function setDefaultAction($defaultAction) |
||||
| 310 | { |
||||
| 311 | $this->defaultaction = $defaultAction; |
||||
| 312 | } |
||||
| 313 | |||||
| 314 | /** |
||||
| 315 | * @return bool |
||||
| 316 | */ |
||||
| 317 | public function getActive() |
||||
| 318 | { |
||||
| 319 | return $this->active == 1; |
||||
| 320 | } |
||||
| 321 | |||||
| 322 | /** |
||||
| 323 | * @param bool $active |
||||
| 324 | */ |
||||
| 325 | public function setActive($active) |
||||
| 326 | { |
||||
| 327 | $this->active = $active ? 1 : 0; |
||||
| 328 | } |
||||
| 329 | |||||
| 330 | /** |
||||
| 331 | * @return bool |
||||
| 332 | */ |
||||
| 333 | public function getPreloadOnly() |
||||
| 334 | { |
||||
| 335 | return $this->preloadonly == 1; |
||||
| 336 | } |
||||
| 337 | |||||
| 338 | /** |
||||
| 339 | * @param bool $preloadonly |
||||
| 340 | */ |
||||
| 341 | public function setPreloadOnly($preloadonly) |
||||
| 342 | { |
||||
| 343 | $this->preloadonly = $preloadonly ? 1 : 0; |
||||
| 344 | } |
||||
| 345 | |||||
| 346 | /** |
||||
| 347 | * @return int|null |
||||
| 348 | */ |
||||
| 349 | public function getQueue(): ?int |
||||
| 350 | { |
||||
| 351 | return $this->queue; |
||||
| 352 | } |
||||
| 353 | |||||
| 354 | /** |
||||
| 355 | * @return RequestQueue|null |
||||
| 356 | */ |
||||
| 357 | public function getQueueObject(): ?RequestQueue |
||||
| 358 | { |
||||
| 359 | if ($this->queue === null) { |
||||
| 360 | return null; |
||||
| 361 | } |
||||
| 362 | |||||
| 363 | /** @var $dataObject RequestQueue|false */ |
||||
| 364 | $dataObject = RequestQueue::getById($this->queue, $this->getDatabase()); |
||||
| 365 | |||||
| 366 | if ($dataObject === false) { |
||||
|
0 ignored issues
–
show
|
|||||
| 367 | return null; |
||||
| 368 | } |
||||
| 369 | |||||
| 370 | return $dataObject; |
||||
| 371 | } |
||||
| 372 | |||||
| 373 | /** |
||||
| 374 | * @param int|null $queue |
||||
| 375 | */ |
||||
| 376 | public function setQueue(?int $queue): void |
||||
| 377 | { |
||||
| 378 | $this->queue = $queue; |
||||
| 379 | } |
||||
| 380 | |||||
| 381 | public function getDomain(): int |
||||
| 382 | { |
||||
| 383 | return $this->domain; |
||||
| 384 | } |
||||
| 385 | |||||
| 386 | public function setDomain(int $domain): void |
||||
| 387 | { |
||||
| 388 | $this->domain = $domain; |
||||
| 389 | } |
||||
| 390 | } |
||||
| 391 |