xref: /plugin/struct/meta/AccessTable.php (revision 9c00b26c19669fd871d6edbc7843179c3cfd94b1)
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
59f411d872SAndreas Gohr        if(!$schema->getId()) {
60f411d872SAndreas Gohr            throw new StructException('Schema does not exist. Only data of existing schemas can be accessed');
61f411d872SAndreas Gohr        }
62f411d872SAndreas Gohr
63f411d872SAndreas Gohr        $this->schema = $schema;
64f411d872SAndreas Gohr        $this->pid = $pid;
65897aef42SAndreas Gohr        $this->setTimestamp($ts);
66f411d872SAndreas Gohr        foreach($this->schema->getColumns() as $col) {
67f411d872SAndreas Gohr            $this->labels[$col->getColref()] = $col->getType()->getLabel();
68f411d872SAndreas Gohr        }
69f411d872SAndreas Gohr    }
70f411d872SAndreas Gohr
71f411d872SAndreas Gohr    /**
72f411d872SAndreas Gohr     * gives access to the schema
73f411d872SAndreas Gohr     *
74f411d872SAndreas Gohr     * @return Schema
75f411d872SAndreas Gohr     */
76f411d872SAndreas Gohr    public function getSchema() {
77f411d872SAndreas Gohr        return $this->schema;
78f411d872SAndreas Gohr    }
79f411d872SAndreas Gohr
80f411d872SAndreas Gohr    /**
81f107f479SAndreas Gohr     * The current pid
82f107f479SAndreas Gohr     *
83f107f479SAndreas Gohr     * @return int|string
84f107f479SAndreas Gohr     */
85f107f479SAndreas Gohr    public function getPid() {
86f107f479SAndreas Gohr        return $this->pid;
87f107f479SAndreas Gohr    }
88f107f479SAndreas Gohr
89f107f479SAndreas Gohr    /**
90f411d872SAndreas Gohr     * Should remove the current data, by either deleting or ovewriting it
91f411d872SAndreas Gohr     *
92f411d872SAndreas Gohr     * @return bool if the delete succeeded
93f411d872SAndreas Gohr     */
94f411d872SAndreas Gohr    abstract public function clearData();
95f411d872SAndreas Gohr
96f411d872SAndreas Gohr    /**
97f411d872SAndreas Gohr     * Save the data to the database.
98f411d872SAndreas Gohr     *
99f411d872SAndreas Gohr     * We differentiate between single-value-column and multi-value-column by the value to the respective column-name,
100f411d872SAndreas Gohr     * i.e. depending on if that is a string or an array, respectively.
101f411d872SAndreas Gohr     *
102f411d872SAndreas Gohr     * @param array $data typelabel => value for single fields or typelabel => array(value, value, ...) for multi fields
103f411d872SAndreas Gohr     * @return bool success of saving the data to the database
104f411d872SAndreas Gohr     */
105f411d872SAndreas Gohr    abstract public function saveData($data);
106f411d872SAndreas Gohr
107f411d872SAndreas Gohr    /**
108f411d872SAndreas Gohr     * Should empty or invisible (inpage) fields be returned?
109f411d872SAndreas Gohr     *
110f411d872SAndreas Gohr     * Defaults to false
111f411d872SAndreas Gohr     *
112f411d872SAndreas Gohr     * @param null|bool $set new value, null to read only
113f411d872SAndreas Gohr     * @return bool current value (after set)
114f411d872SAndreas Gohr     */
115f411d872SAndreas Gohr    public function optionSkipEmpty($set = null) {
116f411d872SAndreas Gohr        if(!is_null($set)) {
117f411d872SAndreas Gohr            $this->opt_skipempty = $set;
118f411d872SAndreas Gohr        }
119f411d872SAndreas Gohr        return $this->opt_skipempty;
120f411d872SAndreas Gohr    }
121f411d872SAndreas Gohr
122f411d872SAndreas Gohr    /**
123f411d872SAndreas Gohr     * Get the value of a single column
124f411d872SAndreas Gohr     *
125f411d872SAndreas Gohr     * @param Column $column
126f411d872SAndreas Gohr     * @return Value|null
127f411d872SAndreas Gohr     */
128f411d872SAndreas Gohr    public function getDataColumn($column) {
129f411d872SAndreas Gohr        $data = $this->getData();
130f411d872SAndreas Gohr        foreach($data as $value) {
131f411d872SAndreas Gohr            if($value->getColumn() == $column) {
132f411d872SAndreas Gohr                return $value;
133f411d872SAndreas Gohr            }
134f411d872SAndreas Gohr        }
135f411d872SAndreas Gohr        return null;
136f411d872SAndreas Gohr    }
137f411d872SAndreas Gohr
138f411d872SAndreas Gohr    /**
139f411d872SAndreas Gohr     * returns the data saved for the page
140f411d872SAndreas Gohr     *
141f411d872SAndreas Gohr     * @return Value[] a list of values saved for the current page
142f411d872SAndreas Gohr     */
143f411d872SAndreas Gohr    public function getData() {
144f411d872SAndreas Gohr        $data = $this->getDataFromDB();
145f411d872SAndreas Gohr        $data = $this->consolidateData($data, false);
146f411d872SAndreas Gohr        return $data;
147f411d872SAndreas Gohr    }
148f411d872SAndreas Gohr
149f411d872SAndreas Gohr    /**
150f411d872SAndreas Gohr     * returns the data saved for the page as associative array
151f411d872SAndreas Gohr     *
152f411d872SAndreas Gohr     * The array returned is in the same format as used in @see saveData()
153f411d872SAndreas Gohr     *
15490421550SAndreas Gohr     * It always returns raw Values!
15590421550SAndreas Gohr     *
156f411d872SAndreas Gohr     * @return array
157f411d872SAndreas Gohr     */
158f411d872SAndreas Gohr    public function getDataArray() {
159f411d872SAndreas Gohr        $data = $this->getDataFromDB();
160f411d872SAndreas Gohr        $data = $this->consolidateData($data, true);
161f411d872SAndreas Gohr        return $data;
162f411d872SAndreas Gohr    }
163f411d872SAndreas Gohr
164f411d872SAndreas Gohr    /**
165f411d872SAndreas Gohr     * Return the data in pseudo syntax
166f411d872SAndreas Gohr     */
167f411d872SAndreas Gohr    public function getDataPseudoSyntax() {
168f411d872SAndreas Gohr        $result = '';
169f411d872SAndreas Gohr        $data = $this->getDataArray();
170f411d872SAndreas Gohr        foreach($data as $key => $value) {
171f411d872SAndreas Gohr            $key = $this->schema->getTable() . ".$key";
172f411d872SAndreas Gohr            if(is_array($value)) $value = join(', ', $value);
173f411d872SAndreas Gohr            $result .= sprintf("% -20s : %s\n", $key, $value);
174f411d872SAndreas Gohr        }
175f411d872SAndreas Gohr        return $result;
176f411d872SAndreas Gohr    }
177f411d872SAndreas Gohr
178f411d872SAndreas Gohr    /**
179f411d872SAndreas Gohr     * retrieve the data saved for the page from the database. Usually there is no need to call this function.
180f411d872SAndreas Gohr     * Call @see SchemaData::getData instead.
181f411d872SAndreas Gohr     */
182f411d872SAndreas Gohr    protected function getDataFromDB() {
183f411d872SAndreas Gohr        list($sql, $opt) = $this->buildGetDataSQL();
184f411d872SAndreas Gohr
185f411d872SAndreas Gohr        $res = $this->sqlite->query($sql, $opt);
186f411d872SAndreas Gohr        $data = $this->sqlite->res2arr($res);
187*9c00b26cSAndreas Gohr        $this->sqlite->res_close($res);
188f411d872SAndreas Gohr        return $data;
189f411d872SAndreas Gohr    }
190f411d872SAndreas Gohr
191f411d872SAndreas Gohr    /**
192f411d872SAndreas Gohr     * Creates a proper result array from the database data
193f411d872SAndreas Gohr     *
194f411d872SAndreas Gohr     * @param array $DBdata the data as it is retrieved from the database, i.e. by SchemaData::getDataFromDB
195f411d872SAndreas Gohr     * @param bool $asarray return data as associative array (true) or as array of Values (false)
196f411d872SAndreas Gohr     * @return array|Value[]
197f411d872SAndreas Gohr     */
198f411d872SAndreas Gohr    protected function consolidateData($DBdata, $asarray = false) {
199f411d872SAndreas Gohr        $data = array();
200f411d872SAndreas Gohr
201f411d872SAndreas Gohr        $sep = Search::CONCAT_SEPARATOR;
202f411d872SAndreas Gohr
203f411d872SAndreas Gohr        foreach($this->schema->getColumns(false) as $col) {
204f411d872SAndreas Gohr
20590421550SAndreas Gohr            // if no data saved yet, return empty strings
206f411d872SAndreas Gohr            if($DBdata) {
207bab52340SAndreas Gohr                $val = $DBdata[0]['out' . $col->getColref()];
208f411d872SAndreas Gohr            } else {
209f411d872SAndreas Gohr                $val = '';
210f411d872SAndreas Gohr            }
211f411d872SAndreas Gohr
212f411d872SAndreas Gohr            // multi val data is concatenated
213f411d872SAndreas Gohr            if($col->isMulti()) {
214f411d872SAndreas Gohr                $val = explode($sep, $val);
215f411d872SAndreas Gohr                $val = array_filter($val);
216f411d872SAndreas Gohr            }
217f411d872SAndreas Gohr
21890421550SAndreas Gohr            $value = new Value($col, $val);
219f411d872SAndreas Gohr
22090421550SAndreas Gohr            if($this->opt_skipempty && $value->isEmpty()) continue;
22190421550SAndreas Gohr            if($this->opt_skipempty && !$col->isVisibleInPage()) continue; //FIXME is this a correct assumption?
22290421550SAndreas Gohr
22390421550SAndreas Gohr            // for arrays, we return the raw value only
224f411d872SAndreas Gohr            if($asarray) {
22590421550SAndreas Gohr                $data[$col->getLabel()] = $value->getRawValue();
226f411d872SAndreas Gohr            } else {
22790421550SAndreas Gohr                $data[] = $value;
228f411d872SAndreas Gohr            }
229f411d872SAndreas Gohr        }
230f411d872SAndreas Gohr
231f411d872SAndreas Gohr        return $data;
232f411d872SAndreas Gohr    }
233f411d872SAndreas Gohr
234f411d872SAndreas Gohr    /**
235f411d872SAndreas Gohr     * Builds the SQL statement to select the data for this page and schema
236f411d872SAndreas Gohr     *
237f411d872SAndreas Gohr     * @return array Two fields: the SQL string and the parameters array
238f411d872SAndreas Gohr     */
239f411d872SAndreas Gohr    protected function buildGetDataSQL() {
240f411d872SAndreas Gohr        $sep = Search::CONCAT_SEPARATOR;
241f411d872SAndreas Gohr        $stable = 'data_' . $this->schema->getTable();
242f411d872SAndreas Gohr        $mtable = 'multi_' . $this->schema->getTable();
243f411d872SAndreas Gohr
244f411d872SAndreas Gohr        $QB = new QueryBuilder();
245f411d872SAndreas Gohr        $QB->addTable($stable, 'DATA');
246f411d872SAndreas Gohr        $QB->addSelectColumn('DATA', 'pid', 'PID');
247f411d872SAndreas Gohr        $QB->addGroupByStatement('DATA.pid');
248f411d872SAndreas Gohr
249f411d872SAndreas Gohr        foreach($this->schema->getColumns(false) as $col) {
250f411d872SAndreas Gohr
251f411d872SAndreas Gohr            $colref = $col->getColref();
252f411d872SAndreas Gohr            $colname = 'col' . $colref;
253bab52340SAndreas Gohr            $outname = 'out' . $colref;
254f411d872SAndreas Gohr
255f411d872SAndreas Gohr            if($col->getType()->isMulti()) {
256f411d872SAndreas Gohr                $tn = 'M' . $colref;
257f411d872SAndreas Gohr                $QB->addLeftJoin(
258f411d872SAndreas Gohr                    'DATA',
259f411d872SAndreas Gohr                    $mtable,
260f411d872SAndreas Gohr                    $tn,
261f411d872SAndreas Gohr                    "DATA.pid = $tn.pid AND DATA.rev = $tn.rev AND $tn.colref = $colref"
262f411d872SAndreas Gohr                );
263bab52340SAndreas Gohr                $col->getType()->select($QB, $tn, 'value', $outname);
264bab52340SAndreas Gohr                $sel = $QB->getSelectStatement($outname);
265bab52340SAndreas Gohr                $QB->addSelectStatement("GROUP_CONCAT($sel, '$sep')", $outname);
266f411d872SAndreas Gohr            } else {
267bab52340SAndreas Gohr                $col->getType()->select($QB, 'DATA', $colname, $outname);
268bab52340SAndreas Gohr                $QB->addGroupByStatement($outname);
269f411d872SAndreas Gohr            }
270f411d872SAndreas Gohr        }
271f411d872SAndreas Gohr
272f411d872SAndreas Gohr        $pl = $QB->addValue($this->pid);
273f411d872SAndreas Gohr        $QB->filters()->whereAnd("DATA.pid = $pl");
274897aef42SAndreas Gohr        $pl = $QB->addValue($this->getLastRevisionTimestamp());
275f411d872SAndreas Gohr        $QB->filters()->whereAnd("DATA.rev = $pl");
276f411d872SAndreas Gohr
277f411d872SAndreas Gohr        return $QB->getSQL();
278f411d872SAndreas Gohr    }
279f411d872SAndreas Gohr
280f411d872SAndreas Gohr    /**
28113eddb0fSAndreas Gohr     * @param int $ts
28213eddb0fSAndreas Gohr     */
28313eddb0fSAndreas Gohr    public function setTimestamp($ts) {
284897aef42SAndreas Gohr        if($ts && $ts < $this->schema->getTimeStamp()) {
285897aef42SAndreas Gohr            throw new StructException('Given timestamp is not valid for current Schema');
286897aef42SAndreas Gohr        }
287897aef42SAndreas Gohr
28813eddb0fSAndreas Gohr        $this->ts = $ts;
28913eddb0fSAndreas Gohr    }
29013eddb0fSAndreas Gohr
29113eddb0fSAndreas Gohr    /**
292897aef42SAndreas Gohr     * Return the last time an edit happened for this table for the currently set
293897aef42SAndreas Gohr     * time and pid. When the current timestamp is 0, the newest revision is
294897aef42SAndreas Gohr     * returned. Used in @see buildGetDataSQL()
295f411d872SAndreas Gohr     *
296897aef42SAndreas Gohr     * @return int
297f411d872SAndreas Gohr     */
298897aef42SAndreas Gohr    abstract protected function getLastRevisionTimestamp();
29987dc1344SAndreas Gohr
30087dc1344SAndreas Gohr    /**
30187dc1344SAndreas Gohr     * Check if the given data validates against the current types.
30287dc1344SAndreas Gohr     *
30387dc1344SAndreas Gohr     * @param array $data
30493ca6f4fSAndreas Gohr     * @return AccessDataValidator
30587dc1344SAndreas Gohr     */
30687dc1344SAndreas Gohr    public function getValidator($data) {
30793ca6f4fSAndreas Gohr        return new AccessDataValidator($this, $data);
30887dc1344SAndreas Gohr    }
309f411d872SAndreas Gohr}
310f411d872SAndreas Gohr
311f411d872SAndreas Gohr
312