xref: /plugin/approve/helper.php (revision 69eea0e5403b6d9bc69f06877a64b8414e09ed46)
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     * @param $id
24     * @return bool
25     */
26    public function use_approve_here($id) {
27        $res = $this->sqlite()->query('SELECT page FROM page WHERE page=? AND hidden=0', $id);
28        if ($this->sqlite()->res2single($res)) {
29            return true;
30        }
31        return false;
32    }
33
34    /**
35     * @param $id
36     * @return bool|string
37     */
38    public function find_last_approved($id) {
39        $res = $this->sqlite()->query('SELECT rev FROM revision
40                                WHERE page=? AND approved IS NOT NULL
41                                ORDER BY rev DESC LIMIT 1', $id);
42        return $this->sqlite()->res2single($res);
43    }
44
45    public function get_hidden_namespaces_list($no_apr_namespaces=null) {
46        if (!$no_apr_namespaces) {
47            $res = $this->sqlite()->query('SELECT value FROM config WHERE key=?', 'no_apr_namespaces');
48            $no_apr_namespaces = $this->sqlite()->res2single($res);
49        }
50
51        $no_apr_namespaces_list = preg_split('/\s+/', $no_apr_namespaces,-1,
52            PREG_SPLIT_NO_EMPTY);
53        $no_apr_namespaces_list = array_map(function ($namespace) {
54            return ltrim($namespace, ':');
55        }, $no_apr_namespaces_list);
56
57        return $no_apr_namespaces_list;
58    }
59
60    /**
61     * @param $id
62     * @return bool|string
63     */
64    public function in_hidden_namespace($id, $no_apr_namespaces=null) {
65        $no_apr_namespaces_list = $this->get_hidden_namespaces_list($no_apr_namespaces);
66        $id = ltrim($id, ':');
67        foreach ($no_apr_namespaces_list as $namespace) {
68            if (substr($id, 0, strlen($namespace)) == $namespace) {
69                return true;
70            }
71        }
72        return false;
73    }
74
75//    /**
76//     * Check if we should use approve in page
77//     *
78//     * @param string $id
79//     *
80//     * @return bool
81//     */
82//    function use_approve_here($id) {
83//        $apr_namespaces = $this->getConf('apr_namespaces');
84//        $no_apr_namespaces = $this->getConf('no_apr_namespaces');
85//
86//        if ($this->in_namespace($no_apr_namespaces, $id)) {
87//            return false;
88//        //use apr_namespaces
89//        } elseif (trim($apr_namespaces) != '') {
90//            if ($this->in_namespace($apr_namespaces, $id)) {
91//                return true;
92//            }
93//            return false;
94//        }
95//
96//        return true;
97//    }
98     /**
99     * checks if an id is within one of the namespaces in $namespace_list
100     *
101     * @param string $namespace_list
102     * @param string $id
103     *
104     * @return bool
105     */
106    function in_namespace($namespace_list, $id) {
107        // PHP apparantly does not have closures -
108        // so we will parse $valid ourselves. Wasteful.
109        $namespace_list = preg_split('/\s+/', $namespace_list);
110
111        //if(count($valid) == 0) { return true; }//whole wiki matches
112        if(count($namespace_list) == 1 && $namespace_list[0] == "") { return false; }//whole wiki matches
113
114        $id = trim($id, ':');
115        $id = explode(':', $id);
116
117        // Check against all possible namespaces
118        foreach($namespace_list as $namespace) {
119            $namespace = explode(':', $namespace);
120            $current_ns_depth = 0;
121            $total_ns_depth = count($namespace);
122            $matching = true;
123
124            // Check each element, untill all elements of $v satisfied
125            while($current_ns_depth < $total_ns_depth) {
126                if($namespace[$current_ns_depth] != $id[$current_ns_depth]) {
127                    // not a match
128                    $matching = false;
129                    break;
130                }
131                $current_ns_depth += 1;
132            }
133            if($matching) { return true; } // a match
134        }
135        return false;
136    }
137
138    function page_sum($ID, $REV) {
139		$m = p_get_metadata($ID);
140		$changelog = new PageChangeLog($ID);
141
142		//sprawdź status aktualnej strony
143		if ($REV != 0) {
144			$ch = $changelog->getRevisionInfo($REV);
145			$sum = $ch['sum'];
146		} else {
147			$sum = $m['last_change']['sum'];
148		}
149		return $sum;
150	}
151}
152