Code Coverage
 
Lines
Functions and Methods
Classes and Traits
Total
100.00% covered (success)
100.00%
38 / 38
100.00% covered (success)
100.00%
4 / 4
CRAP
100.00% covered (success)
100.00%
1 / 1
Revision
100.00% covered (success)
100.00%
38 / 38
100.00% covered (success)
100.00%
4 / 4
11
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
 create
100.00% covered (success)
100.00%
10 / 10
100.00% covered (success)
100.00%
1 / 1
1
 get
100.00% covered (success)
100.00%
20 / 20
100.00% covered (success)
100.00%
1 / 1
8
 delete
100.00% covered (success)
100.00%
7 / 7
100.00% covered (success)
100.00%
1 / 1
1
1<?php
2
3/**
4 * The FAQ revisions 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 2020-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     2020-11-01
16 */
17
18declare(strict_types=1);
19
20namespace phpMyFAQ\Administration;
21
22use phpMyFAQ\Configuration;
23use phpMyFAQ\Database;
24
25/**
26 * Class Revision
27 *
28 * @package phpMyFAQ
29 */
30readonly class Revision
31{
32    /**
33     * Revision constructor.
34     */
35    public function __construct(
36        private Configuration $configuration,
37    ) {
38    }
39
40    /**
41     * Adds a new revision from a given FAQ ID and FAQ language
42     */
43    public function create(int $faqId, string $faqLanguage): bool
44    {
45        $query = sprintf(
46            "
47            INSERT INTO 
48                %sfaqdata_revisions 
49            SELECT 
50                id, lang, solution_id, revision_id + 1, active, sticky, keywords, thema, content, author, email, 
51                comment, updated, date_start, date_end, created, notes, sticky_order 
52            FROM 
53                %sfaqdata 
54            WHERE 
55                id = %d 
56              AND 
57                lang = '%s'",
58            Database::getTablePrefix(),
59            Database::getTablePrefix(),
60            $faqId,
61            $this->configuration->getDb()->escape($faqLanguage),
62        );
63
64        $this->configuration->getDb()->query($query);
65
66        return true;
67    }
68
69    /**
70     * Gets all revisions from a given FAQ ID and FAQ language
71     *
72     * @return list<array{revision_id: int, updated: string, author: string}>
73     */
74    public function get(int $faqId, string $faqLanguage, string $faqAuthor): array
75    {
76        $revisionData = [];
77
78        $query = sprintf(
79            "SELECT 
80                revision_id, updated, author FROM %sfaqdata_revisions
81            WHERE
82                id = %d
83            AND
84                lang = '%s'
85            ORDER BY 
86                revision_id",
87            Database::getTablePrefix(),
88            $faqId,
89            $this->configuration->getDb()->escape($faqLanguage),
90        );
91
92        $result = $this->configuration->getDb()->query($query);
93
94        if ($this->configuration->getDb()->numRows($result) > 0) {
95            while (true) {
96                $row = $this->configuration->getDb()->fetchObject($result);
97                if ($row === false || $row === null || $row === []) {
98                    break;
99                }
100
101                $revisionData[] = [
102                    'revision_id' => (int) $row->revision_id,
103                    'updated' => $faqId === 0 ? date(format: 'YmdHis') : (string) $row->updated,
104                    'author' => $faqId === 0 ? ucfirst($faqAuthor) : (string) $row->author,
105                ];
106            }
107        }
108
109        return $revisionData;
110    }
111
112    /**
113     * Deletes all revisions for a given FAQ ID and FAQ language
114     */
115    public function delete(int $faqId, string $faqLanguage): bool
116    {
117        $query = sprintf(
118            "DELETE FROM %sfaqdata_revisions WHERE id = %d AND lang = '%s'",
119            Database::getTablePrefix(),
120            $faqId,
121            $this->configuration->getDb()->escape($faqLanguage),
122        );
123
124        return (bool) $this->configuration->getDb()->query($query);
125    }
126}