xref: /plugin/struct/meta/Assignments.php (revision 1a8d1235c6995c984a6599cb7da8732054383e1e)
1fb31ca9fSAndreas Gohr<?php
2fb31ca9fSAndreas Gohr
3fb31ca9fSAndreas Gohrnamespace plugin\struct\meta;
4fb31ca9fSAndreas Gohr
5*1a8d1235SAndreas Gohr/**
6*1a8d1235SAndreas Gohr * Class Assignments
7*1a8d1235SAndreas Gohr *
8*1a8d1235SAndreas Gohr * Manages the assignment of schemas (table names) to pages and namespaces
9*1a8d1235SAndreas Gohr *
10*1a8d1235SAndreas Gohr * @package plugin\struct\meta
11*1a8d1235SAndreas Gohr */
12fb31ca9fSAndreas Gohrclass Assignments {
13fb31ca9fSAndreas Gohr
14fb31ca9fSAndreas Gohr    /** @var \helper_plugin_sqlite|null */
15fb31ca9fSAndreas Gohr    protected $sqlite;
16fb31ca9fSAndreas Gohr
17fb31ca9fSAndreas Gohr    /** @var  array All the assignments */
18fb31ca9fSAndreas Gohr    protected $assignments;
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
28fb31ca9fSAndreas Gohr        if($this->sqlite) $this->load();
29fb31ca9fSAndreas Gohr    }
30fb31ca9fSAndreas Gohr
31fb31ca9fSAndreas Gohr    /**
32fb31ca9fSAndreas Gohr     * Load existing assignments
33fb31ca9fSAndreas Gohr     */
34fb31ca9fSAndreas Gohr    protected function load() {
35fb31ca9fSAndreas Gohr        $sql = 'SELECT * FROM schema_assignments ORDER BY assign';
36fb31ca9fSAndreas Gohr        $res = $this->sqlite->query($sql);
37fb31ca9fSAndreas Gohr        $this->assignments = $this->sqlite->res2arr($res);
38fb31ca9fSAndreas Gohr        $this->sqlite->res_close($res);
39fb31ca9fSAndreas Gohr    }
40fb31ca9fSAndreas Gohr
41fb31ca9fSAndreas Gohr    /**
42*1a8d1235SAndreas Gohr     * Add a new assignment to the assignment table
43*1a8d1235SAndreas Gohr     *
44*1a8d1235SAndreas Gohr     * @param string $assign
45*1a8d1235SAndreas Gohr     * @param string $table
46*1a8d1235SAndreas Gohr     * @return bool
47*1a8d1235SAndreas Gohr     */
48*1a8d1235SAndreas Gohr    public function add($assign, $table) {
49*1a8d1235SAndreas Gohr        $sql = 'REPLACE INTO schema_assignments (assign, tbl) VALUES (?,?)';
50*1a8d1235SAndreas Gohr        return (bool) $this->sqlite->query($sql, array($assign, $table));
51*1a8d1235SAndreas Gohr    }
52*1a8d1235SAndreas Gohr
53*1a8d1235SAndreas Gohr    /**
54*1a8d1235SAndreas Gohr     * Remove an existing assignment from the assignment table
55*1a8d1235SAndreas Gohr     *
56*1a8d1235SAndreas Gohr     * @param string $assign
57*1a8d1235SAndreas Gohr     * @param string $table
58*1a8d1235SAndreas Gohr     * @return bool
59*1a8d1235SAndreas Gohr     */
60*1a8d1235SAndreas Gohr    public function remove($assign, $table) {
61*1a8d1235SAndreas Gohr        $sql = 'DELETE FROM schema_assignments WHERE assign = ? AND tbl = ?';
62*1a8d1235SAndreas Gohr        return (bool) $this->sqlite->query($sql, array($assign, $table));
63*1a8d1235SAndreas Gohr    }
64*1a8d1235SAndreas Gohr
65*1a8d1235SAndreas Gohr    /**
66*1a8d1235SAndreas Gohr     * Get the whole assignments table
67*1a8d1235SAndreas Gohr     *
68*1a8d1235SAndreas Gohr     * @return array
69*1a8d1235SAndreas Gohr     */
70*1a8d1235SAndreas Gohr    public function getAll() {
71*1a8d1235SAndreas Gohr        return $this->assignments;
72*1a8d1235SAndreas Gohr    }
73*1a8d1235SAndreas Gohr
74*1a8d1235SAndreas Gohr    /**
75fb31ca9fSAndreas Gohr     * Returns a list of table names assigned to the given page
76fb31ca9fSAndreas Gohr     *
77fb31ca9fSAndreas Gohr     * @param string $page
78fb31ca9fSAndreas Gohr     * @return string[] tables assigned
79fb31ca9fSAndreas Gohr     */
80fb31ca9fSAndreas Gohr    public function getPageAssignments($page) {
81fb31ca9fSAndreas Gohr        $tables = array();
82fb31ca9fSAndreas Gohr
83fb31ca9fSAndreas Gohr        $page = cleanID($page);
84fb31ca9fSAndreas Gohr        $pns = ':' . getNS($page) . ':';
85fb31ca9fSAndreas Gohr
86fb31ca9fSAndreas Gohr        foreach($this->assignments as $row) {
87fb31ca9fSAndreas Gohr            $ass = $row['assign'];
88fb31ca9fSAndreas Gohr            $tbl = $row['tbl'];
89fb31ca9fSAndreas Gohr
90fb31ca9fSAndreas Gohr            $ans = ':' . cleanID($ass) . ':';
91fb31ca9fSAndreas Gohr
92fb31ca9fSAndreas Gohr            if(substr($ass, -2) == '**') {
93fb31ca9fSAndreas Gohr                // upper namespaces match
94fb31ca9fSAndreas Gohr                if(strpos($pns, $ans) === 0) {
95fb31ca9fSAndreas Gohr                    $tables[] = $tbl;
96fb31ca9fSAndreas Gohr                }
97fb31ca9fSAndreas Gohr            } else if(substr($ass, -1) == '*') {
98fb31ca9fSAndreas Gohr                // namespaces match exact
99fb31ca9fSAndreas Gohr                if($ans == $pns) {
100fb31ca9fSAndreas Gohr                    $tables[] = $tbl;
101fb31ca9fSAndreas Gohr                }
102fb31ca9fSAndreas Gohr            } else {
103fb31ca9fSAndreas Gohr                // exact match
104fb31ca9fSAndreas Gohr                if(cleanID($ass) == $page) {
105fb31ca9fSAndreas Gohr                    $tables[] = $tbl;
106fb31ca9fSAndreas Gohr                }
107fb31ca9fSAndreas Gohr            }
108fb31ca9fSAndreas Gohr        }
109fb31ca9fSAndreas Gohr
110fb31ca9fSAndreas Gohr        return array_unique($tables);
111fb31ca9fSAndreas Gohr    }
112fb31ca9fSAndreas Gohr}
113