xref: /plugin/structpublish/meta/Revision.php (revision e31c94d7935dccc7709b66e483c744379995197d)
1<?php
2
3namespace dokuwiki\plugin\structpublish\meta;
4
5use dokuwiki\plugin\struct\meta\ConfigParser;
6use dokuwiki\plugin\struct\meta\Schema;
7use dokuwiki\plugin\struct\meta\SearchConfig;
8use dokuwiki\plugin\struct\meta\Value;
9
10class Revision
11{
12    /** @var \helper_plugin_sqlite */
13    protected $sqlite;
14
15    protected $schema;
16
17    protected $id;
18    protected $rev;
19    protected $published;
20    protected $status;
21    protected $version;
22    protected $user;
23    protected $date;
24    /**
25     * @var bool|\dokuwiki\plugin\struct\meta\Column
26     */
27    protected $statusCol;
28    protected $versionCol;
29    protected $userCol;
30    protected $dateCol;
31    protected $revisionCol;
32
33    /**
34     * @param $sqlite
35     * @param string $id
36     * @param int $rev
37     */
38    public function __construct($sqlite, $id, $rev)
39    {
40        $this->sqlite = $sqlite;
41        $this->id = $id;
42        $this->rev = $rev;
43        $this->published = 0;
44
45        $this->schema = new Schema('structpublish');
46        $this->statusCol = $this->schema->findColumn('status');
47        $this->versionCol = $this->schema->findColumn('version');
48        $this->userCol = $this->schema->findColumn('user');
49        $this->dateCol = $this->schema->findColumn('date');
50        $this->revisionCol = $this->schema->findColumn('revision');
51
52        /** @var Value[] $values */
53        $values = $this->getCoreData('revision=' . $this->rev);
54
55        if (!empty($values)) {
56            $this->status = $values[$this->statusCol->getColref() - 1]->getRawValue();
57            $this->version = $values[$this->versionCol->getColref() - 1]->getRawValue();
58            $this->user = $values[$this->userCol->getColref() - 1]->getRawValue();
59            $this->date = $values[$this->dateCol->getColref() - 1]->getRawValue();
60        }
61    }
62
63    public function save()
64    {
65        // drafts reference the latest version
66        if ($this->status === Constants::STATUS_DRAFT) {
67            //FIXME no rev yet
68            $this->setVersion($this->getVersion());
69        }
70
71        if ($this->status === Constants::STATUS_PUBLISHED) {
72            $this->published = 1;
73        }
74
75        $this->updateCoreData($this->id);
76        // TODO reset publish status of older revisions
77
78    }
79
80    /**
81     * @return int
82     */
83    public function getVersion()
84    {
85        return (int)$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;
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    public function getDate()
145    {
146        return $this->date;
147    }
148
149    public function setDate($time)
150    {
151        $this->date = date('Y-m-d', $time);
152    }
153
154    /**
155     * Update publish status in the core table
156     */
157    protected function updateCoreData($pid, $rid = 0)
158    {
159        $data = [
160            'status' => $this->status,
161            'user' => $this->user,
162            'date' => $this->date,
163            'revision' => $this->rev,
164            'version' => $this->version,
165        ];
166        $schema = new Schema('structpublish', 0);
167        $access = new AccessTableStructpublish($schema, $pid, 0, $rid);
168        $access->setPublished($this->published);
169        $access->saveData($data);
170    }
171
172    public function getCoreData($andFilter = '')
173    {
174        $lines = [
175            'schema: structpublish',
176            'cols: *',
177            'filter: %pageid% = $ID$'
178        ];
179
180        if ($andFilter) {
181            $lines[] = 'filter: ' . $andFilter;
182        }
183
184        $parser = new ConfigParser($lines);
185        $config = $parser->getConfig();
186        $search = new SearchConfig($config, $this->sqlite);
187        $data = $search->execute();
188        if (!empty($data)) {
189            // FIXME
190            return $data[array_key_last($data)];
191        }
192        return [];
193    }
194
195    /**
196     * Get a property of the latest published revision associated with the current one
197     *
198     * @param string $key
199     * @return string
200     */
201    public function getLatestPublished($key)
202    {
203        $latestPublished = $this->getCoreData('status=' . Constants::STATUS_PUBLISHED);
204        if (!$latestPublished) return '';
205
206        $data = [
207            'status' => $latestPublished[$this->statusCol->getColref() - 1]->getRawValue(),
208            'user' => $latestPublished[$this->userCol->getColref() - 1]->getRawValue(),
209            'date' => $latestPublished[$this->dateCol->getColref() - 1]->getRawValue(),
210            'revision' => $latestPublished[$this->revisionCol->getColref() - 1]->getRawValue(),
211            'version' => $latestPublished[$this->versionCol->getColref() - 1]->getRawValue(),
212        ];
213
214        return $data[$key] ?? '';
215    }
216}
217