xref: /plugin/structpublish/meta/AccessTableStructpublish.php (revision dafa98129e3380a0db14657af63bb43f9cf8d39e)
1<?php
2
3namespace dokuwiki\plugin\structpublish\meta;
4
5use dokuwiki\plugin\struct\meta\AccessTable;
6use dokuwiki\plugin\struct\meta\AccessTableSerial;
7
8/**
9 * Class AccessTableStructpublish
10 *
11 * Load and save publish data
12 *
13 * @package dokuwiki\plugin\struct\meta
14 */
15class AccessTableStructpublish extends AccessTableSerial
16{
17    protected $published = 0;
18
19    public function __construct($table, $pid, $ts = 0, $rid = 0)
20    {
21        parent::__construct($table, $pid, $ts, $rid);
22    }
23
24    /**
25     * @param int $published
26     * @return void
27     */
28    public function setPublished($published)
29    {
30        $this->published = $published;
31    }
32
33    /**
34     * @inheritDoc
35     */
36    protected function getSingleSql()
37    {
38        $cols = array_merge($this->getSingleNoninputCols(), $this->singleCols);
39        $cols = join(',', $cols);
40        $vals = array_merge($this->getSingleNoninputValues(), $this->singleValues);
41        $rid = $this->getRid() ?: "(SELECT (COALESCE(MAX(rid), 0 ) + 1) FROM $this->stable)";
42
43        return "REPLACE INTO $this->stable (rid, $cols)
44                      VALUES ($rid," . trim(str_repeat('?,', count($vals)), ',') . ');';
45    }
46
47    /**
48     * @inheritDoc
49     */
50    protected function getMultiSql()
51    {
52        return '';
53    }
54
55    /**
56     * @inheritDoc
57     */
58    protected function getSingleNoninputCols()
59    {
60        return ['pid', 'rev', 'latest', 'published'];
61    }
62
63    /**
64     * @inheritDoc
65     */
66    protected function getSingleNoninputValues()
67    {
68        return [$this->pid, AccessTable::DEFAULT_REV, AccessTable::DEFAULT_LATEST, $this->published];
69    }
70
71    /**
72     * @return int|bool
73     */
74    protected function getLastRevisionTimestamp()
75    {
76        $table = 'data_structpublish';
77        $where = "WHERE pid = ?";
78        $opts = [$this->pid];
79        if ($this->ts) {
80            $where .= " AND REV > 0 AND rev <= ?";
81            $opts[] = $this->ts;
82        }
83
84        /** @noinspection SqlResolve */
85        $sql = "SELECT rev FROM $table $where ORDER BY rev DESC LIMIT 1";
86        $res = $this->sqlite->query($sql, $opts);
87        $ret = $this->sqlite->res2single($res);
88        $this->sqlite->res_close($res);
89        // make sure we don't cast empty result to 0 (serial data has rev = 0)
90        if ($ret !== false) $ret = (int)$ret;
91        return $ret;
92    }
93}
94