xref: /plugin/struct/meta/AccessTable.php (revision 87dc1344fb5ebba89b6bf5f09fe8e70f0793e751)
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    /**
84f411d872SAndreas Gohr     * Should remove the current data, by either deleting or ovewriting it
85f411d872SAndreas Gohr     *
86f411d872SAndreas Gohr     * @return bool if the delete succeeded
87f411d872SAndreas Gohr     */
88f411d872SAndreas Gohr    abstract public function clearData();
89f411d872SAndreas Gohr
90f411d872SAndreas Gohr    /**
91f411d872SAndreas Gohr     * Save the data to the database.
92f411d872SAndreas Gohr     *
93f411d872SAndreas Gohr     * We differentiate between single-value-column and multi-value-column by the value to the respective column-name,
94f411d872SAndreas Gohr     * i.e. depending on if that is a string or an array, respectively.
95f411d872SAndreas Gohr     *
96f411d872SAndreas Gohr     * @param array $data typelabel => value for single fields or typelabel => array(value, value, ...) for multi fields
97f411d872SAndreas Gohr     * @return bool success of saving the data to the database
98f411d872SAndreas Gohr     */
99f411d872SAndreas Gohr    abstract public function saveData($data);
100f411d872SAndreas Gohr
101f411d872SAndreas Gohr    /**
102f411d872SAndreas Gohr     * Should empty or invisible (inpage) fields be returned?
103f411d872SAndreas Gohr     *
104f411d872SAndreas Gohr     * Defaults to false
105f411d872SAndreas Gohr     *
106f411d872SAndreas Gohr     * @param null|bool $set new value, null to read only
107f411d872SAndreas Gohr     * @return bool current value (after set)
108f411d872SAndreas Gohr     */
109f411d872SAndreas Gohr    public function optionSkipEmpty($set = null) {
110f411d872SAndreas Gohr        if(!is_null($set)) {
111f411d872SAndreas Gohr            $this->opt_skipempty = $set;
112f411d872SAndreas Gohr        }
113f411d872SAndreas Gohr        return $this->opt_skipempty;
114f411d872SAndreas Gohr    }
115f411d872SAndreas Gohr
116f411d872SAndreas Gohr    /**
117f411d872SAndreas Gohr     * Get the value of a single column
118f411d872SAndreas Gohr     *
119f411d872SAndreas Gohr     * @param Column $column
120f411d872SAndreas Gohr     * @return Value|null
121f411d872SAndreas Gohr     */
122f411d872SAndreas Gohr    public function getDataColumn($column) {
123f411d872SAndreas Gohr        $data = $this->getData();
124f411d872SAndreas Gohr        foreach($data as $value) {
125f411d872SAndreas Gohr            if($value->getColumn() == $column) {
126f411d872SAndreas Gohr                return $value;
127f411d872SAndreas Gohr            }
128f411d872SAndreas Gohr        }
129f411d872SAndreas Gohr        return null;
130f411d872SAndreas Gohr    }
131f411d872SAndreas Gohr
132f411d872SAndreas Gohr    /**
133f411d872SAndreas Gohr     * returns the data saved for the page
134f411d872SAndreas Gohr     *
135f411d872SAndreas Gohr     * @return Value[] a list of values saved for the current page
136f411d872SAndreas Gohr     */
137f411d872SAndreas Gohr    public function getData() {
138f411d872SAndreas Gohr        $data = $this->getDataFromDB();
139f411d872SAndreas Gohr        $data = $this->consolidateData($data, false);
140f411d872SAndreas Gohr        return $data;
141f411d872SAndreas Gohr    }
142f411d872SAndreas Gohr
143f411d872SAndreas Gohr    /**
144f411d872SAndreas Gohr     * returns the data saved for the page as associative array
145f411d872SAndreas Gohr     *
146f411d872SAndreas Gohr     * The array returned is in the same format as used in @see saveData()
147f411d872SAndreas Gohr     *
14890421550SAndreas Gohr     * It always returns raw Values!
14990421550SAndreas Gohr     *
150f411d872SAndreas Gohr     * @return array
151f411d872SAndreas Gohr     */
152f411d872SAndreas Gohr    public function getDataArray() {
153f411d872SAndreas Gohr        $data = $this->getDataFromDB();
154f411d872SAndreas Gohr        $data = $this->consolidateData($data, true);
155f411d872SAndreas Gohr        return $data;
156f411d872SAndreas Gohr    }
157f411d872SAndreas Gohr
158f411d872SAndreas Gohr    /**
159f411d872SAndreas Gohr     * Return the data in pseudo syntax
160f411d872SAndreas Gohr     */
161f411d872SAndreas Gohr    public function getDataPseudoSyntax() {
162f411d872SAndreas Gohr        $result = '';
163f411d872SAndreas Gohr        $data = $this->getDataArray();
164f411d872SAndreas Gohr        foreach($data as $key => $value) {
165f411d872SAndreas Gohr            $key = $this->schema->getTable() . ".$key";
166f411d872SAndreas Gohr            if(is_array($value)) $value = join(', ', $value);
167f411d872SAndreas Gohr            $result .= sprintf("% -20s : %s\n", $key, $value);
168f411d872SAndreas Gohr        }
169f411d872SAndreas Gohr        return $result;
170f411d872SAndreas Gohr    }
171f411d872SAndreas Gohr
172f411d872SAndreas Gohr    /**
173f411d872SAndreas Gohr     * retrieve the data saved for the page from the database. Usually there is no need to call this function.
174f411d872SAndreas Gohr     * Call @see SchemaData::getData instead.
175f411d872SAndreas Gohr     */
176f411d872SAndreas Gohr    protected function getDataFromDB() {
177f411d872SAndreas Gohr        list($sql, $opt) = $this->buildGetDataSQL();
178f411d872SAndreas Gohr
179f411d872SAndreas Gohr        $res = $this->sqlite->query($sql, $opt);
180f411d872SAndreas Gohr        $data = $this->sqlite->res2arr($res);
181f411d872SAndreas Gohr
182f411d872SAndreas Gohr        return $data;
183f411d872SAndreas Gohr    }
184f411d872SAndreas Gohr
185f411d872SAndreas Gohr    /**
186f411d872SAndreas Gohr     * Creates a proper result array from the database data
187f411d872SAndreas Gohr     *
188f411d872SAndreas Gohr     * @param array $DBdata the data as it is retrieved from the database, i.e. by SchemaData::getDataFromDB
189f411d872SAndreas Gohr     * @param bool $asarray return data as associative array (true) or as array of Values (false)
190f411d872SAndreas Gohr     * @return array|Value[]
191f411d872SAndreas Gohr     */
192f411d872SAndreas Gohr    protected function consolidateData($DBdata, $asarray = false) {
193f411d872SAndreas Gohr        $data = array();
194f411d872SAndreas Gohr
195f411d872SAndreas Gohr        $sep = Search::CONCAT_SEPARATOR;
196f411d872SAndreas Gohr
197f411d872SAndreas Gohr        foreach($this->schema->getColumns(false) as $col) {
198f411d872SAndreas Gohr
19990421550SAndreas Gohr            // if no data saved yet, return empty strings
200f411d872SAndreas Gohr            if($DBdata) {
201f411d872SAndreas Gohr                $val = $DBdata[0]['col' . $col->getColref()];
202f411d872SAndreas Gohr            } else {
203f411d872SAndreas Gohr                $val = '';
204f411d872SAndreas Gohr            }
205f411d872SAndreas Gohr
206f411d872SAndreas Gohr            // multi val data is concatenated
207f411d872SAndreas Gohr            if($col->isMulti()) {
208f411d872SAndreas Gohr                $val = explode($sep, $val);
209f411d872SAndreas Gohr                $val = array_filter($val);
210f411d872SAndreas Gohr            }
211f411d872SAndreas Gohr
21290421550SAndreas Gohr            $value = new Value($col, $val);
213f411d872SAndreas Gohr
21490421550SAndreas Gohr            if($this->opt_skipempty && $value->isEmpty()) continue;
21590421550SAndreas Gohr            if($this->opt_skipempty && !$col->isVisibleInPage()) continue; //FIXME is this a correct assumption?
21690421550SAndreas Gohr
21790421550SAndreas Gohr            // for arrays, we return the raw value only
218f411d872SAndreas Gohr            if($asarray) {
21990421550SAndreas Gohr                $data[$col->getLabel()] = $value->getRawValue();
220f411d872SAndreas Gohr            } else {
22190421550SAndreas Gohr                $data[] = $value;
222f411d872SAndreas Gohr            }
223f411d872SAndreas Gohr        }
224f411d872SAndreas Gohr
225f411d872SAndreas Gohr        return $data;
226f411d872SAndreas Gohr    }
227f411d872SAndreas Gohr
228f411d872SAndreas Gohr    /**
229f411d872SAndreas Gohr     * Builds the SQL statement to select the data for this page and schema
230f411d872SAndreas Gohr     *
231f411d872SAndreas Gohr     * @return array Two fields: the SQL string and the parameters array
232f411d872SAndreas Gohr     */
233f411d872SAndreas Gohr    protected function buildGetDataSQL() {
234f411d872SAndreas Gohr        $sep = Search::CONCAT_SEPARATOR;
235f411d872SAndreas Gohr        $stable = 'data_' . $this->schema->getTable();
236f411d872SAndreas Gohr        $mtable = 'multi_' . $this->schema->getTable();
237f411d872SAndreas Gohr
238f411d872SAndreas Gohr        $QB = new QueryBuilder();
239f411d872SAndreas Gohr        $QB->addTable($stable, 'DATA');
240f411d872SAndreas Gohr        $QB->addSelectColumn('DATA', 'pid', 'PID');
241f411d872SAndreas Gohr        $QB->addGroupByStatement('DATA.pid');
242f411d872SAndreas Gohr
243f411d872SAndreas Gohr        foreach($this->schema->getColumns(false) as $col) {
244f411d872SAndreas Gohr
245f411d872SAndreas Gohr            $colref = $col->getColref();
246f411d872SAndreas Gohr            $colname = 'col' . $colref;
247f411d872SAndreas Gohr
248f411d872SAndreas Gohr            if($col->getType()->isMulti()) {
249f411d872SAndreas Gohr                $tn = 'M' . $colref;
250f411d872SAndreas Gohr                $QB->addLeftJoin(
251f411d872SAndreas Gohr                    'DATA',
252f411d872SAndreas Gohr                    $mtable,
253f411d872SAndreas Gohr                    $tn,
254f411d872SAndreas Gohr                    "DATA.pid = $tn.pid AND DATA.rev = $tn.rev AND $tn.colref = $colref"
255f411d872SAndreas Gohr                );
256f411d872SAndreas Gohr                $col->getType()->select($QB, $tn, 'value', $colname);
257f411d872SAndreas Gohr                $sel = $QB->getSelectStatement($colname);
258f411d872SAndreas Gohr                $QB->addSelectStatement("GROUP_CONCAT($sel, '$sep')", $colname);
259f411d872SAndreas Gohr            } else {
260f411d872SAndreas Gohr                $col->getType()->select($QB, 'DATA', $colname, $colname);
261f411d872SAndreas Gohr                $QB->addGroupByStatement($colname);
262f411d872SAndreas Gohr            }
263f411d872SAndreas Gohr        }
264f411d872SAndreas Gohr
265f411d872SAndreas Gohr        $pl = $QB->addValue($this->pid);
266f411d872SAndreas Gohr        $QB->filters()->whereAnd("DATA.pid = $pl");
267897aef42SAndreas Gohr        $pl = $QB->addValue($this->getLastRevisionTimestamp());
268f411d872SAndreas Gohr        $QB->filters()->whereAnd("DATA.rev = $pl");
269f411d872SAndreas Gohr
270f411d872SAndreas Gohr        return $QB->getSQL();
271f411d872SAndreas Gohr    }
272f411d872SAndreas Gohr
273f411d872SAndreas Gohr    /**
27413eddb0fSAndreas Gohr     * @param int $ts
27513eddb0fSAndreas Gohr     */
27613eddb0fSAndreas Gohr    public function setTimestamp($ts) {
277897aef42SAndreas Gohr        if($ts && $ts < $this->schema->getTimeStamp()) {
278897aef42SAndreas Gohr            throw new StructException('Given timestamp is not valid for current Schema');
279897aef42SAndreas Gohr        }
280897aef42SAndreas Gohr
28113eddb0fSAndreas Gohr        $this->ts = $ts;
28213eddb0fSAndreas Gohr    }
28313eddb0fSAndreas Gohr
28413eddb0fSAndreas Gohr    /**
285897aef42SAndreas Gohr     * Return the last time an edit happened for this table for the currently set
286897aef42SAndreas Gohr     * time and pid. When the current timestamp is 0, the newest revision is
287897aef42SAndreas Gohr     * returned. Used in @see buildGetDataSQL()
288f411d872SAndreas Gohr     *
289897aef42SAndreas Gohr     * @return int
290f411d872SAndreas Gohr     */
291897aef42SAndreas Gohr    abstract protected function getLastRevisionTimestamp();
292*87dc1344SAndreas Gohr
293*87dc1344SAndreas Gohr    /**
294*87dc1344SAndreas Gohr     * Check if the given data validates against the current types.
295*87dc1344SAndreas Gohr     *
296*87dc1344SAndreas Gohr     * @param array $data
297*87dc1344SAndreas Gohr     * @return ValidationResult
298*87dc1344SAndreas Gohr     */
299*87dc1344SAndreas Gohr    public function getValidator($data) {
300*87dc1344SAndreas Gohr        return new ValidationResult($this, $data);
301*87dc1344SAndreas Gohr    }
302f411d872SAndreas Gohr}
303f411d872SAndreas Gohr
304f411d872SAndreas Gohr
305