xref: /plugin/struct/meta/Assignments.php (revision 0173e75d152b967dfccff1f374df50fcf7c15dd4)
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
1733d7be6aSAndreas 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
2833d7be6aSAndreas Gohr        if($this->sqlite) $this->loadPatterns();
29fb31ca9fSAndreas Gohr    }
30fb31ca9fSAndreas Gohr
31fb31ca9fSAndreas Gohr    /**
3249d38573SAndreas Gohr     * Load existing assignment patterns
33fb31ca9fSAndreas Gohr     */
3433d7be6aSAndreas 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     */
4833d7be6aSAndreas Gohr    public function addPattern($pattern, $table) {
49ed60c3b3SAndreas Gohr        // add the pattern
5049d38573SAndreas Gohr        $sql = 'REPLACE INTO schema_assignments_patterns (pattern, tbl) VALUES (?,?)';
51ed60c3b3SAndreas Gohr        $ok = (bool) $this->sqlite->query($sql, array($pattern, $table));
52ed60c3b3SAndreas Gohr
53ed60c3b3SAndreas Gohr        // reload patterns
54ed60c3b3SAndreas Gohr        $this->loadPatterns();
55ed60c3b3SAndreas Gohr
56ed60c3b3SAndreas Gohr        // fetch all pages where the schema isn't assigned, yet
57ed60c3b3SAndreas Gohr        $sql = 'SELECT pid FROM schema_assignments WHERE tbl != ? OR assigned != 1';
58ed60c3b3SAndreas Gohr        $res = $this->sqlite->query($sql, $table);
590e9e058fSAndreas Gohr        $pagerows = $this->sqlite->res2arr($res);
60ed60c3b3SAndreas Gohr        $this->sqlite->res_close($res);
61ed60c3b3SAndreas Gohr
62ed60c3b3SAndreas Gohr        // reevalute the pages and assign when needed
630e9e058fSAndreas Gohr        foreach($pagerows as $row) {
640e9e058fSAndreas Gohr            $tables = $this->getPageAssignments($row['pid'], true);
65ed60c3b3SAndreas Gohr            if(in_array($table, $tables)) {
660e9e058fSAndreas Gohr                $this->assignPageSchema($row['pid'], $table);
67ed60c3b3SAndreas Gohr            }
68ed60c3b3SAndreas Gohr        }
69ed60c3b3SAndreas Gohr
70ed60c3b3SAndreas Gohr        return $ok;
711a8d1235SAndreas Gohr    }
721a8d1235SAndreas Gohr
731a8d1235SAndreas Gohr    /**
7449d38573SAndreas Gohr     * Remove an existing assignment pattern from the pattern table
751a8d1235SAndreas Gohr     *
7649d38573SAndreas Gohr     * @param string $pattern
771a8d1235SAndreas Gohr     * @param string $table
781a8d1235SAndreas Gohr     * @return bool
791a8d1235SAndreas Gohr     */
8033d7be6aSAndreas Gohr    public function removePattern($pattern, $table) {
81ed60c3b3SAndreas Gohr        // remove the pattern
8249d38573SAndreas Gohr        $sql = 'DELETE FROM schema_assignments_patterns WHERE pattern = ? AND tbl = ?';
83ed60c3b3SAndreas Gohr        $ok = (bool) $this->sqlite->query($sql, array($pattern, $table));
84ed60c3b3SAndreas Gohr
85ed60c3b3SAndreas Gohr        // reload patterns
86ed60c3b3SAndreas Gohr        $this->loadPatterns();
87ed60c3b3SAndreas Gohr
88ed60c3b3SAndreas Gohr        // fetch possibly affected pages
89ed60c3b3SAndreas Gohr        $sql = 'SELECT pid FROM schema_assignments WHERE tbl = ?';
90ed60c3b3SAndreas Gohr        $res = $this->sqlite->query($sql, $table);
910e9e058fSAndreas Gohr        $pagerows = $this->sqlite->res2arr($res);
92ed60c3b3SAndreas Gohr        $this->sqlite->res_close($res);
93ed60c3b3SAndreas Gohr
94ed60c3b3SAndreas Gohr        // reevalute the pages and unassign when needed
950e9e058fSAndreas Gohr        foreach($pagerows as $row) {
96be94e9d9SAndreas Gohr            $tables = $this->getPageAssignments($row['pid'], true);
97ed60c3b3SAndreas Gohr            if(!in_array($table, $tables)) {
98be94e9d9SAndreas Gohr                $this->deassignPageSchema($row['pid'], $table);
99ed60c3b3SAndreas Gohr            }
100ed60c3b3SAndreas Gohr        }
101ed60c3b3SAndreas Gohr
102ed60c3b3SAndreas Gohr        return $ok;
103ed60c3b3SAndreas Gohr    }
104ed60c3b3SAndreas Gohr
105ed60c3b3SAndreas Gohr    /**
106*0173e75dSAndreas Gohr     * Rechecks all assignments of a given page against the current patterns
107*0173e75dSAndreas Gohr     *
108*0173e75dSAndreas Gohr     * @param string $pid
109*0173e75dSAndreas Gohr     */
110*0173e75dSAndreas Gohr    public function reevaluatePageAssignments($pid) {
111*0173e75dSAndreas Gohr        // reload patterns
112*0173e75dSAndreas Gohr        $this->loadPatterns();
113*0173e75dSAndreas Gohr        $tables = $this->getPageAssignments($pid, true);
114*0173e75dSAndreas Gohr
115*0173e75dSAndreas Gohr        // fetch possibly affected tables
116*0173e75dSAndreas Gohr        $sql = 'SELECT tbl FROM schema_assignments WHERE pid = ?';
117*0173e75dSAndreas Gohr        $res = $this->sqlite->query($sql, $pid);
118*0173e75dSAndreas Gohr        $tablerows = $this->sqlite->res2arr($res);
119*0173e75dSAndreas Gohr        $this->sqlite->res_close($res);
120*0173e75dSAndreas Gohr
121*0173e75dSAndreas Gohr        // reevalute the tables and apply assignments
122*0173e75dSAndreas Gohr        foreach($tablerows as $row) {
123*0173e75dSAndreas Gohr            if(in_array($row['tbl'], $tables)) {
124*0173e75dSAndreas Gohr                $this->assignPageSchema($pid, $row['tbl']);
125*0173e75dSAndreas Gohr            } else {
126*0173e75dSAndreas Gohr                $this->deassignPageSchema($pid, $row['tbl']);
127*0173e75dSAndreas Gohr            }
128*0173e75dSAndreas Gohr        }
129*0173e75dSAndreas Gohr    }
130*0173e75dSAndreas Gohr
131*0173e75dSAndreas Gohr    /**
1320e9e058fSAndreas Gohr     * Clear all patterns - deassigns all pages
1330e9e058fSAndreas Gohr     *
1340e9e058fSAndreas Gohr     * This is mostly useful for testing and not used in the interface currently
1350e9e058fSAndreas Gohr     *
136153400c7SAndreas Gohr     * @param bool $full fully delete all previous assignments
1370e9e058fSAndreas Gohr     * @return bool
1380e9e058fSAndreas Gohr     */
139153400c7SAndreas Gohr    public function clear($full=false) {
1400e9e058fSAndreas Gohr        $sql = 'DELETE FROM schema_assignments_patterns';
1410e9e058fSAndreas Gohr        $ok = (bool) $this->sqlite->query($sql);
1420e9e058fSAndreas Gohr
143153400c7SAndreas Gohr        if($full) {
144153400c7SAndreas Gohr            $sql = 'DELETE FROM schema_assignments';
145153400c7SAndreas Gohr        } else {
1460e9e058fSAndreas Gohr            $sql = 'UPDATE schema_assignments SET assigned = 0';
147153400c7SAndreas Gohr        }
1480e9e058fSAndreas Gohr        $ok = $ok && (bool) $this->sqlite->query($sql);
1490e9e058fSAndreas Gohr
1500e9e058fSAndreas Gohr        // reload patterns
1510e9e058fSAndreas Gohr        $this->loadPatterns();
1520e9e058fSAndreas Gohr
1530e9e058fSAndreas Gohr        return $ok;
1540e9e058fSAndreas Gohr    }
1550e9e058fSAndreas Gohr
1560e9e058fSAndreas Gohr    /**
157ed60c3b3SAndreas Gohr     * Add page to assignments
158ed60c3b3SAndreas Gohr     *
159ed60c3b3SAndreas Gohr     * @param string $page
160ed60c3b3SAndreas Gohr     * @param string $table
161ed60c3b3SAndreas Gohr     * @return bool
162ed60c3b3SAndreas Gohr     */
163ed713594SAndreas Gohr    public function assignPageSchema($page, $table) {
164ed60c3b3SAndreas Gohr        $sql = 'REPLACE INTO schema_assignments (pid, tbl, assigned) VALUES (?, ?, 1)';
165ed60c3b3SAndreas Gohr        return (bool) $this->sqlite->query($sql, array($page, $table));
166ed60c3b3SAndreas Gohr    }
167ed60c3b3SAndreas Gohr
168ed60c3b3SAndreas Gohr    /**
169ed60c3b3SAndreas Gohr     * Remove page from assignments
170ed60c3b3SAndreas Gohr     *
171ed60c3b3SAndreas Gohr     * @param string $page
172ed60c3b3SAndreas Gohr     * @param string $table
173ed60c3b3SAndreas Gohr     * @return bool
174ed60c3b3SAndreas Gohr     */
175ed713594SAndreas Gohr    public function deassignPageSchema($page, $table) {
176ed60c3b3SAndreas Gohr        $sql = 'REPLACE INTO schema_assignments (pid, tbl, assigned) VALUES (?, ?, 0)';
177ed60c3b3SAndreas Gohr        return (bool) $this->sqlite->query($sql, array($page, $table));
1781a8d1235SAndreas Gohr    }
1791a8d1235SAndreas Gohr
1801a8d1235SAndreas Gohr    /**
18149d38573SAndreas Gohr     * Get the whole pattern table
1821a8d1235SAndreas Gohr     *
1831a8d1235SAndreas Gohr     * @return array
1841a8d1235SAndreas Gohr     */
18533d7be6aSAndreas Gohr    public function getAllPatterns() {
18649d38573SAndreas Gohr        return $this->patterns;
1871a8d1235SAndreas Gohr    }
1881a8d1235SAndreas Gohr
1891a8d1235SAndreas Gohr    /**
190fb31ca9fSAndreas Gohr     * Returns a list of table names assigned to the given page
191fb31ca9fSAndreas Gohr     *
192fb31ca9fSAndreas Gohr     * @param string $page
1939ff81b7fSAndreas Gohr     * @param bool $checkpatterns Should the current patterns be re-evaluated?
1949ff81b7fSAndreas Gohr     * @return \string[] tables assigned
195fb31ca9fSAndreas Gohr     */
1969ff81b7fSAndreas Gohr    public function getPageAssignments($page, $checkpatterns=true) {
197fb31ca9fSAndreas Gohr        $tables = array();
198fb31ca9fSAndreas Gohr        $page = cleanID($page);
199fb31ca9fSAndreas Gohr
2009ff81b7fSAndreas Gohr        if($checkpatterns) {
2019ff81b7fSAndreas Gohr            // evaluate patterns
2029ff81b7fSAndreas Gohr            $pns = ':' . getNS($page) . ':';
20349d38573SAndreas Gohr            foreach($this->patterns as $row) {
204ed60c3b3SAndreas Gohr                if($this->matchPagePattern($row['pattern'], $page, $pns)) {
205ed60c3b3SAndreas Gohr                    $tables[] = $row['tbl'];
206fb31ca9fSAndreas Gohr                }
207fb31ca9fSAndreas Gohr            }
2089ff81b7fSAndreas Gohr        } else {
2099ff81b7fSAndreas Gohr            // just select
2109ff81b7fSAndreas Gohr            $sql = 'SELECT tbl FROM schema_assignments WHERE pid = ? AND assigned = 1';
2119ff81b7fSAndreas Gohr            $res = $this->sqlite->query($sql, array($page));
2129ff81b7fSAndreas Gohr            $list = $this->sqlite->res2arr($res);
2139ff81b7fSAndreas Gohr            $this->sqlite->res_close($res);
2149ff81b7fSAndreas Gohr            foreach($list as $row) {
2159ff81b7fSAndreas Gohr                $tables[] = $row['tbl'];
2169ff81b7fSAndreas Gohr            }
2179ff81b7fSAndreas Gohr        }
218fb31ca9fSAndreas Gohr
219fb31ca9fSAndreas Gohr        return array_unique($tables);
220fb31ca9fSAndreas Gohr    }
22156672c36SAndreas Gohr
22256672c36SAndreas Gohr    /**
223153400c7SAndreas Gohr     * Get the pages known to struct and their assignment state
224153400c7SAndreas Gohr     *
225153400c7SAndreas Gohr     * @param null|string $schema limit results to the given schema
226153400c7SAndreas Gohr     * @param bool $assignedonly limit results to currently assigned only
227153400c7SAndreas Gohr     * @return array
228153400c7SAndreas Gohr     */
229153400c7SAndreas Gohr    public function getPages($schema = null, $assignedonly = false) {
230153400c7SAndreas Gohr        $sql = 'SELECT pid, tbl, assigned FROM schema_assignments WHERE 1=1';
231153400c7SAndreas Gohr
232153400c7SAndreas Gohr        $opts = array();
233153400c7SAndreas Gohr        if($schema) {
234153400c7SAndreas Gohr            $sql .= ' AND tbl = ?';
235153400c7SAndreas Gohr            $opts[] = $schema;
236153400c7SAndreas Gohr        }
237153400c7SAndreas Gohr        if($assignedonly) {
238153400c7SAndreas Gohr            $sql .= ' AND assigned = 1';
239153400c7SAndreas Gohr        }
240153400c7SAndreas Gohr
241153400c7SAndreas Gohr        $sql .= ' ORDER BY pid, tbl';
242153400c7SAndreas Gohr
243153400c7SAndreas Gohr        $res = $this->sqlite->query($sql, $opts);
244153400c7SAndreas Gohr        $list = $this->sqlite->res2arr($res);
245153400c7SAndreas Gohr        $this->sqlite->res_close($res);
246153400c7SAndreas Gohr
247153400c7SAndreas Gohr        $result = array();
248153400c7SAndreas Gohr        foreach($list as $row) {
249153400c7SAndreas Gohr            $pid = $row['pid'];
250153400c7SAndreas Gohr            $tbl = $row['tbl'];
251153400c7SAndreas Gohr            if(!isset($result[$pid])) $result[$pid] = array();
252153400c7SAndreas Gohr            $result[$pid][$tbl] = (bool) $row['assigned'];
253153400c7SAndreas Gohr        }
254153400c7SAndreas Gohr
255153400c7SAndreas Gohr        return $result;
256153400c7SAndreas Gohr    }
257153400c7SAndreas Gohr
258153400c7SAndreas Gohr    /**
259ed60c3b3SAndreas Gohr     * Check if the given pattern matches the given page
260ed60c3b3SAndreas Gohr     *
261ed60c3b3SAndreas Gohr     * @param string $pattern the pattern to check against
262ed60c3b3SAndreas Gohr     * @param string $page the cleaned pageid to check
263ed60c3b3SAndreas Gohr     * @param string|null $pns optimization, the colon wrapped namespace of the page, set null for automatic
264ed60c3b3SAndreas Gohr     * @return bool
265ed60c3b3SAndreas Gohr     */
266ed60c3b3SAndreas Gohr    protected function matchPagePattern($pattern, $page, $pns = null) {
2670e9e058fSAndreas Gohr        if(trim($pattern,':') == '**') return true; // match all
2680e9e058fSAndreas Gohr
2699914e87eSAndreas Gohr        // regex patterns
2709914e87eSAndreas Gohr        if($pattern{0} == '/') {
2719914e87eSAndreas Gohr            return (bool) preg_match($pattern, ":$page");
2729914e87eSAndreas Gohr        }
2739914e87eSAndreas Gohr
274ed60c3b3SAndreas Gohr        if(is_null($pns)) {
275ed60c3b3SAndreas Gohr            $pns = ':' . getNS($page) . ':';
276ed60c3b3SAndreas Gohr        }
277ed60c3b3SAndreas Gohr
278ed60c3b3SAndreas Gohr        $ans = ':' . cleanID($pattern) . ':';
279ed60c3b3SAndreas Gohr        if(substr($pattern, -2) == '**') {
280ed60c3b3SAndreas Gohr            // upper namespaces match
281ed60c3b3SAndreas Gohr            if(strpos($pns, $ans) === 0) {
282ed60c3b3SAndreas Gohr                return true;
283ed60c3b3SAndreas Gohr            }
284ed60c3b3SAndreas Gohr        } else if(substr($pattern, -1) == '*') {
285ed60c3b3SAndreas Gohr            // namespaces match exact
286ed60c3b3SAndreas Gohr            if($ans == $pns) {
287ed60c3b3SAndreas Gohr                return true;
288ed60c3b3SAndreas Gohr            }
289ed60c3b3SAndreas Gohr        } else {
290ed60c3b3SAndreas Gohr            // exact match
291ed60c3b3SAndreas Gohr            if(cleanID($pattern) == $page) {
292ed60c3b3SAndreas Gohr                return true;
293ed60c3b3SAndreas Gohr            }
294ed60c3b3SAndreas Gohr        }
295ed60c3b3SAndreas Gohr
296ed60c3b3SAndreas Gohr        return false;
297ed60c3b3SAndreas Gohr    }
298ed60c3b3SAndreas Gohr
299ed60c3b3SAndreas Gohr    /**
30056672c36SAndreas Gohr     * Returns all tables of schemas that existed and stored data for the page back then
30156672c36SAndreas Gohr     *
3020e9e058fSAndreas Gohr     * @deprecated because we're always only interested in the current state of affairs, even when restoring.
30356672c36SAndreas Gohr     *
30456672c36SAndreas Gohr     * @param string $page
30556672c36SAndreas Gohr     * @param string $ts
30656672c36SAndreas Gohr     * @return array
30756672c36SAndreas Gohr     */
30856672c36SAndreas Gohr    public function getHistoricAssignments($page, $ts) {
30956672c36SAndreas Gohr        $sql = "SELECT DISTINCT tbl FROM schemas WHERE ts <= ? ORDER BY ts DESC";
31056672c36SAndreas Gohr        $res = $this->sqlite->query($sql, $ts);
31156672c36SAndreas Gohr        $tables = $this->sqlite->res2arr($res);
31256672c36SAndreas Gohr        $this->sqlite->res_close($res);
31356672c36SAndreas Gohr
31456672c36SAndreas Gohr        $assigned = array();
31556672c36SAndreas Gohr        foreach($tables as $row) {
31656672c36SAndreas Gohr            $table = $row['tbl'];
317ed60c3b3SAndreas Gohr            /** @noinspection SqlResolve */
31856672c36SAndreas Gohr            $sql = "SELECT pid FROM data_$table WHERE pid = ? AND rev <= ? LIMIT 1";
31956672c36SAndreas Gohr            $res = $this->sqlite->query($sql, $page, $ts);
32056672c36SAndreas Gohr            $found = $this->sqlite->res2arr($res);
32156672c36SAndreas Gohr            $this->sqlite->res_close($res);
32256672c36SAndreas Gohr
32356672c36SAndreas Gohr            if($found) $assigned[] = $table;
32456672c36SAndreas Gohr        }
32556672c36SAndreas Gohr
32656672c36SAndreas Gohr        return $assigned;
32756672c36SAndreas Gohr    }
328fb31ca9fSAndreas Gohr}
329