xref: /plugin/struct/meta/AccessTablePage.php (revision 79b29326ae29dcbf2571b932f1b531c400b9460b)
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";
59*79b29326SAnna Dabrowska        $ret = $this->sqlite->queryValue($sql, $opts);
6025e910deSAnna Dabrowska        // make sure we don't cast empty result to 0 (serial data has rev = 0)
6125e910deSAnna Dabrowska        if ($ret !== false) $ret = (int)$ret;
62308cc83fSAndreas Gohr        return $ret;
63308cc83fSAndreas Gohr    }
64308cc83fSAndreas Gohr
65308cc83fSAndreas Gohr    /**
66308cc83fSAndreas Gohr     * @inheritDoc
67308cc83fSAndreas Gohr     */
68308cc83fSAndreas Gohr    protected function validateTypeData($data)
69308cc83fSAndreas Gohr    {
70308cc83fSAndreas Gohr        if ($this->ts == 0) {
71308cc83fSAndreas Gohr            throw new StructException("Saving with zero timestamp does not work.");
72308cc83fSAndreas Gohr        }
73308cc83fSAndreas Gohr        return true;
74308cc83fSAndreas Gohr    }
75308cc83fSAndreas Gohr
76308cc83fSAndreas Gohr    /**
779e5ba324SAnna Dabrowska     * Remove latest status from previous page data
78308cc83fSAndreas Gohr     */
79308cc83fSAndreas Gohr    protected function beforeSave()
80308cc83fSAndreas Gohr    {
81308cc83fSAndreas Gohr        /** @noinspection SqlResolve */
82308cc83fSAndreas Gohr        $ok = $this->sqlite->query(
839e5ba324SAnna Dabrowska            "UPDATE $this->stable SET latest = 0 WHERE latest = 1 AND pid = ? AND rid = 0",
84308cc83fSAndreas Gohr            [$this->pid]
85308cc83fSAndreas Gohr        );
86308cc83fSAndreas Gohr        /** @noinspection SqlResolve */
87308cc83fSAndreas Gohr        return $ok && $this->sqlite->query(
889e5ba324SAnna Dabrowska            "UPDATE $this->mtable SET latest = 0 WHERE latest = 1 AND pid = ? AND rid = 0",
89308cc83fSAndreas Gohr            [$this->pid]
90308cc83fSAndreas Gohr        );
91308cc83fSAndreas Gohr    }
92308cc83fSAndreas Gohr
93308cc83fSAndreas Gohr    /**
94fc6ac2e5SAnna Dabrowska     * Names of non-input columns to be inserted into SQL query.
95fc6ac2e5SAnna Dabrowska     * Field 'published' is skipped because only plugins use it and
96fc6ac2e5SAnna Dabrowska     * we don't want to interfere with the default NULL value
97308cc83fSAndreas Gohr     */
98308cc83fSAndreas Gohr    protected function getSingleNoninputCols()
99308cc83fSAndreas Gohr    {
100308cc83fSAndreas Gohr        return ['rid, pid, rev, latest'];
101308cc83fSAndreas Gohr    }
102308cc83fSAndreas Gohr
103308cc83fSAndreas Gohr    /**
104308cc83fSAndreas Gohr     * @inheritDoc
105308cc83fSAndreas Gohr     */
106308cc83fSAndreas Gohr    protected function getSingleNoninputValues()
107308cc83fSAndreas Gohr    {
108308cc83fSAndreas Gohr        return [self::DEFAULT_PAGE_RID, $this->pid, $this->ts, 1];
109308cc83fSAndreas Gohr    }
110308cc83fSAndreas Gohr
111308cc83fSAndreas Gohr    /**
112308cc83fSAndreas Gohr     * @inheritDoc
113308cc83fSAndreas Gohr     */
114308cc83fSAndreas Gohr    protected function getMultiSql()
115308cc83fSAndreas Gohr    {
116308cc83fSAndreas Gohr        /** @noinspection SqlResolve */
117308cc83fSAndreas Gohr        return "INSERT INTO $this->mtable (latest, rev, pid, rid, colref, row, value) VALUES (?,?,?,?,?,?,?)";
118308cc83fSAndreas Gohr    }
119308cc83fSAndreas Gohr
120308cc83fSAndreas Gohr    /**
121308cc83fSAndreas Gohr     * @inheritDoc
122308cc83fSAndreas Gohr     */
123308cc83fSAndreas Gohr    protected function getMultiNoninputValues()
124308cc83fSAndreas Gohr    {
125308cc83fSAndreas Gohr        return [AccessTable::DEFAULT_LATEST, $this->ts, $this->pid, self::DEFAULT_PAGE_RID];
126308cc83fSAndreas Gohr    }
127308cc83fSAndreas Gohr}
128