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_DISTINCT', 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 global $auth; 92 if (!$auth) return AUTH_DELETE; 93 94 static $aclcache = []; 95 96 if (isset($aclcache[$pageid])) { 97 return $aclcache[$pageid]; 98 } 99 100 if (isHiddenPage($pageid)) { 101 $acl = AUTH_NONE; 102 } else { 103 $acl = auth_quickaclcheck($pageid); 104 } 105 $aclcache[$pageid] = $acl; 106 return $acl; 107 } 108 109 /** 110 * Wrapper around page_exists() with static caching 111 * 112 * This function is registered as a SQL function named PAGEEXISTS 113 * 114 * @param string $pageid 115 * @return int 0|1 116 */ 117 public static function pageExists($pageid) 118 { 119 static $cache = []; 120 if (!isset($cache[$pageid])) { 121 $cache[$pageid] = page_exists($pageid); 122 123 } 124 return (int)$cache[$pageid]; 125 } 126 127 /** 128 * Match a regular expression against a value 129 * 130 * This function is registered as a SQL function named REGEXP 131 * 132 * @param string $regexp 133 * @param string $value 134 * @return bool 135 */ 136 public static function regExp($regexp, $value) 137 { 138 $regexp = addcslashes($regexp, '/'); 139 return (bool)preg_match('/' . $regexp . '/u', $value); 140 } 141 142 /** 143 * Resolves a page ID (relative namespaces, plurals etc) 144 * 145 * This function is registered as a SQL function named RESOLVEPAGE 146 * 147 * @param string $page The page ID to resolve 148 * @param string $context The page ID (not namespace!) to resolve the page with 149 * @return null|string 150 */ 151 public static function resolvePage($page, $context) 152 { 153 if (is_null($page)) return null; 154 if (is_null($context)) return cleanID($page); 155 156 $ns = getNS($context); 157 resolve_pageid($ns, $page, $exists); 158 return $page; 159 } 160 161} 162