xref: /plugin/sqlite/Functions.php (revision a7a40fb292e55f99ed1ae34981feed1314e2696b)
18da7d805SAndreas Gohr<?php
2*a7a40fb2SAnna Dabrowska
38da7d805SAndreas Gohr/**
48da7d805SAndreas Gohr * @noinspection PhpUndefinedMethodInspection
58da7d805SAndreas Gohr * @noinspection PhpComposerExtensionStubsInspection
68da7d805SAndreas Gohr */
78da7d805SAndreas Gohr
88da7d805SAndreas Gohrnamespace dokuwiki\plugin\sqlite;
98da7d805SAndreas Gohr
108da7d805SAndreas Gohr/**
118da7d805SAndreas Gohr * SQLite registered functions
128da7d805SAndreas Gohr */
138da7d805SAndreas Gohrclass Functions
148da7d805SAndreas Gohr{
158da7d805SAndreas Gohr    /**
168da7d805SAndreas Gohr     * Register all standard functions
178da7d805SAndreas Gohr     *
188da7d805SAndreas Gohr     * @param \PDO $pdo
198da7d805SAndreas Gohr     */
208da7d805SAndreas Gohr    public static function register($pdo)
218da7d805SAndreas Gohr    {
22ded78afbSAndreas Gohr        $pdo->sqliteCreateAggregate(
23ded78afbSAndreas Gohr            'GROUP_CONCAT_DISTINCT',
24ded78afbSAndreas Gohr            [Functions::class, 'GroupConcatStep'],
25ded78afbSAndreas Gohr            [Functions::class, 'GroupConcatFinalize']
26ded78afbSAndreas Gohr        );
27ded78afbSAndreas Gohr
288da7d805SAndreas Gohr        $pdo->sqliteCreateFunction('GETACCESSLEVEL', [Functions::class, 'getAccessLevel'], 1);
298da7d805SAndreas Gohr        $pdo->sqliteCreateFunction('PAGEEXISTS', [Functions::class, 'pageExists'], 1);
308da7d805SAndreas Gohr        $pdo->sqliteCreateFunction('REGEXP', [Functions::class, 'regExp'], 2);
318da7d805SAndreas Gohr        $pdo->sqliteCreateFunction('CLEANID', 'cleanID', 1);
328da7d805SAndreas Gohr        $pdo->sqliteCreateFunction('RESOLVEPAGE', [Functions::class, 'resolvePage'], 1);
338da7d805SAndreas Gohr    }
348da7d805SAndreas Gohr
358da7d805SAndreas Gohr    /**
36ded78afbSAndreas Gohr     * Aggregation function for SQLite via PDO
37ded78afbSAndreas Gohr     *
38ded78afbSAndreas Gohr     * @link http://devzone.zend.com/article/863-SQLite-Lean-Mean-DB-Machine
39ded78afbSAndreas Gohr     *
40ded78afbSAndreas Gohr     * @param null|array &$context (reference) argument where processed data can be stored
41ded78afbSAndreas Gohr     * @param int $rownumber current row number
42ded78afbSAndreas Gohr     * @param string $string column value
43ded78afbSAndreas Gohr     * @param string $separator separator added between values
44ded78afbSAndreas Gohr     */
45ded78afbSAndreas Gohr    public static function GroupConcatStep(&$context, $rownumber, $string, $separator = ',')
46ded78afbSAndreas Gohr    {
47ded78afbSAndreas Gohr        if (is_null($context)) {
48ded78afbSAndreas Gohr            $context = [
49ded78afbSAndreas Gohr                'sep' => $separator,
50ded78afbSAndreas Gohr                'data' => []
51ded78afbSAndreas Gohr            ];
52ded78afbSAndreas Gohr        }
53ded78afbSAndreas Gohr
54ded78afbSAndreas Gohr        $context['data'][] = $string;
55ded78afbSAndreas Gohr        return $context;
56ded78afbSAndreas Gohr    }
57ded78afbSAndreas Gohr
58ded78afbSAndreas Gohr    /**
59ded78afbSAndreas Gohr     * Aggregation function for SQLite via PDO
60ded78afbSAndreas Gohr     *
61ded78afbSAndreas Gohr     * @link http://devzone.zend.com/article/863-SQLite-Lean-Mean-DB-Machine
62ded78afbSAndreas Gohr     *
63ded78afbSAndreas Gohr     * @param null|array &$context (reference) data as collected in step callback
64ded78afbSAndreas Gohr     * @param int $rownumber number of rows over which the aggregate was performed.
65ded78afbSAndreas Gohr     * @return null|string
66ded78afbSAndreas Gohr     */
67ded78afbSAndreas Gohr    public static function GroupConcatFinalize(&$context, $rownumber)
68ded78afbSAndreas Gohr    {
69ded78afbSAndreas Gohr        if (!is_array($context)) {
70ded78afbSAndreas Gohr            return null;
71ded78afbSAndreas Gohr        }
72ded78afbSAndreas Gohr        $context['data'] = array_unique($context['data']);
73ded78afbSAndreas Gohr        if (empty($context['data'][0])) {
74ded78afbSAndreas Gohr            return null;
75ded78afbSAndreas Gohr        }
76*a7a40fb2SAnna Dabrowska        return implode($context['sep'], $context['data']);
77ded78afbSAndreas Gohr    }
78ded78afbSAndreas Gohr
79ded78afbSAndreas Gohr
80ded78afbSAndreas Gohr    /**
818da7d805SAndreas Gohr     * Callback checks the permissions for the current user
828da7d805SAndreas Gohr     *
838da7d805SAndreas Gohr     * This function is registered as a SQL function named GETACCESSLEVEL
848da7d805SAndreas Gohr     *
858da7d805SAndreas Gohr     * @param string $pageid page ID (needs to be resolved and cleaned)
868da7d805SAndreas Gohr     * @return int permission level
878da7d805SAndreas Gohr     */
888da7d805SAndreas Gohr    public static function getAccessLevel($pageid)
898da7d805SAndreas Gohr    {
90da6f67b7SAndreas Gohr        global $auth;
91da6f67b7SAndreas Gohr        if (!$auth) return AUTH_DELETE;
92da6f67b7SAndreas Gohr
938da7d805SAndreas Gohr        static $aclcache = [];
948da7d805SAndreas Gohr
958da7d805SAndreas Gohr        if (isset($aclcache[$pageid])) {
968da7d805SAndreas Gohr            return $aclcache[$pageid];
978da7d805SAndreas Gohr        }
988da7d805SAndreas Gohr
998da7d805SAndreas Gohr        if (isHiddenPage($pageid)) {
1008da7d805SAndreas Gohr            $acl = AUTH_NONE;
1018da7d805SAndreas Gohr        } else {
1028da7d805SAndreas Gohr            $acl = auth_quickaclcheck($pageid);
1038da7d805SAndreas Gohr        }
1048da7d805SAndreas Gohr        $aclcache[$pageid] = $acl;
1058da7d805SAndreas Gohr        return $acl;
1068da7d805SAndreas Gohr    }
1078da7d805SAndreas Gohr
1088da7d805SAndreas Gohr    /**
1098da7d805SAndreas Gohr     * Wrapper around page_exists() with static caching
1108da7d805SAndreas Gohr     *
1118da7d805SAndreas Gohr     * This function is registered as a SQL function named PAGEEXISTS
1128da7d805SAndreas Gohr     *
1138da7d805SAndreas Gohr     * @param string $pageid
1148da7d805SAndreas Gohr     * @return int 0|1
1158da7d805SAndreas Gohr     */
1168da7d805SAndreas Gohr    public static function pageExists($pageid)
1178da7d805SAndreas Gohr    {
1188da7d805SAndreas Gohr        static $cache = [];
1198da7d805SAndreas Gohr        if (!isset($cache[$pageid])) {
1208da7d805SAndreas Gohr            $cache[$pageid] = page_exists($pageid);
1218da7d805SAndreas Gohr        }
1228da7d805SAndreas Gohr        return (int)$cache[$pageid];
1238da7d805SAndreas Gohr    }
1248da7d805SAndreas Gohr
1258da7d805SAndreas Gohr    /**
1268da7d805SAndreas Gohr     * Match a regular expression against a value
1278da7d805SAndreas Gohr     *
1288da7d805SAndreas Gohr     * This function is registered as a SQL function named REGEXP
1298da7d805SAndreas Gohr     *
1308da7d805SAndreas Gohr     * @param string $regexp
1318da7d805SAndreas Gohr     * @param string $value
1328da7d805SAndreas Gohr     * @return bool
1338da7d805SAndreas Gohr     */
1348da7d805SAndreas Gohr    public static function regExp($regexp, $value)
1358da7d805SAndreas Gohr    {
1368da7d805SAndreas Gohr        $regexp = addcslashes($regexp, '/');
1378da7d805SAndreas Gohr        return (bool)preg_match('/' . $regexp . '/u', $value);
1388da7d805SAndreas Gohr    }
1398da7d805SAndreas Gohr
1408da7d805SAndreas Gohr    /**
1418da7d805SAndreas Gohr     * Resolves a page ID (relative namespaces, plurals etc)
1428da7d805SAndreas Gohr     *
1438da7d805SAndreas Gohr     * This function is registered as a SQL function named RESOLVEPAGE
1448da7d805SAndreas Gohr     *
1458da7d805SAndreas Gohr     * @param string $page The page ID to resolve
1468da7d805SAndreas Gohr     * @param string $context The page ID (not namespace!) to resolve the page with
1478da7d805SAndreas Gohr     * @return null|string
1488da7d805SAndreas Gohr     */
1498da7d805SAndreas Gohr    public static function resolvePage($page, $context)
1508da7d805SAndreas Gohr    {
1518da7d805SAndreas Gohr        if (is_null($page)) return null;
1528da7d805SAndreas Gohr        if (is_null($context)) return cleanID($page);
1538da7d805SAndreas Gohr
1548da7d805SAndreas Gohr        $ns = getNS($context);
1558da7d805SAndreas Gohr        resolve_pageid($ns, $page, $exists);
1568da7d805SAndreas Gohr        return $page;
1578da7d805SAndreas Gohr    }
1588da7d805SAndreas Gohr}
159