Issues (186)

Branch: metrics-fix

includes/DataObjects/Domain.php (3 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\OptimisticLockFailedException;
16
use Waca\PdoDatabase;
17
use Waca\WebRequest;
18
19
class Domain extends DataObject
20
{
21
    /** @var string */
22
    private $shortname;
23
    /** @var string */
24
    private $longname;
25
    /** @var string */
26
    private $wikiarticlepath;
27
    /** @var string */
28
    private $wikiapipath;
29
    /** @var int */
30
    private $enabled = 0;
31
    /** @var int|null */
32
    private $defaultclose;
33
    /** @var string */
34
    private $defaultlanguage = 'en';
35
    /** @var string */
36
    private $emailreplyaddress;
37
    /** @var string|null */
38
    private $notificationtarget;
39
    /** @var string */
40
    private $localdocumentation;
41
42
    /** @var Domain Cache variable of the current domain */
43
    private static $currentDomain;
44
45
    public static function getCurrent(PdoDatabase $database)
46
    {
47
        if (self::$currentDomain === null) {
48
            $sessionDomain = WebRequest::getSessionDomain();
49
50
            if ($sessionDomain !== null) {
51
                /** @var Domain $domain */
52
                $domain = self::getById($sessionDomain, $database);
53
54
                if ($domain === false) {
0 ignored issues
show
The condition $domain === false is always false.
Loading history...
55
                    self::$currentDomain = self::getById(1, $database); // FIXME: #594 User::getCurrent($database)->getDefaultDomain();
56
                }
57
                else {
58
                    self::$currentDomain = $domain;
59
                }
60
            }
61
            else {
62
                self::$currentDomain = self::getById(1, $database); // FIXME: #594 User::getCurrent($database)->getDefaultDomain();
0 ignored issues
show
Documentation Bug introduced by
It seems like self::getById(1, $database) of type false is incompatible with the declared type Waca\DataObjects\Domain of property $currentDomain.

Our type inference engine has found an assignment to a property that is incompatible with the declared type of that property.

Either this assignment is in error or the assigned type should be added to the documentation/type hint for that property..

Loading history...
63
            }
64
        }
65
66
        return self::$currentDomain;
67
    }
68
69
    public static function getByShortName(string $shortName, PdoDatabase $database)
70
    {
71
        $statement = $database->prepare(<<<SQL
72
            SELECT * FROM domain WHERE shortname = :name;
73
SQL
74
        );
75
76
        $statement->execute([
77
            ':name' => $shortName,
78
        ]);
79
80
        /** @var RequestForm|false $result */
81
        $result = $statement->fetchObject(get_called_class());
82
83
        if ($result !== false) {
84
            $result->setDatabase($database);
85
        }
86
87
        return $result;
88
    }
89
90
    public static function getAll(PdoDatabase $database) {
91
        $statement = $database->prepare("SELECT * FROM domain;");
92
        $statement->execute();
93
94
        $resultObject = $statement->fetchAll(PDO::FETCH_CLASS, get_called_class());
95
96
        /** @var Domain $t */
97
        foreach ($resultObject as $t) {
98
            $t->setDatabase($database);
99
        }
100
101
        return $resultObject;
102
    }
103
104
    public function save()
105
    {
106
        if ($this->isNew()) {
107
            // insert
108
            $statement = $this->dbObject->prepare(<<<SQL
109
                INSERT INTO domain (
110
                    shortname, longname, wikiarticlepath, wikiapipath, enabled, defaultclose, defaultlanguage, 
111
                    emailreplyaddress, notificationtarget, localdocumentation
112
                ) VALUES (
113
                    :shortname, :longname, :wikiarticlepath, :wikiapipath, :enabled, :defaultclose, :defaultlanguage,
114
                    :emailreplyaddress, :notificationtarget, :localdocumentation
115
                );
116
SQL
117
            );
118
119
            $statement->bindValue(":shortname", $this->shortname);
120
            $statement->bindValue(":longname", $this->longname);
121
            $statement->bindValue(":wikiarticlepath", $this->wikiarticlepath);
122
            $statement->bindValue(":wikiapipath", $this->wikiapipath);
123
            $statement->bindValue(":enabled", $this->enabled);
124
            $statement->bindValue(":defaultclose", $this->defaultclose);
125
            $statement->bindValue(":defaultlanguage", $this->defaultlanguage);
126
            $statement->bindValue(":emailreplyaddress", $this->emailreplyaddress);
127
            $statement->bindValue(":notificationtarget", $this->notificationtarget);
128
            $statement->bindValue(":localdocumentation", $this->localdocumentation);
129
130
131
            if ($statement->execute()) {
132
                $this->id = (int)$this->dbObject->lastInsertId();
133
            }
134
            else {
135
                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

135
                throw new Exception(/** @scrutinizer ignore-type */ $statement->errorInfo());
Loading history...
136
            }
137
        }
138
        else {
139
            $statement = $this->dbObject->prepare(<<<SQL
140
                UPDATE domain SET
141
                    longname = :longname,
142
                    wikiarticlepath = :wikiarticlepath,
143
                    wikiapipath = :wikiapipath,
144
                    enabled = :enabled,
145
                    defaultclose = :defaultclose,
146
                    defaultlanguage = :defaultlanguage,
147
                    emailreplyaddress = :emailreplyaddress,
148
                    notificationtarget = :notificationtarget,
149
                    localdocumentation = :localdocumentation,
150
                
151
                    updateversion = updateversion + 1
152
				WHERE id = :id AND updateversion = :updateversion;
153
SQL
154
            );
155
156
            $statement->bindValue(":longname", $this->longname);
157
            $statement->bindValue(":wikiarticlepath", $this->wikiarticlepath);
158
            $statement->bindValue(":wikiapipath", $this->wikiapipath);
159
            $statement->bindValue(":enabled", $this->enabled);
160
            $statement->bindValue(":defaultclose", $this->defaultclose);
161
            $statement->bindValue(":defaultlanguage", $this->defaultlanguage);
162
            $statement->bindValue(":emailreplyaddress", $this->emailreplyaddress);
163
            $statement->bindValue(":notificationtarget", $this->notificationtarget);
164
            $statement->bindValue(":localdocumentation", $this->localdocumentation);
165
166
            $statement->bindValue(':id', $this->id);
167
            $statement->bindValue(':updateversion', $this->updateversion);
168
169
            if (!$statement->execute()) {
170
                throw new Exception($statement->errorInfo());
171
            }
172
173
            if ($statement->rowCount() !== 1) {
174
                throw new OptimisticLockFailedException();
175
            }
176
177
            $this->updateversion++;
178
        }
179
    }
180
181
    /**
182
     * @return string
183
     */
184
    public function getShortName(): string
185
    {
186
        return $this->shortname;
187
    }
188
189
    /**
190
     * @param string $shortName
191
     */
192
    public function setShortName(string $shortName): void
193
    {
194
        $this->shortname = $shortName;
195
    }
196
197
    /**
198
     * @return string
199
     */
200
    public function getLongName(): string
201
    {
202
        return $this->longname;
203
    }
204
205
    /**
206
     * @param string $longName
207
     */
208
    public function setLongName(string $longName): void
209
    {
210
        $this->longname = $longName;
211
    }
212
213
    /**
214
     * @return string
215
     */
216
    public function getWikiArticlePath(): string
217
    {
218
        return $this->wikiarticlepath;
219
    }
220
221
    /**
222
     * @param string $wikiArticlePath
223
     */
224
    public function setWikiArticlePath(string $wikiArticlePath): void
225
    {
226
        $this->wikiarticlepath = $wikiArticlePath;
227
    }
228
229
    /**
230
     * @return string
231
     */
232
    public function getWikiApiPath(): string
233
    {
234
        return $this->wikiapipath;
235
    }
236
237
    /**
238
     * @param string $wikiApiPath
239
     */
240
    public function setWikiApiPath(string $wikiApiPath): void
241
    {
242
        $this->wikiapipath = $wikiApiPath;
243
    }
244
245
    /**
246
     * @return bool
247
     */
248
    public function isEnabled(): bool
249
    {
250
        return $this->enabled == 1;
251
    }
252
253
    /**
254
     * @param bool $enabled
255
     */
256
    public function setEnabled(bool $enabled): void
257
    {
258
        $this->enabled = $enabled ? 1 : 0;
259
    }
260
261
    /**
262
     * @return int
263
     */
264
    public function getDefaultClose(): ?int
265
    {
266
        return $this->defaultclose;
267
    }
268
269
    /**
270
     * @param int $defaultClose
271
     */
272
    public function setDefaultClose(?int $defaultClose): void
273
    {
274
        $this->defaultclose = $defaultClose;
275
    }
276
277
    /**
278
     * @return string
279
     */
280
    public function getDefaultLanguage(): string
281
    {
282
        return $this->defaultlanguage;
283
    }
284
285
    /**
286
     * @param string $defaultLanguage
287
     */
288
    public function setDefaultLanguage(string $defaultLanguage): void
289
    {
290
        $this->defaultlanguage = $defaultLanguage;
291
    }
292
293
    /**
294
     * @return string
295
     */
296
    public function getEmailReplyAddress(): string
297
    {
298
        return $this->emailreplyaddress;
299
    }
300
301
    /**
302
     * @param string $emailReplyAddress
303
     */
304
    public function setEmailReplyAddress(string $emailReplyAddress): void
305
    {
306
        $this->emailreplyaddress = $emailReplyAddress;
307
    }
308
309
    /**
310
     * @return string|null
311
     */
312
    public function getNotificationTarget(): ?string
313
    {
314
        return $this->notificationtarget;
315
    }
316
317
    /**
318
     * @param string|null $notificationTarget
319
     */
320
    public function setNotificationTarget(?string $notificationTarget): void
321
    {
322
        $this->notificationtarget = $notificationTarget;
323
    }
324
325
    /**
326
     * @return string
327
     */
328
    public function getLocalDocumentation(): string
329
    {
330
        return $this->localdocumentation;
331
    }
332
333
    /**
334
     * @param string $localDocumentation
335
     */
336
    public function setLocalDocumentation(string $localDocumentation): void
337
    {
338
        $this->localdocumentation = $localDocumentation;
339
    }
340
}