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 15f411d872SAndreas Gohr // options on how to retieve data 16f411d872SAndreas Gohr protected $opt_skipempty = false; 17f411d872SAndreas Gohr protected $opt_rawvalue = false; 18f411d872SAndreas Gohr 19f411d872SAndreas Gohr /** 20f411d872SAndreas Gohr * Factory Method to access a data or lookup table 21f411d872SAndreas Gohr * 22f411d872SAndreas Gohr * @param Schema $schema schema to load 23f411d872SAndreas Gohr * @param string|int $pid Page or row id to access 24*897aef42SAndreas Gohr * @param int $ts Time at which the data should be read or written, 0 for now 2594c9aa4cSAndreas Gohr * @return AccessTableData|AccessTableLookup 26f411d872SAndreas Gohr */ 27*897aef42SAndreas Gohr public static function bySchema(Schema $schema, $pid, $ts = 0) { 28f411d872SAndreas Gohr if($schema->isLookup()) { 29*897aef42SAndreas Gohr return new AccessTableLookup($schema, $pid, $ts); 30f411d872SAndreas Gohr } else { 31*897aef42SAndreas Gohr return new AccessTableData($schema, $pid, $ts); 32f411d872SAndreas Gohr } 33f411d872SAndreas Gohr } 34f411d872SAndreas Gohr 35f411d872SAndreas Gohr /** 36f411d872SAndreas Gohr * Factory Method to access a data or lookup table 37f411d872SAndreas Gohr * 38f411d872SAndreas Gohr * @param string $tablename schema to load 39f411d872SAndreas Gohr * @param string|int $pid Page or row id to access 40*897aef42SAndreas Gohr * @param int $ts Time at which the data should be read or written, 0 for now 4194c9aa4cSAndreas Gohr * @return AccessTableData|AccessTableLookup 42f411d872SAndreas Gohr */ 43f411d872SAndreas Gohr public static function byTableName($tablename, $pid, $ts = 0) { 44f411d872SAndreas Gohr $schema = new Schema($tablename, $ts); 45*897aef42SAndreas Gohr return self::bySchema($schema, $pid, $ts); 46f411d872SAndreas Gohr } 47f411d872SAndreas Gohr 48f411d872SAndreas Gohr /** 49f411d872SAndreas Gohr * AccessTable constructor 50f411d872SAndreas Gohr * 51*897aef42SAndreas Gohr * @param Schema $schema The schema valid at $ts 52*897aef42SAndreas Gohr * @param string|int $pid 53*897aef42SAndreas Gohr * @param int $ts Time at which the data should be read or written, 0 for now 54f411d872SAndreas Gohr */ 55*897aef42SAndreas Gohr public function __construct(Schema $schema, $pid, $ts = 0) { 56f411d872SAndreas Gohr /** @var \helper_plugin_struct_db $helper */ 57f411d872SAndreas Gohr $helper = plugin_load('helper', 'struct_db'); 58f411d872SAndreas Gohr $this->sqlite = $helper->getDB(); 59f411d872SAndreas Gohr if(!$this->sqlite) { 60f411d872SAndreas Gohr throw new StructException('Sqlite plugin required'); 61f411d872SAndreas Gohr } 62f411d872SAndreas Gohr 63f411d872SAndreas Gohr if(!$schema->getId()) { 64f411d872SAndreas Gohr throw new StructException('Schema does not exist. Only data of existing schemas can be accessed'); 65f411d872SAndreas Gohr } 66f411d872SAndreas Gohr 67f411d872SAndreas Gohr $this->schema = $schema; 68f411d872SAndreas Gohr $this->pid = $pid; 69*897aef42SAndreas Gohr $this->setTimestamp($ts); 70f411d872SAndreas Gohr foreach($this->schema->getColumns() as $col) { 71f411d872SAndreas Gohr $this->labels[$col->getColref()] = $col->getType()->getLabel(); 72f411d872SAndreas Gohr } 73f411d872SAndreas Gohr } 74f411d872SAndreas Gohr 75f411d872SAndreas Gohr /** 76f411d872SAndreas Gohr * gives access to the schema 77f411d872SAndreas Gohr * 78f411d872SAndreas Gohr * @return Schema 79f411d872SAndreas Gohr */ 80f411d872SAndreas Gohr public function getSchema() { 81f411d872SAndreas Gohr return $this->schema; 82f411d872SAndreas Gohr } 83f411d872SAndreas Gohr 84f411d872SAndreas Gohr /** 85f411d872SAndreas Gohr * Should remove the current data, by either deleting or ovewriting it 86f411d872SAndreas Gohr * 87f411d872SAndreas Gohr * @return bool if the delete succeeded 88f411d872SAndreas Gohr */ 89f411d872SAndreas Gohr abstract public function clearData(); 90f411d872SAndreas Gohr 91f411d872SAndreas Gohr /** 92f411d872SAndreas Gohr * Save the data to the database. 93f411d872SAndreas Gohr * 94f411d872SAndreas Gohr * We differentiate between single-value-column and multi-value-column by the value to the respective column-name, 95f411d872SAndreas Gohr * i.e. depending on if that is a string or an array, respectively. 96f411d872SAndreas Gohr * 97f411d872SAndreas Gohr * @param array $data typelabel => value for single fields or typelabel => array(value, value, ...) for multi fields 98f411d872SAndreas Gohr * @return bool success of saving the data to the database 99f411d872SAndreas Gohr */ 100f411d872SAndreas Gohr abstract public function saveData($data); 101f411d872SAndreas Gohr 102f411d872SAndreas Gohr /** 103f411d872SAndreas Gohr * Should empty or invisible (inpage) fields be returned? 104f411d872SAndreas Gohr * 105f411d872SAndreas Gohr * Defaults to false 106f411d872SAndreas Gohr * 107f411d872SAndreas Gohr * @param null|bool $set new value, null to read only 108f411d872SAndreas Gohr * @return bool current value (after set) 109f411d872SAndreas Gohr */ 110f411d872SAndreas Gohr public function optionSkipEmpty($set = null) { 111f411d872SAndreas Gohr if(!is_null($set)) { 112f411d872SAndreas Gohr $this->opt_skipempty = $set; 113f411d872SAndreas Gohr } 114f411d872SAndreas Gohr return $this->opt_skipempty; 115f411d872SAndreas Gohr } 116f411d872SAndreas Gohr 117f411d872SAndreas Gohr /** 118f411d872SAndreas Gohr * Should the values be returned raw or are complex returns okay? 119f411d872SAndreas Gohr * 120f411d872SAndreas Gohr * Defaults to false = complex values okay 121f411d872SAndreas Gohr * 122f411d872SAndreas Gohr * @param null|bool $set new value, null to read only 123f411d872SAndreas Gohr * @return bool current value (after set) 124f411d872SAndreas Gohr */ 125f411d872SAndreas Gohr public function optionRawValue($set = null) { 126f411d872SAndreas Gohr if(!is_null($set)) { 127f411d872SAndreas Gohr $this->opt_rawvalue = $set; 128f411d872SAndreas Gohr } 129f411d872SAndreas Gohr return $this->opt_rawvalue; 130f411d872SAndreas Gohr } 131f411d872SAndreas Gohr 132f411d872SAndreas Gohr 133f411d872SAndreas Gohr /** 134f411d872SAndreas Gohr * Get the value of a single column 135f411d872SAndreas Gohr * 136f411d872SAndreas Gohr * @param Column $column 137f411d872SAndreas Gohr * @return Value|null 138f411d872SAndreas Gohr */ 139f411d872SAndreas Gohr public function getDataColumn($column) { 140f411d872SAndreas Gohr $data = $this->getData(); 141f411d872SAndreas Gohr foreach($data as $value) { 142f411d872SAndreas Gohr if($value->getColumn() == $column) { 143f411d872SAndreas Gohr return $value; 144f411d872SAndreas Gohr } 145f411d872SAndreas Gohr } 146f411d872SAndreas Gohr return null; 147f411d872SAndreas Gohr } 148f411d872SAndreas Gohr 149f411d872SAndreas Gohr /** 150f411d872SAndreas Gohr * returns the data saved for the page 151f411d872SAndreas Gohr * 152f411d872SAndreas Gohr * @return Value[] a list of values saved for the current page 153f411d872SAndreas Gohr */ 154f411d872SAndreas Gohr public function getData() { 155*897aef42SAndreas Gohr $this->getLastRevisionTimestamp($this->pid, $this->ts); 156f411d872SAndreas Gohr $data = $this->getDataFromDB(); 157f411d872SAndreas Gohr $data = $this->consolidateData($data, false); 158f411d872SAndreas Gohr return $data; 159f411d872SAndreas Gohr } 160f411d872SAndreas Gohr 161f411d872SAndreas Gohr /** 162f411d872SAndreas Gohr * returns the data saved for the page as associative array 163f411d872SAndreas Gohr * 164f411d872SAndreas Gohr * The array returned is in the same format as used in @see saveData() 165f411d872SAndreas Gohr * 166f411d872SAndreas Gohr * @return array 167f411d872SAndreas Gohr */ 168f411d872SAndreas Gohr public function getDataArray() { 169*897aef42SAndreas Gohr $this->getLastRevisionTimestamp($this->pid, $this->ts); 170f411d872SAndreas Gohr $data = $this->getDataFromDB(); 171f411d872SAndreas Gohr $data = $this->consolidateData($data, true); 172f411d872SAndreas Gohr return $data; 173f411d872SAndreas Gohr } 174f411d872SAndreas Gohr 175f411d872SAndreas Gohr /** 176f411d872SAndreas Gohr * Return the data in pseudo syntax 177f411d872SAndreas Gohr */ 178f411d872SAndreas Gohr public function getDataPseudoSyntax() { 179f411d872SAndreas Gohr $result = ''; 180f411d872SAndreas Gohr $data = $this->getDataArray(); 181f411d872SAndreas Gohr foreach($data as $key => $value) { 182f411d872SAndreas Gohr $key = $this->schema->getTable() . ".$key"; 183f411d872SAndreas Gohr if(is_array($value)) $value = join(', ', $value); 184f411d872SAndreas Gohr $result .= sprintf("% -20s : %s\n", $key, $value); 185f411d872SAndreas Gohr } 186f411d872SAndreas Gohr return $result; 187f411d872SAndreas Gohr } 188f411d872SAndreas Gohr 189f411d872SAndreas Gohr /** 190f411d872SAndreas Gohr * retrieve the data saved for the page from the database. Usually there is no need to call this function. 191f411d872SAndreas Gohr * Call @see SchemaData::getData instead. 192f411d872SAndreas Gohr */ 193f411d872SAndreas Gohr protected function getDataFromDB() { 194f411d872SAndreas Gohr list($sql, $opt) = $this->buildGetDataSQL(); 195f411d872SAndreas Gohr 196f411d872SAndreas Gohr $res = $this->sqlite->query($sql, $opt); 197f411d872SAndreas Gohr $data = $this->sqlite->res2arr($res); 198f411d872SAndreas Gohr 199f411d872SAndreas Gohr return $data; 200f411d872SAndreas Gohr } 201f411d872SAndreas Gohr 202f411d872SAndreas Gohr /** 203f411d872SAndreas Gohr * Creates a proper result array from the database data 204f411d872SAndreas Gohr * 205f411d872SAndreas Gohr * @param array $DBdata the data as it is retrieved from the database, i.e. by SchemaData::getDataFromDB 206f411d872SAndreas Gohr * @param bool $asarray return data as associative array (true) or as array of Values (false) 207f411d872SAndreas Gohr * @return array|Value[] 208f411d872SAndreas Gohr */ 209f411d872SAndreas Gohr protected function consolidateData($DBdata, $asarray = false) { 210f411d872SAndreas Gohr $data = array(); 211f411d872SAndreas Gohr 212f411d872SAndreas Gohr $sep = Search::CONCAT_SEPARATOR; 213f411d872SAndreas Gohr 214f411d872SAndreas Gohr foreach($this->schema->getColumns(false) as $col) { 215f411d872SAndreas Gohr 216f411d872SAndreas Gohr // if no data saved, yet return empty strings 217f411d872SAndreas Gohr if($DBdata) { 218f411d872SAndreas Gohr $val = $DBdata[0]['col'.$col->getColref()]; 219f411d872SAndreas Gohr } else { 220f411d872SAndreas Gohr $val = ''; 221f411d872SAndreas Gohr } 222f411d872SAndreas Gohr 223f411d872SAndreas Gohr // multi val data is concatenated 224f411d872SAndreas Gohr if($col->isMulti()) { 225f411d872SAndreas Gohr $val = explode($sep, $val); 226f411d872SAndreas Gohr if($this->opt_rawvalue) { 227f411d872SAndreas Gohr $val = array_map( 228f411d872SAndreas Gohr function ($val) use ($col) { // FIXME requires PHP 5.4+ 229f411d872SAndreas Gohr return $col->getType()->rawValue($val); 230f411d872SAndreas Gohr }, 231f411d872SAndreas Gohr $val 232f411d872SAndreas Gohr ); 233f411d872SAndreas Gohr } 234f411d872SAndreas Gohr $val = array_filter($val); 235f411d872SAndreas Gohr } else { 236f411d872SAndreas Gohr if($this->opt_rawvalue) { 237f411d872SAndreas Gohr $val = $col->getType()->rawValue($val); 238f411d872SAndreas Gohr } 239f411d872SAndreas Gohr } 240f411d872SAndreas Gohr 241f411d872SAndreas Gohr if($this->opt_skipempty && ($val === '' || $val == array())) continue; 242f411d872SAndreas Gohr if($this->opt_skipempty && !$col->isVisibleInPage()) continue; 243f411d872SAndreas Gohr 244f411d872SAndreas Gohr if($asarray) { 245f411d872SAndreas Gohr $data[$col->getLabel()] = $val; 246f411d872SAndreas Gohr } else { 247f411d872SAndreas Gohr $data[] = new Value($col, $val); 248f411d872SAndreas Gohr } 249f411d872SAndreas Gohr } 250f411d872SAndreas Gohr 251f411d872SAndreas Gohr return $data; 252f411d872SAndreas Gohr } 253f411d872SAndreas Gohr 254f411d872SAndreas Gohr /** 255f411d872SAndreas Gohr * Builds the SQL statement to select the data for this page and schema 256f411d872SAndreas Gohr * 257f411d872SAndreas Gohr * @return array Two fields: the SQL string and the parameters array 258f411d872SAndreas Gohr */ 259f411d872SAndreas Gohr protected function buildGetDataSQL() { 260f411d872SAndreas Gohr $sep = Search::CONCAT_SEPARATOR; 261f411d872SAndreas Gohr $stable = 'data_' . $this->schema->getTable(); 262f411d872SAndreas Gohr $mtable = 'multi_' . $this->schema->getTable(); 263f411d872SAndreas Gohr 264f411d872SAndreas Gohr $QB = new QueryBuilder(); 265f411d872SAndreas Gohr $QB->addTable($stable, 'DATA'); 266f411d872SAndreas Gohr $QB->addSelectColumn('DATA', 'pid', 'PID'); 267f411d872SAndreas Gohr $QB->addGroupByStatement('DATA.pid'); 268f411d872SAndreas Gohr 269f411d872SAndreas Gohr foreach($this->schema->getColumns(false) as $col) { 270f411d872SAndreas Gohr 271f411d872SAndreas Gohr $colref = $col->getColref(); 272f411d872SAndreas Gohr $colname = 'col'.$colref; 273f411d872SAndreas Gohr 274f411d872SAndreas Gohr if($col->getType()->isMulti()) { 275f411d872SAndreas Gohr $tn = 'M' . $colref; 276f411d872SAndreas Gohr $QB->addLeftJoin( 277f411d872SAndreas Gohr 'DATA', 278f411d872SAndreas Gohr $mtable, 279f411d872SAndreas Gohr $tn, 280f411d872SAndreas Gohr "DATA.pid = $tn.pid AND DATA.rev = $tn.rev AND $tn.colref = $colref" 281f411d872SAndreas Gohr ); 282f411d872SAndreas Gohr $col->getType()->select($QB, $tn, 'value', $colname); 283f411d872SAndreas Gohr $sel = $QB->getSelectStatement($colname); 284f411d872SAndreas Gohr $QB->addSelectStatement("GROUP_CONCAT($sel, '$sep')", $colname); 285f411d872SAndreas Gohr } else { 286f411d872SAndreas Gohr $col->getType()->select($QB, 'DATA', $colname, $colname); 287f411d872SAndreas Gohr $QB->addGroupByStatement($colname); 288f411d872SAndreas Gohr } 289f411d872SAndreas Gohr } 290f411d872SAndreas Gohr 291f411d872SAndreas Gohr $pl = $QB->addValue($this->pid); 292f411d872SAndreas Gohr $QB->filters()->whereAnd("DATA.pid = $pl"); 293*897aef42SAndreas Gohr $pl = $QB->addValue($this->getLastRevisionTimestamp()); 294f411d872SAndreas Gohr $QB->filters()->whereAnd("DATA.rev = $pl"); 295f411d872SAndreas Gohr 296f411d872SAndreas Gohr return $QB->getSQL(); 297f411d872SAndreas Gohr } 298f411d872SAndreas Gohr 299f411d872SAndreas Gohr /** 30013eddb0fSAndreas Gohr * @param int $ts 30113eddb0fSAndreas Gohr */ 30213eddb0fSAndreas Gohr public function setTimestamp($ts) { 303*897aef42SAndreas Gohr if($ts && $ts < $this->schema->getTimeStamp()) { 304*897aef42SAndreas Gohr throw new StructException('Given timestamp is not valid for current Schema'); 305*897aef42SAndreas Gohr } 306*897aef42SAndreas Gohr 30713eddb0fSAndreas Gohr $this->ts = $ts; 30813eddb0fSAndreas Gohr } 30913eddb0fSAndreas Gohr 31013eddb0fSAndreas Gohr 31113eddb0fSAndreas Gohr /** 312*897aef42SAndreas Gohr * Return the last time an edit happened for this table for the currently set 313*897aef42SAndreas Gohr * time and pid. When the current timestamp is 0, the newest revision is 314*897aef42SAndreas Gohr * returned. Used in @see buildGetDataSQL() 315f411d872SAndreas Gohr * 316*897aef42SAndreas Gohr * @return int 317f411d872SAndreas Gohr */ 318*897aef42SAndreas Gohr abstract protected function getLastRevisionTimestamp(); 319f411d872SAndreas Gohr} 320f411d872SAndreas Gohr 321f411d872SAndreas Gohr 322