xref: /plugin/struct/meta/Assignments.php (revision 9914e87efa9c3f3c75f0e56619423f12ea7ca649)
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    /**
1060e9e058fSAndreas Gohr     * Clear all patterns - deassigns all pages
1070e9e058fSAndreas Gohr     *
1080e9e058fSAndreas Gohr     * This is mostly useful for testing and not used in the interface currently
1090e9e058fSAndreas Gohr     *
110153400c7SAndreas Gohr     * @param bool $full fully delete all previous assignments
1110e9e058fSAndreas Gohr     * @return bool
1120e9e058fSAndreas Gohr     */
113153400c7SAndreas Gohr    public function clear($full=false) {
1140e9e058fSAndreas Gohr        $sql = 'DELETE FROM schema_assignments_patterns';
1150e9e058fSAndreas Gohr        $ok = (bool) $this->sqlite->query($sql);
1160e9e058fSAndreas Gohr
117153400c7SAndreas Gohr        if($full) {
118153400c7SAndreas Gohr            $sql = 'DELETE FROM schema_assignments';
119153400c7SAndreas Gohr        } else {
1200e9e058fSAndreas Gohr            $sql = 'UPDATE schema_assignments SET assigned = 0';
121153400c7SAndreas Gohr        }
1220e9e058fSAndreas Gohr        $ok = $ok && (bool) $this->sqlite->query($sql);
1230e9e058fSAndreas Gohr
1240e9e058fSAndreas Gohr        // reload patterns
1250e9e058fSAndreas Gohr        $this->loadPatterns();
1260e9e058fSAndreas Gohr
1270e9e058fSAndreas Gohr        return $ok;
1280e9e058fSAndreas Gohr    }
1290e9e058fSAndreas Gohr
1300e9e058fSAndreas Gohr    /**
131ed60c3b3SAndreas Gohr     * Add page to assignments
132ed60c3b3SAndreas Gohr     *
133ed60c3b3SAndreas Gohr     * @param string $page
134ed60c3b3SAndreas Gohr     * @param string $table
135ed60c3b3SAndreas Gohr     * @return bool
136ed60c3b3SAndreas Gohr     */
137ed713594SAndreas Gohr    public function assignPageSchema($page, $table) {
138ed60c3b3SAndreas Gohr        $sql = 'REPLACE INTO schema_assignments (pid, tbl, assigned) VALUES (?, ?, 1)';
139ed60c3b3SAndreas Gohr        return (bool) $this->sqlite->query($sql, array($page, $table));
140ed60c3b3SAndreas Gohr    }
141ed60c3b3SAndreas Gohr
142ed60c3b3SAndreas Gohr    /**
143ed60c3b3SAndreas Gohr     * Remove page from assignments
144ed60c3b3SAndreas Gohr     *
145ed60c3b3SAndreas Gohr     * @param string $page
146ed60c3b3SAndreas Gohr     * @param string $table
147ed60c3b3SAndreas Gohr     * @return bool
148ed60c3b3SAndreas Gohr     */
149ed713594SAndreas Gohr    public function deassignPageSchema($page, $table) {
150ed60c3b3SAndreas Gohr        $sql = 'REPLACE INTO schema_assignments (pid, tbl, assigned) VALUES (?, ?, 0)';
151ed60c3b3SAndreas Gohr        return (bool) $this->sqlite->query($sql, array($page, $table));
1521a8d1235SAndreas Gohr    }
1531a8d1235SAndreas Gohr
1541a8d1235SAndreas Gohr    /**
15549d38573SAndreas Gohr     * Get the whole pattern table
1561a8d1235SAndreas Gohr     *
1571a8d1235SAndreas Gohr     * @return array
1581a8d1235SAndreas Gohr     */
15933d7be6aSAndreas Gohr    public function getAllPatterns() {
16049d38573SAndreas Gohr        return $this->patterns;
1611a8d1235SAndreas Gohr    }
1621a8d1235SAndreas Gohr
1631a8d1235SAndreas Gohr    /**
164fb31ca9fSAndreas Gohr     * Returns a list of table names assigned to the given page
165fb31ca9fSAndreas Gohr     *
166fb31ca9fSAndreas Gohr     * @param string $page
1679ff81b7fSAndreas Gohr     * @param bool $checkpatterns Should the current patterns be re-evaluated?
1689ff81b7fSAndreas Gohr     * @return \string[] tables assigned
169fb31ca9fSAndreas Gohr     */
1709ff81b7fSAndreas Gohr    public function getPageAssignments($page, $checkpatterns=true) {
171fb31ca9fSAndreas Gohr        $tables = array();
172fb31ca9fSAndreas Gohr        $page = cleanID($page);
173fb31ca9fSAndreas Gohr
1749ff81b7fSAndreas Gohr        if($checkpatterns) {
1759ff81b7fSAndreas Gohr            // evaluate patterns
1769ff81b7fSAndreas Gohr            $pns = ':' . getNS($page) . ':';
17749d38573SAndreas Gohr            foreach($this->patterns as $row) {
178ed60c3b3SAndreas Gohr                if($this->matchPagePattern($row['pattern'], $page, $pns)) {
179ed60c3b3SAndreas Gohr                    $tables[] = $row['tbl'];
180fb31ca9fSAndreas Gohr                }
181fb31ca9fSAndreas Gohr            }
1829ff81b7fSAndreas Gohr        } else {
1839ff81b7fSAndreas Gohr            // just select
1849ff81b7fSAndreas Gohr            $sql = 'SELECT tbl FROM schema_assignments WHERE pid = ? AND assigned = 1';
1859ff81b7fSAndreas Gohr            $res = $this->sqlite->query($sql, array($page));
1869ff81b7fSAndreas Gohr            $list = $this->sqlite->res2arr($res);
1879ff81b7fSAndreas Gohr            $this->sqlite->res_close($res);
1889ff81b7fSAndreas Gohr            foreach($list as $row) {
1899ff81b7fSAndreas Gohr                $tables[] = $row['tbl'];
1909ff81b7fSAndreas Gohr            }
1919ff81b7fSAndreas Gohr        }
192fb31ca9fSAndreas Gohr
193fb31ca9fSAndreas Gohr        return array_unique($tables);
194fb31ca9fSAndreas Gohr    }
19556672c36SAndreas Gohr
19656672c36SAndreas Gohr    /**
197153400c7SAndreas Gohr     * Get the pages known to struct and their assignment state
198153400c7SAndreas Gohr     *
199153400c7SAndreas Gohr     * @param null|string $schema limit results to the given schema
200153400c7SAndreas Gohr     * @param bool $assignedonly limit results to currently assigned only
201153400c7SAndreas Gohr     * @return array
202153400c7SAndreas Gohr     */
203153400c7SAndreas Gohr    public function getPages($schema = null, $assignedonly = false) {
204153400c7SAndreas Gohr        $sql = 'SELECT pid, tbl, assigned FROM schema_assignments WHERE 1=1';
205153400c7SAndreas Gohr
206153400c7SAndreas Gohr        $opts = array();
207153400c7SAndreas Gohr        if($schema) {
208153400c7SAndreas Gohr            $sql .= ' AND tbl = ?';
209153400c7SAndreas Gohr            $opts[] = $schema;
210153400c7SAndreas Gohr        }
211153400c7SAndreas Gohr        if($assignedonly) {
212153400c7SAndreas Gohr            $sql .= ' AND assigned = 1';
213153400c7SAndreas Gohr        }
214153400c7SAndreas Gohr
215153400c7SAndreas Gohr        $sql .= ' ORDER BY pid, tbl';
216153400c7SAndreas Gohr
217153400c7SAndreas Gohr        $res = $this->sqlite->query($sql, $opts);
218153400c7SAndreas Gohr        $list = $this->sqlite->res2arr($res);
219153400c7SAndreas Gohr        $this->sqlite->res_close($res);
220153400c7SAndreas Gohr
221153400c7SAndreas Gohr        $result = array();
222153400c7SAndreas Gohr        foreach($list as $row) {
223153400c7SAndreas Gohr            $pid = $row['pid'];
224153400c7SAndreas Gohr            $tbl = $row['tbl'];
225153400c7SAndreas Gohr            if(!isset($result[$pid])) $result[$pid] = array();
226153400c7SAndreas Gohr            $result[$pid][$tbl] = (bool) $row['assigned'];
227153400c7SAndreas Gohr        }
228153400c7SAndreas Gohr
229153400c7SAndreas Gohr        return $result;
230153400c7SAndreas Gohr    }
231153400c7SAndreas Gohr
232153400c7SAndreas Gohr    /**
233ed60c3b3SAndreas Gohr     * Check if the given pattern matches the given page
234ed60c3b3SAndreas Gohr     *
235ed60c3b3SAndreas Gohr     * @param string $pattern the pattern to check against
236ed60c3b3SAndreas Gohr     * @param string $page the cleaned pageid to check
237ed60c3b3SAndreas Gohr     * @param string|null $pns optimization, the colon wrapped namespace of the page, set null for automatic
238ed60c3b3SAndreas Gohr     * @return bool
239ed60c3b3SAndreas Gohr     */
240ed60c3b3SAndreas Gohr    protected function matchPagePattern($pattern, $page, $pns = null) {
2410e9e058fSAndreas Gohr        if(trim($pattern,':') == '**') return true; // match all
2420e9e058fSAndreas Gohr
243*9914e87eSAndreas Gohr        // regex patterns
244*9914e87eSAndreas Gohr        if($pattern{0} == '/') {
245*9914e87eSAndreas Gohr            return (bool) preg_match($pattern, ":$page");
246*9914e87eSAndreas Gohr        }
247*9914e87eSAndreas Gohr
248ed60c3b3SAndreas Gohr        if(is_null($pns)) {
249ed60c3b3SAndreas Gohr            $pns = ':' . getNS($page) . ':';
250ed60c3b3SAndreas Gohr        }
251ed60c3b3SAndreas Gohr
252ed60c3b3SAndreas Gohr        $ans = ':' . cleanID($pattern) . ':';
253ed60c3b3SAndreas Gohr        if(substr($pattern, -2) == '**') {
254ed60c3b3SAndreas Gohr            // upper namespaces match
255ed60c3b3SAndreas Gohr            if(strpos($pns, $ans) === 0) {
256ed60c3b3SAndreas Gohr                return true;
257ed60c3b3SAndreas Gohr            }
258ed60c3b3SAndreas Gohr        } else if(substr($pattern, -1) == '*') {
259ed60c3b3SAndreas Gohr            // namespaces match exact
260ed60c3b3SAndreas Gohr            if($ans == $pns) {
261ed60c3b3SAndreas Gohr                return true;
262ed60c3b3SAndreas Gohr            }
263ed60c3b3SAndreas Gohr        } else {
264ed60c3b3SAndreas Gohr            // exact match
265ed60c3b3SAndreas Gohr            if(cleanID($pattern) == $page) {
266ed60c3b3SAndreas Gohr                return true;
267ed60c3b3SAndreas Gohr            }
268ed60c3b3SAndreas Gohr        }
269ed60c3b3SAndreas Gohr
270ed60c3b3SAndreas Gohr        return false;
271ed60c3b3SAndreas Gohr    }
272ed60c3b3SAndreas Gohr
273ed60c3b3SAndreas Gohr    /**
27456672c36SAndreas Gohr     * Returns all tables of schemas that existed and stored data for the page back then
27556672c36SAndreas Gohr     *
2760e9e058fSAndreas Gohr     * @deprecated because we're always only interested in the current state of affairs, even when restoring.
27756672c36SAndreas Gohr     *
27856672c36SAndreas Gohr     * @param string $page
27956672c36SAndreas Gohr     * @param string $ts
28056672c36SAndreas Gohr     * @return array
28156672c36SAndreas Gohr     */
28256672c36SAndreas Gohr    public function getHistoricAssignments($page, $ts) {
28356672c36SAndreas Gohr        $sql = "SELECT DISTINCT tbl FROM schemas WHERE ts <= ? ORDER BY ts DESC";
28456672c36SAndreas Gohr        $res = $this->sqlite->query($sql, $ts);
28556672c36SAndreas Gohr        $tables = $this->sqlite->res2arr($res);
28656672c36SAndreas Gohr        $this->sqlite->res_close($res);
28756672c36SAndreas Gohr
28856672c36SAndreas Gohr        $assigned = array();
28956672c36SAndreas Gohr        foreach($tables as $row) {
29056672c36SAndreas Gohr            $table = $row['tbl'];
291ed60c3b3SAndreas Gohr            /** @noinspection SqlResolve */
29256672c36SAndreas Gohr            $sql = "SELECT pid FROM data_$table WHERE pid = ? AND rev <= ? LIMIT 1";
29356672c36SAndreas Gohr            $res = $this->sqlite->query($sql, $page, $ts);
29456672c36SAndreas Gohr            $found = $this->sqlite->res2arr($res);
29556672c36SAndreas Gohr            $this->sqlite->res_close($res);
29656672c36SAndreas Gohr
29756672c36SAndreas Gohr            if($found) $assigned[] = $table;
29856672c36SAndreas Gohr        }
29956672c36SAndreas Gohr
30056672c36SAndreas Gohr        return $assigned;
30156672c36SAndreas Gohr    }
302fb31ca9fSAndreas Gohr}
303