xref: /plugin/struct/meta/Assignments.php (revision 8da1363a879ba9a8b634e6f2405e59f9bbe42736)
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 */
14fb31ca9fSAndreas Gohrclass Assignments {
15fb31ca9fSAndreas Gohr
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
22fc26989eSAndreas Gohr    /** @var  string[] All lookup schemas for error checking */
23fc26989eSAndreas Gohr    protected $lookups;
24fc26989eSAndreas Gohr
25025cb9daSAndreas Gohr    /** @var Assignments */
26025cb9daSAndreas Gohr    protected static $instance = null;
27025cb9daSAndreas Gohr
28025cb9daSAndreas Gohr    /**
29025cb9daSAndreas Gohr     * Get the singleton instance of the Assignments
30025cb9daSAndreas Gohr     *
31025cb9daSAndreas Gohr     * @param bool $forcereload create a new instace to reload the assignment data
32025cb9daSAndreas Gohr     * @return Assignments
33025cb9daSAndreas Gohr     */
34025cb9daSAndreas Gohr    public static function getInstance($forcereload = false) {
35025cb9daSAndreas Gohr        if(is_null(self::$instance) or $forcereload) {
36025cb9daSAndreas Gohr            $class = get_called_class();
37025cb9daSAndreas Gohr            self::$instance = new $class();
38025cb9daSAndreas Gohr        }
39025cb9daSAndreas Gohr        return self::$instance;
40025cb9daSAndreas Gohr    }
41025cb9daSAndreas Gohr
42fb31ca9fSAndreas Gohr    /**
43fb31ca9fSAndreas Gohr     * Assignments constructor.
44025cb9daSAndreas Gohr     *
45025cb9daSAndreas Gohr     * Not public. Use Assignments::getInstance() instead
46fb31ca9fSAndreas Gohr     */
47025cb9daSAndreas Gohr    protected function __construct() {
48fb31ca9fSAndreas Gohr        /** @var \helper_plugin_struct_db $helper */
49fb31ca9fSAndreas Gohr        $helper = plugin_load('helper', 'struct_db');
50fb31ca9fSAndreas Gohr        $this->sqlite = $helper->getDB();
51fb31ca9fSAndreas Gohr
52fc26989eSAndreas Gohr        $this->loadPatterns();
53fc26989eSAndreas Gohr        $this->lookups = Schema::getAll('lookup');
54fb31ca9fSAndreas Gohr    }
55fb31ca9fSAndreas Gohr
56025cb9daSAndreas Gohr
57025cb9daSAndreas Gohr
58fb31ca9fSAndreas Gohr    /**
5949d38573SAndreas Gohr     * Load existing assignment patterns
60fb31ca9fSAndreas Gohr     */
6133d7be6aSAndreas Gohr    protected function loadPatterns() {
6249d38573SAndreas Gohr        $sql = 'SELECT * FROM schema_assignments_patterns ORDER BY pattern';
63fb31ca9fSAndreas Gohr        $res = $this->sqlite->query($sql);
6449d38573SAndreas Gohr        $this->patterns = $this->sqlite->res2arr($res);
65fb31ca9fSAndreas Gohr        $this->sqlite->res_close($res);
66fb31ca9fSAndreas Gohr    }
67fb31ca9fSAndreas Gohr
68fb31ca9fSAndreas Gohr    /**
6949d38573SAndreas Gohr     * Add a new assignment pattern to the pattern table
701a8d1235SAndreas Gohr     *
7149d38573SAndreas Gohr     * @param string $pattern
721a8d1235SAndreas Gohr     * @param string $table
731a8d1235SAndreas Gohr     * @return bool
741a8d1235SAndreas Gohr     */
7533d7be6aSAndreas Gohr    public function addPattern($pattern, $table) {
76fc26989eSAndreas Gohr        if(in_array($table, $this->lookups)) {
77fc26989eSAndreas Gohr            throw new StructException('nolookupassign');
78fc26989eSAndreas Gohr        }
79fc26989eSAndreas Gohr
80ed60c3b3SAndreas Gohr        // add the pattern
8149d38573SAndreas Gohr        $sql = 'REPLACE INTO schema_assignments_patterns (pattern, tbl) VALUES (?,?)';
82ed60c3b3SAndreas Gohr        $ok = (bool) $this->sqlite->query($sql, array($pattern, $table));
83ed60c3b3SAndreas Gohr
84ed60c3b3SAndreas Gohr        // reload patterns
85ed60c3b3SAndreas Gohr        $this->loadPatterns();
86b25bb9feSMichael Grosse        $this->propagatePageAssignments($table);
87ed60c3b3SAndreas Gohr
88ed60c3b3SAndreas Gohr
89ed60c3b3SAndreas Gohr        return $ok;
901a8d1235SAndreas Gohr    }
911a8d1235SAndreas Gohr
921a8d1235SAndreas Gohr    /**
9349d38573SAndreas Gohr     * Remove an existing assignment pattern from the pattern table
941a8d1235SAndreas Gohr     *
9549d38573SAndreas Gohr     * @param string $pattern
961a8d1235SAndreas Gohr     * @param string $table
971a8d1235SAndreas Gohr     * @return bool
981a8d1235SAndreas Gohr     */
9933d7be6aSAndreas Gohr    public function removePattern($pattern, $table) {
100ed60c3b3SAndreas Gohr        // remove the pattern
10149d38573SAndreas Gohr        $sql = 'DELETE FROM schema_assignments_patterns WHERE pattern = ? AND tbl = ?';
102ed60c3b3SAndreas Gohr        $ok = (bool) $this->sqlite->query($sql, array($pattern, $table));
103ed60c3b3SAndreas Gohr
104ed60c3b3SAndreas Gohr        // reload patterns
105ed60c3b3SAndreas Gohr        $this->loadPatterns();
106ed60c3b3SAndreas Gohr
107ed60c3b3SAndreas Gohr        // fetch possibly affected pages
108ed60c3b3SAndreas Gohr        $sql = 'SELECT pid FROM schema_assignments WHERE tbl = ?';
109ed60c3b3SAndreas Gohr        $res = $this->sqlite->query($sql, $table);
1100e9e058fSAndreas Gohr        $pagerows = $this->sqlite->res2arr($res);
111ed60c3b3SAndreas Gohr        $this->sqlite->res_close($res);
112ed60c3b3SAndreas Gohr
113ed60c3b3SAndreas Gohr        // reevalute the pages and unassign when needed
1140e9e058fSAndreas Gohr        foreach($pagerows as $row) {
115be94e9d9SAndreas Gohr            $tables = $this->getPageAssignments($row['pid'], true);
116ed60c3b3SAndreas Gohr            if(!in_array($table, $tables)) {
117be94e9d9SAndreas Gohr                $this->deassignPageSchema($row['pid'], $table);
118ed60c3b3SAndreas Gohr            }
119ed60c3b3SAndreas Gohr        }
120ed60c3b3SAndreas Gohr
121ed60c3b3SAndreas Gohr        return $ok;
122ed60c3b3SAndreas Gohr    }
123ed60c3b3SAndreas Gohr
124ed60c3b3SAndreas Gohr    /**
1250173e75dSAndreas Gohr     * Rechecks all assignments of a given page against the current patterns
1260173e75dSAndreas Gohr     *
1270173e75dSAndreas Gohr     * @param string $pid
1280173e75dSAndreas Gohr     */
1290173e75dSAndreas Gohr    public function reevaluatePageAssignments($pid) {
1300173e75dSAndreas Gohr        // reload patterns
1310173e75dSAndreas Gohr        $this->loadPatterns();
1320173e75dSAndreas Gohr        $tables = $this->getPageAssignments($pid, true);
1330173e75dSAndreas Gohr
1340173e75dSAndreas Gohr        // fetch possibly affected tables
1350173e75dSAndreas Gohr        $sql = 'SELECT tbl FROM schema_assignments WHERE pid = ?';
1360173e75dSAndreas Gohr        $res = $this->sqlite->query($sql, $pid);
1370173e75dSAndreas Gohr        $tablerows = $this->sqlite->res2arr($res);
1380173e75dSAndreas Gohr        $this->sqlite->res_close($res);
1390173e75dSAndreas Gohr
1400173e75dSAndreas Gohr        // reevalute the tables and apply assignments
1410173e75dSAndreas Gohr        foreach($tablerows as $row) {
1420173e75dSAndreas Gohr            if(in_array($row['tbl'], $tables)) {
1430173e75dSAndreas Gohr                $this->assignPageSchema($pid, $row['tbl']);
1440173e75dSAndreas Gohr            } else {
1450173e75dSAndreas Gohr                $this->deassignPageSchema($pid, $row['tbl']);
1460173e75dSAndreas Gohr            }
1470173e75dSAndreas Gohr        }
1480173e75dSAndreas Gohr    }
1490173e75dSAndreas Gohr
1500173e75dSAndreas Gohr    /**
1510e9e058fSAndreas Gohr     * Clear all patterns - deassigns all pages
1520e9e058fSAndreas Gohr     *
1530e9e058fSAndreas Gohr     * This is mostly useful for testing and not used in the interface currently
1540e9e058fSAndreas Gohr     *
155153400c7SAndreas Gohr     * @param bool $full fully delete all previous assignments
1560e9e058fSAndreas Gohr     * @return bool
1570e9e058fSAndreas Gohr     */
158153400c7SAndreas Gohr    public function clear($full=false) {
1590e9e058fSAndreas Gohr        $sql = 'DELETE FROM schema_assignments_patterns';
1600e9e058fSAndreas Gohr        $ok = (bool) $this->sqlite->query($sql);
1610e9e058fSAndreas Gohr
162153400c7SAndreas Gohr        if($full) {
163153400c7SAndreas Gohr            $sql = 'DELETE FROM schema_assignments';
164153400c7SAndreas Gohr        } else {
1650e9e058fSAndreas Gohr            $sql = 'UPDATE schema_assignments SET assigned = 0';
166153400c7SAndreas Gohr        }
1670e9e058fSAndreas Gohr        $ok = $ok && (bool) $this->sqlite->query($sql);
1680e9e058fSAndreas Gohr
1690e9e058fSAndreas Gohr        // reload patterns
1700e9e058fSAndreas Gohr        $this->loadPatterns();
1710e9e058fSAndreas Gohr
1720e9e058fSAndreas Gohr        return $ok;
1730e9e058fSAndreas Gohr    }
1740e9e058fSAndreas Gohr
1750e9e058fSAndreas Gohr    /**
176ed60c3b3SAndreas Gohr     * Add page to assignments
177ed60c3b3SAndreas Gohr     *
178ed60c3b3SAndreas Gohr     * @param string $page
179ed60c3b3SAndreas Gohr     * @param string $table
180ed60c3b3SAndreas Gohr     * @return bool
181ed60c3b3SAndreas Gohr     */
182ed713594SAndreas Gohr    public function assignPageSchema($page, $table) {
183ed60c3b3SAndreas Gohr        $sql = 'REPLACE INTO schema_assignments (pid, tbl, assigned) VALUES (?, ?, 1)';
184ed60c3b3SAndreas Gohr        return (bool) $this->sqlite->query($sql, array($page, $table));
185ed60c3b3SAndreas Gohr    }
186ed60c3b3SAndreas Gohr
187ed60c3b3SAndreas Gohr    /**
188ed60c3b3SAndreas Gohr     * Remove page from assignments
189ed60c3b3SAndreas Gohr     *
190ed60c3b3SAndreas Gohr     * @param string $page
191ed60c3b3SAndreas Gohr     * @param string $table
192ed60c3b3SAndreas Gohr     * @return bool
193ed60c3b3SAndreas Gohr     */
194ed713594SAndreas Gohr    public function deassignPageSchema($page, $table) {
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     */
20433d7be6aSAndreas Gohr    public function getAllPatterns() {
20549d38573SAndreas Gohr        return $this->patterns;
2061a8d1235SAndreas Gohr    }
2071a8d1235SAndreas Gohr
2081a8d1235SAndreas Gohr    /**
209fb31ca9fSAndreas Gohr     * Returns a list of table names assigned to the given page
210fb31ca9fSAndreas Gohr     *
211fb31ca9fSAndreas Gohr     * @param string $page
2129ff81b7fSAndreas Gohr     * @param bool $checkpatterns Should the current patterns be re-evaluated?
2139ff81b7fSAndreas Gohr     * @return \string[] tables assigned
214fb31ca9fSAndreas Gohr     */
2159ff81b7fSAndreas Gohr    public function getPageAssignments($page, $checkpatterns=true) {
216fb31ca9fSAndreas Gohr        $tables = array();
217fb31ca9fSAndreas Gohr        $page = cleanID($page);
218fb31ca9fSAndreas Gohr
2199ff81b7fSAndreas Gohr        if($checkpatterns) {
2209ff81b7fSAndreas Gohr            // evaluate patterns
2219ff81b7fSAndreas Gohr            $pns = ':' . getNS($page) . ':';
22249d38573SAndreas Gohr            foreach($this->patterns as $row) {
223ed60c3b3SAndreas Gohr                if($this->matchPagePattern($row['pattern'], $page, $pns)) {
224fc26989eSAndreas Gohr                    if(in_array($row['tbl'], $this->lookups)) continue; // wrong assignment
225ed60c3b3SAndreas Gohr                    $tables[] = $row['tbl'];
226fb31ca9fSAndreas Gohr                }
227fb31ca9fSAndreas Gohr            }
2289ff81b7fSAndreas Gohr        } else {
2299ff81b7fSAndreas Gohr            // just select
2309ff81b7fSAndreas Gohr            $sql = 'SELECT tbl FROM schema_assignments WHERE pid = ? AND assigned = 1';
2319ff81b7fSAndreas Gohr            $res = $this->sqlite->query($sql, array($page));
2329ff81b7fSAndreas Gohr            $list = $this->sqlite->res2arr($res);
2339ff81b7fSAndreas Gohr            $this->sqlite->res_close($res);
2349ff81b7fSAndreas Gohr            foreach($list as $row) {
235fc26989eSAndreas Gohr                if(in_array($row['tbl'], $this->lookups)) continue; // wrong assignment
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     */
250153400c7SAndreas Gohr    public function getPages($schema = null, $assignedonly = false) {
251153400c7SAndreas Gohr        $sql = 'SELECT pid, tbl, assigned FROM schema_assignments WHERE 1=1';
252153400c7SAndreas Gohr
253153400c7SAndreas Gohr        $opts = array();
254153400c7SAndreas Gohr        if($schema) {
255153400c7SAndreas Gohr            $sql .= ' AND tbl = ?';
256153400c7SAndreas Gohr            $opts[] = $schema;
257153400c7SAndreas Gohr        }
258153400c7SAndreas Gohr        if($assignedonly) {
259153400c7SAndreas Gohr            $sql .= ' AND assigned = 1';
260153400c7SAndreas Gohr        }
261153400c7SAndreas Gohr
262153400c7SAndreas Gohr        $sql .= ' ORDER BY pid, tbl';
263153400c7SAndreas Gohr
264153400c7SAndreas Gohr        $res = $this->sqlite->query($sql, $opts);
265153400c7SAndreas Gohr        $list = $this->sqlite->res2arr($res);
266153400c7SAndreas Gohr        $this->sqlite->res_close($res);
267153400c7SAndreas Gohr
268153400c7SAndreas Gohr        $result = array();
269153400c7SAndreas Gohr        foreach($list as $row) {
270153400c7SAndreas Gohr            $pid = $row['pid'];
271153400c7SAndreas Gohr            $tbl = $row['tbl'];
272153400c7SAndreas Gohr            if(!isset($result[$pid])) $result[$pid] = array();
273153400c7SAndreas Gohr            $result[$pid][$tbl] = (bool) $row['assigned'];
274153400c7SAndreas Gohr        }
275153400c7SAndreas Gohr
276153400c7SAndreas Gohr        return $result;
277153400c7SAndreas Gohr    }
278153400c7SAndreas Gohr
279153400c7SAndreas Gohr    /**
280ed60c3b3SAndreas Gohr     * Check if the given pattern matches the given page
281ed60c3b3SAndreas Gohr     *
282ed60c3b3SAndreas Gohr     * @param string $pattern the pattern to check against
283ed60c3b3SAndreas Gohr     * @param string $page the cleaned pageid to check
284ed60c3b3SAndreas Gohr     * @param string|null $pns optimization, the colon wrapped namespace of the page, set null for automatic
285ed60c3b3SAndreas Gohr     * @return bool
286ed60c3b3SAndreas Gohr     */
287ed60c3b3SAndreas Gohr    protected function matchPagePattern($pattern, $page, $pns = null) {
2880e9e058fSAndreas Gohr        if(trim($pattern,':') == '**') return true; // match all
2890e9e058fSAndreas Gohr
2909914e87eSAndreas Gohr        // regex patterns
291*8da1363aSAndreas Gohr        if($pattern[0] == '/') {
2929914e87eSAndreas Gohr            return (bool) preg_match($pattern, ":$page");
2939914e87eSAndreas Gohr        }
2949914e87eSAndreas Gohr
295ed60c3b3SAndreas Gohr        if(is_null($pns)) {
296ed60c3b3SAndreas Gohr            $pns = ':' . getNS($page) . ':';
297ed60c3b3SAndreas Gohr        }
298ed60c3b3SAndreas Gohr
299ed60c3b3SAndreas Gohr        $ans = ':' . cleanID($pattern) . ':';
300ed60c3b3SAndreas Gohr        if(substr($pattern, -2) == '**') {
301ed60c3b3SAndreas Gohr            // upper namespaces match
302ed60c3b3SAndreas Gohr            if(strpos($pns, $ans) === 0) {
303ed60c3b3SAndreas Gohr                return true;
304ed60c3b3SAndreas Gohr            }
305ed60c3b3SAndreas Gohr        } else if(substr($pattern, -1) == '*') {
306ed60c3b3SAndreas Gohr            // namespaces match exact
307ed60c3b3SAndreas Gohr            if($ans == $pns) {
308ed60c3b3SAndreas Gohr                return true;
309ed60c3b3SAndreas Gohr            }
310ed60c3b3SAndreas Gohr        } else {
311ed60c3b3SAndreas Gohr            // exact match
312ed60c3b3SAndreas Gohr            if(cleanID($pattern) == $page) {
313ed60c3b3SAndreas Gohr                return true;
314ed60c3b3SAndreas Gohr            }
315ed60c3b3SAndreas Gohr        }
316ed60c3b3SAndreas Gohr
317ed60c3b3SAndreas Gohr        return false;
318ed60c3b3SAndreas Gohr    }
319ed60c3b3SAndreas Gohr
320ed60c3b3SAndreas Gohr    /**
32156672c36SAndreas Gohr     * Returns all tables of schemas that existed and stored data for the page back then
32256672c36SAndreas Gohr     *
3230e9e058fSAndreas Gohr     * @deprecated because we're always only interested in the current state of affairs, even when restoring.
32456672c36SAndreas Gohr     *
32556672c36SAndreas Gohr     * @param string $page
32656672c36SAndreas Gohr     * @param string $ts
32756672c36SAndreas Gohr     * @return array
32856672c36SAndreas Gohr     */
32956672c36SAndreas Gohr    public function getHistoricAssignments($page, $ts) {
33056672c36SAndreas Gohr        $sql = "SELECT DISTINCT tbl FROM schemas WHERE ts <= ? ORDER BY ts DESC";
33156672c36SAndreas Gohr        $res = $this->sqlite->query($sql, $ts);
33256672c36SAndreas Gohr        $tables = $this->sqlite->res2arr($res);
33356672c36SAndreas Gohr        $this->sqlite->res_close($res);
33456672c36SAndreas Gohr
33556672c36SAndreas Gohr        $assigned = array();
33656672c36SAndreas Gohr        foreach($tables as $row) {
33756672c36SAndreas Gohr            $table = $row['tbl'];
338ed60c3b3SAndreas Gohr            /** @noinspection SqlResolve */
33956672c36SAndreas Gohr            $sql = "SELECT pid FROM data_$table WHERE pid = ? AND rev <= ? LIMIT 1";
34056672c36SAndreas Gohr            $res = $this->sqlite->query($sql, $page, $ts);
34156672c36SAndreas Gohr            $found = $this->sqlite->res2arr($res);
34256672c36SAndreas Gohr            $this->sqlite->res_close($res);
34356672c36SAndreas Gohr
34456672c36SAndreas Gohr            if($found) $assigned[] = $table;
34556672c36SAndreas Gohr        }
34656672c36SAndreas Gohr
34756672c36SAndreas Gohr        return $assigned;
34856672c36SAndreas Gohr    }
349b25bb9feSMichael Grosse
350b25bb9feSMichael Grosse    /**
351b25bb9feSMichael Grosse     * fetch all pages where the schema isn't assigned, yet and reevaluate the page assignments for those pages and assign when needed
352b25bb9feSMichael Grosse     *
353b25bb9feSMichael Grosse     * @param $table
354b25bb9feSMichael Grosse     */
355b25bb9feSMichael Grosse    public function propagatePageAssignments($table) {
356b25bb9feSMichael Grosse        $sql = 'SELECT pid FROM schema_assignments WHERE tbl != ? OR assigned != 1';
357b25bb9feSMichael Grosse        $res = $this->sqlite->query($sql, $table);
358b25bb9feSMichael Grosse        $pagerows = $this->sqlite->res2arr($res);
359b25bb9feSMichael Grosse        $this->sqlite->res_close($res);
360b25bb9feSMichael Grosse
361b25bb9feSMichael Grosse        foreach ($pagerows as $row) {
362b25bb9feSMichael Grosse            $tables = $this->getPageAssignments($row['pid'], true);
363b25bb9feSMichael Grosse            if (in_array($table, $tables)) {
364b25bb9feSMichael Grosse                $this->assignPageSchema($row['pid'], $table);
365b25bb9feSMichael Grosse            }
366b25bb9feSMichael Grosse        }
367b25bb9feSMichael Grosse    }
368fb31ca9fSAndreas Gohr}
369