xref: /plugin/struct/meta/AccessTable.php (revision f107f479f012223bf40b4635e52c57f37c706200)
1f411d872SAndreas Gohr<?php
2f411d872SAndreas Gohr
3f411d872SAndreas Gohrnamespace dokuwiki\plugin\struct\meta;
4f411d872SAndreas Gohr
5f411d872SAndreas Gohrabstract class AccessTable {
6f411d872SAndreas Gohr
7f411d872SAndreas Gohr    /** @var  Schema */
8f411d872SAndreas Gohr    protected $schema;
9f411d872SAndreas Gohr    protected $pid;
10f411d872SAndreas Gohr    protected $labels = array();
11f411d872SAndreas Gohr    protected $ts     = 0;
12f411d872SAndreas Gohr    /** @var \helper_plugin_sqlite */
13f411d872SAndreas Gohr    protected $sqlite;
14f411d872SAndreas Gohr
1590421550SAndreas Gohr    // options on how to retrieve data
16f411d872SAndreas Gohr    protected $opt_skipempty = false;
17f411d872SAndreas Gohr
18f411d872SAndreas Gohr    /**
19f411d872SAndreas Gohr     * Factory Method to access a data or lookup table
20f411d872SAndreas Gohr     *
21f411d872SAndreas Gohr     * @param Schema $schema schema to load
22f411d872SAndreas Gohr     * @param string|int $pid Page or row id to access
23897aef42SAndreas Gohr     * @param int $ts Time at which the data should be read or written, 0 for now
2494c9aa4cSAndreas Gohr     * @return AccessTableData|AccessTableLookup
25f411d872SAndreas Gohr     */
26897aef42SAndreas Gohr    public static function bySchema(Schema $schema, $pid, $ts = 0) {
27f411d872SAndreas Gohr        if($schema->isLookup()) {
28897aef42SAndreas Gohr            return new AccessTableLookup($schema, $pid, $ts);
29f411d872SAndreas Gohr        } else {
30897aef42SAndreas Gohr            return new AccessTableData($schema, $pid, $ts);
31f411d872SAndreas Gohr        }
32f411d872SAndreas Gohr    }
33f411d872SAndreas Gohr
34f411d872SAndreas Gohr    /**
35f411d872SAndreas Gohr     * Factory Method to access a data or lookup table
36f411d872SAndreas Gohr     *
37f411d872SAndreas Gohr     * @param string $tablename schema to load
38f411d872SAndreas Gohr     * @param string|int $pid Page or row id to access
39897aef42SAndreas Gohr     * @param int $ts Time at which the data should be read or written, 0 for now
4094c9aa4cSAndreas Gohr     * @return AccessTableData|AccessTableLookup
41f411d872SAndreas Gohr     */
42f411d872SAndreas Gohr    public static function byTableName($tablename, $pid, $ts = 0) {
43f411d872SAndreas Gohr        $schema = new Schema($tablename, $ts);
44897aef42SAndreas Gohr        return self::bySchema($schema, $pid, $ts);
45f411d872SAndreas Gohr    }
46f411d872SAndreas Gohr
47f411d872SAndreas Gohr    /**
48f411d872SAndreas Gohr     * AccessTable constructor
49f411d872SAndreas Gohr     *
50897aef42SAndreas Gohr     * @param Schema $schema The schema valid at $ts
51897aef42SAndreas Gohr     * @param string|int $pid
52897aef42SAndreas Gohr     * @param int $ts Time at which the data should be read or written, 0 for now
53f411d872SAndreas Gohr     */
54897aef42SAndreas Gohr    public function __construct(Schema $schema, $pid, $ts = 0) {
55f411d872SAndreas Gohr        /** @var \helper_plugin_struct_db $helper */
56f411d872SAndreas Gohr        $helper = plugin_load('helper', 'struct_db');
57f411d872SAndreas Gohr        $this->sqlite = $helper->getDB();
58f411d872SAndreas Gohr        if(!$this->sqlite) {
59f411d872SAndreas Gohr            throw new StructException('Sqlite plugin required');
60f411d872SAndreas Gohr        }
61f411d872SAndreas Gohr
62f411d872SAndreas Gohr        if(!$schema->getId()) {
63f411d872SAndreas Gohr            throw new StructException('Schema does not exist. Only data of existing schemas can be accessed');
64f411d872SAndreas Gohr        }
65f411d872SAndreas Gohr
66f411d872SAndreas Gohr        $this->schema = $schema;
67f411d872SAndreas Gohr        $this->pid = $pid;
68897aef42SAndreas Gohr        $this->setTimestamp($ts);
69f411d872SAndreas Gohr        foreach($this->schema->getColumns() as $col) {
70f411d872SAndreas Gohr            $this->labels[$col->getColref()] = $col->getType()->getLabel();
71f411d872SAndreas Gohr        }
72f411d872SAndreas Gohr    }
73f411d872SAndreas Gohr
74f411d872SAndreas Gohr    /**
75f411d872SAndreas Gohr     * gives access to the schema
76f411d872SAndreas Gohr     *
77f411d872SAndreas Gohr     * @return Schema
78f411d872SAndreas Gohr     */
79f411d872SAndreas Gohr    public function getSchema() {
80f411d872SAndreas Gohr        return $this->schema;
81f411d872SAndreas Gohr    }
82f411d872SAndreas Gohr
83f411d872SAndreas Gohr    /**
84*f107f479SAndreas Gohr     * The current pid
85*f107f479SAndreas Gohr     *
86*f107f479SAndreas Gohr     * @return int|string
87*f107f479SAndreas Gohr     */
88*f107f479SAndreas Gohr    public function getPid() {
89*f107f479SAndreas Gohr        return $this->pid;
90*f107f479SAndreas Gohr    }
91*f107f479SAndreas Gohr
92*f107f479SAndreas Gohr    /**
93f411d872SAndreas Gohr     * Should remove the current data, by either deleting or ovewriting it
94f411d872SAndreas Gohr     *
95f411d872SAndreas Gohr     * @return bool if the delete succeeded
96f411d872SAndreas Gohr     */
97f411d872SAndreas Gohr    abstract public function clearData();
98f411d872SAndreas Gohr
99f411d872SAndreas Gohr    /**
100f411d872SAndreas Gohr     * Save the data to the database.
101f411d872SAndreas Gohr     *
102f411d872SAndreas Gohr     * We differentiate between single-value-column and multi-value-column by the value to the respective column-name,
103f411d872SAndreas Gohr     * i.e. depending on if that is a string or an array, respectively.
104f411d872SAndreas Gohr     *
105f411d872SAndreas Gohr     * @param array $data typelabel => value for single fields or typelabel => array(value, value, ...) for multi fields
106f411d872SAndreas Gohr     * @return bool success of saving the data to the database
107f411d872SAndreas Gohr     */
108f411d872SAndreas Gohr    abstract public function saveData($data);
109f411d872SAndreas Gohr
110f411d872SAndreas Gohr    /**
111f411d872SAndreas Gohr     * Should empty or invisible (inpage) fields be returned?
112f411d872SAndreas Gohr     *
113f411d872SAndreas Gohr     * Defaults to false
114f411d872SAndreas Gohr     *
115f411d872SAndreas Gohr     * @param null|bool $set new value, null to read only
116f411d872SAndreas Gohr     * @return bool current value (after set)
117f411d872SAndreas Gohr     */
118f411d872SAndreas Gohr    public function optionSkipEmpty($set = null) {
119f411d872SAndreas Gohr        if(!is_null($set)) {
120f411d872SAndreas Gohr            $this->opt_skipempty = $set;
121f411d872SAndreas Gohr        }
122f411d872SAndreas Gohr        return $this->opt_skipempty;
123f411d872SAndreas Gohr    }
124f411d872SAndreas Gohr
125f411d872SAndreas Gohr    /**
126f411d872SAndreas Gohr     * Get the value of a single column
127f411d872SAndreas Gohr     *
128f411d872SAndreas Gohr     * @param Column $column
129f411d872SAndreas Gohr     * @return Value|null
130f411d872SAndreas Gohr     */
131f411d872SAndreas Gohr    public function getDataColumn($column) {
132f411d872SAndreas Gohr        $data = $this->getData();
133f411d872SAndreas Gohr        foreach($data as $value) {
134f411d872SAndreas Gohr            if($value->getColumn() == $column) {
135f411d872SAndreas Gohr                return $value;
136f411d872SAndreas Gohr            }
137f411d872SAndreas Gohr        }
138f411d872SAndreas Gohr        return null;
139f411d872SAndreas Gohr    }
140f411d872SAndreas Gohr
141f411d872SAndreas Gohr    /**
142f411d872SAndreas Gohr     * returns the data saved for the page
143f411d872SAndreas Gohr     *
144f411d872SAndreas Gohr     * @return Value[] a list of values saved for the current page
145f411d872SAndreas Gohr     */
146f411d872SAndreas Gohr    public function getData() {
147f411d872SAndreas Gohr        $data = $this->getDataFromDB();
148f411d872SAndreas Gohr        $data = $this->consolidateData($data, false);
149f411d872SAndreas Gohr        return $data;
150f411d872SAndreas Gohr    }
151f411d872SAndreas Gohr
152f411d872SAndreas Gohr    /**
153f411d872SAndreas Gohr     * returns the data saved for the page as associative array
154f411d872SAndreas Gohr     *
155f411d872SAndreas Gohr     * The array returned is in the same format as used in @see saveData()
156f411d872SAndreas Gohr     *
15790421550SAndreas Gohr     * It always returns raw Values!
15890421550SAndreas Gohr     *
159f411d872SAndreas Gohr     * @return array
160f411d872SAndreas Gohr     */
161f411d872SAndreas Gohr    public function getDataArray() {
162f411d872SAndreas Gohr        $data = $this->getDataFromDB();
163f411d872SAndreas Gohr        $data = $this->consolidateData($data, true);
164f411d872SAndreas Gohr        return $data;
165f411d872SAndreas Gohr    }
166f411d872SAndreas Gohr
167f411d872SAndreas Gohr    /**
168f411d872SAndreas Gohr     * Return the data in pseudo syntax
169f411d872SAndreas Gohr     */
170f411d872SAndreas Gohr    public function getDataPseudoSyntax() {
171f411d872SAndreas Gohr        $result = '';
172f411d872SAndreas Gohr        $data = $this->getDataArray();
173f411d872SAndreas Gohr        foreach($data as $key => $value) {
174f411d872SAndreas Gohr            $key = $this->schema->getTable() . ".$key";
175f411d872SAndreas Gohr            if(is_array($value)) $value = join(', ', $value);
176f411d872SAndreas Gohr            $result .= sprintf("% -20s : %s\n", $key, $value);
177f411d872SAndreas Gohr        }
178f411d872SAndreas Gohr        return $result;
179f411d872SAndreas Gohr    }
180f411d872SAndreas Gohr
181f411d872SAndreas Gohr    /**
182f411d872SAndreas Gohr     * retrieve the data saved for the page from the database. Usually there is no need to call this function.
183f411d872SAndreas Gohr     * Call @see SchemaData::getData instead.
184f411d872SAndreas Gohr     */
185f411d872SAndreas Gohr    protected function getDataFromDB() {
186f411d872SAndreas Gohr        list($sql, $opt) = $this->buildGetDataSQL();
187f411d872SAndreas Gohr
188f411d872SAndreas Gohr        $res = $this->sqlite->query($sql, $opt);
189f411d872SAndreas Gohr        $data = $this->sqlite->res2arr($res);
190f411d872SAndreas Gohr
191f411d872SAndreas Gohr        return $data;
192f411d872SAndreas Gohr    }
193f411d872SAndreas Gohr
194f411d872SAndreas Gohr    /**
195f411d872SAndreas Gohr     * Creates a proper result array from the database data
196f411d872SAndreas Gohr     *
197f411d872SAndreas Gohr     * @param array $DBdata the data as it is retrieved from the database, i.e. by SchemaData::getDataFromDB
198f411d872SAndreas Gohr     * @param bool $asarray return data as associative array (true) or as array of Values (false)
199f411d872SAndreas Gohr     * @return array|Value[]
200f411d872SAndreas Gohr     */
201f411d872SAndreas Gohr    protected function consolidateData($DBdata, $asarray = false) {
202f411d872SAndreas Gohr        $data = array();
203f411d872SAndreas Gohr
204f411d872SAndreas Gohr        $sep = Search::CONCAT_SEPARATOR;
205f411d872SAndreas Gohr
206f411d872SAndreas Gohr        foreach($this->schema->getColumns(false) as $col) {
207f411d872SAndreas Gohr
20890421550SAndreas Gohr            // if no data saved yet, return empty strings
209f411d872SAndreas Gohr            if($DBdata) {
210bab52340SAndreas Gohr                $val = $DBdata[0]['out' . $col->getColref()];
211f411d872SAndreas Gohr            } else {
212f411d872SAndreas Gohr                $val = '';
213f411d872SAndreas Gohr            }
214f411d872SAndreas Gohr
215f411d872SAndreas Gohr            // multi val data is concatenated
216f411d872SAndreas Gohr            if($col->isMulti()) {
217f411d872SAndreas Gohr                $val = explode($sep, $val);
218f411d872SAndreas Gohr                $val = array_filter($val);
219f411d872SAndreas Gohr            }
220f411d872SAndreas Gohr
22190421550SAndreas Gohr            $value = new Value($col, $val);
222f411d872SAndreas Gohr
22390421550SAndreas Gohr            if($this->opt_skipempty && $value->isEmpty()) continue;
22490421550SAndreas Gohr            if($this->opt_skipempty && !$col->isVisibleInPage()) continue; //FIXME is this a correct assumption?
22590421550SAndreas Gohr
22690421550SAndreas Gohr            // for arrays, we return the raw value only
227f411d872SAndreas Gohr            if($asarray) {
22890421550SAndreas Gohr                $data[$col->getLabel()] = $value->getRawValue();
229f411d872SAndreas Gohr            } else {
23090421550SAndreas Gohr                $data[] = $value;
231f411d872SAndreas Gohr            }
232f411d872SAndreas Gohr        }
233f411d872SAndreas Gohr
234f411d872SAndreas Gohr        return $data;
235f411d872SAndreas Gohr    }
236f411d872SAndreas Gohr
237f411d872SAndreas Gohr    /**
238f411d872SAndreas Gohr     * Builds the SQL statement to select the data for this page and schema
239f411d872SAndreas Gohr     *
240f411d872SAndreas Gohr     * @return array Two fields: the SQL string and the parameters array
241f411d872SAndreas Gohr     */
242f411d872SAndreas Gohr    protected function buildGetDataSQL() {
243f411d872SAndreas Gohr        $sep = Search::CONCAT_SEPARATOR;
244f411d872SAndreas Gohr        $stable = 'data_' . $this->schema->getTable();
245f411d872SAndreas Gohr        $mtable = 'multi_' . $this->schema->getTable();
246f411d872SAndreas Gohr
247f411d872SAndreas Gohr        $QB = new QueryBuilder();
248f411d872SAndreas Gohr        $QB->addTable($stable, 'DATA');
249f411d872SAndreas Gohr        $QB->addSelectColumn('DATA', 'pid', 'PID');
250f411d872SAndreas Gohr        $QB->addGroupByStatement('DATA.pid');
251f411d872SAndreas Gohr
252f411d872SAndreas Gohr        foreach($this->schema->getColumns(false) as $col) {
253f411d872SAndreas Gohr
254f411d872SAndreas Gohr            $colref = $col->getColref();
255f411d872SAndreas Gohr            $colname = 'col' . $colref;
256bab52340SAndreas Gohr            $outname = 'out' . $colref;
257f411d872SAndreas Gohr
258f411d872SAndreas Gohr            if($col->getType()->isMulti()) {
259f411d872SAndreas Gohr                $tn = 'M' . $colref;
260f411d872SAndreas Gohr                $QB->addLeftJoin(
261f411d872SAndreas Gohr                    'DATA',
262f411d872SAndreas Gohr                    $mtable,
263f411d872SAndreas Gohr                    $tn,
264f411d872SAndreas Gohr                    "DATA.pid = $tn.pid AND DATA.rev = $tn.rev AND $tn.colref = $colref"
265f411d872SAndreas Gohr                );
266bab52340SAndreas Gohr                $col->getType()->select($QB, $tn, 'value', $outname);
267bab52340SAndreas Gohr                $sel = $QB->getSelectStatement($outname);
268bab52340SAndreas Gohr                $QB->addSelectStatement("GROUP_CONCAT($sel, '$sep')", $outname);
269f411d872SAndreas Gohr            } else {
270bab52340SAndreas Gohr                $col->getType()->select($QB, 'DATA', $colname, $outname);
271bab52340SAndreas Gohr                $QB->addGroupByStatement($outname);
272f411d872SAndreas Gohr            }
273f411d872SAndreas Gohr        }
274f411d872SAndreas Gohr
275f411d872SAndreas Gohr        $pl = $QB->addValue($this->pid);
276f411d872SAndreas Gohr        $QB->filters()->whereAnd("DATA.pid = $pl");
277897aef42SAndreas Gohr        $pl = $QB->addValue($this->getLastRevisionTimestamp());
278f411d872SAndreas Gohr        $QB->filters()->whereAnd("DATA.rev = $pl");
279f411d872SAndreas Gohr
280f411d872SAndreas Gohr        return $QB->getSQL();
281f411d872SAndreas Gohr    }
282f411d872SAndreas Gohr
283f411d872SAndreas Gohr    /**
28413eddb0fSAndreas Gohr     * @param int $ts
28513eddb0fSAndreas Gohr     */
28613eddb0fSAndreas Gohr    public function setTimestamp($ts) {
287897aef42SAndreas Gohr        if($ts && $ts < $this->schema->getTimeStamp()) {
288897aef42SAndreas Gohr            throw new StructException('Given timestamp is not valid for current Schema');
289897aef42SAndreas Gohr        }
290897aef42SAndreas Gohr
29113eddb0fSAndreas Gohr        $this->ts = $ts;
29213eddb0fSAndreas Gohr    }
29313eddb0fSAndreas Gohr
29413eddb0fSAndreas Gohr    /**
295897aef42SAndreas Gohr     * Return the last time an edit happened for this table for the currently set
296897aef42SAndreas Gohr     * time and pid. When the current timestamp is 0, the newest revision is
297897aef42SAndreas Gohr     * returned. Used in @see buildGetDataSQL()
298f411d872SAndreas Gohr     *
299897aef42SAndreas Gohr     * @return int
300f411d872SAndreas Gohr     */
301897aef42SAndreas Gohr    abstract protected function getLastRevisionTimestamp();
30287dc1344SAndreas Gohr
30387dc1344SAndreas Gohr    /**
30487dc1344SAndreas Gohr     * Check if the given data validates against the current types.
30587dc1344SAndreas Gohr     *
30687dc1344SAndreas Gohr     * @param array $data
30793ca6f4fSAndreas Gohr     * @return AccessDataValidator
30887dc1344SAndreas Gohr     */
30987dc1344SAndreas Gohr    public function getValidator($data) {
31093ca6f4fSAndreas Gohr        return new AccessDataValidator($this, $data);
31187dc1344SAndreas Gohr    }
312f411d872SAndreas Gohr}
313f411d872SAndreas Gohr
314f411d872SAndreas Gohr
315