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