Blame
Date:
Mon Feb 21 05:02:01 2022 UTC
Message:
Daily backup
001
2021-12-17
jrmu
<?php if (!defined('PmWiki')) exit();
002
2021-12-17
jrmu
/* Copyright 2007-2019 Patrick R. Michaud (pmichaud@pobox.com)
003
2021-12-17
jrmu
This file is part of PmWiki; you can redistribute it and/or modify
004
2021-12-17
jrmu
it under the terms of the GNU General Public License as published
005
2021-12-17
jrmu
by the Free Software Foundation; either version 2 of the License, or
006
2021-12-17
jrmu
(at your option) any later version. See pmwiki.php for full details.
007
2021-12-17
jrmu
008
2021-12-17
jrmu
This script implements "markup expressions" -- a method to
009
2021-12-17
jrmu
do simple computations and manipulations from markup. The
010
2021-12-17
jrmu
generic form of a markup expression is "{(func arg1 arg2)}",
011
2021-12-17
jrmu
where the named function (held in the $MarkupExpr array)
012
2021-12-17
jrmu
is called with arg1 and arg2 as arguments.
013
2021-12-17
jrmu
014
2021-12-17
jrmu
Markup expressions can be nested. For example, to strip
015
2021-12-17
jrmu
off the first five characters and convert the remainder to
016
2021-12-17
jrmu
lowercase, an author can write:
017
2021-12-17
jrmu
018
2021-12-17
jrmu
{(tolower (substr "HELLOWORLD" 5))} # produces "world"
019
2021-12-17
jrmu
020
2021-12-17
jrmu
Some "built-in" expressions defined by this recipe include:
021
2021-12-17
jrmu
substr - extract a portion of a string
022
2021-12-17
jrmu
ftime - date/time formatting
023
2021-12-17
jrmu
strlen - length of a string
024
2021-12-17
jrmu
rand - generate a random number
025
2021-12-17
jrmu
pagename - build a pagename from a string
026
2021-12-17
jrmu
toupper - convert string to uppercase
027
2021-12-17
jrmu
tolower - convert string to lowercase
028
2021-12-17
jrmu
ucfirst - convert first character to uppercase
029
2021-12-17
jrmu
ucwords - convert first character of each word to uppercase
030
2021-12-17
jrmu
asspaced - spaceformatting of wikiwords
031
2021-12-17
jrmu
032
2021-12-17
jrmu
Custom expressions may be added by other recipes by adding
033
2021-12-17
jrmu
entries into the $MarkupExpr array. Each entry's key is
034
2021-12-17
jrmu
the name of the function, the value is the code to be evaluated
035
2021-12-17
jrmu
for that function (similar to the way $FmtPV works). By default,
036
2021-12-17
jrmu
any arguments for the expression are placed into the $args array:
037
2021-12-17
jrmu
038
2021-12-17
jrmu
## expressions like {(myfunc foo bar)}
039
2021-12-17
jrmu
$MarkupExpr['myfunc'] = 'myfunc($args[0], $args[1])';
040
2021-12-17
jrmu
041
2021-12-17
jrmu
The expression arguments are parsed using ParseArgs(), and the
042
2021-12-17
jrmu
result of this parsing is available through the $argp array:
043
2021-12-17
jrmu
044
2021-12-17
jrmu
## expressions like {(myfunc fmt=foo output=bar)}
045
2021-12-17
jrmu
$MarkupExpr['myfunc'] = 'myfunc($argp["fmt"], $argp["output"])';
046
2021-12-17
jrmu
047
2021-12-17
jrmu
Finally, if the code in $MarkupExpr contains '$params', then
048
2021-12-17
jrmu
it is executed directly without any preprocessing into arguments,
049
2021-12-17
jrmu
and $params contains the entire argument string. Note that $params
050
2021-12-17
jrmu
may contain escaped values representing quoted arguments and
051
2021-12-17
jrmu
results of other expressions; these values may be un-escaped
052
2021-12-17
jrmu
by using "preg_replace_callback($rpat, 'cb_expandkpv', $params)".
053
2021-12-17
jrmu
054
2021-12-17
jrmu
Script maintained by Petko YOTOV www.pmwiki.org/petko
055
2021-12-17
jrmu
*/
056
2021-12-17
jrmu
Markup('{(', '>{$var}',
057
2021-12-17
jrmu
'/\\{(\\(\\w+\\b.*?\\))\\}/',
058
2021-12-17
jrmu
"MarkupMarkupExpression");
059
2021-12-17
jrmu
060
2021-12-17
jrmu
function MarkupMarkupExpression($m) {
061
2021-12-17
jrmu
extract($GLOBALS["MarkupToHTML"]); # get $pagename
062
2021-12-17
jrmu
return MarkupExpression($pagename, $m[1]);
063
2021-12-17
jrmu
}
064
2021-12-17
jrmu
065
2021-12-17
jrmu
SDVA($MarkupExpr, array(
066
2021-12-17
jrmu
'substr' => 'call_user_func_array("substr", $args)',
067
2021-12-17
jrmu
'strlen' => 'strlen($args[0])',
068
2021-12-17
jrmu
'ftime' => 'ME_ftime(@$args[0], @$args[1], $argp)',
069
2021-12-17
jrmu
'rand' => '($args) ? rand($args[0], $args[1]) : rand()',
070
2021-12-17
jrmu
'ucfirst' => 'ucfirst($args[0])',
071
2021-12-17
jrmu
'ucwords' => 'ucwords($args[0])',
072
2021-12-17
jrmu
'tolower' => 'strtolower($args[0])',
073
2021-12-17
jrmu
'toupper' => 'strtoupper($args[0])',
074
2021-12-17
jrmu
'mod' => '0 + (intval($args[0]) % intval($args[1]))',
075
2021-12-17
jrmu
'asspaced' => '$GLOBALS["AsSpacedFunction"]($args[0])',
076
2021-12-17
jrmu
'pagename' => 'MakePageName($pagename, preg_replace_callback($rpat, "cb_expandkpv", $params))',
077
2021-12-17
jrmu
));
078
2021-12-17
jrmu
079
2021-12-17
jrmu
function cb_keep_m0_p($m) { return Keep($m[0],'P'); }
080
2021-12-17
jrmu
function cb_keep_m2_p($m) { return Keep($m[2],'P'); }
081
2021-12-17
jrmu
082
2021-12-17
jrmu
function MarkupExpression($pagename, $expr) {
083
2021-12-17
jrmu
global $KeepToken, $KPV, $MarkupExpr;
084
2021-12-17
jrmu
$rpat = "/$KeepToken(\\d+P)$KeepToken/";
085
2021-12-17
jrmu
086
2021-12-17
jrmu
$expr = preg_replace_callback('/([\'"])(.*?)\\1/','cb_keep_m2_p', $expr);
087
2021-12-17
jrmu
$expr = preg_replace_callback('/\\(\\W/', 'cb_keep_m0_p', $expr);
088
2021-12-17
jrmu
while (preg_match('/\\((\\w+)(\\s[^()]*)?\\)/', $expr, $match)) {
089
2021-12-17
jrmu
list($repl, $func, $params) = $match;
090
2021-12-17
jrmu
$code = @$MarkupExpr[$func];
091
2021-12-17
jrmu
## if not a valid function, save this string as-is and exit
092
2021-12-17
jrmu
if (!$code) break;
093
2021-12-17
jrmu
## if the code uses '$params', we just evaluate directly
094
2021-12-17
jrmu
if (strpos($code, '$params') !== false) {
095
2021-12-17
jrmu
$out = eval("return ({$code});");
096
2021-12-17
jrmu
if ($expr == $repl) { $expr = $out; break; }
097
2021-12-17
jrmu
$expr = str_replace($repl, $out, $expr);
098
2021-12-17
jrmu
continue;
099
2021-12-17
jrmu
}
100
2021-12-17
jrmu
## otherwise, we parse arguments into $args before evaluating
101
2021-12-17
jrmu
$argp = ParseArgs($params);
102
2021-12-17
jrmu
$x = $argp['#']; $args = array();
103
2021-12-17
jrmu
while ($x) {
104
2021-12-17
jrmu
list($k, $v) = array_splice($x, 0, 2);
105
2021-12-17
jrmu
if ($k == '' || $k == '+' || $k == '-')
106
2021-12-17
jrmu
$args[] = $k.preg_replace_callback($rpat, 'cb_expandkpv', $v);
107
2021-12-17
jrmu
}
108
2021-12-17
jrmu
## fix any quoted arguments
109
2021-12-17
jrmu
foreach ($argp as $k => $v)
110
2021-12-17
jrmu
if (!is_array($v)) $argp[$k] = preg_replace_callback($rpat, 'cb_expandkpv', $v);
111
2021-12-17
jrmu
$out = eval("return ({$code});");
112
2021-12-17
jrmu
if ($expr == $repl) { $expr = $out; break; }
113
2021-12-17
jrmu
$expr = str_replace($repl, Keep($out, 'P'), $expr);
114
2021-12-17
jrmu
}
115
2021-12-17
jrmu
return preg_replace_callback($rpat, 'cb_expandkpv', $expr);
116
2021-12-17
jrmu
}
117
2021-12-17
jrmu
118
2021-12-17
jrmu
## ME_ftime handles {(ftime ...)} expressions.
119
2021-12-17
jrmu
##
120
2021-12-17
jrmu
function ME_ftime($arg0 = '', $arg1 = '', $argp = NULL) {
121
2021-12-17
jrmu
global $TimeFmt, $Now, $FTimeFmt;
122
2021-12-17
jrmu
if (@$argp['fmt']) $fmt = $argp['fmt'];
123
2021-12-17
jrmu
else if (strpos($arg0, '%') !== false) { $fmt = $arg0; $arg0 = $arg1; }
124
2021-12-17
jrmu
else if (strpos($arg1, '%') !== false) $fmt = $arg1;
125
2021-12-17
jrmu
## determine the timestamp
126
2021-12-17
jrmu
if (isset($argp['when'])) list($time, $x) = DRange($argp['when']);
127
2021-12-17
jrmu
else if ($arg0 > '') list($time, $x) = DRange($arg0);
128
2021-12-17
jrmu
else $time = $Now;
129
2021-12-17
jrmu
$dtz = function_exists('date_default_timezone_get') # tz=Europe/Paris
130
2021-12-17
jrmu
? date_default_timezone_get() : false;
131
2021-12-17
jrmu
if (@$argp['tz'] && $dtz) @date_default_timezone_set($argp['tz']);
132
2021-12-17
jrmu
$dloc = setlocale(LC_TIME, 0);
133
2021-12-17
jrmu
if(@$argp['locale']) # locale=fr_FR.utf8,bg_BG,C
134
2021-12-17
jrmu
setlocale(LC_TIME, preg_split('/[, ]+/', $argp['locale'], null, PREG_SPLIT_NO_EMPTY));
135
2021-12-17
jrmu
if (@$fmt == '') { SDV($FTimeFmt, $TimeFmt); $fmt = $FTimeFmt; }
136
2021-12-17
jrmu
## make sure we have %F available for ISO dates
137
2021-12-17
jrmu
$fmt = str_replace(array('%F', '%s'), array('%Y-%m-%d', $time), $fmt);
138
2021-12-17
jrmu
$ret = strftime($fmt, $time);
139
2021-12-17
jrmu
if (@$argp['tz'] && $dtz) date_default_timezone_set($dtz);
140
2021-12-17
jrmu
if(@$argp['locale']) setlocale(LC_TIME, $dloc);
141
2021-12-17
jrmu
return $ret;
142
2021-12-17
jrmu
}
143
2021-12-17
jrmu
IRCNow