xref: /plugin/approve/helper.php (revision c7d53eaba29e207f5d28ea550cd66a04c154a49d)
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     * @param null $maintainer
55     * @return bool
56     */
57    public function use_approve_here($id, &$maintainer=null) {
58
59        //check if we should update no_apr_namespace
60        $this->no_apr_namespace();
61
62        $res = $this->sqlite()->query('SELECT page, maintainer FROM page WHERE page=? AND hidden=0', $id);
63        $row = $this->sqlite()->res2row($res);
64        $maintainer = $row['maintainer'];
65        if ($row) {
66            return true;
67        }
68        return false;
69    }
70
71    /**
72     * @param $id
73     * @return bool|string
74     */
75    public function find_last_approved($id) {
76        $res = $this->sqlite()->query('SELECT rev FROM revision
77                                WHERE page=? AND approved IS NOT NULL
78                                ORDER BY rev DESC LIMIT 1', $id);
79        return $this->sqlite()->res2single($res);
80    }
81
82    public function get_hidden_namespaces_list($no_apr_namespaces=null) {
83        if (!$no_apr_namespaces) {
84            $no_apr_namespaces = $this->no_apr_namespace();
85        }
86
87        $no_apr_namespaces_list = preg_split('/\s+/', $no_apr_namespaces,-1,
88            PREG_SPLIT_NO_EMPTY);
89        $no_apr_namespaces_list = array_map(function ($namespace) {
90            return ltrim($namespace, ':');
91        }, $no_apr_namespaces_list);
92
93        return $no_apr_namespaces_list;
94    }
95
96    /**
97     * @param $id
98     * @param null $no_apr_namespaces
99     * @return bool|string
100     */
101    public function in_hidden_namespace($id, $no_apr_namespaces=null) {
102        $no_apr_namespaces_list = $this->get_hidden_namespaces_list($no_apr_namespaces);
103        $id = ltrim($id, ':');
104        foreach ($no_apr_namespaces_list as $namespace) {
105            if (substr($id, 0, strlen($namespace)) == $namespace) {
106                return true;
107            }
108        }
109        return false;
110    }
111
112//    /**
113//     * Check if we should use approve in page
114//     *
115//     * @param string $id
116//     *
117//     * @return bool
118//     */
119//    function use_approve_here($id) {
120//        $apr_namespaces = $this->getConf('apr_namespaces');
121//        $no_apr_namespaces = $this->getConf('no_apr_namespaces');
122//
123//        if ($this->in_namespace($no_apr_namespaces, $id)) {
124//            return false;
125//        //use apr_namespaces
126//        } elseif (trim($apr_namespaces) != '') {
127//            if ($this->in_namespace($apr_namespaces, $id)) {
128//                return true;
129//            }
130//            return false;
131//        }
132//
133//        return true;
134//    }
135     /**
136     * checks if an id is within one of the namespaces in $namespace_list
137     *
138     * @param string $namespace_list
139     * @param string $id
140     *
141     * @return bool
142     */
143    function in_namespace($namespace_list, $id) {
144        // PHP apparantly does not have closures -
145        // so we will parse $valid ourselves. Wasteful.
146        $namespace_list = preg_split('/\s+/', $namespace_list);
147
148        //if(count($valid) == 0) { return true; }//whole wiki matches
149        if(count($namespace_list) == 1 && $namespace_list[0] == "") { return false; }//whole wiki matches
150
151        $id = trim($id, ':');
152        $id = explode(':', $id);
153
154        // Check against all possible namespaces
155        foreach($namespace_list as $namespace) {
156            $namespace = explode(':', $namespace);
157            $current_ns_depth = 0;
158            $total_ns_depth = count($namespace);
159            $matching = true;
160
161            // Check each element, untill all elements of $v satisfied
162            while($current_ns_depth < $total_ns_depth) {
163                if($namespace[$current_ns_depth] != $id[$current_ns_depth]) {
164                    // not a match
165                    $matching = false;
166                    break;
167                }
168                $current_ns_depth += 1;
169            }
170            if($matching) { return true; } // a match
171        }
172        return false;
173    }
174
175    function page_sum($ID, $REV) {
176		$m = p_get_metadata($ID);
177		$changelog = new PageChangeLog($ID);
178
179		//sprawdź status aktualnej strony
180		if ($REV != 0) {
181			$ch = $changelog->getRevisionInfo($REV);
182			$sum = $ch['sum'];
183		} else {
184			$sum = $m['last_change']['sum'];
185		}
186		return $sum;
187	}
188}
189