xref: /plugin/struct/meta/AccessTablePage.php (revision 308cc83fd5391df29d21d2bc1306c8da49fdb335)
1*308cc83fSAndreas Gohr<?php
2*308cc83fSAndreas Gohr
3*308cc83fSAndreas Gohrnamespace dokuwiki\plugin\struct\meta;
4*308cc83fSAndreas Gohr
5*308cc83fSAndreas Gohr/**
6*308cc83fSAndreas Gohr * Class AccessTableData
7*308cc83fSAndreas Gohr * @package dokuwiki\plugin\struct\meta
8*308cc83fSAndreas Gohr *
9*308cc83fSAndreas Gohr * This class is for accessing the data stored for a page in a schema
10*308cc83fSAndreas Gohr *
11*308cc83fSAndreas Gohr */
12*308cc83fSAndreas Gohrclass AccessTablePage extends AccessTable
13*308cc83fSAndreas Gohr{
14*308cc83fSAndreas Gohr
15*308cc83fSAndreas Gohr    const DEFAULT_PAGE_RID = 0;
16*308cc83fSAndreas Gohr
17*308cc83fSAndreas Gohr    public function __construct($schema, $pid, $ts = 0, $rid = 0)
18*308cc83fSAndreas Gohr    {
19*308cc83fSAndreas Gohr        $ts = $ts ?: time();
20*308cc83fSAndreas Gohr        parent::__construct($schema, $pid, $ts, $rid);
21*308cc83fSAndreas Gohr    }
22*308cc83fSAndreas Gohr
23*308cc83fSAndreas Gohr    /**
24*308cc83fSAndreas Gohr     * adds an empty data set for this schema and page
25*308cc83fSAndreas Gohr     *
26*308cc83fSAndreas Gohr     * This is basically a delete for the schema fields of a page
27*308cc83fSAndreas Gohr     *
28*308cc83fSAndreas Gohr     * @return bool
29*308cc83fSAndreas Gohr     */
30*308cc83fSAndreas Gohr    public function clearData()
31*308cc83fSAndreas Gohr    {
32*308cc83fSAndreas Gohr        $data = array();
33*308cc83fSAndreas Gohr
34*308cc83fSAndreas Gohr        foreach ($this->schema->getColumns() as $col) {
35*308cc83fSAndreas Gohr            if ($col->isMulti()) {
36*308cc83fSAndreas Gohr                $data[$col->getLabel()] = array();
37*308cc83fSAndreas Gohr            } else {
38*308cc83fSAndreas Gohr                $data[$col->getLabel()] = null;
39*308cc83fSAndreas Gohr            }
40*308cc83fSAndreas Gohr        }
41*308cc83fSAndreas Gohr
42*308cc83fSAndreas Gohr        return $this->saveData($data);
43*308cc83fSAndreas Gohr    }
44*308cc83fSAndreas Gohr
45*308cc83fSAndreas Gohr    /**
46*308cc83fSAndreas Gohr     * @return int
47*308cc83fSAndreas Gohr     */
48*308cc83fSAndreas Gohr    protected function getLastRevisionTimestamp()
49*308cc83fSAndreas Gohr    {
50*308cc83fSAndreas Gohr        $table = 'data_' . $this->schema->getTable();
51*308cc83fSAndreas Gohr        $where = "WHERE pid = ?";
52*308cc83fSAndreas Gohr        $opts = array($this->pid);
53*308cc83fSAndreas Gohr        if ($this->ts) {
54*308cc83fSAndreas Gohr            $where .= " AND rev <= ?";
55*308cc83fSAndreas Gohr            $opts[] = $this->ts;
56*308cc83fSAndreas Gohr        }
57*308cc83fSAndreas Gohr
58*308cc83fSAndreas Gohr        /** @noinspection SqlResolve */
59*308cc83fSAndreas Gohr        $sql = "SELECT rev FROM $table $where ORDER BY rev DESC LIMIT 1";
60*308cc83fSAndreas Gohr        $res = $this->sqlite->query($sql, $opts);
61*308cc83fSAndreas Gohr        $ret = (int) $this->sqlite->res2single($res);
62*308cc83fSAndreas Gohr        $this->sqlite->res_close($res);
63*308cc83fSAndreas Gohr        return $ret;
64*308cc83fSAndreas Gohr    }
65*308cc83fSAndreas Gohr
66*308cc83fSAndreas Gohr    /**
67*308cc83fSAndreas Gohr     * @inheritDoc
68*308cc83fSAndreas Gohr     */
69*308cc83fSAndreas Gohr    protected function validateTypeData($data)
70*308cc83fSAndreas Gohr    {
71*308cc83fSAndreas Gohr        if ($this->ts == 0) {
72*308cc83fSAndreas Gohr            throw new StructException("Saving with zero timestamp does not work.");
73*308cc83fSAndreas Gohr        }
74*308cc83fSAndreas Gohr        return true;
75*308cc83fSAndreas Gohr    }
76*308cc83fSAndreas Gohr
77*308cc83fSAndreas Gohr    /**
78*308cc83fSAndreas Gohr     * Remove latest status from previous data
79*308cc83fSAndreas Gohr     */
80*308cc83fSAndreas Gohr    protected function beforeSave()
81*308cc83fSAndreas Gohr    {
82*308cc83fSAndreas Gohr        /** @noinspection SqlResolve */
83*308cc83fSAndreas Gohr        $ok = $this->sqlite->query(
84*308cc83fSAndreas Gohr            "UPDATE $this->stable SET latest = 0 WHERE latest = 1 AND pid = ?",
85*308cc83fSAndreas Gohr            [$this->pid]
86*308cc83fSAndreas Gohr        );
87*308cc83fSAndreas Gohr        /** @noinspection SqlResolve */
88*308cc83fSAndreas Gohr        return $ok && $this->sqlite->query(
89*308cc83fSAndreas Gohr            "UPDATE $this->mtable SET latest = 0 WHERE latest = 1 AND pid = ?",
90*308cc83fSAndreas Gohr            [$this->pid]
91*308cc83fSAndreas Gohr        );
92*308cc83fSAndreas Gohr    }
93*308cc83fSAndreas Gohr
94*308cc83fSAndreas Gohr    /**
95*308cc83fSAndreas Gohr     * @inheritDoc
96*308cc83fSAndreas Gohr     */
97*308cc83fSAndreas Gohr    protected function getSingleNoninputCols()
98*308cc83fSAndreas Gohr    {
99*308cc83fSAndreas Gohr        return ['rid, pid, rev, latest'];
100*308cc83fSAndreas Gohr    }
101*308cc83fSAndreas Gohr
102*308cc83fSAndreas Gohr    /**
103*308cc83fSAndreas Gohr     * @inheritDoc
104*308cc83fSAndreas Gohr     */
105*308cc83fSAndreas Gohr    protected function getSingleNoninputValues()
106*308cc83fSAndreas Gohr    {
107*308cc83fSAndreas Gohr        return [self::DEFAULT_PAGE_RID, $this->pid, $this->ts, 1];
108*308cc83fSAndreas Gohr    }
109*308cc83fSAndreas Gohr
110*308cc83fSAndreas Gohr    /**
111*308cc83fSAndreas Gohr     * @inheritDoc
112*308cc83fSAndreas Gohr     */
113*308cc83fSAndreas Gohr    protected function getMultiSql()
114*308cc83fSAndreas Gohr    {
115*308cc83fSAndreas Gohr        /** @noinspection SqlResolve */
116*308cc83fSAndreas Gohr        return "INSERT INTO $this->mtable (latest, rev, pid, rid, colref, row, value) VALUES (?,?,?,?,?,?,?)";
117*308cc83fSAndreas Gohr    }
118*308cc83fSAndreas Gohr
119*308cc83fSAndreas Gohr    /**
120*308cc83fSAndreas Gohr     * @inheritDoc
121*308cc83fSAndreas Gohr     */
122*308cc83fSAndreas Gohr    protected function getMultiNoninputValues()
123*308cc83fSAndreas Gohr    {
124*308cc83fSAndreas Gohr        return [AccessTable::DEFAULT_LATEST, $this->ts, $this->pid, self::DEFAULT_PAGE_RID];
125*308cc83fSAndreas Gohr    }
126*308cc83fSAndreas Gohr}
127