xref: /plugin/structpublish/meta/Revision.php (revision 3449f9ce9770752ac28fe0246797d80e125747d5)
1<?php
2
3namespace dokuwiki\plugin\structpublish\meta;
4
5class Revision
6{
7    const STATUS_DRAFT = 'draft';
8    const STATUS_APPROVED = 'approved';
9    const STATUS_PUBLISHED = 'published';
10
11    protected $sqlite;
12    protected $schemas;
13    protected $id;
14    protected $rev;
15    protected $status;
16    protected $version = 0;
17    protected $user;
18
19    /**
20     * @param $sqlite
21     * @param string $id
22     * @param int $rev
23     */
24    public function __construct($sqlite, $id, $rev)
25    {
26        $this->sqlite = $sqlite;
27        $this->id = $id;
28        $this->rev = $rev;
29
30        $sql = 'SELECT * FROM structpublish_revisions WHERE id = ? AND rev = ?';
31        $res = $sqlite->query($sql, $id, $rev);
32        $vals = $sqlite->res2row($res);
33
34        if (!empty($vals)) {
35            $this->status = $vals['status'];
36            $this->version = $vals['version'];
37            $this->user = $vals['user'];
38        }
39    }
40
41    public function save()
42    {
43        // TODO reset publish status of older revisions
44        $sql = 'REPLACE INTO structpublish_revisions (id, rev, status, version, user) VALUES (?,?,?,?,?)';
45        $res = $this->sqlite->query(
46            $sql,
47            $this->id,
48            $this->rev,
49            $this->status,
50            $this->version,
51            $this->user
52        );
53
54        if ($this->status === self::STATUS_PUBLISHED) {
55            $this->updateCoreData();
56        }
57    }
58
59    /**
60     * Returns the latest version for a given id, or 0
61     *
62     * @return int
63     */
64    public function getLatestVersion()
65    {
66        $sql = 'SELECT MAX(version) AS latest FROM structpublish_revisions WHERE id = ?';
67        $res = $this->sqlite->query($sql, $this->id);
68        $res = $this->sqlite->res2arr($res);
69        return $res['latest'] ?? 0;
70    }
71
72    /**
73     * @return string
74     */
75    public function getId()
76    {
77        return $this->id;
78    }
79
80    /**
81     * @return int
82     */
83    public function getVersion()
84    {
85        return $this->version;
86    }
87
88    /**
89     * @param int $version
90     */
91    public function setVersion($version): void
92    {
93        $this->version = $version;
94    }
95
96    /**
97     * @return int
98     */
99    public function getRev()
100    {
101        return $this->rev;
102    }
103
104    /**
105     * @param int $rev
106     */
107    public function setRev($rev): void
108    {
109        $this->rev = $rev;
110    }
111
112    /**
113     * @return string
114     */
115    public function getStatus()
116    {
117        return $this->status ?? self::STATUS_DRAFT;
118    }
119
120    /**
121     * @param string $status
122     */
123    public function setStatus($status): void
124    {
125        $this->status = $status;
126    }
127
128    /**
129     * @return string
130     */
131    public function getUser()
132    {
133        return $this->user;
134    }
135
136    /**
137     * @param string $user
138     */
139    public function setUser($user): void
140    {
141        $this->user = $user;
142    }
143
144    /**
145     * Update publish status in the core table
146     */
147    protected function updateCoreData()
148    {
149        // FIXME we don't know anything about schemas yet!
150//        $sql = 'UPDATE data_schema SET published = NULL WHERE id = ?';
151//        $this->sqlite->query($sql, $this->id);
152    }
153}
154