Code Coverage
 
Lines
Functions and Methods
Classes and Traits
Total
100.00% covered (success)
100.00%
31 / 31
100.00% covered (success)
100.00%
3 / 3
CRAP
100.00% covered (success)
100.00%
1 / 1
Changelog
100.00% covered (success)
100.00%
31 / 31
100.00% covered (success)
100.00%
3 / 3
6
100.00% covered (success)
100.00%
1 / 1
 __construct
100.00% covered (success)
100.00%
1 / 1
100.00% covered (success)
100.00%
1 / 1
1
 add
100.00% covered (success)
100.00%
14 / 14
100.00% covered (success)
100.00%
1 / 1
1
 getByFaqId
100.00% covered (success)
100.00%
16 / 16
100.00% covered (success)
100.00%
1 / 1
4
1<?php
2
3/**
4 * The main changelog class.
5 *
6 * This Source Code Form is subject to the terms of the Mozilla Public License,
7 * v. 2.0. If a copy of the MPL was not distributed with this file, You can
8 * obtain one at https://mozilla.org/MPL/2.0/.
9 *
10 * @package   phpMyFAQ
11 * @author    Thorsten Rinne <thorsten@phpmyfaq.de>
12 * @copyright 2019-2026 phpMyFAQ Team
13 * @license   https://www.mozilla.org/MPL/2.0/ Mozilla Public License Version 2.0
14 * @link      https://www.phpmyfaq.de
15 * @since     2019-11-22
16 */
17
18declare(strict_types=1);
19
20namespace phpMyFAQ\Administration;
21
22use phpMyFAQ\Configuration;
23use phpMyFAQ\Database;
24use Symfony\Component\HttpFoundation\Request;
25
26/**
27 * Class Changelog
28 *
29 * @package phpMyFAQ
30 */
31readonly class Changelog
32{
33    /**
34     * Changelog constructor.
35     */
36    public function __construct(
37        private Configuration $configuration,
38    ) {
39    }
40
41    /**
42     * Adds a new changelog entry in the table "faqchanges".
43     */
44    public function add(int $id, int $userId, string $text, string $lang, int $revisionId = 0): bool
45    {
46        $db = $this->configuration->getDb();
47        $query = sprintf(
48            "INSERT INTO
49                %sfaqchanges
50            (id, beitrag, lang, revision_id, usr, datum, what)
51                VALUES
52            (%d, %d, '%s', %d, %d, %d, '%s')",
53            Database::getTablePrefix(),
54            $db->nextId(Database::getTablePrefix() . 'faqchanges', 'id'),
55            $id,
56            $db->escape($lang),
57            $revisionId,
58            $userId,
59            (int) Request::createFromGlobals()->server->get('REQUEST_TIME'),
60            $db->escape($text),
61        );
62
63        return (bool) $db->query($query);
64    }
65
66    /**
67     * Returns the changelog of a FAQ record.
68     */
69    public function getByFaqId(int $recordId): array
70    {
71        $entries = [];
72
73        $query = sprintf('
74            SELECT
75                DISTINCT revision_id, usr, datum, what
76            FROM
77                %sfaqchanges
78            WHERE
79                beitrag = %d
80            ORDER BY revision_id, datum DESC', Database::getTablePrefix(), $recordId);
81
82        $result = $this->configuration->getDb()->query($query);
83        if ($result) {
84            while (true) {
85                $row = $this->configuration->getDb()->fetchObject($result);
86                if (!$row) {
87                    break;
88                }
89
90                $entries[] = [
91                    'revision_id' => $row->revision_id,
92                    'user' => $row->usr,
93                    'date' => $row->datum,
94                    'changelog' => $row->what,
95                ];
96            }
97        }
98
99        return $entries;
100    }
101}