Issues (195)

includes/DataObjects/Comment.php (1 issue)

Labels
Severity
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 DateTimeImmutable;
12
use Exception;
13
use PDO;
14
use Waca\DataObject;
15
use Waca\Exceptions\OptimisticLockFailedException;
16
use Waca\PdoDatabase;
17
18
/**
19
 * Comment data object
20
 */
21
class Comment extends DataObject
22
{
23
    private $time;
24
    private $user;
25
    private $comment;
26
    private $visibility = "user";
27
    private $request;
28
    private $flagged = 0;
29
    private $edited;
30
31
    /**
32
     * Retrieves all comments for a request, optionally filtered
33
     *
34
     * @param integer     $id             Request ID to search by
35
     * @param PdoDatabase $database
36
     * @param bool        $showRestricted True to show all comments, False to show only unprotected comments, and protected
37
     *                                    comments visible to $userId
38
     * @param bool        $showCheckuser
39
     * @param null|int    $userId         User to filter by
40
     *
41
     * @return Comment[]
42
     */
43
    public static function getForRequest($id, PdoDatabase $database, $showRestricted = false, $showCheckuser = false, $userId = null)
44
    {
45
        $parameters = ['requester', 'user'];
46
        if ($showCheckuser) {
47
            $parameters[] = 'checkuser';
48
        }
49
        if ($showRestricted) {
50
            $parameters[] = 'admin';
51
        }
52
53
        $visibilityPlaceholders = str_repeat('?,', count($parameters) - 1) . '?';
54
55
        $statement = $database->prepare(<<<SQL
56
SELECT * FROM comment
57
WHERE (visibility in (${visibilityPlaceholders}) OR user = ?) AND request = ?;
58
SQL
59
        );
60
61
        $parameters[] = $userId;
62
        $parameters[] = $id;
63
64
        $statement->execute($parameters);
65
66
        $result = array();
67
        /** @var Comment $v */
68
        foreach ($statement->fetchAll(PDO::FETCH_CLASS, get_called_class()) as $v) {
69
            $v->setDatabase($database);
70
            $result[] = $v;
71
        }
72
73
        return $result;
74
    }
75
76
    public static function getFlaggedComments(PdoDatabase $database)
77
    {
78
        $statement = $database->prepare('SELECT * FROM comment WHERE flagged = 1;');
79
        $statement->execute();
80
81
        $result = array();
82
        /** @var Comment $v */
83
        foreach ($statement->fetchAll(PDO::FETCH_CLASS, get_called_class()) as $v) {
84
            $v->setDatabase($database);
85
            $result[] = $v;
86
        }
87
88
        return $result;
89
    }
90
91
    /**
92
     * @throws Exception
93
     */
94
    public function save()
95
    {
96
        if ($this->isNew()) {
97
            // insert
98
            $statement = $this->dbObject->prepare(<<<SQL
99
INSERT INTO comment ( time, user, comment, visibility, request, flagged )
100
VALUES ( CURRENT_TIMESTAMP(), :user, :comment, :visibility, :request, :flagged );
101
SQL
102
            );
103
            $statement->bindValue(":user", $this->user);
104
            $statement->bindValue(":comment", $this->comment);
105
            $statement->bindValue(":visibility", $this->visibility);
106
            $statement->bindValue(":request", $this->request);
107
            $statement->bindValue(":flagged", $this->flagged);
108
109
            if ($statement->execute()) {
110
                $this->id = (int)$this->dbObject->lastInsertId();
111
            }
112
            else {
113
                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

113
                throw new Exception(/** @scrutinizer ignore-type */ $statement->errorInfo());
Loading history...
114
            }
115
        }
116
        else {
117
            // update
118
            $statement = $this->dbObject->prepare(<<<SQL
119
UPDATE comment
120
SET comment = :comment, visibility = :visibility, flagged = :flagged, edited = :edited, updateversion = updateversion + 1
121
WHERE id = :id AND updateversion = :updateversion;
122
SQL
123
            );
124
125
            $statement->bindValue(':id', $this->id);
126
            $statement->bindValue(':updateversion', $this->updateversion);
127
128
            $statement->bindValue(':comment', $this->comment);
129
            $statement->bindValue(':visibility', $this->visibility);
130
            $statement->bindValue(":flagged", $this->flagged);
131
            $statement->bindValue(":edited", $this->edited);
132
133
            if (!$statement->execute()) {
134
                throw new Exception($statement->errorInfo());
135
            }
136
137
            if ($statement->rowCount() !== 1) {
138
                throw new OptimisticLockFailedException();
139
            }
140
141
            $this->updateversion++;
142
        }
143
    }
144
145
    /**
146
     * @return DateTimeImmutable
147
     */
148
    public function getTime()
149
    {
150
        return new DateTimeImmutable($this->time);
151
    }
152
153
    /**
154
     * @return int
155
     */
156
    public function getUser()
157
    {
158
        return $this->user;
159
    }
160
161
    /**
162
     * @param int $user
163
     */
164
    public function setUser($user)
165
    {
166
        $this->user = $user;
167
    }
168
169
    /**
170
     * @return string
171
     */
172
    public function getComment()
173
    {
174
        return $this->comment;
175
    }
176
177
    /**
178
     * @param string $comment
179
     */
180
    public function setComment($comment)
181
    {
182
        $this->comment = $comment;
183
    }
184
185
    /**
186
     * @return string
187
     */
188
    public function getVisibility()
189
    {
190
        return $this->visibility;
191
    }
192
193
    /**
194
     * @param string $visibility
195
     */
196
    public function setVisibility($visibility)
197
    {
198
        $this->visibility = $visibility;
199
    }
200
201
    /**
202
     * @return int
203
     */
204
    public function getRequest()
205
    {
206
        return $this->request;
207
    }
208
209
    /**
210
     * @param int $request
211
     */
212
    public function setRequest($request)
213
    {
214
        $this->request = $request;
215
    }
216
217
    /**
218
     * @return bool
219
     */
220
    public function getFlagged() : bool
221
    {
222
        return $this->flagged == 1;
223
    }
224
225
    /**
226
     * @param bool $flagged
227
     */
228
    public function setFlagged(bool $flagged): void
229
    {
230
        $this->flagged = $flagged ? 1 : 0;
231
    }
232
233
    public function touchEdited() : void
234
    {
235
        $dateTimeImmutable = new DateTimeImmutable("now");
236
        $this->edited = $dateTimeImmutable->format('Y-m-d H:i:s');
237
    }
238
239
    public function getEdited() : ?DateTimeImmutable
240
    {
241
        if ($this->edited === null) {
242
            return null;
243
        }
244
245
        return new DateTimeImmutable($this->edited);
246
    }
247
}
248