Issues (186)

includes/DataObjects/RequestQueue.php (5 issues)

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
class RequestQueue extends DataObject
20
{
21
    const DEFAULT_DEFAULT = 'default';
22
    const DEFAULT_ANTISPOOF = 'antispoof';
23
    const DEFAULT_TITLEBLACKLIST = 'titleblacklist';
24
25
    /** @var int */
26
    private $enabled = 0;
27
    /** @var int */
28
    private $isdefault = 0;
29
    /** @var int */
30
    private $defaultantispoof = 0;
31
    /** @var int */
32
    private $defaulttitleblacklist = 0;
33
    /** @var int */
34
    private $domain;
35
    /** @var string */
36
    private $apiname;
37
    /** @var string */
38
    private $displayname;
39
    /** @var string */
40
    private $header;
41
    /** @var string|null */
42
    private $help;
43
    /**
44
     * @var string
45
     * @deprecated Removal due as part of #607
46
     */
47
    private $logname;
48
    /**
49
     * @param PdoDatabase $database
50
     *
51
     * @return RequestQueue[]
52
     */
53
    public static function getAllQueues(PdoDatabase $database)
54
    {
55
        $statement = $database->prepare(<<<SQL
56
            SELECT * FROM requestqueue;
57
SQL
58
        );
59
        $statement->execute();
60
61
        $resultObject = $statement->fetchAll(PDO::FETCH_CLASS, get_called_class());
62
63
        /** @var RequestQueue $t */
64
        foreach ($resultObject as $t) {
65
            $t->setDatabase($database);
66
        }
67
68
        return $resultObject;
69
    }
70
71
    /**
72
     * @param PdoDatabase $database
73
     *
74
     * @return RequestQueue[]
75
     */
76
    public static function getEnabledQueues(PdoDatabase $database)
77
    {
78
        $statement = $database->prepare(<<<SQL
79
            SELECT * FROM requestqueue WHERE enabled = 1;
80
SQL
81
        );
82
        $statement->execute();
83
84
        $resultObject = $statement->fetchAll(PDO::FETCH_CLASS, get_called_class());
85
86
        /** @var RequestQueue $t */
87
        foreach ($resultObject as $t) {
88
            $t->setDatabase($database);
89
        }
90
91
        return $resultObject;
92
    }
93
94
    /**
95
     * @param PdoDatabase $database
96
     * @param string      $apiName
97
     * @param int         $domain
98
     *
99
     * @return false|RequestQueue
100
     */
101
    public static function getByApiName(PdoDatabase $database, string $apiName, int $domain)
102
    {
103
        $statement = $database->prepare(<<<SQL
104
            SELECT * FROM requestqueue WHERE apiname = :apiName AND domain = :domain;
105
SQL
106
        );
107
108
        $statement->execute([
109
            ':apiName' => $apiName,
110
            ':domain'  => $domain,
111
        ]);
112
113
        /** @var RequestQueue|false $result */
114
        $result = $statement->fetchObject(get_called_class());
115
116
        if ($result !== false) {
117
            $result->setDatabase($database);
118
        }
119
120
        return $result;
121
    }
122
123
    /**
124
     * @param PdoDatabase $database
125
     * @param string      $displayName
126
     * @param int         $domain
127
     *
128
     * @return false|RequestQueue
129
     */
130
    public static function getByDisplayName(PdoDatabase $database, string $displayName, int $domain)
131
    {
132
        $statement = $database->prepare(<<<SQL
133
            SELECT * FROM requestqueue WHERE displayname = :displayName AND domain = :domain;
134
SQL
135
        );
136
137
        $statement->execute([
138
            ':displayName' => $displayName,
139
            ':domain'      => $domain,
140
        ]);
141
142
        /** @var RequestQueue|false $result */
143
        $result = $statement->fetchObject(get_called_class());
144
145
        if ($result !== false) {
146
            $result->setDatabase($database);
147
        }
148
149
        return $result;
150
    }
151
152
    /**
153
     * @param PdoDatabase $database
154
     * @param string      $header
155
     * @param int         $domain
156
     *
157
     * @return false|RequestQueue
158
     */
159
    public static function getByHeader(PdoDatabase $database, string $header, int $domain)
160
    {
161
        $statement = $database->prepare(<<<SQL
162
            SELECT * FROM requestqueue WHERE header = :header AND domain = :domain;
163
SQL
164
        );
165
166
        $statement->execute([
167
            ':header' => $header,
168
            ':domain' => $domain,
169
        ]);
170
171
        /** @var RequestQueue|false $result */
172
        $result = $statement->fetchObject(get_called_class());
173
174
        if ($result !== false) {
175
            $result->setDatabase($database);
176
        }
177
178
        return $result;
179
    }
180
181
    /**
182
     * @param PdoDatabase $database
183
     * @param int         $domain
184
     *
185
     * @param string      $defaultType The type of default queue to get.
186
     *
187
     * @return false|RequestQueue
188
     * @throws ApplicationLogicException
189
     */
190
    public static function getDefaultQueue(PdoDatabase $database, int $domain, string $defaultType = 'default')
191
    {
192
        switch ($defaultType) {
193
            case self::DEFAULT_DEFAULT:
194
                $sql = 'SELECT * FROM requestqueue WHERE isdefault = 1 AND domain = :domain;';
195
                break;
196
            case self::DEFAULT_ANTISPOOF:
197
                $sql = 'SELECT * FROM requestqueue WHERE defaultantispoof = 1 AND domain = :domain;';
198
                break;
199
            case self::DEFAULT_TITLEBLACKLIST:
200
                $sql = 'SELECT * FROM requestqueue WHERE defaulttitleblacklist = 1 AND domain = :domain;';
201
                break;
202
            default:
203
                throw new ApplicationLogicException('Unknown request for default queue');
204
        }
205
206
        $statement = $database->prepare($sql);
207
208
        $statement->execute([':domain' => $domain]);
209
210
        /** @var RequestQueue|false $result */
211
        $result = $statement->fetchObject(get_called_class());
212
213
        if ($result !== false) {
214
            $result->setDatabase($database);
215
        }
216
217
        return $result;
218
    }
219
220
    public function save()
221
    {
222
        // find and squish existing defaults
223
        if ($this->isDefault()) {
224
            $squishStatement = $this->dbObject->prepare('UPDATE requestqueue SET isdefault = 0 WHERE isdefault = 1 AND domain = :domain;');
225
            $squishStatement->execute([':domain' => $this->domain]);
226
        }
227
228
        if ($this->isDefaultAntispoof()) {
229
            $squishStatement = $this->dbObject->prepare('UPDATE requestqueue SET defaultantispoof = 0 WHERE defaultantispoof = 1 AND domain = :domain;');
230
            $squishStatement->execute([':domain' => $this->domain]);
231
        }
232
233
        if ($this->isDefaultTitleBlacklist()) {
234
            $squishStatement = $this->dbObject->prepare('UPDATE requestqueue SET defaulttitleblacklist = 0 WHERE defaulttitleblacklist = 1 AND domain = :domain;');
235
            $squishStatement->execute([':domain' => $this->domain]);
236
        }
237
238
        if ($this->isNew()) {
239
            // insert
240
            $statement = $this->dbObject->prepare(<<<SQL
241
                INSERT INTO requestqueue (
242
                    enabled, isdefault, defaultantispoof, defaulttitleblacklist, domain, apiname, displayname, header, help, logname
243
                ) VALUES (
244
                    :enabled, :isdefault, :defaultantispoof, :defaulttitleblacklist, :domain, :apiname, :displayname, :header, :help, :logname
245
                );
246
SQL
247
            );
248
249
            $statement->bindValue(":enabled", $this->enabled);
250
            $statement->bindValue(":isdefault", $this->isdefault);
251
            $statement->bindValue(":defaultantispoof", $this->defaultantispoof);
252
            $statement->bindValue(":defaulttitleblacklist", $this->defaulttitleblacklist);
253
            $statement->bindValue(":domain", $this->domain);
254
            $statement->bindValue(":apiname", $this->apiname);
255
            $statement->bindValue(":displayname", $this->displayname);
256
            $statement->bindValue(":header", $this->header);
257
            $statement->bindValue(":help", $this->help);
258
            $statement->bindValue(":logname", $this->logname);
0 ignored issues
show
Deprecated Code introduced by
The property Waca\DataObjects\RequestQueue::$logname has been deprecated: Removal due as part of #607 ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-deprecated  annotation

258
            $statement->bindValue(":logname", /** @scrutinizer ignore-deprecated */ $this->logname);

This property has been deprecated. The supplier of the class has supplied an explanatory message.

The explanatory message should give you some clue as to whether and when the property will be removed from the class and what other property to use instead.

Loading history...
259
260
            if ($statement->execute()) {
261
                $this->id = (int)$this->dbObject->lastInsertId();
262
            }
263
            else {
264
                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 ignore-type  annotation

264
                throw new Exception(/** @scrutinizer ignore-type */ $statement->errorInfo());
Loading history...
265
            }
266
        }
267
        else {
268
            $statement = $this->dbObject->prepare(<<<SQL
269
                UPDATE requestqueue SET
270
                    enabled = :enabled,
271
                    isdefault = :isdefault,
272
                    defaultantispoof = :defaultantispoof,
273
                    defaulttitleblacklist = :defaulttitleblacklist,
274
                    domain = :domain,
275
                    apiname = :apiname,
276
                    displayname = :displayname,
277
                    header = :header,
278
                    help = :help,
279
                    logname = :logname,
280
                
281
                    updateversion = updateversion + 1
282
				WHERE id = :id AND updateversion = :updateversion;
283
SQL
284
            );
285
286
            $statement->bindValue(":enabled", $this->enabled);
287
            $statement->bindValue(":isdefault", $this->isdefault);
288
            $statement->bindValue(":defaultantispoof", $this->defaultantispoof);
289
            $statement->bindValue(":defaulttitleblacklist", $this->defaulttitleblacklist);
290
            $statement->bindValue(":domain", $this->domain);
291
            $statement->bindValue(":apiname", $this->apiname);
292
            $statement->bindValue(":displayname", $this->displayname);
293
            $statement->bindValue(":header", $this->header);
294
            $statement->bindValue(":help", $this->help);
295
            $statement->bindValue(":logname", $this->logname);
0 ignored issues
show
Deprecated Code introduced by
The property Waca\DataObjects\RequestQueue::$logname has been deprecated: Removal due as part of #607 ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-deprecated  annotation

295
            $statement->bindValue(":logname", /** @scrutinizer ignore-deprecated */ $this->logname);

This property has been deprecated. The supplier of the class has supplied an explanatory message.

The explanatory message should give you some clue as to whether and when the property will be removed from the class and what other property to use instead.

Loading history...
296
297
            $statement->bindValue(':id', $this->id);
298
            $statement->bindValue(':updateversion', $this->updateversion);
299
300
            if (!$statement->execute()) {
301
                throw new Exception($statement->errorInfo());
302
            }
303
304
            if ($statement->rowCount() !== 1) {
305
                throw new OptimisticLockFailedException();
306
            }
307
308
            $this->updateversion++;
309
        }
310
    }
311
312
    /**
313
     * @return bool
314
     */
315
    public function isEnabled(): bool
316
    {
317
        return $this->enabled == 1;
318
    }
319
320
    /**
321
     * @param bool $enabled
322
     */
323
    public function setEnabled(bool $enabled): void
324
    {
325
        $this->enabled = $enabled ? 1 : 0;
326
    }
327
328
    /**
329
     * @return bool
330
     */
331
    public function isDefault(): bool
332
    {
333
        return $this->isdefault == 1;
334
    }
335
336
    /**
337
     * @param bool $isDefault
338
     */
339
    public function setDefault(bool $isDefault): void
340
    {
341
        $this->isdefault = $isDefault ? 1 : 0;
342
    }
343
344
    /**
345
     * @return bool
346
     */
347
    public function isDefaultAntispoof(): bool
348
    {
349
        return $this->defaultantispoof == 1;
350
    }
351
352
    /**
353
     * @param bool $isDefault
354
     */
355
    public function setDefaultAntispoof(bool $isDefault): void
356
    {
357
        $this->defaultantispoof = $isDefault ? 1 : 0;
358
    }
359
360
    /**
361
     * @return bool
362
     */
363
    public function isDefaultTitleBlacklist(): bool
364
    {
365
        return $this->defaulttitleblacklist == 1;
366
    }
367
368
    /**
369
     * @param bool $isDefault
370
     */
371
    public function setDefaultTitleBlacklist(bool $isDefault): void
372
    {
373
        $this->defaulttitleblacklist = $isDefault ? 1 : 0;
374
    }
375
376
    /**
377
     * @return int
378
     */
379
    public function getDomain(): int
380
    {
381
        return $this->domain;
382
    }
383
384
    /**
385
     * @param int $domain
386
     */
387
    public function setDomain(int $domain): void
388
    {
389
        $this->domain = $domain;
390
    }
391
392
    /**
393
     * @return string
394
     */
395
    public function getApiName(): string
396
    {
397
        return $this->apiname;
398
    }
399
400
    /**
401
     * @param string $apiName
402
     */
403
    public function setApiName(string $apiName): void
404
    {
405
        $this->apiname = $apiName;
406
    }
407
408
    /**
409
     * @return string
410
     */
411
    public function getDisplayName(): string
412
    {
413
        return $this->displayname;
414
    }
415
416
    /**
417
     * @param string $displayName
418
     */
419
    public function setDisplayName(string $displayName): void
420
    {
421
        $this->displayname = $displayName;
422
    }
423
424
    /**
425
     * @return string
426
     */
427
    public function getHeader(): string
428
    {
429
        return $this->header;
430
    }
431
432
    /**
433
     * @param string $header
434
     */
435
    public function setHeader(string $header): void
436
    {
437
        $this->header = $header;
438
    }
439
440
    /**
441
     * @return string|null
442
     */
443
    public function getHelp(): ?string
444
    {
445
        return $this->help;
446
    }
447
448
    /**
449
     * @param string|null $help
450
     */
451
    public function setHelp(?string $help): void
452
    {
453
        $this->help = $help;
454
    }
455
456
    /**
457
     * @return string
458
     * @deprecated
459
     */
460
    public function getLogName(): string
461
    {
462
        return $this->logname;
0 ignored issues
show
Deprecated Code introduced by
The property Waca\DataObjects\RequestQueue::$logname has been deprecated: Removal due as part of #607 ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-deprecated  annotation

462
        return /** @scrutinizer ignore-deprecated */ $this->logname;

This property has been deprecated. The supplier of the class has supplied an explanatory message.

The explanatory message should give you some clue as to whether and when the property will be removed from the class and what other property to use instead.

Loading history...
463
    }
464
465
    /**
466
     * @param string $logName
467
     *
468
     * @deprecated
469
     */
470
    public function setLogName(string $logName): void
471
    {
472
        $this->logname = $logName;
0 ignored issues
show
Deprecated Code introduced by
The property Waca\DataObjects\RequestQueue::$logname has been deprecated: Removal due as part of #607 ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-deprecated  annotation

472
        /** @scrutinizer ignore-deprecated */ $this->logname = $logName;

This property has been deprecated. The supplier of the class has supplied an explanatory message.

The explanatory message should give you some clue as to whether and when the property will be removed from the class and what other property to use instead.

Loading history...
473
    }
474
}