1fb31ca9fSAndreas Gohr<?php 2fb31ca9fSAndreas Gohr 3fb31ca9fSAndreas Gohrnamespace plugin\struct\meta; 4fb31ca9fSAndreas Gohr 51a8d1235SAndreas Gohr/** 61a8d1235SAndreas Gohr * Class Assignments 71a8d1235SAndreas Gohr * 81a8d1235SAndreas Gohr * Manages the assignment of schemas (table names) to pages and namespaces 91a8d1235SAndreas Gohr * 101a8d1235SAndreas Gohr * @package plugin\struct\meta 111a8d1235SAndreas Gohr */ 12fb31ca9fSAndreas Gohrclass Assignments { 13fb31ca9fSAndreas Gohr 14fb31ca9fSAndreas Gohr /** @var \helper_plugin_sqlite|null */ 15fb31ca9fSAndreas Gohr protected $sqlite; 16fb31ca9fSAndreas Gohr 17*33d7be6aSAndreas Gohr /** @var array All the assignments patterns */ 1849d38573SAndreas Gohr protected $patterns; 19fb31ca9fSAndreas Gohr 20fb31ca9fSAndreas Gohr /** 21fb31ca9fSAndreas Gohr * Assignments constructor. 22fb31ca9fSAndreas Gohr */ 23fb31ca9fSAndreas Gohr public function __construct() { 24fb31ca9fSAndreas Gohr /** @var \helper_plugin_struct_db $helper */ 25fb31ca9fSAndreas Gohr $helper = plugin_load('helper', 'struct_db'); 26fb31ca9fSAndreas Gohr $this->sqlite = $helper->getDB(); 27fb31ca9fSAndreas Gohr 28*33d7be6aSAndreas Gohr if($this->sqlite) $this->loadPatterns(); 29fb31ca9fSAndreas Gohr } 30fb31ca9fSAndreas Gohr 31fb31ca9fSAndreas Gohr /** 3249d38573SAndreas Gohr * Load existing assignment patterns 33fb31ca9fSAndreas Gohr */ 34*33d7be6aSAndreas Gohr protected function loadPatterns() { 3549d38573SAndreas Gohr $sql = 'SELECT * FROM schema_assignments_patterns ORDER BY pattern'; 36fb31ca9fSAndreas Gohr $res = $this->sqlite->query($sql); 3749d38573SAndreas Gohr $this->patterns = $this->sqlite->res2arr($res); 38fb31ca9fSAndreas Gohr $this->sqlite->res_close($res); 39fb31ca9fSAndreas Gohr } 40fb31ca9fSAndreas Gohr 41fb31ca9fSAndreas Gohr /** 4249d38573SAndreas Gohr * Add a new assignment pattern to the pattern table 431a8d1235SAndreas Gohr * 4449d38573SAndreas Gohr * @param string $pattern 451a8d1235SAndreas Gohr * @param string $table 461a8d1235SAndreas Gohr * @return bool 471a8d1235SAndreas Gohr */ 48*33d7be6aSAndreas Gohr public function addPattern($pattern, $table) { 4949d38573SAndreas Gohr $sql = 'REPLACE INTO schema_assignments_patterns (pattern, tbl) VALUES (?,?)'; 5049d38573SAndreas Gohr return (bool) $this->sqlite->query($sql, array($pattern, $table)); 511a8d1235SAndreas Gohr } 521a8d1235SAndreas Gohr 531a8d1235SAndreas Gohr /** 5449d38573SAndreas Gohr * Remove an existing assignment pattern from the pattern table 551a8d1235SAndreas Gohr * 5649d38573SAndreas Gohr * @param string $pattern 571a8d1235SAndreas Gohr * @param string $table 581a8d1235SAndreas Gohr * @return bool 591a8d1235SAndreas Gohr */ 60*33d7be6aSAndreas Gohr public function removePattern($pattern, $table) { 6149d38573SAndreas Gohr $sql = 'DELETE FROM schema_assignments_patterns WHERE pattern = ? AND tbl = ?'; 6249d38573SAndreas Gohr return (bool) $this->sqlite->query($sql, array($pattern, $table)); 631a8d1235SAndreas Gohr } 641a8d1235SAndreas Gohr 651a8d1235SAndreas Gohr /** 6649d38573SAndreas Gohr * Get the whole pattern table 671a8d1235SAndreas Gohr * 681a8d1235SAndreas Gohr * @return array 691a8d1235SAndreas Gohr */ 70*33d7be6aSAndreas Gohr public function getAllPatterns() { 7149d38573SAndreas Gohr return $this->patterns; 721a8d1235SAndreas Gohr } 731a8d1235SAndreas Gohr 741a8d1235SAndreas Gohr /** 75fb31ca9fSAndreas Gohr * Returns a list of table names assigned to the given page 76fb31ca9fSAndreas Gohr * 77fb31ca9fSAndreas Gohr * @param string $page 78fb31ca9fSAndreas Gohr * @return string[] tables assigned 79fb31ca9fSAndreas Gohr */ 80fb31ca9fSAndreas Gohr public function getPageAssignments($page) { 81fb31ca9fSAndreas Gohr $tables = array(); 82fb31ca9fSAndreas Gohr 83fb31ca9fSAndreas Gohr $page = cleanID($page); 84fb31ca9fSAndreas Gohr $pns = ':' . getNS($page) . ':'; 85fb31ca9fSAndreas Gohr 8649d38573SAndreas Gohr foreach($this->patterns as $row) { 8749d38573SAndreas Gohr $pat = $row['pattern']; 88fb31ca9fSAndreas Gohr $tbl = $row['tbl']; 89fb31ca9fSAndreas Gohr 9049d38573SAndreas Gohr $ans = ':' . cleanID($pat) . ':'; 91fb31ca9fSAndreas Gohr 9249d38573SAndreas Gohr if(substr($pat, -2) == '**') { 93fb31ca9fSAndreas Gohr // upper namespaces match 94fb31ca9fSAndreas Gohr if(strpos($pns, $ans) === 0) { 95fb31ca9fSAndreas Gohr $tables[] = $tbl; 96fb31ca9fSAndreas Gohr } 9749d38573SAndreas Gohr } else if(substr($pat, -1) == '*') { 98fb31ca9fSAndreas Gohr // namespaces match exact 99fb31ca9fSAndreas Gohr if($ans == $pns) { 100fb31ca9fSAndreas Gohr $tables[] = $tbl; 101fb31ca9fSAndreas Gohr } 102fb31ca9fSAndreas Gohr } else { 103fb31ca9fSAndreas Gohr // exact match 10449d38573SAndreas Gohr if(cleanID($pat) == $page) { 105fb31ca9fSAndreas Gohr $tables[] = $tbl; 106fb31ca9fSAndreas Gohr } 107fb31ca9fSAndreas Gohr } 108fb31ca9fSAndreas Gohr } 109fb31ca9fSAndreas Gohr 110fb31ca9fSAndreas Gohr return array_unique($tables); 111fb31ca9fSAndreas Gohr } 11256672c36SAndreas Gohr 11356672c36SAndreas Gohr /** 11456672c36SAndreas Gohr * Returns all tables of schemas that existed and stored data for the page back then 11556672c36SAndreas Gohr * 11656672c36SAndreas Gohr * @todo this is not used currently and can probably be removed again, because we're 11756672c36SAndreas Gohr * always only interested in the current state of affairs, even when restoring. 11856672c36SAndreas Gohr * 11956672c36SAndreas Gohr * @param string $page 12056672c36SAndreas Gohr * @param string $ts 12156672c36SAndreas Gohr * @return array 12256672c36SAndreas Gohr */ 12356672c36SAndreas Gohr public function getHistoricAssignments($page, $ts) { 12456672c36SAndreas Gohr $sql = "SELECT DISTINCT tbl FROM schemas WHERE ts <= ? ORDER BY ts DESC"; 12556672c36SAndreas Gohr $res = $this->sqlite->query($sql, $ts); 12656672c36SAndreas Gohr $tables = $this->sqlite->res2arr($res); 12756672c36SAndreas Gohr $this->sqlite->res_close($res); 12856672c36SAndreas Gohr 12956672c36SAndreas Gohr $assigned = array(); 13056672c36SAndreas Gohr foreach ($tables as $row) { 13156672c36SAndreas Gohr $table = $row['tbl']; 13256672c36SAndreas Gohr $sql = "SELECT pid FROM data_$table WHERE pid = ? AND rev <= ? LIMIT 1"; 13356672c36SAndreas Gohr $res = $this->sqlite->query($sql, $page, $ts); 13456672c36SAndreas Gohr $found = $this->sqlite->res2arr($res); 13556672c36SAndreas Gohr $this->sqlite->res_close($res); 13656672c36SAndreas Gohr 13756672c36SAndreas Gohr if($found) $assigned[] = $table; 13856672c36SAndreas Gohr } 13956672c36SAndreas Gohr 14056672c36SAndreas Gohr return $assigned; 14156672c36SAndreas Gohr } 142fb31ca9fSAndreas Gohr} 143