xref: /plugin/sqlite/Functions.php (revision 3a56750b42e05745f84fd97217f7133805e622eb)
1<?php
2/**
3 * @noinspection PhpUndefinedMethodInspection
4 * @noinspection PhpComposerExtensionStubsInspection
5 */
6
7
8namespace dokuwiki\plugin\sqlite;
9
10/**
11 * SQLite registered functions
12 */
13class Functions
14{
15
16    /**
17     * Register all standard functions
18     *
19     * @param \PDO $pdo
20     */
21    public static function register($pdo)
22    {
23        $pdo->sqliteCreateAggregate(
24            'group_concat',
25            [Functions::class, 'GroupConcatStep'],
26            [Functions::class, 'GroupConcatFinalize']
27        );
28
29        $pdo->sqliteCreateFunction('GETACCESSLEVEL', [Functions::class, 'getAccessLevel'], 1);
30        $pdo->sqliteCreateFunction('PAGEEXISTS', [Functions::class, 'pageExists'], 1);
31        $pdo->sqliteCreateFunction('REGEXP', [Functions::class, 'regExp'], 2);
32        $pdo->sqliteCreateFunction('CLEANID', 'cleanID', 1);
33        $pdo->sqliteCreateFunction('RESOLVEPAGE', [Functions::class, 'resolvePage'], 1);
34    }
35
36    /**
37     * Aggregation function for SQLite via PDO
38     *
39     * @link http://devzone.zend.com/article/863-SQLite-Lean-Mean-DB-Machine
40     *
41     * @param null|array &$context (reference) argument where processed data can be stored
42     * @param int $rownumber current row number
43     * @param string $string column value
44     * @param string $separator separator added between values
45     */
46    public static function GroupConcatStep(&$context, $rownumber, $string, $separator = ',')
47    {
48        if (is_null($context)) {
49            $context = [
50                'sep' => $separator,
51                'data' => []
52            ];
53        }
54
55        $context['data'][] = $string;
56        return $context;
57    }
58
59    /**
60     * Aggregation function for SQLite via PDO
61     *
62     * @link http://devzone.zend.com/article/863-SQLite-Lean-Mean-DB-Machine
63     *
64     * @param null|array &$context (reference) data as collected in step callback
65     * @param int $rownumber number of rows over which the aggregate was performed.
66     * @return null|string
67     */
68    public static function GroupConcatFinalize(&$context, $rownumber)
69    {
70        if (!is_array($context)) {
71            return null;
72        }
73        $context['data'] = array_unique($context['data']);
74        if (empty($context['data'][0])) {
75            return null;
76        }
77        return join($context['sep'], $context['data']);
78    }
79
80
81    /**
82     * Callback checks the permissions for the current user
83     *
84     * This function is registered as a SQL function named GETACCESSLEVEL
85     *
86     * @param string $pageid page ID (needs to be resolved and cleaned)
87     * @return int permission level
88     */
89    public static function getAccessLevel($pageid)
90    {
91        static $aclcache = [];
92
93        if (isset($aclcache[$pageid])) {
94            return $aclcache[$pageid];
95        }
96
97        if (isHiddenPage($pageid)) {
98            $acl = AUTH_NONE;
99        } else {
100            $acl = auth_quickaclcheck($pageid);
101        }
102        $aclcache[$pageid] = $acl;
103        return $acl;
104    }
105
106    /**
107     * Wrapper around page_exists() with static caching
108     *
109     * This function is registered as a SQL function named PAGEEXISTS
110     *
111     * @param string $pageid
112     * @return int 0|1
113     */
114    public static function pageExists($pageid)
115    {
116        static $cache = [];
117        if (!isset($cache[$pageid])) {
118            $cache[$pageid] = page_exists($pageid);
119
120        }
121        return (int)$cache[$pageid];
122    }
123
124    /**
125     * Match a regular expression against a value
126     *
127     * This function is registered as a SQL function named REGEXP
128     *
129     * @param string $regexp
130     * @param string $value
131     * @return bool
132     */
133    public static function regExp($regexp, $value)
134    {
135        $regexp = addcslashes($regexp, '/');
136        return (bool)preg_match('/' . $regexp . '/u', $value);
137    }
138
139    /**
140     * Resolves a page ID (relative namespaces, plurals etc)
141     *
142     * This function is registered as a SQL function named RESOLVEPAGE
143     *
144     * @param string $page The page ID to resolve
145     * @param string $context The page ID (not namespace!) to resolve the page with
146     * @return null|string
147     */
148    public static function resolvePage($page, $context)
149    {
150        if (is_null($page)) return null;
151        if (is_null($context)) return cleanID($page);
152
153        $ns = getNS($context);
154        resolve_pageid($ns, $page, $exists);
155        return $page;
156    }
157
158}
159