xref: /plugin/struct/meta/Assignments.php (revision fb31ca9fa3249b10a91554bcd044f27dda620b99)
1*fb31ca9fSAndreas Gohr<?php
2*fb31ca9fSAndreas Gohr
3*fb31ca9fSAndreas Gohrnamespace plugin\struct\meta;
4*fb31ca9fSAndreas Gohr
5*fb31ca9fSAndreas Gohrclass Assignments {
6*fb31ca9fSAndreas Gohr
7*fb31ca9fSAndreas Gohr    /** @var \helper_plugin_sqlite|null */
8*fb31ca9fSAndreas Gohr    protected $sqlite;
9*fb31ca9fSAndreas Gohr
10*fb31ca9fSAndreas Gohr    /** @var  array All the assignments */
11*fb31ca9fSAndreas Gohr    protected $assignments;
12*fb31ca9fSAndreas Gohr
13*fb31ca9fSAndreas Gohr    /**
14*fb31ca9fSAndreas Gohr     * Assignments constructor.
15*fb31ca9fSAndreas Gohr     */
16*fb31ca9fSAndreas Gohr    public function __construct() {
17*fb31ca9fSAndreas Gohr        /** @var \helper_plugin_struct_db $helper */
18*fb31ca9fSAndreas Gohr        $helper = plugin_load('helper', 'struct_db');
19*fb31ca9fSAndreas Gohr        $this->sqlite = $helper->getDB();
20*fb31ca9fSAndreas Gohr
21*fb31ca9fSAndreas Gohr        if($this->sqlite) $this->load();
22*fb31ca9fSAndreas Gohr    }
23*fb31ca9fSAndreas Gohr
24*fb31ca9fSAndreas Gohr    /**
25*fb31ca9fSAndreas Gohr     * Load existing assignments
26*fb31ca9fSAndreas Gohr     */
27*fb31ca9fSAndreas Gohr    protected function load() {
28*fb31ca9fSAndreas Gohr        $sql = 'SELECT * FROM schema_assignments ORDER BY assign';
29*fb31ca9fSAndreas Gohr        $res = $this->sqlite->query($sql);
30*fb31ca9fSAndreas Gohr        $this->assignments = $this->sqlite->res2arr($res);
31*fb31ca9fSAndreas Gohr        $this->sqlite->res_close($res);
32*fb31ca9fSAndreas Gohr    }
33*fb31ca9fSAndreas Gohr
34*fb31ca9fSAndreas Gohr    /**
35*fb31ca9fSAndreas Gohr     * Returns a list of table names assigned to the given page
36*fb31ca9fSAndreas Gohr     *
37*fb31ca9fSAndreas Gohr     * @param string $page
38*fb31ca9fSAndreas Gohr     * @return string[] tables assigned
39*fb31ca9fSAndreas Gohr     */
40*fb31ca9fSAndreas Gohr    public function getPageAssignments($page) {
41*fb31ca9fSAndreas Gohr        $tables = array();
42*fb31ca9fSAndreas Gohr
43*fb31ca9fSAndreas Gohr        $page = cleanID($page);
44*fb31ca9fSAndreas Gohr        $pns = ':' . getNS($page) . ':';
45*fb31ca9fSAndreas Gohr
46*fb31ca9fSAndreas Gohr        foreach($this->assignments as $row) {
47*fb31ca9fSAndreas Gohr            $ass = $row['assign'];
48*fb31ca9fSAndreas Gohr            $tbl = $row['tbl'];
49*fb31ca9fSAndreas Gohr
50*fb31ca9fSAndreas Gohr            $ans = ':' . cleanID($ass) . ':';
51*fb31ca9fSAndreas Gohr
52*fb31ca9fSAndreas Gohr            if(substr($ass, -2) == '**') {
53*fb31ca9fSAndreas Gohr                // upper namespaces match
54*fb31ca9fSAndreas Gohr                if(strpos($pns, $ans) === 0) {
55*fb31ca9fSAndreas Gohr                    $tables[] = $tbl;
56*fb31ca9fSAndreas Gohr                }
57*fb31ca9fSAndreas Gohr            } else if(substr($ass, -1) == '*') {
58*fb31ca9fSAndreas Gohr                // namespaces match exact
59*fb31ca9fSAndreas Gohr                if($ans == $pns) {
60*fb31ca9fSAndreas Gohr                    $tables[] = $tbl;
61*fb31ca9fSAndreas Gohr                }
62*fb31ca9fSAndreas Gohr            } else {
63*fb31ca9fSAndreas Gohr                // exact match
64*fb31ca9fSAndreas Gohr                if(cleanID($ass) == $page) {
65*fb31ca9fSAndreas Gohr                    $tables[] = $tbl;
66*fb31ca9fSAndreas Gohr                }
67*fb31ca9fSAndreas Gohr            }
68*fb31ca9fSAndreas Gohr        }
69*fb31ca9fSAndreas Gohr
70*fb31ca9fSAndreas Gohr        return array_unique($tables);
71*fb31ca9fSAndreas Gohr    }
72*fb31ca9fSAndreas Gohr}
73