Check-in [d3026e7ff2]
Many hyperlinks are disabled.
Use anonymous login
to enable hyperlinks.
Overview
Comment: | Move db() and some utility code into include script. (So extroot scripts are no longer standalone; still better than the exhaustive duplication.) |
---|---|
Timelines: | family | ancestors | descendants | both | trunk |
Files: | files | file ages | folders |
SHA1: |
d3026e7ff28cda605fa3477026d6e90b |
User & Date: | mario 2021-10-19 15:43:44 |
Context
2021-10-19
| ||
15:43 | Enable saving check-in: 105698b631 user: mario tags: trunk | |
15:43 | Move db() and some utility code into include script. (So extroot scripts are no longer standalone; still better than the exhaustive duplication.) check-in: d3026e7ff2 user: mario tags: trunk | |
05:07 | Change cat/type: check-in: 3259bf6c4e user: mario tags: trunk | |
Changes
Added extroot/fossil_common.php.
> > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > | 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 | <?php # encoding: utf-8 # api: php # type: functions # category: database # title: Fossil utility code # description: database and EXTCGI helper code # version: 0.1 # state: alpha # config: - # # Combines database and IO utility code for extroot/ scripts. # # ยท db("SELECT * FROM config WHERE name=?", ["col"]) # ยท get_config("project-description", "โฆ") # ยท h("html context output") # ยท is_admin() # ยท config(meta($fn)) - PMD # #-- init ini_set("display_errors", !empty($_REQUEST["dbg"])); $_SERVER["FOSSIL_SELF"] = "https://$_SERVER[SERVER_NAME]$_SERVER[FOSSIL_URI]/"; /** * Database query shorthand. (Using active fossil repository.) * * @param string $sql Query with placeholders * @param array $params Bound parameters * @param bool $fetch Immediate ->fetchAll() * @return array|PDOStatement|PDO */ function db($sql="", $params=[], $fetch=TRUE) { static $db; if (empty($db)) { if (!preg_match("~^/\w[/\w.-]+\w\.(fs?l?|fossil|sqlite)$~", $_SERVER["FOSSIL_REPOSITORY"])) { die("db(): FOSSIL_REPOSITORY doesn't look right. Abort."); } $db = new PDO("sqlite::memory:"); $db->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_WARNING); $db->query("ATTACH DATABASE '$_SERVER[FOSSIL_REPOSITORY]' AS 'repo'"); } if ($params) { $stmt = $db->prepare($sql); $stmt->execute($params); return $fetch ? $stmt->fetchAll(PDO::FETCH_ASSOC) : $stmt; } elseif ($sql) { return $db->query($sql)->fetchAll(PDO::FETCH_ASSOC); } else { return $db; } } /** * Query fossil `config` table. * * @param string $name Option * @param array $default Fallback * @return string */ function get_config($name, $default) { $r = db("SELECT value FROM config WHERE name=?", [$name]); return $r ? $r[0]["value"] : $default; } /** * HTML escape shorthand (for interpolation {$h(expr)} in strings). * * @param string $s Raw string * @return string */ function h($s) { return htmlspecialchars($s, ENT_QUOTES, "utf-8"); } $h = "h"; /** * Test if active user had admin "s" capabilities. * * @return bool */ function is_admin() { return strpos($_SERVER["FOSSIL_CAPABILITIES"], "s") !== false; } /** * Extract PMD (plugin meta data) from file. * Multilang, pluginconf-derived, but still fairly crude. * * @param string $fn Source file * @return array */ function meta($fn) { $src = file_get_contents($fn, false, NULL, 0, 4096); $src = preg_replace("~^#!.+|\R~", "\n", $src); # clean CRLF / shebang preg_match_all("~(^\h{0,4}(#|//|/?\*).*\n)+~m", $src, $uu); # get comments (โ not consecutive) $src = preg_replace("~^\h{0,4}(#|//|/?\*)\h{0,3}\r*~m", "", implode("", $uu[0])); # strip any #// prefix preg_match_all("~^([\w-]+):(.*$\\n(?:(?![\w-]+:).+$\\n)*)~m", $src, $uu); # extract fields (multiline) $r = array_combine($uu[1], $uu[2]); array_change_key_case($r); # โ still unmapped hyphens if (count($doc = preg_split("~\R\h*\R~", trim($src), 2)) == 2) { ; # comment block $r["__doc__"] = $doc[1]; } return $r; } /** * PMD config: list extraction. * * @param array $meta From meta() * @return array */ function config($meta, $r=[]) { if (empty($meta["config"])) { return []; } preg_match_all("~\{ (.+?) \} | \< (.+?) \>~x", $meta["config"], $def); # iterate over each {โฆ} block foreach (array_merge($def[1], $def[2]) as $row) if ($row) { preg_match_all("~ [\"':$]?(\w+)[\"']? \s*[:=]+\s* (?: \"([^\"]*)\" | '([^']*)' | ([^,]*) )~x", $row, $kv, PREG_SET_ORDER); $opt = []; # visit each key:value pair foreach ($kv as $f) { $f = array_values(array_filter($f, "strlen")); $opt[$f[1]] = isset($f[2]) ? $f[2] : ""; } $r[] = $opt; } return $r; } |