xref: /plugin/approve/helper.php (revision 7e838775bb98919b34a6dedb9eef1e80575b5d32)
1<?php
2
3// must be run within Dokuwiki
4if (!defined('DOKU_INC')) die();
5class helper_plugin_approve extends DokuWiki_Plugin {
6
7    /** @var helper_plugin_sqlite */
8    protected $sqlite;
9
10    /**
11     * @return helper_plugin_sqlite
12     */
13    protected function sqlite() {
14        if (!$this->sqlite) {
15            /** @var helper_plugin_approve_db $db_helper */
16            $db_helper = plugin_load('helper', 'approve_db');
17            $this->sqlite = $db_helper->getDB();
18        }
19        return $this->sqlite;
20    }
21
22    /**
23     * @return string
24     */
25    public function no_apr_namespace() {
26        //check for config update
27        $key = 'no_apr_namespaces';
28        $res = $this->sqlite()->query('SELECT value FROM config WHERE key=?', $key);
29        $no_apr_namespaces_db = $this->sqlite()->res2single($res);
30        $no_apr_namespaces_conf = $this->getConf($key);
31        //update internal config
32        if ($no_apr_namespaces_db != $no_apr_namespaces_conf) {
33            $this->sqlite()->query('UPDATE config SET value=? WHERE key=?', $no_apr_namespaces_conf, $key);
34
35            $res = $this->sqlite()->query('SELECT page, hidden FROM page');
36            $pages = $this->sqlite()->res2arr($res);
37            foreach ($pages as $page) {
38                $id = $page['page'];
39                $hidden = $page['hidden'];
40                $in_hidden_namespace = $this->in_hidden_namespace($id, $no_apr_namespaces_conf);
41                $new_hidden = $in_hidden_namespace ? '1' : '0';
42
43                if ($hidden != $new_hidden) {
44                    $this->sqlite()->query('UPDATE page SET hidden=? WHERE page=?', $new_hidden, $id);
45                }
46            }
47        }
48
49        return $no_apr_namespaces_conf;
50    }
51
52    /**
53     * @param $id
54     * @return bool
55     */
56    public function use_approve_here($id) {
57
58        //check if we should update no_apr_namespace
59        $this->no_apr_namespace();
60
61        $res = $this->sqlite()->query('SELECT page FROM page WHERE page=? AND hidden=0', $id);
62        if ($this->sqlite()->res2single($res)) {
63            return true;
64        }
65        return false;
66    }
67
68    /**
69     * @param $id
70     * @return bool|string
71     */
72    public function find_last_approved($id) {
73        $res = $this->sqlite()->query('SELECT rev FROM revision
74                                WHERE page=? AND approved IS NOT NULL
75                                ORDER BY rev DESC LIMIT 1', $id);
76        return $this->sqlite()->res2single($res);
77    }
78
79    public function get_hidden_namespaces_list($no_apr_namespaces=null) {
80        if (!$no_apr_namespaces) {
81            $no_apr_namespaces = $this->no_apr_namespace();
82        }
83
84        $no_apr_namespaces_list = preg_split('/\s+/', $no_apr_namespaces,-1,
85            PREG_SPLIT_NO_EMPTY);
86        $no_apr_namespaces_list = array_map(function ($namespace) {
87            return ltrim($namespace, ':');
88        }, $no_apr_namespaces_list);
89
90        return $no_apr_namespaces_list;
91    }
92
93    /**
94     * @param $id
95     * @param null $no_apr_namespaces
96     * @return bool|string
97     */
98    public function in_hidden_namespace($id, $no_apr_namespaces=null) {
99        $no_apr_namespaces_list = $this->get_hidden_namespaces_list($no_apr_namespaces);
100        $id = ltrim($id, ':');
101        foreach ($no_apr_namespaces_list as $namespace) {
102            if (substr($id, 0, strlen($namespace)) == $namespace) {
103                return true;
104            }
105        }
106        return false;
107    }
108
109//    /**
110//     * Check if we should use approve in page
111//     *
112//     * @param string $id
113//     *
114//     * @return bool
115//     */
116//    function use_approve_here($id) {
117//        $apr_namespaces = $this->getConf('apr_namespaces');
118//        $no_apr_namespaces = $this->getConf('no_apr_namespaces');
119//
120//        if ($this->in_namespace($no_apr_namespaces, $id)) {
121//            return false;
122//        //use apr_namespaces
123//        } elseif (trim($apr_namespaces) != '') {
124//            if ($this->in_namespace($apr_namespaces, $id)) {
125//                return true;
126//            }
127//            return false;
128//        }
129//
130//        return true;
131//    }
132     /**
133     * checks if an id is within one of the namespaces in $namespace_list
134     *
135     * @param string $namespace_list
136     * @param string $id
137     *
138     * @return bool
139     */
140    function in_namespace($namespace_list, $id) {
141        // PHP apparantly does not have closures -
142        // so we will parse $valid ourselves. Wasteful.
143        $namespace_list = preg_split('/\s+/', $namespace_list);
144
145        //if(count($valid) == 0) { return true; }//whole wiki matches
146        if(count($namespace_list) == 1 && $namespace_list[0] == "") { return false; }//whole wiki matches
147
148        $id = trim($id, ':');
149        $id = explode(':', $id);
150
151        // Check against all possible namespaces
152        foreach($namespace_list as $namespace) {
153            $namespace = explode(':', $namespace);
154            $current_ns_depth = 0;
155            $total_ns_depth = count($namespace);
156            $matching = true;
157
158            // Check each element, untill all elements of $v satisfied
159            while($current_ns_depth < $total_ns_depth) {
160                if($namespace[$current_ns_depth] != $id[$current_ns_depth]) {
161                    // not a match
162                    $matching = false;
163                    break;
164                }
165                $current_ns_depth += 1;
166            }
167            if($matching) { return true; } // a match
168        }
169        return false;
170    }
171
172    function page_sum($ID, $REV) {
173		$m = p_get_metadata($ID);
174		$changelog = new PageChangeLog($ID);
175
176		//sprawdź status aktualnej strony
177		if ($REV != 0) {
178			$ch = $changelog->getRevisionInfo($REV);
179			$sum = $ch['sum'];
180		} else {
181			$sum = $m['last_change']['sum'];
182		}
183		return $sum;
184	}
185}
186