xref: /plugin/struct/meta/Assignments.php (revision 56672c369c850b8e8ce1e1f730cb14b72334f6f0)
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
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    /**
421a8d1235SAndreas Gohr     * Add a new assignment to the assignment table
431a8d1235SAndreas Gohr     *
441a8d1235SAndreas Gohr     * @param string $assign
451a8d1235SAndreas Gohr     * @param string $table
461a8d1235SAndreas Gohr     * @return bool
471a8d1235SAndreas Gohr     */
481a8d1235SAndreas Gohr    public function add($assign, $table) {
491a8d1235SAndreas Gohr        $sql = 'REPLACE INTO schema_assignments (assign, tbl) VALUES (?,?)';
501a8d1235SAndreas Gohr        return (bool) $this->sqlite->query($sql, array($assign, $table));
511a8d1235SAndreas Gohr    }
521a8d1235SAndreas Gohr
531a8d1235SAndreas Gohr    /**
541a8d1235SAndreas Gohr     * Remove an existing assignment from the assignment table
551a8d1235SAndreas Gohr     *
561a8d1235SAndreas Gohr     * @param string $assign
571a8d1235SAndreas Gohr     * @param string $table
581a8d1235SAndreas Gohr     * @return bool
591a8d1235SAndreas Gohr     */
601a8d1235SAndreas Gohr    public function remove($assign, $table) {
611a8d1235SAndreas Gohr        $sql = 'DELETE FROM schema_assignments WHERE assign = ? AND tbl = ?';
621a8d1235SAndreas Gohr        return (bool) $this->sqlite->query($sql, array($assign, $table));
631a8d1235SAndreas Gohr    }
641a8d1235SAndreas Gohr
651a8d1235SAndreas Gohr    /**
661a8d1235SAndreas Gohr     * Get the whole assignments table
671a8d1235SAndreas Gohr     *
681a8d1235SAndreas Gohr     * @return array
691a8d1235SAndreas Gohr     */
701a8d1235SAndreas Gohr    public function getAll() {
711a8d1235SAndreas Gohr        return $this->assignments;
721a8d1235SAndreas Gohr    }
731a8d1235SAndreas Gohr
741a8d1235SAndreas 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    }
112*56672c36SAndreas Gohr
113*56672c36SAndreas Gohr    /**
114*56672c36SAndreas Gohr     * Returns all tables of schemas that existed and stored data for the page back then
115*56672c36SAndreas Gohr     *
116*56672c36SAndreas Gohr     * @todo this is not used currently and can probably be removed again, because we're
117*56672c36SAndreas Gohr     *       always only interested in the current state of affairs, even when restoring.
118*56672c36SAndreas Gohr     *
119*56672c36SAndreas Gohr     * @param string $page
120*56672c36SAndreas Gohr     * @param string $ts
121*56672c36SAndreas Gohr     * @return array
122*56672c36SAndreas Gohr     */
123*56672c36SAndreas Gohr    public function getHistoricAssignments($page, $ts) {
124*56672c36SAndreas Gohr        $sql = "SELECT DISTINCT tbl FROM schemas WHERE ts <= ? ORDER BY ts DESC";
125*56672c36SAndreas Gohr        $res = $this->sqlite->query($sql, $ts);
126*56672c36SAndreas Gohr        $tables = $this->sqlite->res2arr($res);
127*56672c36SAndreas Gohr        $this->sqlite->res_close($res);
128*56672c36SAndreas Gohr
129*56672c36SAndreas Gohr        $assigned = array();
130*56672c36SAndreas Gohr        foreach ($tables as $row) {
131*56672c36SAndreas Gohr            $table = $row['tbl'];
132*56672c36SAndreas Gohr            $sql = "SELECT pid FROM data_$table WHERE pid = ? AND rev <= ? LIMIT 1";
133*56672c36SAndreas Gohr            $res = $this->sqlite->query($sql, $page, $ts);
134*56672c36SAndreas Gohr            $found = $this->sqlite->res2arr($res);
135*56672c36SAndreas Gohr            $this->sqlite->res_close($res);
136*56672c36SAndreas Gohr
137*56672c36SAndreas Gohr            if($found) $assigned[] = $table;
138*56672c36SAndreas Gohr        }
139*56672c36SAndreas Gohr
140*56672c36SAndreas Gohr        return $assigned;
141*56672c36SAndreas Gohr    }
142fb31ca9fSAndreas Gohr}
143