xref: /plugin/struct/meta/AccessTablePage.php (revision fc6ac2e560a8d00924bd642434731ce9aa18206d)
1308cc83fSAndreas Gohr<?php
2308cc83fSAndreas Gohr
3308cc83fSAndreas Gohrnamespace dokuwiki\plugin\struct\meta;
4308cc83fSAndreas Gohr
5308cc83fSAndreas Gohr/**
6308cc83fSAndreas Gohr * Class AccessTableData
7308cc83fSAndreas Gohr * @package dokuwiki\plugin\struct\meta
8308cc83fSAndreas Gohr *
9308cc83fSAndreas Gohr * This class is for accessing the data stored for a page in a schema
10308cc83fSAndreas Gohr *
11308cc83fSAndreas Gohr */
12308cc83fSAndreas Gohrclass AccessTablePage extends AccessTable
13308cc83fSAndreas Gohr{
1417a3a578SAndreas Gohr    public const DEFAULT_PAGE_RID = 0;
15308cc83fSAndreas Gohr
16308cc83fSAndreas Gohr    public function __construct($schema, $pid, $ts = 0, $rid = 0)
17308cc83fSAndreas Gohr    {
18308cc83fSAndreas Gohr        $ts = $ts ?: time();
19308cc83fSAndreas Gohr        parent::__construct($schema, $pid, $ts, $rid);
20308cc83fSAndreas Gohr    }
21308cc83fSAndreas Gohr
22308cc83fSAndreas Gohr    /**
23308cc83fSAndreas Gohr     * adds an empty data set for this schema and page
24308cc83fSAndreas Gohr     *
25308cc83fSAndreas Gohr     * This is basically a delete for the schema fields of a page
26308cc83fSAndreas Gohr     *
27308cc83fSAndreas Gohr     * @return bool
28308cc83fSAndreas Gohr     */
29308cc83fSAndreas Gohr    public function clearData()
30308cc83fSAndreas Gohr    {
31308cc83fSAndreas Gohr        $data = array();
32308cc83fSAndreas Gohr
33308cc83fSAndreas Gohr        foreach ($this->schema->getColumns() as $col) {
34308cc83fSAndreas Gohr            if ($col->isMulti()) {
35308cc83fSAndreas Gohr                $data[$col->getLabel()] = array();
36308cc83fSAndreas Gohr            } else {
37308cc83fSAndreas Gohr                $data[$col->getLabel()] = null;
38308cc83fSAndreas Gohr            }
39308cc83fSAndreas Gohr        }
40308cc83fSAndreas Gohr
41308cc83fSAndreas Gohr        return $this->saveData($data);
42308cc83fSAndreas Gohr    }
43308cc83fSAndreas Gohr
44308cc83fSAndreas Gohr    /**
4525e910deSAnna Dabrowska     * @return int|bool
46308cc83fSAndreas Gohr     */
47308cc83fSAndreas Gohr    protected function getLastRevisionTimestamp()
48308cc83fSAndreas Gohr    {
49308cc83fSAndreas Gohr        $table = 'data_' . $this->schema->getTable();
50308cc83fSAndreas Gohr        $where = "WHERE pid = ?";
5125e910deSAnna Dabrowska        $opts = [$this->pid];
52308cc83fSAndreas Gohr        if ($this->ts) {
5325e910deSAnna Dabrowska            $where .= " AND REV > 0 AND rev <= ?";
54308cc83fSAndreas Gohr            $opts[] = $this->ts;
55308cc83fSAndreas Gohr        }
56308cc83fSAndreas Gohr
57308cc83fSAndreas Gohr        /** @noinspection SqlResolve */
58308cc83fSAndreas Gohr        $sql = "SELECT rev FROM $table $where ORDER BY rev DESC LIMIT 1";
59308cc83fSAndreas Gohr        $res = $this->sqlite->query($sql, $opts);
6025e910deSAnna Dabrowska        $ret = $this->sqlite->res2single($res);
61308cc83fSAndreas Gohr        $this->sqlite->res_close($res);
6225e910deSAnna Dabrowska        // make sure we don't cast empty result to 0 (serial data has rev = 0)
6325e910deSAnna Dabrowska        if ($ret !== false) $ret = (int)$ret;
64308cc83fSAndreas Gohr        return $ret;
65308cc83fSAndreas Gohr    }
66308cc83fSAndreas Gohr
67308cc83fSAndreas Gohr    /**
68308cc83fSAndreas Gohr     * @inheritDoc
69308cc83fSAndreas Gohr     */
70308cc83fSAndreas Gohr    protected function validateTypeData($data)
71308cc83fSAndreas Gohr    {
72308cc83fSAndreas Gohr        if ($this->ts == 0) {
73308cc83fSAndreas Gohr            throw new StructException("Saving with zero timestamp does not work.");
74308cc83fSAndreas Gohr        }
75308cc83fSAndreas Gohr        return true;
76308cc83fSAndreas Gohr    }
77308cc83fSAndreas Gohr
78308cc83fSAndreas Gohr    /**
799e5ba324SAnna Dabrowska     * Remove latest status from previous page data
80308cc83fSAndreas Gohr     */
81308cc83fSAndreas Gohr    protected function beforeSave()
82308cc83fSAndreas Gohr    {
83308cc83fSAndreas Gohr        /** @noinspection SqlResolve */
84308cc83fSAndreas Gohr        $ok = $this->sqlite->query(
859e5ba324SAnna Dabrowska            "UPDATE $this->stable SET latest = 0 WHERE latest = 1 AND pid = ? AND rid = 0",
86308cc83fSAndreas Gohr            [$this->pid]
87308cc83fSAndreas Gohr        );
88308cc83fSAndreas Gohr        /** @noinspection SqlResolve */
89308cc83fSAndreas Gohr        return $ok && $this->sqlite->query(
909e5ba324SAnna Dabrowska            "UPDATE $this->mtable SET latest = 0 WHERE latest = 1 AND pid = ? AND rid = 0",
91308cc83fSAndreas Gohr            [$this->pid]
92308cc83fSAndreas Gohr        );
93308cc83fSAndreas Gohr    }
94308cc83fSAndreas Gohr
95308cc83fSAndreas Gohr    /**
96*fc6ac2e5SAnna Dabrowska     * Names of non-input columns to be inserted into SQL query.
97*fc6ac2e5SAnna Dabrowska     * Field 'published' is skipped because only plugins use it and
98*fc6ac2e5SAnna Dabrowska     * we don't want to interfere with the default NULL value
99308cc83fSAndreas Gohr     */
100308cc83fSAndreas Gohr    protected function getSingleNoninputCols()
101308cc83fSAndreas Gohr    {
102308cc83fSAndreas Gohr        return ['rid, pid, rev, latest'];
103308cc83fSAndreas Gohr    }
104308cc83fSAndreas Gohr
105308cc83fSAndreas Gohr    /**
106308cc83fSAndreas Gohr     * @inheritDoc
107308cc83fSAndreas Gohr     */
108308cc83fSAndreas Gohr    protected function getSingleNoninputValues()
109308cc83fSAndreas Gohr    {
110308cc83fSAndreas Gohr        return [self::DEFAULT_PAGE_RID, $this->pid, $this->ts, 1];
111308cc83fSAndreas Gohr    }
112308cc83fSAndreas Gohr
113308cc83fSAndreas Gohr    /**
114308cc83fSAndreas Gohr     * @inheritDoc
115308cc83fSAndreas Gohr     */
116308cc83fSAndreas Gohr    protected function getMultiSql()
117308cc83fSAndreas Gohr    {
118308cc83fSAndreas Gohr        /** @noinspection SqlResolve */
119308cc83fSAndreas Gohr        return "INSERT INTO $this->mtable (latest, rev, pid, rid, colref, row, value) VALUES (?,?,?,?,?,?,?)";
120308cc83fSAndreas Gohr    }
121308cc83fSAndreas Gohr
122308cc83fSAndreas Gohr    /**
123308cc83fSAndreas Gohr     * @inheritDoc
124308cc83fSAndreas Gohr     */
125308cc83fSAndreas Gohr    protected function getMultiNoninputValues()
126308cc83fSAndreas Gohr    {
127308cc83fSAndreas Gohr        return [AccessTable::DEFAULT_LATEST, $this->ts, $this->pid, self::DEFAULT_PAGE_RID];
128308cc83fSAndreas Gohr    }
129308cc83fSAndreas Gohr}
130