xref: /plugin/structpublish/meta/Revision.php (revision 910e7e15ab7eacafbaa096c48916ba3bb1c5258f)
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    const STATUS_DRAFT = 'draft';
13    const STATUS_APPROVED = 'approved';
14    const STATUS_PUBLISHED = 'published';
15
16    /** @var \helper_plugin_sqlite */
17    protected $sqlite;
18    protected $schemas;
19    protected $id;
20    protected $rev;
21    protected $status;
22    protected $version;
23    protected $user;
24    protected $date;
25
26    /**
27     * @param $sqlite
28     * @param string $id
29     * @param int $rev
30     */
31    public function __construct($sqlite, $id, $rev)
32    {
33        $this->sqlite = $sqlite;
34        $this->id = $id;
35        $this->rev = $rev;
36
37        $schema = new Schema('structpublish');
38        $statusCol = $schema->findColumn('status');
39        $versionCol = $schema->findColumn('version');
40        $userCol = $schema->findColumn('user');
41        $dateCol = $schema->findColumn('date');
42
43        /** @var Value[] $values */
44        $values = $this->getCoreData($id);
45
46        if (!empty($values)) {
47            $this->status = $values[$statusCol->getColref() - 1]->getRawValue();
48            $this->version = $values[$versionCol->getColref() - 1]->getRawValue();
49            $this->user = $values[$userCol->getColref() - 1]->getRawValue();
50            $this->date = $values[$dateCol->getColref() - 1]->getRawValue();
51        }
52    }
53
54    public function save()
55    {
56        // drafts reference the latest version
57        if ($this->status === self::STATUS_DRAFT) {
58            //FIXME no rev yet
59            $this->setVersion($this->getVersion());
60        }
61
62        // TODO reset publish status of older revisions
63
64            $this->updateCoreData($this->id);
65    }
66
67    /**
68     * @return int
69     */
70    public function getVersion()
71    {
72        return (int)$this->version;
73    }
74
75    /**
76     * @param int $version
77     */
78    public function setVersion($version): void
79    {
80        $this->version = $version;
81    }
82
83    /**
84     * @return int
85     */
86    public function getRev()
87    {
88        return $this->rev;
89    }
90
91    /**
92     * @param int $rev
93     */
94    public function setRev($rev): void
95    {
96        $this->rev = $rev;
97    }
98
99    /**
100     * @return string
101     */
102    public function getStatus()
103    {
104        return $this->status;
105    }
106
107    /**
108     * @param string $status
109     */
110    public function setStatus($status): void
111    {
112        $this->status = $status;
113    }
114
115    /**
116     * @return string
117     */
118    public function getUser()
119    {
120        return $this->user;
121    }
122
123    /**
124     * @param string $user
125     */
126    public function setUser($user): void
127    {
128        $this->user = $user;
129    }
130
131    public function getDate()
132    {
133        return $this->date;
134    }
135
136    public function setDate($time)
137    {
138        $this->date = date('Y-m-d', $time);
139    }
140
141    /**
142     * Update publish status in the core table
143     */
144    protected function updateCoreData($pid, $rid = 0)
145    {
146        $data = [
147            'status' => $this->status,
148            'user' => $this->user,
149            'date' => $this->date,
150            'revision' => $this->rev,
151            'version' => $this->version,
152        ];
153        $schema = new Schema('structpublish', 0);
154        $access = new AccessTableStructpublish($schema, $pid, 0, $rid);
155        $access->saveData($data);
156    }
157
158    public function getCoreData($id)
159    {
160        $lines = [
161            'schema: structpublish',
162            'cols: *',
163            'filter: %pageid% = $ID$'
164        ];
165        $parser = new ConfigParser($lines);
166        $config = $parser->getConfig();
167        $search = new SearchConfig($config);
168        $data = $search->execute();
169        if (!empty($data)) {
170            return $data[array_key_last($data)];
171        }
172        return [];
173    }
174}
175