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