Blame
Date:
Fri Mar 11 05:00:19 2022 UTC
Message:
Daily backup
001
2021-12-17
jrmu
<?php if (!defined('PmWiki')) exit();
002
2021-12-17
jrmu
/* Copyright 2004-2020 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 (:pagelist:) and friends -- it's one
009
2021-12-17
jrmu
of the nastiest scripts you'll ever encounter. Part of the reason
010
2021-12-17
jrmu
for this is that page listings are so powerful and flexible, so
011
2021-12-17
jrmu
that adds complexity. They're also expensive, so we have to
012
2021-12-17
jrmu
optimize them wherever we can.
013
2021-12-17
jrmu
014
2021-12-17
jrmu
The core function is FmtPageList(), which will generate a
015
2021-12-17
jrmu
listing according to a wide variety of options. FmtPageList takes
016
2021-12-17
jrmu
care of initial option processing, and then calls a "FPL"
017
2021-12-17
jrmu
(format page list) function to obtain the formatted output.
018
2021-12-17
jrmu
The FPL function is chosen by the 'fmt=' option to (:pagelist:).
019
2021-12-17
jrmu
020
2021-12-17
jrmu
Each FPL function calls MakePageList() to obtain the list
021
2021-12-17
jrmu
of pages, formats the list somehow, and returns the results
022
2021-12-17
jrmu
to FmtPageList. FmtPageList then returns the output to
023
2021-12-17
jrmu
the caller, and calls Keep() (preserves HTML) or PRR() (re-evaluate
024
2021-12-17
jrmu
as markup) as appropriate for the output being returned.
025
2021-12-17
jrmu
026
2021-12-17
jrmu
Script maintained by Petko YOTOV www.pmwiki.org/petko
027
2021-12-17
jrmu
*/
028
2021-12-17
jrmu
029
2021-12-17
jrmu
## $PageIndexFile is the index file for term searches and link= option
030
2021-12-17
jrmu
if (IsEnabled($EnablePageIndex, 1)) {
031
2021-12-17
jrmu
SDV($PageIndexFile, "$WorkDir/.pageindex");
032
2021-12-17
jrmu
$EditFunctions[] = 'PostPageIndex';
033
2021-12-17
jrmu
}
034
2021-12-17
jrmu
035
2021-12-17
jrmu
SDV($StrFoldFunction, 'strtolower');
036
2021-12-17
jrmu
SDV($PageListSortCmpFunction, 'strcasecmp');
037
2021-12-17
jrmu
038
2021-12-17
jrmu
## $SearchPatterns holds patterns for list= option
039
2021-12-17
jrmu
SDV($SearchPatterns['all'], array());
040
2021-12-17
jrmu
SDVA($SearchPatterns['normal'], array(
041
2021-12-17
jrmu
'recent' => '!\.(All)?Recent(Changes|Uploads)$!',
042
2021-12-17
jrmu
'group' => '!\.Group(Print)?(Header|Footer|Attributes)$!',
043
2021-12-17
jrmu
'self' => str_replace('.', '\\.', "!^$pagename$!")));
044
2021-12-17
jrmu
045
2021-12-17
jrmu
# The list=grouphomes search pattern requires to scan
046
2021-12-17
jrmu
# all PageStore directories to get the pagenames.
047
2021-12-17
jrmu
# This takes (a tiny amoint of) time, so we only do it when needed.
048
2021-12-17
jrmu
function EnablePageListGroupHomes() {
049
2021-12-17
jrmu
global $SearchPatterns;
050
2021-12-17
jrmu
if(isset($SearchPatterns['grouphomes'])) return;
051
2021-12-17
jrmu
052
2021-12-17
jrmu
$groups = $homes = array();
053
2021-12-17
jrmu
foreach(ListPages() as $pn) {
054
2021-12-17
jrmu
list($g, $n) = explode(".", $pn);
055
2021-12-17
jrmu
@$groups[$g]++;
056
2021-12-17
jrmu
}
057
2021-12-17
jrmu
foreach($groups as $g => $cnt) {
058
2021-12-17
jrmu
$homes[] = MakePageName("$g.$g", "$g.");
059
2021-12-17
jrmu
}
060
2021-12-17
jrmu
$SearchPatterns['grouphomes'] = array('/^('.implode('|', $homes).')$/');
061
2021-12-17
jrmu
}
062
2021-12-17
jrmu
063
2021-12-17
jrmu
## $FPLFormatOpt is a list of options associated with fmt=
064
2021-12-17
jrmu
## values. 'default' is used for any undefined values of fmt=.
065
2021-12-17
jrmu
SDVA($FPLFormatOpt, array(
066
2021-12-17
jrmu
'default' => array('fn' => 'FPLTemplate', 'fmt' => '#default'),
067
2021-12-17
jrmu
'bygroup' => array('fn' => 'FPLTemplate', 'template' => '#bygroup',
068
2021-12-17
jrmu
'class' => 'fplbygroup'),
069
2021-12-17
jrmu
'simple' => array('fn' => 'FPLTemplate', 'template' => '#simple',
070
2021-12-17
jrmu
'class' => 'fplsimple'),
071
2021-12-17
jrmu
'group' => array('fn' => 'FPLTemplate', 'template' => '#group',
072
2021-12-17
jrmu
'class' => 'fplgroup'),
073
2021-12-17
jrmu
'title' => array('fn' => 'FPLTemplate', 'template' => '#title',
074
2021-12-17
jrmu
'class' => 'fpltitle', 'order' => 'title'),
075
2021-12-17
jrmu
'count' => array('fn' => 'FPLCountA'),
076
2021-12-17
jrmu
));
077
2021-12-17
jrmu
078
2021-12-17
jrmu
SDV($SearchResultsFmt, "<div class='wikisearch'>\$[SearchFor]
079
2021-12-17
jrmu
<div class='vspace'></div>\$MatchList
080
2021-12-17
jrmu
<div class='vspace'></div>\$[SearchFound]</div>");
081
2021-12-17
jrmu
SDV($SearchQuery, str_replace('$', '&#036;',
082
2021-12-17
jrmu
PHSC(stripmagic(@$_REQUEST['q']), ENT_NOQUOTES)));
083
2021-12-17
jrmu
XLSDV('en', array(
084
2021-12-17
jrmu
'SearchFor' => 'Results of search for <em>$Needle</em>:',
085
2021-12-17
jrmu
'SearchFound' =>
086
2021-12-17
jrmu
'$MatchCount pages found out of $MatchSearched pages searched.'));
087
2021-12-17
jrmu
088
2021-12-17
jrmu
SDV($PageListArgPattern, '((?:\\$:?)?\\w[-\\w]*)[:=]');
089
2021-12-17
jrmu
090
2021-12-17
jrmu
Markup('pagelist', 'directives',
091
2021-12-17
jrmu
'/\\(:pagelist(\\s+.*?)?:\\)/i', "MarkupPageList");
092
2021-12-17
jrmu
Markup('searchbox', 'directives',
093
2021-12-17
jrmu
'/\\(:searchbox(\\s.*?)?:\\)/', "MarkupPageList");
094
2021-12-17
jrmu
Markup('searchresults', 'directives',
095
2021-12-17
jrmu
'/\\(:searchresults(\\s+.*?)?:\\)/i', "MarkupPageList");
096
2021-12-17
jrmu
097
2021-12-17
jrmu
function MarkupPageList($m) {
098
2021-12-17
jrmu
extract($GLOBALS["MarkupToHTML"]); # get $pagename, $markupid
099
2021-12-17
jrmu
switch ($markupid) {
100
2021-12-17
jrmu
case 'pagelist':
101
2021-12-17
jrmu
return FmtPageList('$MatchList', $pagename, array('o' => $m[1].' '));
102
2021-12-17
jrmu
case 'searchbox':
103
2021-12-17
jrmu
return SearchBox($pagename,
104
2021-12-17
jrmu
ParseArgs(@$m[1], $GLOBALS['PageListArgPattern']));
105
2021-12-17
jrmu
case 'searchresults':
106
2021-12-17
jrmu
return FmtPageList($GLOBALS['SearchResultsFmt'],
107
2021-12-17
jrmu
$pagename, array('req' => 1, 'request'=>1, 'o' => @$m[1]));
108
2021-12-17
jrmu
}
109
2021-12-17
jrmu
}
110
2021-12-17
jrmu
111
2021-12-17
jrmu
# called from PageListIf and FPLExpandItemVars
112
2021-12-17
jrmu
class cb_pl_expandvars extends PPRC {
113
2021-12-17
jrmu
function pl_expandvars($m) {
114
2021-12-17
jrmu
$pn = $this->vars;
115
2021-12-17
jrmu
return PVSE(PageVar($pn, $m[2], $m[1]));
116
2021-12-17
jrmu
}
117
2021-12-17
jrmu
}
118
2021-12-17
jrmu
119
2021-12-17
jrmu
SDV($SaveAttrPatterns['/\\(:(searchresults|pagelist)(\\s+.*?)?:\\)/i'], ' ');
120
2021-12-17
jrmu
121
2021-12-17
jrmu
SDV($HandleActions['search'], 'HandleSearchA');
122
2021-12-17
jrmu
SDV($HandleAuth['search'], 'read');
123
2021-12-17
jrmu
SDV($ActionTitleFmt['search'], '| $[Search Results]');
124
2021-12-17
jrmu
125
2021-12-17
jrmu
SDVA($PageListFilters, array(
126
2021-12-17
jrmu
'PageListCache' => 80,
127
2021-12-17
jrmu
'PageListProtect' => 90,
128
2021-12-17
jrmu
'PageListSources' => 100,
129
2021-12-17
jrmu
'PageListPasswords' => 120,
130
2021-12-17
jrmu
'PageListIf' => 140,
131
2021-12-17
jrmu
'PageListTermsTargets' => 160,
132
2021-12-17
jrmu
'PageListVariables' => 180,
133
2021-12-17
jrmu
'PageListSort' => 900,
134
2021-12-17
jrmu
));
135
2021-12-17
jrmu
136
2021-12-17
jrmu
function CorePageListSorts($x, $y, $o) {
137
2021-12-17
jrmu
global $PCache;
138
2021-12-17
jrmu
if($o == 'title')
139
2021-12-17
jrmu
return @strcasecmp($PCache[$x]['=title'],$PCache[$y]['=title']);
140
2021-12-17
jrmu
return @($PCache[$x][$o]-$PCache[$y][$o]);
141
2021-12-17
jrmu
}
142
2021-12-17
jrmu
foreach(array('random', 'size', 'time', 'ctime', 'title') as $o)
143
2021-12-17
jrmu
SDV($PageListSortCmp[$o], 'CorePageListSorts');
144
2021-12-17
jrmu
145
2021-12-17
jrmu
define('PAGELIST_PRE' , 1);
146
2021-12-17
jrmu
define('PAGELIST_ITEM', 2);
147
2021-12-17
jrmu
define('PAGELIST_POST', 4);
148
2021-12-17
jrmu
149
2021-12-17
jrmu
## SearchBox generates the output of the (:searchbox:) markup.
150
2021-12-17
jrmu
## If $SearchBoxFmt is defined, that is used, otherwise a searchbox
151
2021-12-17
jrmu
## is generated. Options include group=, size=, label=.
152
2021-12-17
jrmu
function SearchBox($pagename, $opt) {
153
2021-12-17
jrmu
global $SearchBoxFmt, $SearchBoxInputType, $SearchBoxOpt, $SearchQuery, $EnablePathInfo;
154
2021-12-17
jrmu
if (isset($SearchBoxFmt)) return Keep(FmtPageName($SearchBoxFmt, $pagename));
155
2021-12-17
jrmu
SDVA($SearchBoxOpt, array('size' => '40',
156
2021-12-17
jrmu
'label' => FmtPageName('$[Search]', $pagename),
157
2021-12-17
jrmu
'value' => str_replace("'", "&#039;", $SearchQuery)));
158
2021-12-17
jrmu
$opt = array_merge((array)$SearchBoxOpt, @$_GET, (array)$opt);
159
2021-12-17
jrmu
$opt['action'] = 'search';
160
2021-12-17
jrmu
$target = (@$opt['target'])
161
2021-12-17
jrmu
? MakePageName($pagename, $opt['target']) : $pagename;
162
2021-12-17
jrmu
$opt['n'] = IsEnabled($EnablePathInfo, 0) ? '' : $target;
163
2021-12-17
jrmu
$out = FmtPageName(" class='wikisearch' action='\$PageUrl' method='get'>",
164
2021-12-17
jrmu
$target);
165
2021-12-17
jrmu
foreach($opt as $k => $v) {
166
2021-12-17
jrmu
if ($v == '' || is_array($v)) continue;
167
2021-12-17
jrmu
$v = str_replace("'", "&#039;", $v);
168
2021-12-17
jrmu
$opt[$k] = $v;
169
2021-12-17
jrmu
if(preg_match('/^(q|label|value|size|placeholder|aria-\\w+)$/', $k)) continue;
170
2021-12-17
jrmu
$k = str_replace("'", "&#039;", $k);
171
2021-12-17
jrmu
$out .= "<input type='hidden' name='$k' value='$v' />";
172
2021-12-17
jrmu
}
173
2021-12-17
jrmu
SDV($SearchBoxInputType, 'text');
174
2021-12-17
jrmu
$out .= "<input type='$SearchBoxInputType' name='q' value='{$opt['value']}' ";
175
2021-12-17
jrmu
$attrs = preg_grep('/^(placeholder|aria-\\w+)/', array_keys($opt));
176
2021-12-17
jrmu
foreach ($attrs as $k) $out .= " $k='{$opt[$k]}' ";
177
2021-12-17
jrmu
$out .= " class='inputbox searchbox' size='{$opt['size']}' /><input type='submit'
178
2021-12-17
jrmu
class='inputbutton searchbutton' value='{$opt['label']}' />";
179
2021-12-17
jrmu
return '<form '.Keep($out).'</form>';
180
2021-12-17
jrmu
}
181
2021-12-17
jrmu
182
2021-12-17
jrmu
183
2021-12-17
jrmu
## FmtPageList combines options from markup, request form, and url,
184
2021-12-17
jrmu
## calls the appropriate formatting function, and returns the string.
185
2021-12-17
jrmu
function FmtPageList($outfmt, $pagename, $opt) {
186
2021-12-17
jrmu
global $GroupPattern, $FmtV, $PageListArgPattern,
187
2021-12-17
jrmu
$FPLFormatOpt, $FPLFunctions;
188
2021-12-17
jrmu
# get any form or url-submitted request
189
2021-12-17
jrmu
$rq = PHSC(stripmagic(@$_REQUEST['q']), ENT_NOQUOTES);
190
2021-12-17
jrmu
# build the search string
191
2021-12-17
jrmu
$FmtV['$Needle'] = $opt['o'] . ' ' . $rq;
192
2021-12-17
jrmu
# Handle "group/" at the beginning of the form-submitted request
193
2021-12-17
jrmu
if (preg_match("!^($GroupPattern(\\|$GroupPattern)*)?/!i", $rq, $match)) {
194
2021-12-17
jrmu
$opt['group'] = @$match[1];
195
2021-12-17
jrmu
$rq = substr($rq, strlen(@$match[1])+1);
196
2021-12-17
jrmu
}
197
2021-12-17
jrmu
$opt = array_merge($opt, ParseArgs($opt['o'], $PageListArgPattern));
198
2021-12-17
jrmu
# merge markup options with form and url
199
2021-12-17
jrmu
if (@$opt['request'] && @$_REQUEST) {
200
2021-12-17
jrmu
$rkeys = preg_grep('/^=/', array_keys($_REQUEST), PREG_GREP_INVERT);
201
2021-12-17
jrmu
if ($opt['request'] != '1') {
202
2021-12-17
jrmu
list($incl, $excl) = GlobToPCRE($opt['request']);
203
2021-12-17
jrmu
if ($excl) $rkeys = array_diff($rkeys, preg_grep("/$excl/", $rkeys));
204
2021-12-17
jrmu
if ($incl) $rkeys = preg_grep("/$incl/", $rkeys);
205
2021-12-17
jrmu
}
206
2021-12-17
jrmu
$cleanrequest = array();
207
2021-12-17
jrmu
foreach($rkeys as $k) {
208
2021-12-17
jrmu
$cleanrequest[$k] = stripmagic($_REQUEST[$k]);
209
2021-12-17
jrmu
if(substr($k, 0, 4)=='ptv_') # defined separately in forms
210
2021-12-17
jrmu
$cleanrequest['$:'.substr($k, 4)] = stripmagic($_REQUEST[$k]);
211
2021-12-17
jrmu
}
212
2021-12-17
jrmu
$opt = array_merge($opt, ParseArgs($rq, $PageListArgPattern), $cleanrequest);
213
2021-12-17
jrmu
}
214
2021-12-17
jrmu
215
2021-12-17
jrmu
# non-posted blank search requests return nothing
216
2021-12-17
jrmu
if (@($opt['req'] && !$opt['-'] && !$opt[''] && !$opt['+'] && !$opt['q']))
217
2021-12-17
jrmu
return '';
218
2021-12-17
jrmu
# terms and group to be included and excluded
219
2021-12-17
jrmu
$GLOBALS['SearchIncl'] = array_merge((array)@$opt[''], (array)@$opt['+']);
220
2021-12-17
jrmu
$GLOBALS['SearchExcl'] = (array)@$opt['-'];
221
2021-12-17
jrmu
$GLOBALS['SearchGroup'] = @$opt['group'];
222
2021-12-17
jrmu
$fmt = @$opt['fmt']; if (!$fmt) $fmt = 'default';
223
2021-12-17
jrmu
$fmtopt = @$FPLFormatOpt[$fmt];
224
2021-12-17
jrmu
if (!is_array($fmtopt)) {
225
2021-12-17
jrmu
if ($fmtopt) $fmtopt = array('fn' => $fmtopt);
226
2021-12-17
jrmu
elseif (@$FPLFunctions[$fmt])
227
2021-12-17
jrmu
$fmtopt = array('fn' => $FPLFunctions[$fmt]);
228
2021-12-17
jrmu
else $fmtopt = $FPLFormatOpt['default'];
229
2021-12-17
jrmu
}
230
2021-12-17
jrmu
$fmtfn = @$fmtopt['fn'];
231
2021-12-17
jrmu
if (!is_callable($fmtfn)) $fmtfn = $FPLFormatOpt['default']['fn'];
232
2021-12-17
jrmu
$matches = array();
233
2021-12-17
jrmu
$opt = array_merge($fmtopt, $opt);
234
2021-12-17
jrmu
$out = $fmtfn($pagename, $matches, $opt);
235
2021-12-17
jrmu
$FmtV['$MatchCount'] = count($matches);
236
2021-12-17
jrmu
if ($outfmt != '$MatchList')
237
2021-12-17
jrmu
{ $FmtV['$MatchList'] = $out; $out = FmtPageName($outfmt, $pagename); }
238
2021-12-17
jrmu
if ($out[0] == '<') $out = Keep($out);
239
2021-12-17
jrmu
return PRR($out);
240
2021-12-17
jrmu
}
241
2021-12-17
jrmu
242
2021-12-17
jrmu
243
2021-12-17
jrmu
## MakePageList generates a list of pages using the specifications given
244
2021-12-17
jrmu
## by $opt.
245
2021-12-17
jrmu
function MakePageList($pagename, $opt, $retpages = 1) {
246
2021-12-17
jrmu
global $MakePageListOpt, $PageListFilters, $PCache;
247
2021-12-17
jrmu
248
2021-12-17
jrmu
StopWatch('MakePageList pre');
249
2021-12-17
jrmu
SDVA($MakePageListOpt, array('list' => 'default'));
250
2021-12-17
jrmu
$opt = array_merge((array)$MakePageListOpt, (array)$opt);
251
2021-12-17
jrmu
if (!@$opt['order'] && !@$opt['trail']) $opt['order'] = 'name';
252
2021-12-17
jrmu
$opt['order'] = preg_replace('/[^-\\w:$]+/', ',', @$opt['order']);
253
2021-12-17
jrmu
254
2021-12-17
jrmu
ksort($opt); $opt['=key'] = md5(serialize($opt));
255
2021-12-17
jrmu
256
2021-12-17
jrmu
$itemfilters = array(); $postfilters = array();
257
2021-12-17
jrmu
asort($PageListFilters);
258
2021-12-17
jrmu
$opt['=phase'] = PAGELIST_PRE; $list=array(); $pn=NULL; $page=NULL;
259
2021-12-17
jrmu
foreach($PageListFilters as $fn => $v) {
260
2021-12-17
jrmu
if ($v<0) continue;
261
2021-12-17
jrmu
$ret = $fn($list, $opt, $pagename, $page);
262
2021-12-17
jrmu
if ($ret & PAGELIST_ITEM) $itemfilters[] = $fn;
263
2021-12-17
jrmu
if ($ret & PAGELIST_POST) $postfilters[] = $fn;
264
2021-12-17
jrmu
}
265
2021-12-17
jrmu
266
2021-12-17
jrmu
StopWatch("MakePageList items count=".count($list).", filters=".implode(',',$itemfilters));
267
2021-12-17
jrmu
$opt['=phase'] = PAGELIST_ITEM;
268
2021-12-17
jrmu
$matches = array(); $opt['=readc'] = 0;
269
2021-12-17
jrmu
foreach((array)$list as $pn) {
270
2021-12-17
jrmu
$page = array();
271
2021-12-17
jrmu
foreach((array)$itemfilters as $fn)
272
2021-12-17
jrmu
if (!$fn($list, $opt, $pn, $page)) continue 2;
273
2021-12-17
jrmu
$page['pagename'] = $page['name'] = $pn;
274
2021-12-17
jrmu
PCache($pn, $page);
275
2021-12-17
jrmu
$matches[] = $pn;
276
2021-12-17
jrmu
}
277
2021-12-17
jrmu
$list = $matches;
278
2021-12-17
jrmu
StopWatch("MakePageList post count=".count($list).", readc={$opt['=readc']}");
279
2021-12-17
jrmu
280
2021-12-17
jrmu
$opt['=phase'] = PAGELIST_POST; $pn=NULL; $page=NULL;
281
2021-12-17
jrmu
foreach((array)$postfilters as $fn)
282
2021-12-17
jrmu
$fn($list, $opt, $pagename, $page);
283
2021-12-17
jrmu
284
2021-12-17
jrmu
if ($retpages)
285
2021-12-17
jrmu
for($i=0; $i<count($list); $i++)
286
2021-12-17
jrmu
$list[$i] = &$PCache[$list[$i]];
287
2021-12-17
jrmu
StopWatch('MakePageList end');
288
2021-12-17
jrmu
return $list;
289
2021-12-17
jrmu
}
290
2021-12-17
jrmu
291
2021-12-17
jrmu
292
2021-12-17
jrmu
function PageListProtect(&$list, &$opt, $pn, &$page) {
293
2021-12-17
jrmu
global $EnablePageListProtect;
294
2021-12-17
jrmu
295
2021-12-17
jrmu
switch ($opt['=phase']) {
296
2021-12-17
jrmu
case PAGELIST_PRE:
297
2021-12-17
jrmu
if (!IsEnabled($EnablePageListProtect, 1) && @$opt['readf'] < 1000)
298
2021-12-17
jrmu
return 0;
299
2021-12-17
jrmu
StopWatch("PageListProtect enabled");
300
2021-12-17
jrmu
$opt['=protectexclude'] = array();
301
2021-12-17
jrmu
$opt['=protectsafe'] = (array)@$opt['=protectsafe'];
302
2021-12-17
jrmu
return PAGELIST_ITEM|PAGELIST_POST;
303
2021-12-17
jrmu
304
2021-12-17
jrmu
case PAGELIST_ITEM:
305
2021-12-17
jrmu
if (@$opt['=protectsafe'][$pn]) return 1;
306
2021-12-17
jrmu
$page = RetrieveAuthPage($pn, 'ALWAYS', false, READPAGE_CURRENT);
307
2021-12-17
jrmu
$opt['=readc']++;
308
2021-12-17
jrmu
if (!$page['=auth']['read']) $opt['=protectexclude'][$pn] = 1;
309
2021-12-17
jrmu
if (!$page['=passwd']['read']) $opt['=protectsafe'][$pn] = 1;
310
2021-12-17
jrmu
else NoCache();
311
2021-12-17
jrmu
return 1;
312
2021-12-17
jrmu
313
2021-12-17
jrmu
case PAGELIST_POST:
314
2021-12-17
jrmu
$excl = array_keys($opt['=protectexclude']);
315
2021-12-17
jrmu
$safe = array_keys($opt['=protectsafe']);
316
2021-12-17
jrmu
StopWatch("PageListProtect excluded=" .count($excl)
317
2021-12-17
jrmu
. ", safe=" . count($safe));
318
2021-12-17
jrmu
$list = array_diff($list, $excl);
319
2021-12-17
jrmu
return 1;
320
2021-12-17
jrmu
}
321
2021-12-17
jrmu
}
322
2021-12-17
jrmu
323
2021-12-17
jrmu
324
2021-12-17
jrmu
function PageListSources(&$list, &$opt, $pn, &$page) {
325
2021-12-17
jrmu
global $SearchPatterns;
326
2021-12-17
jrmu
327
2021-12-17
jrmu
StopWatch('PageListSources begin');
328
2021-12-17
jrmu
if ($opt['list'] == 'grouphomes') EnablePageListGroupHomes();
329
2021-12-17
jrmu
## add the list= option to our list of pagename filter patterns
330
2021-12-17
jrmu
$opt['=pnfilter'] = array_merge((array)@$opt['=pnfilter'],
331
2021-12-17
jrmu
(array)@$SearchPatterns[$opt['list']]);
332
2021-12-17
jrmu
333
2021-12-17
jrmu
if (@$opt['group']) $opt['=pnfilter'][] = FixGlob($opt['group'], '$1$2.*');
334
2021-12-17
jrmu
if (@$opt['name']) $opt['=pnfilter'][] = FixGlob($opt['name'], '$1*.$2');
335
2021-12-17
jrmu
336
2021-12-17
jrmu
if (@$opt['trail']) {
337
2021-12-17
jrmu
$trail = ReadTrail($pn, $opt['trail']);
338
2021-12-17
jrmu
$tlist = array();
339
2021-12-17
jrmu
foreach($trail as $tstop) {
340
2021-12-17
jrmu
$n = $tstop['pagename'];
341
2021-12-17
jrmu
$tlist[] = $n;
342
2021-12-17
jrmu
$tstop['parentnames'] = array();
343
2021-12-17
jrmu
PCache($n, $tstop);
344
2021-12-17
jrmu
}
345
2021-12-17
jrmu
foreach($trail as $tstop)
346
2021-12-17
jrmu
$PCache[$tstop['pagename']]['parentnames'][] =
347
2021-12-17
jrmu
@$trail[$tstop['parent']]['pagename'];
348
2021-12-17
jrmu
if (!@$opt['=cached']) $list = MatchPageNames($tlist, $opt['=pnfilter']);
349
2021-12-17
jrmu
} else if (!@$opt['=cached']) $list = ListPages($opt['=pnfilter']);
350
2021-12-17
jrmu
StopWatch("PageListSources end count=".count($list));
351
2021-12-17
jrmu
return 0;
352
2021-12-17
jrmu
}
353
2021-12-17
jrmu
354
2021-12-17
jrmu
355
2021-12-17
jrmu
function PageListPasswords(&$list, &$opt, $pn, &$page) {
356
2021-12-17
jrmu
if ($opt['=phase'] == PAGELIST_PRE)
357
2021-12-17
jrmu
return (@$opt['passwd'] > '' && !@$opt['=cached']) ? PAGELIST_ITEM : 0;
358
2021-12-17
jrmu
359
2021-12-17
jrmu
if (!$page) { $page = ReadPage($pn, READPAGE_CURRENT); $opt['=readc']++; }
360
2021-12-17
jrmu
if (!$page) return 0;
361
2021-12-17
jrmu
return (boolean)preg_grep('/^passwd/', array_keys($page));
362
2021-12-17
jrmu
}
363
2021-12-17
jrmu
364
2021-12-17
jrmu
365
2021-12-17
jrmu
function PageListIf(&$list, &$opt, $pn, &$page) {
366
2021-12-17
jrmu
global $Conditions, $Cursor;
367
2021-12-17
jrmu
368
2021-12-17
jrmu
## See if we have any "if" processing to perform
369
2021-12-17
jrmu
if ($opt['=phase'] == PAGELIST_PRE)
370
2021-12-17
jrmu
return (@$opt['if'] > '') ? PAGELIST_ITEM : 0;
371
2021-12-17
jrmu
372
2021-12-17
jrmu
$condspec = $opt['if'];
373
2021-12-17
jrmu
$Cursor['='] = $pn;
374
2021-12-17
jrmu
$varpat = '\\{([=*]|!?[-\\w.\\/\\x80-\\xff]*)(\\$:?\\w+)\\}';
375
2021-12-17
jrmu
while (preg_match("/$varpat/", $condspec, $match)) {
376
2021-12-17
jrmu
$cb = new cb_pl_expandvars($pn);
377
2021-12-17
jrmu
$condspec = preg_replace_callback("/$varpat/",
378
2021-12-17
jrmu
array($cb, 'pl_expandvars'), $condspec);
379
2021-12-17
jrmu
}
380
2021-12-17
jrmu
if (!preg_match("/^\\s*(!?)\\s*(\\S*)\\s*(.*?)\\s*$/", $condspec, $match))
381
2021-12-17
jrmu
return 0;
382
2021-12-17
jrmu
list($x, $not, $condname, $condparm) = $match;
383
2021-12-17
jrmu
if (!isset($Conditions[$condname])) return 1;
384
2021-12-17
jrmu
$tf = (int)@eval("return ({$Conditions[$condname]});");
385
2021-12-17
jrmu
return (boolean)($tf xor $not);
386
2021-12-17
jrmu
}
387
2021-12-17
jrmu
388
2021-12-17
jrmu
function PageListTermsTargets(&$list, &$opt, $pn, &$page) {
389
2021-12-17
jrmu
global $FmtV;
390
2021-12-17
jrmu
static $reindex = array();
391
2021-12-17
jrmu
$fold = $GLOBALS['StrFoldFunction'];
392
2021-12-17
jrmu
393
2021-12-17
jrmu
switch ($opt['=phase']) {
394
2021-12-17
jrmu
case PAGELIST_PRE:
395
2021-12-17
jrmu
$FmtV['$MatchSearched'] = count($list);
396
2021-12-17
jrmu
$incl = array(); $excl = array();
397
2021-12-17
jrmu
foreach((array)@$opt[''] as $i) { $incl[] = $fold($i); }
398
2021-12-17
jrmu
foreach((array)@$opt['+'] as $i) { $incl[] = $fold($i); }
399
2021-12-17
jrmu
foreach((array)@$opt['-'] as $i) { $excl[] = $fold($i); }
400
2021-12-17
jrmu
401
2021-12-17
jrmu
$indexterms = PageIndexTerms($incl);
402
2021-12-17
jrmu
foreach($incl as $i) {
403
2021-12-17
jrmu
$delim = (!preg_match('/[^\\w\\x80-\\xff]/', $i)) ? '$' : '/';
404
2021-12-17
jrmu
$opt['=inclp'][] = $delim . preg_quote($i,$delim) . $delim . 'i';
405
2021-12-17
jrmu
}
406
2021-12-17
jrmu
if ($excl)
407
2021-12-17
jrmu
$opt['=exclp'][] = '$'.implode('|', array_map('preg_quote',$excl)).'$i';
408
2021-12-17
jrmu
409
2021-12-17
jrmu
if (@$opt['link']) {
410
2021-12-17
jrmu
$link = MakePageName($pn, $opt['link']);
411
2021-12-17
jrmu
$opt['=linkp'] = "/(^|,)$link(,|$)/i";
412
2021-12-17
jrmu
$indexterms[] = " $link ";
413
2021-12-17
jrmu
}
414
2021-12-17
jrmu
415
2021-12-17
jrmu
if (@$opt['=cached']) return 0;
416
2021-12-17
jrmu
if ($indexterms) {
417
2021-12-17
jrmu
StopWatch("PageListTermsTargets begin count=".count($list));
418
2021-12-17
jrmu
$xlist = PageIndexGrep($indexterms, true);
419
2021-12-17
jrmu
$list = array_diff($list, $xlist);
420
2021-12-17
jrmu
StopWatch("PageListTermsTargets end count=".count($list));
421
2021-12-17
jrmu
}
422
2021-12-17
jrmu
423
2021-12-17
jrmu
if (@$opt['=inclp'] || @$opt['=exclp'] || @$opt['=linkp'])
424
2021-12-17
jrmu
return PAGELIST_ITEM|PAGELIST_POST;
425
2021-12-17
jrmu
return 0;
426
2021-12-17
jrmu
427
2021-12-17
jrmu
case PAGELIST_ITEM:
428
2021-12-17
jrmu
if (!$page) { $page = ReadPage($pn, READPAGE_CURRENT); $opt['=readc']++; }
429
2021-12-17
jrmu
if (!$page) return 0;
430
2021-12-17
jrmu
if (@$opt['=linkp'] && !preg_match($opt['=linkp'], @$page['targets']))
431
2021-12-17
jrmu
{ $reindex[] = $pn; return 0; }
432
2021-12-17
jrmu
if (@$opt['=inclp'] || @$opt['=exclp']) {
433
2021-12-17
jrmu
$text = $fold($pn."\n".@$page['targets']."\n".@$page['text']);
434
2021-12-17
jrmu
foreach((array)@$opt['=exclp'] as $i)
435
2021-12-17
jrmu
if (preg_match($i, $text)) return 0;
436
2021-12-17
jrmu
foreach((array)@$opt['=inclp'] as $i)
437
2021-12-17
jrmu
if (!preg_match($i, $text)) {
438
2021-12-17
jrmu
if ($i[0] == '$') $reindex[] = $pn;
439
2021-12-17
jrmu
return 0;
440
2021-12-17
jrmu
}
441
2021-12-17
jrmu
}
442
2021-12-17
jrmu
return 1;
443
2021-12-17
jrmu
444
2021-12-17
jrmu
case PAGELIST_POST:
445
2021-12-17
jrmu
if ($reindex) PageIndexQueueUpdate($reindex);
446
2021-12-17
jrmu
$reindex = array();
447
2021-12-17
jrmu
return 0;
448
2021-12-17
jrmu
}
449
2021-12-17
jrmu
}
450
2021-12-17
jrmu
451
2021-12-17
jrmu
452
2021-12-17
jrmu
function PageListVariables(&$list, &$opt, $pn, &$page) {
453
2021-12-17
jrmu
global $PageListVarFoldFn, $StrFoldFunction;
454
2021-12-17
jrmu
$fold = empty($PageListVarFoldFn)
455
2021-12-17
jrmu
? $StrFoldFunction : $PageListVarFoldFn;
456
2021-12-17
jrmu
457
2021-12-17
jrmu
switch ($opt['=phase']) {
458
2021-12-17
jrmu
case PAGELIST_PRE:
459
2021-12-17
jrmu
$varlist = preg_grep('/^\\$/', array_keys($opt));
460
2021-12-17
jrmu
if (!$varlist) return 0;
461
2021-12-17
jrmu
foreach($varlist as $v) {
462
2021-12-17
jrmu
list($inclp, $exclp) = GlobToPCRE($opt[$v]);
463
2021-12-17
jrmu
if ($inclp) $opt['=varinclp'][$v] = $fold("/$inclp/i");
464
2021-12-17
jrmu
if ($exclp) $opt['=varexclp'][$v] = $fold("/$exclp/i");
465
2021-12-17
jrmu
}
466
2021-12-17
jrmu
return PAGELIST_ITEM;
467
2021-12-17
jrmu
468
2021-12-17
jrmu
case PAGELIST_ITEM:
469
2021-12-17
jrmu
if (@$opt['=varinclp'])
470
2021-12-17
jrmu
foreach($opt['=varinclp'] as $v => $pat)
471
2021-12-17
jrmu
if (!preg_match($pat, $fold(PageVar($pn, $v)))) return 0;
472
2021-12-17
jrmu
if (@$opt['=varexclp'])
473
2021-12-17
jrmu
foreach($opt['=varexclp'] as $v => $pat)
474
2021-12-17
jrmu
if (preg_match($pat, $fold(PageVar($pn, $v)))) return 0;
475
2021-12-17
jrmu
return 1;
476
2021-12-17
jrmu
}
477
2021-12-17
jrmu
}
478
2021-12-17
jrmu
479
2021-12-17
jrmu
480
2021-12-17
jrmu
function PageListSort(&$list, &$opt, $pn, &$page) {
481
2021-12-17
jrmu
global $PageListSortCmp, $PCache, $PageListSortRead;
482
2021-12-17
jrmu
SDVA($PageListSortRead, array('name' => 0, 'group' => 0, 'random' => 0,
483
2021-12-17
jrmu
'title' => 0));
484
2021-12-17
jrmu
485
2021-12-17
jrmu
switch ($opt['=phase']) {
486
2021-12-17
jrmu
case PAGELIST_PRE:
487
2021-12-17
jrmu
$ret = 0;
488
2021-12-17
jrmu
foreach(preg_split('/[^-\\w:$]+/', @$opt['order'], -1, PREG_SPLIT_NO_EMPTY)
489
2021-12-17
jrmu
as $o) {
490
2021-12-17
jrmu
$ret |= PAGELIST_POST;
491
2021-12-17
jrmu
$r = '+';
492
2021-12-17
jrmu
if ($o[0] == '-') { $r = '-'; $o = substr($o, 1); }
493
2021-12-17
jrmu
$opt['=order'][$o] = $r;
494
2021-12-17
jrmu
if ($o[0] != '$' &&
495
2021-12-17
jrmu
(!isset($PageListSortRead[$o]) || $PageListSortRead[$o]))
496
2021-12-17
jrmu
$ret |= PAGELIST_ITEM;
497
2021-12-17
jrmu
}
498
2021-12-17
jrmu
StopWatch(@"PageListSort pre ret=$ret order={$opt['order']}");
499
2021-12-17
jrmu
return $ret;
500
2021-12-17
jrmu
501
2021-12-17
jrmu
case PAGELIST_ITEM:
502
2021-12-17
jrmu
if (!$page) { $page = ReadPage($pn, READPAGE_CURRENT); $opt['=readc']++; }
503
2021-12-17
jrmu
return 1;
504
2021-12-17
jrmu
}
505
2021-12-17
jrmu
506
2021-12-17
jrmu
## case PAGELIST_POST
507
2021-12-17
jrmu
StopWatch('PageListSort begin');
508
2021-12-17
jrmu
$order = $opt['=order'];
509
2021-12-17
jrmu
if (@$order['title'])
510
2021-12-17
jrmu
foreach($list as $pn) $PCache[$pn]['=title'] = PageVar($pn, '$Title');
511
2021-12-17
jrmu
if (@$order['group'])
512
2021-12-17
jrmu
foreach($list as $pn) $PCache[$pn]['group'] = PageVar($pn, '$Group');
513
2021-12-17
jrmu
if (@$order['random'])
514
2021-12-17
jrmu
{ NoCache(); foreach($list as $pn) $PCache[$pn]['random'] = rand(); }
515
2021-12-17
jrmu
foreach(preg_grep('/^\\$/', array_keys($order)) as $o)
516
2021-12-17
jrmu
foreach($list as $pn)
517
2021-12-17
jrmu
$PCache[$pn][$o] = PageVar($pn, $o);
518
2021-12-17
jrmu
foreach($PageListSortCmp as $o=>$f)
519
2021-12-17
jrmu
if(! is_callable($f)) # DEPRECATED
520
2021-12-17
jrmu
$PageListSortCmp[$o] = create_function( # called by old addon needing update, see pmwiki.org/CustomPagelistSortOrder
521
2021-12-17
jrmu
'$x,$y', "global \$PCache; return {$f};");
522
2021-12-17
jrmu
523
2021-12-17
jrmu
StopWatch('PageListSort sort');
524
2021-12-17
jrmu
if (count($opt['=order'])) {
525
2021-12-17
jrmu
$PCache['=pagelistoptorder'] = $opt['=order'];
526
2021-12-17
jrmu
uasort($list, 'PageListUASort');
527
2021-12-17
jrmu
}
528
2021-12-17
jrmu
StopWatch('PageListSort end');
529
2021-12-17
jrmu
}
530
2021-12-17
jrmu
function PageListUASort($x,$y) {
531
2021-12-17
jrmu
global $PCache, $PageListSortCmp, $PageListSortCmpFunction;
532
2021-12-17
jrmu
foreach($PCache['=pagelistoptorder'] as $o => $r) {
533
2021-12-17
jrmu
$sign = ($r == '-') ? -1 : 1;
534
2021-12-17
jrmu
if (@$PageListSortCmp[$o] && is_callable($PageListSortCmp[$o]))
535
2021-12-17
jrmu
$c = $PageListSortCmp[$o]($x, $y, $o);
536
2021-12-17
jrmu
else
537
2021-12-17
jrmu
$c = @$PageListSortCmpFunction($PCache[$x][$o],$PCache[$y][$o]);
538
2021-12-17
jrmu
if ($c) return $sign*$c;
539
2021-12-17
jrmu
}
540
2021-12-17
jrmu
return 0;
541
2021-12-17
jrmu
}
542
2021-12-17
jrmu
543
2021-12-17
jrmu
function PageListCache(&$list, &$opt, $pn, &$page) {
544
2021-12-17
jrmu
global $PageListCacheDir, $LastModTime, $PageIndexFile;
545
2021-12-17
jrmu
546
2021-12-17
jrmu
if (@!$PageListCacheDir) return 0;
547
2021-12-17
jrmu
if (isset($opt['cache']) && !$opt['cache']) return 0;
548
2021-12-17
jrmu
549
2021-12-17
jrmu
$key = $opt['=key'];
550
2021-12-17
jrmu
$cache = "$PageListCacheDir/$key,cache";
551
2021-12-17
jrmu
switch ($opt['=phase']) {
552
2021-12-17
jrmu
case PAGELIST_PRE:
553
2021-12-17
jrmu
if (!file_exists($cache) || filemtime($cache) <= $LastModTime)
554
2021-12-17
jrmu
return PAGELIST_POST;
555
2021-12-17
jrmu
StopWatch("PageListCache begin load key=$key");
556
2021-12-17
jrmu
list($list, $opt['=protectsafe']) =
557
2021-12-17
jrmu
unserialize(file_get_contents($cache));
558
2021-12-17
jrmu
$opt['=cached'] = 1;
559
2021-12-17
jrmu
StopWatch("PageListCache end load");
560
2021-12-17
jrmu
return 0;
561
2021-12-17
jrmu
562
2021-12-17
jrmu
case PAGELIST_POST:
563
2021-12-17
jrmu
StopWatch("PageListCache begin save key=$key");
564
2021-12-17
jrmu
$fp = @fopen($cache, "w");
565
2021-12-17
jrmu
if ($fp) {
566
2021-12-17
jrmu
fputs($fp, serialize(array($list, $opt['=protectsafe'])));
567
2021-12-17
jrmu
fclose($fp);
568
2021-12-17
jrmu
}
569
2021-12-17
jrmu
StopWatch("PageListCache end save");
570
2021-12-17
jrmu
return 0;
571
2021-12-17
jrmu
}
572
2021-12-17
jrmu
return 0;
573
2021-12-17
jrmu
}
574
2021-12-17
jrmu
575
2021-12-17
jrmu
576
2021-12-17
jrmu
## HandleSearchA performs ?action=search. It's basically the same
577
2021-12-17
jrmu
## as ?action=browse, except it takes its contents from Site.Search.
578
2021-12-17
jrmu
function HandleSearchA($pagename, $level = 'read') {
579
2021-12-17
jrmu
global $PageSearchForm, $FmtV, $HandleSearchFmt,
580
2021-12-17
jrmu
$PageStartFmt, $PageEndFmt;
581
2021-12-17
jrmu
SDV($HandleSearchFmt,array(&$PageStartFmt, '$PageText', &$PageEndFmt));
582
2021-12-17
jrmu
SDV($PageSearchForm, '$[{$SiteGroup}/Search]');
583
2021-12-17
jrmu
$form = RetrieveAuthPage($pagename, $level, true, READPAGE_CURRENT);
584
2021-12-17
jrmu
if (!$form) Abort("?unable to read $pagename");
585
2021-12-17
jrmu
PCache($pagename, $form);
586
2021-12-17
jrmu
$text = preg_replace('/\\[([=@])(.*?)\\1\\]/s', ' ', @$form['text']);
587
2021-12-17
jrmu
if (!preg_match('/\\(:searchresults(\\s.*?)?:\\)/', $text))
588
2021-12-17
jrmu
foreach((array)$PageSearchForm as $formfmt) {
589
2021-12-17
jrmu
$form = ReadPage(FmtPageName($formfmt, $pagename), READPAGE_CURRENT);
590
2021-12-17
jrmu
if ($form['text']) break;
591
2021-12-17
jrmu
}
592
2021-12-17
jrmu
$text = @$form['text'];
593
2021-12-17
jrmu
if (!$text) $text = '(:searchresults:)';
594
2021-12-17
jrmu
$FmtV['$PageText'] = MarkupToHTML($pagename,$text);
595
2021-12-17
jrmu
PrintFmt($pagename, $HandleSearchFmt);
596
2021-12-17
jrmu
}
597
2021-12-17
jrmu
598
2021-12-17
jrmu
599
2021-12-17
jrmu
########################################################################
600
2021-12-17
jrmu
## The functions below provide different formatting options for
601
2021-12-17
jrmu
## the output list, controlled by the fmt= parameter and the
602
2021-12-17
jrmu
## $FPLFormatOpt hash.
603
2021-12-17
jrmu
########################################################################
604
2021-12-17
jrmu
605
2021-12-17
jrmu
## This helper function handles the count= parameter for extracting
606
2021-12-17
jrmu
## a range of pagelist in the list.
607
2021-12-17
jrmu
function CalcRange($range, $n) {
608
2021-12-17
jrmu
if ($n < 1) return array(0, 0);
609
2021-12-17
jrmu
if (strpos($range, '..') === false) {
610
2021-12-17
jrmu
if ($range > 0) return array(1, min($range, $n));
611
2021-12-17
jrmu
if ($range < 0) return array(max($n + $range + 1, 1), $n);
612
2021-12-17
jrmu
return array(1, $n);
613
2021-12-17
jrmu
}
614
2021-12-17
jrmu
list($r0, $r1) = explode('..', $range);
615
2021-12-17
jrmu
if ($r0 < 0) $r0 += $n + 1;
616
2021-12-17
jrmu
if ($r1 < 0) $r1 += $n + 1;
617
2021-12-17
jrmu
else if ($r1 == 0) $r1 = $n;
618
2021-12-17
jrmu
if ($r0 < 1 && $r1 < 1) return array($n+1, $n+1);
619
2021-12-17
jrmu
return array(max($r0, 1), max($r1, 1));
620
2021-12-17
jrmu
}
621
2021-12-17
jrmu
622
2021-12-17
jrmu
623
2021-12-17
jrmu
## FPLCountA handles fmt=count
624
2021-12-17
jrmu
function FPLCountA($pagename, &$matches, $opt) {
625
2021-12-17
jrmu
$matches = array_values(MakePageList($pagename, $opt, 0));
626
2021-12-17
jrmu
return count($matches);
627
2021-12-17
jrmu
}
628
2021-12-17
jrmu
629
2021-12-17
jrmu
SDVA($FPLTemplateFunctions, array(
630
2021-12-17
jrmu
'FPLTemplateLoad' => 100,
631
2021-12-17
jrmu
'FPLTemplateDefaults' => 200,
632
2021-12-17
jrmu
'FPLTemplatePageList' => 300,
633
2021-12-17
jrmu
'FPLTemplateSliceList' => 400,
634
2021-12-17
jrmu
'FPLTemplateFormat' => 500
635
2021-12-17
jrmu
));
636
2021-12-17
jrmu
637
2021-12-17
jrmu
function FPLTemplate($pagename, &$matches, $opt) {
638
2021-12-17
jrmu
global $FPLTemplateFunctions;
639
2021-12-17
jrmu
StopWatch("FPLTemplate: Chain begin");
640
2021-12-17
jrmu
asort($FPLTemplateFunctions, SORT_NUMERIC);
641
2021-12-17
jrmu
$fnlist = $FPLTemplateFunctions;
642
2021-12-17
jrmu
$output = '';
643
2021-12-17
jrmu
foreach($FPLTemplateFunctions as $fn=>$i) {
644
2021-12-17
jrmu
if ($i<0) continue;
645
2021-12-17
jrmu
StopWatch("FPLTemplate: $fn");
646
2021-12-17
jrmu
$fn($pagename, $matches, $opt, $tparts, $output);
647
2021-12-17
jrmu
}
648
2021-12-17
jrmu
StopWatch("FPLTemplate: Chain end");
649
2021-12-17
jrmu
return $output;
650
2021-12-17
jrmu
}
651
2021-12-17
jrmu
652
2021-12-17
jrmu
## Loads a template section
653
2021-12-17
jrmu
function FPLTemplateLoad($pagename, $matches, $opt, &$tparts){
654
2021-12-17
jrmu
global $Cursor, $FPLTemplatePageFmt, $RASPageName, $PageListArgPattern;
655
2021-12-17
jrmu
SDV($FPLTemplatePageFmt, array('{$FullName}',
656
2021-12-17
jrmu
'{$SiteGroup}.LocalTemplates', '{$SiteGroup}.PageListTemplates'));
657
2021-12-17
jrmu
658
2021-12-17
jrmu
$template = @$opt['template'];
659
2021-12-17
jrmu
if (!$template) $template = @$opt['fmt'];
660
2021-12-17
jrmu
$ttext = RetrieveAuthSection($pagename, $template, $FPLTemplatePageFmt);
661
2021-12-17
jrmu
$ttext = PVSE(Qualify($RASPageName, $ttext));
662
2021-12-17
jrmu
663
2021-12-17
jrmu
## save any escapes
664
2021-12-17
jrmu
$ttext = MarkupEscape($ttext);
665
2021-12-17
jrmu
## remove any anchor markups to avoid duplications
666
2021-12-17
jrmu
$ttext = preg_replace('/\\[\\[#[A-Za-z][-.:\\w]*\\]\\]/', '', $ttext);
667
2021-12-17
jrmu
668
2021-12-17
jrmu
## extract portions of template
669
2021-12-17
jrmu
$tparts = preg_split('/\\(:(template)\\s+([-!]?)\\s*(\\w+)\\s*(.*?):\\)/i',
670
2021-12-17
jrmu
$ttext, -1, PREG_SPLIT_DELIM_CAPTURE);
671
2021-12-17
jrmu
}
672
2021-12-17
jrmu
673
2021-12-17
jrmu
## Merge parameters from (:template default :) with those in the (:pagelist:)
674
2021-12-17
jrmu
function FPLTemplateDefaults($pagename, $matches, &$opt, &$tparts){
675
2021-12-17
jrmu
global $PageListArgPattern;
676
2021-12-17
jrmu
$i = 0;
677
2021-12-17
jrmu
while ($i < count($tparts)) {
678
2021-12-17
jrmu
if ($tparts[$i] != 'template') { $i++; continue; }
679
2021-12-17
jrmu
if ($tparts[$i+2] != 'defaults' && $tparts[$i+2] != 'default') { $i+=5; continue; }
680
2021-12-17
jrmu
$pvars = $GLOBALS['MarkupTable']['{$var}']; # expand {$PVars}
681
2021-12-17
jrmu
$ttext = preg_replace_callback($pvars['pat'], $pvars['rep'], $tparts[$i+3]);
682
2021-12-17
jrmu
$opt = array_merge(ParseArgs($ttext, $PageListArgPattern), $opt);
683
2021-12-17
jrmu
array_splice($tparts, $i, 4);
684
2021-12-17
jrmu
}
685
2021-12-17
jrmu
SDVA($opt, array('class' => 'fpltemplate', 'wrap' => 'div'));
686
2021-12-17
jrmu
}
687
2021-12-17
jrmu
688
2021-12-17
jrmu
## get the list of pages
689
2021-12-17
jrmu
function FPLTemplatePageList($pagename, &$matches, &$opt){
690
2021-12-17
jrmu
$matches = array_unique(array_merge((array)$matches, MakePageList($pagename, $opt, 0)));
691
2021-12-17
jrmu
## count matches before any slicing and save value as template var {$$PageListCount}
692
2021-12-17
jrmu
$opt['PageListCount'] = count($matches);
693
2021-12-17
jrmu
}
694
2021-12-17
jrmu
695
2021-12-17
jrmu
## extract page subset according to 'count=' parameter
696
2021-12-17
jrmu
function FPLTemplateSliceList($pagename, &$matches, $opt){
697
2021-12-17
jrmu
if (@$opt['count']) {
698
2021-12-17
jrmu
list($r0, $r1) = CalcRange($opt['count'], count($matches));
699
2021-12-17
jrmu
if ($r1 < $r0)
700
2021-12-17
jrmu
$matches = array_reverse(array_slice($matches, $r1-1, $r0-$r1+1));
701
2021-12-17
jrmu
else
702
2021-12-17
jrmu
$matches = array_slice($matches, $r0-1, $r1-$r0+1);
703
2021-12-17
jrmu
}
704
2021-12-17
jrmu
}
705
2021-12-17
jrmu
706
2021-12-17
jrmu
707
2021-12-17
jrmu
function FPLTemplateFormat($pagename, $matches, $opt, $tparts, &$output){
708
2021-12-17
jrmu
global $Cursor, $FPLTemplateMarkupFunction, $PCache;
709
2021-12-17
jrmu
SDV($FPLTemplateMarkupFunction, 'MarkupToHTML');
710
2021-12-17
jrmu
$savecursor = $Cursor;
711
2021-12-17
jrmu
$pagecount = $groupcount = $grouppagecount = $traildepth = $eachcount = 0;
712
2021-12-17
jrmu
$pseudovars = array('{$$PageCount}' => &$pagecount,
713
2021-12-17
jrmu
'{$$EachCount}' => &$eachcount,
714
2021-12-17
jrmu
'{$$GroupCount}' => &$groupcount,
715
2021-12-17
jrmu
'{$$GroupPageCount}' => &$grouppagecount,
716
2021-12-17
jrmu
'{$$PageTrailDepth}' => &$traildepth);
717
2021-12-17
jrmu
718
2021-12-17
jrmu
foreach(preg_grep('/^[\\w$]/', array_keys($opt)) as $k)
719
2021-12-17
jrmu
if (!is_array($opt[$k]))
720
2021-12-17
jrmu
$pseudovars["{\$\$$k}"] = PHSC($opt[$k], ENT_NOQUOTES);
721
2021-12-17
jrmu
722
2021-12-17
jrmu
$vk = array_keys($pseudovars);
723
2021-12-17
jrmu
$vv = array_values($pseudovars);
724
2021-12-17
jrmu
725
2021-12-17
jrmu
$lgroup = $lcontrol = ''; $out = '';
726
2021-12-17
jrmu
if (count($matches)==0) {
727
2021-12-17
jrmu
$t = 0;
728
2021-12-17
jrmu
while($t < count($tparts)) {
729
2021-12-17
jrmu
if ($tparts[$t]=='template' && $tparts[$t+2]=='none') {
730
2021-12-17
jrmu
$out .= MarkupRestore(FPLExpandItemVars($tparts[$t+4], $matches, 0, $pseudovars));
731
2021-12-17
jrmu
$t+=4;
732
2021-12-17
jrmu
}
733
2021-12-17
jrmu
$t++;
734
2021-12-17
jrmu
}
735
2021-12-17
jrmu
} # else:
736
2021-12-17
jrmu
foreach($matches as $i => $pn) {
737
2021-12-17
jrmu
$traildepth = intval(@$PCache[$pn]['depth']);
738
2021-12-17
jrmu
$group = PageVar($pn, '$Group');
739
2021-12-17
jrmu
if ($group != $lgroup) { $groupcount++; $grouppagecount = 0; $lgroup = $group; }
740
2021-12-17
jrmu
$grouppagecount++; $pagecount++; $eachcount++;
741
2021-12-17
jrmu
742
2021-12-17
jrmu
$t = 0;
743
2021-12-17
jrmu
while ($t < count($tparts)) {
744
2021-12-17
jrmu
if ($tparts[$t] != 'template') { $item = $tparts[$t]; $t++; }
745
2021-12-17
jrmu
else {
746
2021-12-17
jrmu
list($neg, $when, $control, $item) = array_slice($tparts, $t+1, 4); $t+=5;
747
2021-12-17
jrmu
if ($when=='none') continue;
748
2021-12-17
jrmu
if (!$control) {
749
2021-12-17
jrmu
if ($when == 'first' && ($neg xor ($i != 0))) continue;
750
2021-12-17
jrmu
if ($when == 'last' && ($neg xor ($i != count($matches) - 1))) continue;
751
2021-12-17
jrmu
} else {
752
2021-12-17
jrmu
$currcontrol = FPLExpandItemVars($control, $matches, $i, $pseudovars);
753
2021-12-17
jrmu
if($currcontrol != $lcontrol) { $eachcount=1; $lcontrol = $currcontrol; }
754
2021-12-17
jrmu
if ($when == 'first' || !isset($last[$t])) {
755
2021-12-17
jrmu
$curr = FPLExpandItemVars($control, $matches, $i, $pseudovars);
756
2021-12-17
jrmu
757
2021-12-17
jrmu
if ($when == 'first' && ($neg xor (($i != 0) && ($last[$t] == $curr))))
758
2021-12-17
jrmu
{ $last[$t] = $curr; continue; }
759
2021-12-17
jrmu
$last[$t] = $curr;
760
2021-12-17
jrmu
}
761
2021-12-17
jrmu
if ($when == 'last') {
762
2021-12-17
jrmu
$next = FPLExpandItemVars($control, $matches, $i+1, $pseudovars);
763
2021-12-17
jrmu
if ($neg xor ($next == $last[$t] && $i != count($matches) - 1)) continue;
764
2021-12-17
jrmu
$last[$t] = $next;
765
2021-12-17
jrmu
}
766
2021-12-17
jrmu
}
767
2021-12-17
jrmu
}
768
2021-12-17
jrmu
$item = FPLExpandItemVars($item, $matches, $i, $pseudovars);
769
2021-12-17
jrmu
$out .= MarkupRestore($item);
770
2021-12-17
jrmu
}
771
2021-12-17
jrmu
}
772
2021-12-17
jrmu
773
2021-12-17
jrmu
$class = preg_replace('/[^-a-zA-Z0-9\\x80-\\xff]/', ' ', @$opt['class']);
774
2021-12-17
jrmu
if ($class) $class = " class='$class'";
775
2021-12-17
jrmu
$wrap = @$opt['wrap'];
776
2021-12-17
jrmu
if ($wrap != 'inline') {
777
2021-12-17
jrmu
$out = $FPLTemplateMarkupFunction($pagename, $out, array('escape' => 0, 'redirect'=>1));
778
2021-12-17
jrmu
if ($wrap != 'none') $out = "<div$class>$out</div>";
779
2021-12-17
jrmu
}
780
2021-12-17
jrmu
$Cursor = $savecursor;
781
2021-12-17
jrmu
$output .= $out;
782
2021-12-17
jrmu
}
783
2021-12-17
jrmu
## This function moves repeated code blocks out of FPLTemplateFormat()
784
2021-12-17
jrmu
function FPLExpandItemVars($item, $matches, $idx, $psvars) {
785
2021-12-17
jrmu
global $Cursor, $EnableUndefinedTemplateVars;
786
2021-12-17
jrmu
$Cursor['<'] = $Cursor['&lt;'] = (string)@$matches[$idx-1];
787
2021-12-17
jrmu
$Cursor['='] = $pn = (string)@$matches[$idx];
788
2021-12-17
jrmu
$Cursor['>'] = $Cursor['&gt;'] = (string)@$matches[$idx+1];
789
2021-12-17
jrmu
$item = str_replace(array_keys($psvars), array_values($psvars), $item);
790
2021-12-17
jrmu
$cb = new cb_pl_expandvars($pn);
791
2021-12-17
jrmu
$item = preg_replace_callback('/\\{(=|&[lg]t;)(\\$:?\\w[-\\w]*)\\}/',
792
2021-12-17
jrmu
array($cb, 'pl_expandvars'), $item);
793
2021-12-17
jrmu
if (! IsEnabled($EnableUndefinedTemplateVars, 0))
794
2021-12-17
jrmu
$item = preg_replace("/\\{\\$\\$\\w+\\}/", '', $item);
795
2021-12-17
jrmu
return $item;
796
2021-12-17
jrmu
}
797
2021-12-17
jrmu
798
2021-12-17
jrmu
########################################################################
799
2021-12-17
jrmu
## The functions below optimize searches by maintaining a file of
800
2021-12-17
jrmu
## words and link cross references (the "page index").
801
2021-12-17
jrmu
########################################################################
802
2021-12-17
jrmu
803
2021-12-17
jrmu
## PageIndexTerms($terms) takes an array of strings and returns a
804
2021-12-17
jrmu
## normalized list of associated search terms. This reduces the
805
2021-12-17
jrmu
## size of the index and speeds up searches.
806
2021-12-17
jrmu
function PageIndexTerms($terms) {
807
2021-12-17
jrmu
global $StrFoldFunction;
808
2021-12-17
jrmu
$w = array();
809
2021-12-17
jrmu
foreach((array)$terms as $t) {
810
2021-12-17
jrmu
$w = array_merge($w, preg_split('/[^\\w\\x80-\\xff]+/',
811
2021-12-17
jrmu
$StrFoldFunction($t), -1, PREG_SPLIT_NO_EMPTY));
812
2021-12-17
jrmu
}
813
2021-12-17
jrmu
return $w;
814
2021-12-17
jrmu
}
815
2021-12-17
jrmu
816
2021-12-17
jrmu
## The PageIndexUpdate($pagelist) function updates the page index
817
2021-12-17
jrmu
## file with terms and target links for the pages in $pagelist.
818
2021-12-17
jrmu
## The optional $dir parameter allows this function to be called
819
2021-12-17
jrmu
## via register_shutdown_function (which sometimes changes directories
820
2021-12-17
jrmu
## on us).
821
2021-12-17
jrmu
function PageIndexUpdate($pagelist = NULL, $dir = '') {
822
2021-12-17
jrmu
global $EnableReadOnly, $PageIndexUpdateList, $PageIndexFile,
823
2021-12-17
jrmu
$PageIndexTime, $Now;
824
2021-12-17
jrmu
if (IsEnabled($EnableReadOnly, 0)) return;
825
2021-12-17
jrmu
$abort = ignore_user_abort(true);
826
2021-12-17
jrmu
if ($dir) { flush(); chdir($dir); }
827
2021-12-17
jrmu
if (is_null($pagelist))
828
2021-12-17
jrmu
{ $pagelist = (array)$PageIndexUpdateList; $PageIndexUpdateList = array(); }
829
2021-12-17
jrmu
if (!$pagelist || !$PageIndexFile) return;
830
2021-12-17
jrmu
SDV($PageIndexTime, 10);
831
2021-12-17
jrmu
$c = count($pagelist); $updatecount = 0;
832
2021-12-17
jrmu
StopWatch("PageIndexUpdate begin ($c pages to update)");
833
2021-12-17
jrmu
$pagelist = (array)$pagelist;
834
2021-12-17
jrmu
$timeout = time() + $PageIndexTime;
835
2021-12-17
jrmu
$cmpfn = 'PageIndexUpdateSort';
836
2021-12-17
jrmu
Lock(2);
837
2021-12-17
jrmu
$ofp = fopen("$PageIndexFile,new", 'w');
838
2021-12-17
jrmu
foreach($pagelist as $pn) {
839
2021-12-17
jrmu
if (@$updated[$pn]) continue;
840
2021-12-17
jrmu
@$updated[$pn]++;
841
2021-12-17
jrmu
if (time() > $timeout) continue;
842
2021-12-17
jrmu
$page = ReadPage($pn, READPAGE_CURRENT);
843
2021-12-17
jrmu
if ($page) {
844
2021-12-17
jrmu
$targets = str_replace(',', ' ', @$page['targets']);
845
2021-12-17
jrmu
$terms = PageIndexTerms(array(@$page['text'], $targets, $pn));
846
2021-12-17
jrmu
usort($terms, $cmpfn);
847
2021-12-17
jrmu
$x = '';
848
2021-12-17
jrmu
foreach($terms as $t) { if (strpos($x, $t) === false) $x .= " $t"; }
849
2021-12-17
jrmu
fputs($ofp, "$pn:$Now: $targets :$x\n");
850
2021-12-17
jrmu
}
851
2021-12-17
jrmu
$updatecount++;
852
2021-12-17
jrmu
}
853
2021-12-17
jrmu
$ifp = @fopen($PageIndexFile, 'r');
854
2021-12-17
jrmu
if ($ifp) {
855
2021-12-17
jrmu
while (!feof($ifp)) {
856
2021-12-17
jrmu
$line = fgets($ifp, 4096);
857
2021-12-17
jrmu
while (substr($line, -1, 1) != "\n" && !feof($ifp))
858
2021-12-17
jrmu
$line .= fgets($ifp, 4096);
859
2021-12-17
jrmu
$i = strpos($line, ':');
860
2021-12-17
jrmu
if ($i === false) continue;
861
2021-12-17
jrmu
$n = substr($line, 0, $i);
862
2021-12-17
jrmu
if (@$updated[$n]) continue;
863
2021-12-17
jrmu
fputs($ofp, $line);
864
2021-12-17
jrmu
}
865
2021-12-17
jrmu
fclose($ifp);
866
2021-12-17
jrmu
}
867
2021-12-17
jrmu
fclose($ofp);
868
2021-12-17
jrmu
if (file_exists($PageIndexFile)) unlink($PageIndexFile);
869
2021-12-17
jrmu
rename("$PageIndexFile,new", $PageIndexFile);
870
2021-12-17
jrmu
fixperms($PageIndexFile);
871
2021-12-17
jrmu
StopWatch("PageIndexUpdate end ($updatecount updated)");
872
2021-12-17
jrmu
ignore_user_abort($abort);
873
2021-12-17
jrmu
}
874
2021-12-17
jrmu
function PageIndexUpdateSort($a,$b) {return strlen($b)-strlen($a);}
875
2021-12-17
jrmu
876
2021-12-17
jrmu
## PageIndexQueueUpdate specifies pages to be updated in
877
2021-12-17
jrmu
## the index upon shutdown (via register_shutdown function).
878
2021-12-17
jrmu
function PageIndexQueueUpdate($pagelist) {
879
2021-12-17
jrmu
global $PageIndexUpdateList;
880
2021-12-17
jrmu
if (!@$PageIndexUpdateList)
881
2021-12-17
jrmu
register_shutdown_function('PageIndexUpdate', NULL, getcwd());
882
2021-12-17
jrmu
$PageIndexUpdateList = array_merge((array)@$PageIndexUpdateList,
883
2021-12-17
jrmu
(array)$pagelist);
884
2021-12-17
jrmu
$c1 = @count($pagelist); $c2 = count($PageIndexUpdateList);
885
2021-12-17
jrmu
StopWatch("PageIndexQueueUpdate: queued $c1 pages ($c2 total)");
886
2021-12-17
jrmu
}
887
2021-12-17
jrmu
888
2021-12-17
jrmu
## PageIndexGrep returns a list of pages that match the strings
889
2021-12-17
jrmu
## provided. Note that some search terms may need to be normalized
890
2021-12-17
jrmu
## in order to get the desired results (see PageIndexTerms above).
891
2021-12-17
jrmu
## Also note that this just works for the index; if the index is
892
2021-12-17
jrmu
## incomplete, then so are the results returned by this list.
893
2021-12-17
jrmu
## (MakePageList above already knows how to deal with this.)
894
2021-12-17
jrmu
function PageIndexGrep($terms, $invert = false) {
895
2021-12-17
jrmu
global $PageIndexFile;
896
2021-12-17
jrmu
if (!$PageIndexFile) return array();
897
2021-12-17
jrmu
StopWatch('PageIndexGrep begin');
898
2021-12-17
jrmu
$pagelist = array();
899
2021-12-17
jrmu
$fp = @fopen($PageIndexFile, 'r');
900
2021-12-17
jrmu
if ($fp) {
901
2021-12-17
jrmu
$terms = (array)$terms;
902
2021-12-17
jrmu
while (!feof($fp)) {
903
2021-12-17
jrmu
$line = fgets($fp, 4096);
904
2021-12-17
jrmu
while (substr($line, -1, 1) != "\n" && !feof($fp))
905
2021-12-17
jrmu
$line .= fgets($fp, 4096);
906
2021-12-17
jrmu
$i = strpos($line, ':');
907
2021-12-17
jrmu
if (!$i) continue;
908
2021-12-17
jrmu
$add = true;
909
2021-12-17
jrmu
foreach($terms as $t)
910
2021-12-17
jrmu
if (strpos($line, $t) === false) { $add = false; break; }
911
2021-12-17
jrmu
if ($add xor $invert) $pagelist[] = substr($line, 0, $i);
912
2021-12-17
jrmu
}
913
2021-12-17
jrmu
fclose($fp);
914
2021-12-17
jrmu
}
915
2021-12-17
jrmu
StopWatch('PageIndexGrep end');
916
2021-12-17
jrmu
return $pagelist;
917
2021-12-17
jrmu
}
918
2021-12-17
jrmu
919
2021-12-17
jrmu
## PostPageIndex is inserted into $EditFunctions to update
920
2021-12-17
jrmu
## the linkindex whenever a page is saved.
921
2021-12-17
jrmu
function PostPageIndex($pagename, &$page, &$new) {
922
2021-12-17
jrmu
global $IsPagePosted;
923
2021-12-17
jrmu
if ($IsPagePosted) PageIndexQueueUpdate($pagename);
924
2021-12-17
jrmu
}
IRCNow