1fb31ca9fSAndreas Gohr<?php 2fb31ca9fSAndreas Gohr 3ba766201SAndreas Gohrnamespace dokuwiki\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 * 10025cb9daSAndreas Gohr * This is a singleton. Assignment data is only loaded once per request. 11025cb9daSAndreas Gohr * 12ba766201SAndreas Gohr * @package dokuwiki\plugin\struct\meta 131a8d1235SAndreas Gohr */ 14d6d97f60SAnna Dabrowskaclass Assignments 15d6d97f60SAnna Dabrowska{ 16fb31ca9fSAndreas Gohr /** @var \helper_plugin_sqlite|null */ 17fb31ca9fSAndreas Gohr protected $sqlite; 18fb31ca9fSAndreas Gohr 1933d7be6aSAndreas Gohr /** @var array All the assignments patterns */ 2049d38573SAndreas Gohr protected $patterns; 21fb31ca9fSAndreas Gohr 22025cb9daSAndreas Gohr /** @var Assignments */ 23025cb9daSAndreas Gohr protected static $instance = null; 24025cb9daSAndreas Gohr 25025cb9daSAndreas Gohr /** 26025cb9daSAndreas Gohr * Get the singleton instance of the Assignments 27025cb9daSAndreas Gohr * 28025cb9daSAndreas Gohr * @param bool $forcereload create a new instace to reload the assignment data 29025cb9daSAndreas Gohr * @return Assignments 30025cb9daSAndreas Gohr */ 31d6d97f60SAnna Dabrowska public static function getInstance($forcereload = false) 32d6d97f60SAnna Dabrowska { 33025cb9daSAndreas Gohr if (is_null(self::$instance) or $forcereload) { 34025cb9daSAndreas Gohr $class = get_called_class(); 35025cb9daSAndreas Gohr self::$instance = new $class(); 36025cb9daSAndreas Gohr } 37025cb9daSAndreas Gohr return self::$instance; 38025cb9daSAndreas Gohr } 39025cb9daSAndreas Gohr 40fb31ca9fSAndreas Gohr /** 41fb31ca9fSAndreas Gohr * Assignments constructor. 42025cb9daSAndreas Gohr * 43025cb9daSAndreas Gohr * Not public. Use Assignments::getInstance() instead 44fb31ca9fSAndreas Gohr */ 45d6d97f60SAnna Dabrowska protected function __construct() 46d6d97f60SAnna Dabrowska { 47fb31ca9fSAndreas Gohr /** @var \helper_plugin_struct_db $helper */ 48fb31ca9fSAndreas Gohr $helper = plugin_load('helper', 'struct_db'); 49fb31ca9fSAndreas Gohr $this->sqlite = $helper->getDB(); 50fb31ca9fSAndreas Gohr 51fc26989eSAndreas Gohr $this->loadPatterns(); 52fb31ca9fSAndreas Gohr } 53fb31ca9fSAndreas Gohr 54025cb9daSAndreas Gohr 55fb31ca9fSAndreas Gohr /** 5649d38573SAndreas Gohr * Load existing assignment patterns 57fb31ca9fSAndreas Gohr */ 58d6d97f60SAnna Dabrowska protected function loadPatterns() 59d6d97f60SAnna Dabrowska { 6049d38573SAndreas Gohr $sql = 'SELECT * FROM schema_assignments_patterns ORDER BY pattern'; 61fb31ca9fSAndreas Gohr $res = $this->sqlite->query($sql); 6249d38573SAndreas Gohr $this->patterns = $this->sqlite->res2arr($res); 63fb31ca9fSAndreas Gohr $this->sqlite->res_close($res); 64fb31ca9fSAndreas Gohr } 65fb31ca9fSAndreas Gohr 66fb31ca9fSAndreas Gohr /** 6749d38573SAndreas Gohr * Add a new assignment pattern to the pattern table 681a8d1235SAndreas Gohr * 6949d38573SAndreas Gohr * @param string $pattern 701a8d1235SAndreas Gohr * @param string $table 711a8d1235SAndreas Gohr * @return bool 721a8d1235SAndreas Gohr */ 73d6d97f60SAnna Dabrowska public function addPattern($pattern, $table) 74d6d97f60SAnna Dabrowska { 75ed60c3b3SAndreas Gohr // add the pattern 7649d38573SAndreas Gohr $sql = 'REPLACE INTO schema_assignments_patterns (pattern, tbl) VALUES (?,?)'; 77ed60c3b3SAndreas Gohr $ok = (bool)$this->sqlite->query($sql, array($pattern, $table)); 78ed60c3b3SAndreas Gohr 79ed60c3b3SAndreas Gohr // reload patterns 80ed60c3b3SAndreas Gohr $this->loadPatterns(); 81b25bb9feSMichael Grosse $this->propagatePageAssignments($table); 82ed60c3b3SAndreas Gohr 83ed60c3b3SAndreas Gohr 84ed60c3b3SAndreas Gohr return $ok; 851a8d1235SAndreas Gohr } 861a8d1235SAndreas Gohr 871a8d1235SAndreas Gohr /** 8849d38573SAndreas Gohr * Remove an existing assignment pattern from the pattern table 891a8d1235SAndreas Gohr * 9049d38573SAndreas Gohr * @param string $pattern 911a8d1235SAndreas Gohr * @param string $table 921a8d1235SAndreas Gohr * @return bool 931a8d1235SAndreas Gohr */ 94d6d97f60SAnna Dabrowska public function removePattern($pattern, $table) 95d6d97f60SAnna Dabrowska { 96ed60c3b3SAndreas Gohr // remove the pattern 9749d38573SAndreas Gohr $sql = 'DELETE FROM schema_assignments_patterns WHERE pattern = ? AND tbl = ?'; 98ed60c3b3SAndreas Gohr $ok = (bool)$this->sqlite->query($sql, array($pattern, $table)); 99ed60c3b3SAndreas Gohr 100ed60c3b3SAndreas Gohr // reload patterns 101ed60c3b3SAndreas Gohr $this->loadPatterns(); 102ed60c3b3SAndreas Gohr 103ed60c3b3SAndreas Gohr // fetch possibly affected pages 104ed60c3b3SAndreas Gohr $sql = 'SELECT pid FROM schema_assignments WHERE tbl = ?'; 105ed60c3b3SAndreas Gohr $res = $this->sqlite->query($sql, $table); 1060e9e058fSAndreas Gohr $pagerows = $this->sqlite->res2arr($res); 107ed60c3b3SAndreas Gohr $this->sqlite->res_close($res); 108ed60c3b3SAndreas Gohr 109ed60c3b3SAndreas Gohr // reevalute the pages and unassign when needed 1100e9e058fSAndreas Gohr foreach ($pagerows as $row) { 111be94e9d9SAndreas Gohr $tables = $this->getPageAssignments($row['pid'], true); 112ed60c3b3SAndreas Gohr if (!in_array($table, $tables)) { 113be94e9d9SAndreas Gohr $this->deassignPageSchema($row['pid'], $table); 114ed60c3b3SAndreas Gohr } 115ed60c3b3SAndreas Gohr } 116ed60c3b3SAndreas Gohr 117ed60c3b3SAndreas Gohr return $ok; 118ed60c3b3SAndreas Gohr } 119ed60c3b3SAndreas Gohr 120ed60c3b3SAndreas Gohr /** 1210173e75dSAndreas Gohr * Rechecks all assignments of a given page against the current patterns 1220173e75dSAndreas Gohr * 1230173e75dSAndreas Gohr * @param string $pid 1240173e75dSAndreas Gohr */ 125d6d97f60SAnna Dabrowska public function reevaluatePageAssignments($pid) 126d6d97f60SAnna Dabrowska { 1270173e75dSAndreas Gohr // reload patterns 1280173e75dSAndreas Gohr $this->loadPatterns(); 1290173e75dSAndreas Gohr $tables = $this->getPageAssignments($pid, true); 1300173e75dSAndreas Gohr 1310173e75dSAndreas Gohr // fetch possibly affected tables 1320173e75dSAndreas Gohr $sql = 'SELECT tbl FROM schema_assignments WHERE pid = ?'; 1330173e75dSAndreas Gohr $res = $this->sqlite->query($sql, $pid); 1340173e75dSAndreas Gohr $tablerows = $this->sqlite->res2arr($res); 1350173e75dSAndreas Gohr $this->sqlite->res_close($res); 1360173e75dSAndreas Gohr 1370173e75dSAndreas Gohr // reevalute the tables and apply assignments 1380173e75dSAndreas Gohr foreach ($tablerows as $row) { 1390173e75dSAndreas Gohr if (in_array($row['tbl'], $tables)) { 1400173e75dSAndreas Gohr $this->assignPageSchema($pid, $row['tbl']); 1410173e75dSAndreas Gohr } else { 1420173e75dSAndreas Gohr $this->deassignPageSchema($pid, $row['tbl']); 1430173e75dSAndreas Gohr } 1440173e75dSAndreas Gohr } 1450173e75dSAndreas Gohr } 1460173e75dSAndreas Gohr 1470173e75dSAndreas Gohr /** 1480e9e058fSAndreas Gohr * Clear all patterns - deassigns all pages 1490e9e058fSAndreas Gohr * 1500e9e058fSAndreas Gohr * This is mostly useful for testing and not used in the interface currently 1510e9e058fSAndreas Gohr * 152153400c7SAndreas Gohr * @param bool $full fully delete all previous assignments 1530e9e058fSAndreas Gohr * @return bool 1540e9e058fSAndreas Gohr */ 155d6d97f60SAnna Dabrowska public function clear($full = false) 156d6d97f60SAnna Dabrowska { 1570e9e058fSAndreas Gohr $sql = 'DELETE FROM schema_assignments_patterns'; 1580e9e058fSAndreas Gohr $ok = (bool)$this->sqlite->query($sql); 1590e9e058fSAndreas Gohr 160153400c7SAndreas Gohr if ($full) { 161153400c7SAndreas Gohr $sql = 'DELETE FROM schema_assignments'; 162153400c7SAndreas Gohr } else { 1630e9e058fSAndreas Gohr $sql = 'UPDATE schema_assignments SET assigned = 0'; 164153400c7SAndreas Gohr } 1650e9e058fSAndreas Gohr $ok = $ok && (bool)$this->sqlite->query($sql); 1660e9e058fSAndreas Gohr 1670e9e058fSAndreas Gohr // reload patterns 1680e9e058fSAndreas Gohr $this->loadPatterns(); 1690e9e058fSAndreas Gohr 1700e9e058fSAndreas Gohr return $ok; 1710e9e058fSAndreas Gohr } 1720e9e058fSAndreas Gohr 1730e9e058fSAndreas Gohr /** 174ed60c3b3SAndreas Gohr * Add page to assignments 175ed60c3b3SAndreas Gohr * 176ed60c3b3SAndreas Gohr * @param string $page 177ed60c3b3SAndreas Gohr * @param string $table 178ed60c3b3SAndreas Gohr * @return bool 179ed60c3b3SAndreas Gohr */ 180d6d97f60SAnna Dabrowska public function assignPageSchema($page, $table) 181d6d97f60SAnna Dabrowska { 182ed60c3b3SAndreas Gohr $sql = 'REPLACE INTO schema_assignments (pid, tbl, assigned) VALUES (?, ?, 1)'; 183ed60c3b3SAndreas Gohr return (bool)$this->sqlite->query($sql, array($page, $table)); 184ed60c3b3SAndreas Gohr } 185ed60c3b3SAndreas Gohr 186ed60c3b3SAndreas Gohr /** 187ed60c3b3SAndreas Gohr * Remove page from assignments 188ed60c3b3SAndreas Gohr * 189ed60c3b3SAndreas Gohr * @param string $page 190ed60c3b3SAndreas Gohr * @param string $table 191ed60c3b3SAndreas Gohr * @return bool 192ed60c3b3SAndreas Gohr */ 193d6d97f60SAnna Dabrowska public function deassignPageSchema($page, $table) 194d6d97f60SAnna Dabrowska { 195ed60c3b3SAndreas Gohr $sql = 'REPLACE INTO schema_assignments (pid, tbl, assigned) VALUES (?, ?, 0)'; 196ed60c3b3SAndreas Gohr return (bool)$this->sqlite->query($sql, array($page, $table)); 1971a8d1235SAndreas Gohr } 1981a8d1235SAndreas Gohr 1991a8d1235SAndreas Gohr /** 20049d38573SAndreas Gohr * Get the whole pattern table 2011a8d1235SAndreas Gohr * 2021a8d1235SAndreas Gohr * @return array 2031a8d1235SAndreas Gohr */ 204d6d97f60SAnna Dabrowska public function getAllPatterns() 205d6d97f60SAnna Dabrowska { 20649d38573SAndreas Gohr return $this->patterns; 2071a8d1235SAndreas Gohr } 2081a8d1235SAndreas Gohr 2091a8d1235SAndreas Gohr /** 210fb31ca9fSAndreas Gohr * Returns a list of table names assigned to the given page 211fb31ca9fSAndreas Gohr * 212fb31ca9fSAndreas Gohr * @param string $page 2139ff81b7fSAndreas Gohr * @param bool $checkpatterns Should the current patterns be re-evaluated? 2149ff81b7fSAndreas Gohr * @return \string[] tables assigned 215fb31ca9fSAndreas Gohr */ 216d6d97f60SAnna Dabrowska public function getPageAssignments($page, $checkpatterns = true) 217d6d97f60SAnna Dabrowska { 218fb31ca9fSAndreas Gohr $tables = array(); 219fb31ca9fSAndreas Gohr $page = cleanID($page); 220fb31ca9fSAndreas Gohr 2219ff81b7fSAndreas Gohr if ($checkpatterns) { 2229ff81b7fSAndreas Gohr // evaluate patterns 2239ff81b7fSAndreas Gohr $pns = ':' . getNS($page) . ':'; 22449d38573SAndreas Gohr foreach ($this->patterns as $row) { 225ed60c3b3SAndreas Gohr if ($this->matchPagePattern($row['pattern'], $page, $pns)) { 226ed60c3b3SAndreas Gohr $tables[] = $row['tbl']; 227fb31ca9fSAndreas Gohr } 228fb31ca9fSAndreas Gohr } 2299ff81b7fSAndreas Gohr } else { 2309ff81b7fSAndreas Gohr // just select 2319ff81b7fSAndreas Gohr $sql = 'SELECT tbl FROM schema_assignments WHERE pid = ? AND assigned = 1'; 2329ff81b7fSAndreas Gohr $res = $this->sqlite->query($sql, array($page)); 2339ff81b7fSAndreas Gohr $list = $this->sqlite->res2arr($res); 2349ff81b7fSAndreas Gohr $this->sqlite->res_close($res); 2359ff81b7fSAndreas Gohr foreach ($list as $row) { 2369ff81b7fSAndreas Gohr $tables[] = $row['tbl']; 2379ff81b7fSAndreas Gohr } 2389ff81b7fSAndreas Gohr } 239fb31ca9fSAndreas Gohr 240fb31ca9fSAndreas Gohr return array_unique($tables); 241fb31ca9fSAndreas Gohr } 24256672c36SAndreas Gohr 24356672c36SAndreas Gohr /** 244153400c7SAndreas Gohr * Get the pages known to struct and their assignment state 245153400c7SAndreas Gohr * 246153400c7SAndreas Gohr * @param null|string $schema limit results to the given schema 247153400c7SAndreas Gohr * @param bool $assignedonly limit results to currently assigned only 248153400c7SAndreas Gohr * @return array 249153400c7SAndreas Gohr */ 250d6d97f60SAnna Dabrowska public function getPages($schema = null, $assignedonly = false) 251d6d97f60SAnna Dabrowska { 252153400c7SAndreas Gohr $sql = 'SELECT pid, tbl, assigned FROM schema_assignments WHERE 1=1'; 253153400c7SAndreas Gohr 254153400c7SAndreas Gohr $opts = array(); 255153400c7SAndreas Gohr if ($schema) { 256153400c7SAndreas Gohr $sql .= ' AND tbl = ?'; 257153400c7SAndreas Gohr $opts[] = $schema; 258153400c7SAndreas Gohr } 259153400c7SAndreas Gohr if ($assignedonly) { 260153400c7SAndreas Gohr $sql .= ' AND assigned = 1'; 261153400c7SAndreas Gohr } 262153400c7SAndreas Gohr 263153400c7SAndreas Gohr $sql .= ' ORDER BY pid, tbl'; 264153400c7SAndreas Gohr 265153400c7SAndreas Gohr $res = $this->sqlite->query($sql, $opts); 266153400c7SAndreas Gohr $list = $this->sqlite->res2arr($res); 267153400c7SAndreas Gohr $this->sqlite->res_close($res); 268153400c7SAndreas Gohr 269153400c7SAndreas Gohr $result = array(); 270153400c7SAndreas Gohr foreach ($list as $row) { 271153400c7SAndreas Gohr $pid = $row['pid']; 272153400c7SAndreas Gohr $tbl = $row['tbl']; 273153400c7SAndreas Gohr if (!isset($result[$pid])) $result[$pid] = array(); 274153400c7SAndreas Gohr $result[$pid][$tbl] = (bool)$row['assigned']; 275153400c7SAndreas Gohr } 276153400c7SAndreas Gohr 277153400c7SAndreas Gohr return $result; 278153400c7SAndreas Gohr } 279153400c7SAndreas Gohr 280153400c7SAndreas Gohr /** 281ed60c3b3SAndreas Gohr * Check if the given pattern matches the given page 282ed60c3b3SAndreas Gohr * 283ed60c3b3SAndreas Gohr * @param string $pattern the pattern to check against 284ed60c3b3SAndreas Gohr * @param string $page the cleaned pageid to check 285ed60c3b3SAndreas Gohr * @param string|null $pns optimization, the colon wrapped namespace of the page, set null for automatic 286ed60c3b3SAndreas Gohr * @return bool 287ed60c3b3SAndreas Gohr */ 288d6d97f60SAnna Dabrowska protected function matchPagePattern($pattern, $page, $pns = null) 289d6d97f60SAnna Dabrowska { 2900e9e058fSAndreas Gohr if (trim($pattern, ':') == '**') return true; // match all 2910e9e058fSAndreas Gohr 2929914e87eSAndreas Gohr // regex patterns 2938da1363aSAndreas Gohr if ($pattern[0] == '/') { 2949914e87eSAndreas Gohr return (bool)preg_match($pattern, ":$page"); 2959914e87eSAndreas Gohr } 2969914e87eSAndreas Gohr 297ed60c3b3SAndreas Gohr if (is_null($pns)) { 298ed60c3b3SAndreas Gohr $pns = ':' . getNS($page) . ':'; 299ed60c3b3SAndreas Gohr } 300ed60c3b3SAndreas Gohr 301ed60c3b3SAndreas Gohr $ans = ':' . cleanID($pattern) . ':'; 302ed60c3b3SAndreas Gohr if (substr($pattern, -2) == '**') { 303ed60c3b3SAndreas Gohr // upper namespaces match 304ed60c3b3SAndreas Gohr if (strpos($pns, $ans) === 0) { 305ed60c3b3SAndreas Gohr return true; 306ed60c3b3SAndreas Gohr } 307ed60c3b3SAndreas Gohr } elseif (substr($pattern, -1) == '*') { 308ed60c3b3SAndreas Gohr // namespaces match exact 309ed60c3b3SAndreas Gohr if ($ans == $pns) { 310ed60c3b3SAndreas Gohr return true; 311ed60c3b3SAndreas Gohr } 312ed60c3b3SAndreas Gohr } else { 313ed60c3b3SAndreas Gohr // exact match 314ed60c3b3SAndreas Gohr if (cleanID($pattern) == $page) { 315ed60c3b3SAndreas Gohr return true; 316ed60c3b3SAndreas Gohr } 317ed60c3b3SAndreas Gohr } 318ed60c3b3SAndreas Gohr 319ed60c3b3SAndreas Gohr return false; 320ed60c3b3SAndreas Gohr } 321ed60c3b3SAndreas Gohr 322ed60c3b3SAndreas Gohr /** 32356672c36SAndreas Gohr * Returns all tables of schemas that existed and stored data for the page back then 32456672c36SAndreas Gohr * 3250e9e058fSAndreas Gohr * @deprecated because we're always only interested in the current state of affairs, even when restoring. 32656672c36SAndreas Gohr * 32756672c36SAndreas Gohr * @param string $page 32856672c36SAndreas Gohr * @param string $ts 32956672c36SAndreas Gohr * @return array 33056672c36SAndreas Gohr */ 331d6d97f60SAnna Dabrowska public function getHistoricAssignments($page, $ts) 332d6d97f60SAnna Dabrowska { 33356672c36SAndreas Gohr $sql = "SELECT DISTINCT tbl FROM schemas WHERE ts <= ? ORDER BY ts DESC"; 33456672c36SAndreas Gohr $res = $this->sqlite->query($sql, $ts); 33556672c36SAndreas Gohr $tables = $this->sqlite->res2arr($res); 33656672c36SAndreas Gohr $this->sqlite->res_close($res); 33756672c36SAndreas Gohr 33856672c36SAndreas Gohr $assigned = array(); 33956672c36SAndreas Gohr foreach ($tables as $row) { 34056672c36SAndreas Gohr $table = $row['tbl']; 341ed60c3b3SAndreas Gohr /** @noinspection SqlResolve */ 34256672c36SAndreas Gohr $sql = "SELECT pid FROM data_$table WHERE pid = ? AND rev <= ? LIMIT 1"; 34356672c36SAndreas Gohr $res = $this->sqlite->query($sql, $page, $ts); 34456672c36SAndreas Gohr $found = $this->sqlite->res2arr($res); 34556672c36SAndreas Gohr $this->sqlite->res_close($res); 34656672c36SAndreas Gohr 34756672c36SAndreas Gohr if ($found) $assigned[] = $table; 34856672c36SAndreas Gohr } 34956672c36SAndreas Gohr 35056672c36SAndreas Gohr return $assigned; 35156672c36SAndreas Gohr } 352b25bb9feSMichael Grosse 353b25bb9feSMichael Grosse /** 354*17a3a578SAndreas Gohr * fetch all pages where the schema isn't assigned, yet 355*17a3a578SAndreas Gohr * and reevaluate the page assignments for those pages and assign when needed 356b25bb9feSMichael Grosse * 357b25bb9feSMichael Grosse * @param $table 358b25bb9feSMichael Grosse */ 359d6d97f60SAnna Dabrowska public function propagatePageAssignments($table) 360d6d97f60SAnna Dabrowska { 361b25bb9feSMichael Grosse $sql = 'SELECT pid FROM schema_assignments WHERE tbl != ? OR assigned != 1'; 362b25bb9feSMichael Grosse $res = $this->sqlite->query($sql, $table); 363b25bb9feSMichael Grosse $pagerows = $this->sqlite->res2arr($res); 364b25bb9feSMichael Grosse $this->sqlite->res_close($res); 365b25bb9feSMichael Grosse 366b25bb9feSMichael Grosse foreach ($pagerows as $row) { 367b25bb9feSMichael Grosse $tables = $this->getPageAssignments($row['pid'], true); 368b25bb9feSMichael Grosse if (in_array($table, $tables)) { 369b25bb9feSMichael Grosse $this->assignPageSchema($row['pid'], $table); 370b25bb9feSMichael Grosse } 371b25bb9feSMichael Grosse } 372b25bb9feSMichael Grosse } 373fb31ca9fSAndreas Gohr} 374