1 | <?php
|
---|
2 | /**
|
---|
3 | * DokuWiki search functions
|
---|
4 | *
|
---|
5 | * @license GPL 2 (http://www.gnu.org/licenses/gpl.html)
|
---|
6 | * @author Andreas Gohr <[email protected]>
|
---|
7 | */
|
---|
8 |
|
---|
9 | if(!defined('DOKU_INC')) die('meh.');
|
---|
10 |
|
---|
11 | /**
|
---|
12 | * recurse direcory
|
---|
13 | *
|
---|
14 | * This function recurses into a given base directory
|
---|
15 | * and calls the supplied function for each file and directory
|
---|
16 | *
|
---|
17 | * @param array ref $data The results of the search are stored here
|
---|
18 | * @param string $base Where to start the search
|
---|
19 | * @param callback $func Callback (function name or arayy with object,method)
|
---|
20 | * @param string $dir Current directory beyond $base
|
---|
21 | * @param int $lvl Recursion Level
|
---|
22 | * @author Andreas Gohr <[email protected]>
|
---|
23 | */
|
---|
24 | function search(&$data,$base,$func,$opts,$dir='',$lvl=1){
|
---|
25 | $dirs = array();
|
---|
26 | $files = array();
|
---|
27 |
|
---|
28 | //read in directories and files
|
---|
29 | $dh = @opendir($base.'/'.$dir);
|
---|
30 | if(!$dh) return;
|
---|
31 | while(($file = readdir($dh)) !== false){
|
---|
32 | if(preg_match('/^[\._]/',$file)) continue; //skip hidden files and upper dirs
|
---|
33 | if(is_dir($base.'/'.$dir.'/'.$file)){
|
---|
34 | $dirs[] = $dir.'/'.$file;
|
---|
35 | continue;
|
---|
36 | }
|
---|
37 | $files[] = $dir.'/'.$file;
|
---|
38 | }
|
---|
39 | closedir($dh);
|
---|
40 | sort($files);
|
---|
41 | sort($dirs);
|
---|
42 |
|
---|
43 | //give directories to userfunction then recurse
|
---|
44 | foreach($dirs as $dir){
|
---|
45 | if (call_user_func_array($func, array(&$data,$base,$dir,'d',$lvl,$opts))){
|
---|
46 | search($data,$base,$func,$opts,$dir,$lvl+1);
|
---|
47 | }
|
---|
48 | }
|
---|
49 | //now handle the files
|
---|
50 | foreach($files as $file){
|
---|
51 | call_user_func_array($func, array(&$data,$base,$file,'f',$lvl,$opts));
|
---|
52 | }
|
---|
53 | }
|
---|
54 |
|
---|
55 | /**
|
---|
56 | * Wrapper around call_user_func_array.
|
---|
57 | *
|
---|
58 | * @deprecated
|
---|
59 | */
|
---|
60 | function search_callback($func,&$data,$base,$file,$type,$lvl,$opts){
|
---|
61 | return call_user_func_array($func, array(&$data,$base,$file,$type,$lvl,$opts));
|
---|
62 | }
|
---|
63 |
|
---|
64 | /**
|
---|
65 | * The following functions are userfunctions to use with the search
|
---|
66 | * function above. This function is called for every found file or
|
---|
67 | * directory. When a directory is given to the function it has to
|
---|
68 | * decide if this directory should be traversed (true) or not (false)
|
---|
69 | * The function has to accept the following parameters:
|
---|
70 | *
|
---|
71 | * &$data - Reference to the result data structure
|
---|
72 | * $base - Base usually $conf['datadir']
|
---|
73 | * $file - current file or directory relative to $base
|
---|
74 | * $type - Type either 'd' for directory or 'f' for file
|
---|
75 | * $lvl - Current recursion depht
|
---|
76 | * $opts - option array as given to search()
|
---|
77 | *
|
---|
78 | * return values for files are ignored
|
---|
79 | *
|
---|
80 | * All functions should check the ACL for document READ rights
|
---|
81 | * namespaces (directories) are NOT checked as this would break
|
---|
82 | * the recursion (You can have an nonreadable dir over a readable
|
---|
83 | * one deeper nested) also make sure to check the file type (for example
|
---|
84 | * in case of lockfiles).
|
---|
85 | */
|
---|
86 |
|
---|
87 | /**
|
---|
88 | * Searches for pages beginning with the given query
|
---|
89 | *
|
---|
90 | * @author Andreas Gohr <[email protected]>
|
---|
91 | */
|
---|
92 | function search_qsearch(&$data,$base,$file,$type,$lvl,$opts){
|
---|
93 | $opts = array(
|
---|
94 | 'idmatch' => '(^|:)'.preg_quote($opts['query'],'/').'/',
|
---|
95 | 'listfiles' => true,
|
---|
96 | 'pagesonly' => true,
|
---|
97 | );
|
---|
98 | return search_universal($data,$base,$file,$type,$lvl,$opts);
|
---|
99 | }
|
---|
100 |
|
---|
101 | /**
|
---|
102 | * Build the browsable index of pages
|
---|
103 | *
|
---|
104 | * $opts['ns'] is the current namespace
|
---|
105 | *
|
---|
106 | * @author Andreas Gohr <[email protected]>
|
---|
107 | */
|
---|
108 | function search_index(&$data,$base,$file,$type,$lvl,$opts){
|
---|
109 | global $conf;
|
---|
110 | $return = true;
|
---|
111 |
|
---|
112 | $item = array();
|
---|
113 |
|
---|
114 | if($type == 'd' && !preg_match('#^'.$file.'(/|$)#','/'.$opts['ns'])){
|
---|
115 | //add but don't recurse
|
---|
116 | $return = false;
|
---|
117 | }elseif($type == 'f' && ($opts['nofiles'] || substr($file,-4) != '.txt')){
|
---|
118 | //don't add
|
---|
119 | return false;
|
---|
120 | }
|
---|
121 |
|
---|
122 | $id = pathID($file,($type == 'd'));
|
---|
123 |
|
---|
124 | if($type=='d' && $conf['sneaky_index'] && auth_quickaclcheck($id.':') < AUTH_READ){
|
---|
125 | return false;
|
---|
126 | }
|
---|
127 |
|
---|
128 | //check hidden
|
---|
129 | if(isHiddenPage($id)){
|
---|
130 | return false;
|
---|
131 | }
|
---|
132 |
|
---|
133 | //check ACL
|
---|
134 | if($type=='f' && auth_quickaclcheck($id) < AUTH_READ){
|
---|
135 | return false;
|
---|
136 | }
|
---|
137 |
|
---|
138 | $data[]=array( 'id' => $id,
|
---|
139 | 'type' => $type,
|
---|
140 | 'level' => $lvl,
|
---|
141 | 'open' => $return );
|
---|
142 | return $return;
|
---|
143 | }
|
---|
144 |
|
---|
145 | /**
|
---|
146 | * List all namespaces
|
---|
147 | *
|
---|
148 | * @author Andreas Gohr <[email protected]>
|
---|
149 | */
|
---|
150 | function search_namespaces(&$data,$base,$file,$type,$lvl,$opts){
|
---|
151 | $opts = array(
|
---|
152 | 'listdirs' => true,
|
---|
153 | );
|
---|
154 | return search_universal($data,$base,$file,$type,$lvl,$opts);
|
---|
155 | }
|
---|
156 |
|
---|
157 | /**
|
---|
158 | * List all mediafiles in a namespace
|
---|
159 | *
|
---|
160 | * @author Andreas Gohr <[email protected]>
|
---|
161 | */
|
---|
162 | function search_media(&$data,$base,$file,$type,$lvl,$opts){
|
---|
163 |
|
---|
164 | //we do nothing with directories
|
---|
165 | if($type == 'd') {
|
---|
166 | if(!$opts['depth']) return true; // recurse forever
|
---|
167 | $depth = substr_count($file,'/');
|
---|
168 | if($depth >= $opts['depth']) return false; // depth reached
|
---|
169 | return true;
|
---|
170 | }
|
---|
171 |
|
---|
172 | $info = array();
|
---|
173 | $info['id'] = pathID($file,true);
|
---|
174 | if($info['id'] != cleanID($info['id'])){
|
---|
175 | if($opts['showmsg'])
|
---|
176 | msg(hsc($info['id']).' is not a valid file name for DokuWiki - skipped',-1);
|
---|
177 | return false; // skip non-valid files
|
---|
178 | }
|
---|
179 |
|
---|
180 | //check ACL for namespace (we have no ACL for mediafiles)
|
---|
181 | $info['perm'] = auth_quickaclcheck(getNS($info['id']).':*');
|
---|
182 | if(!$opts['skipacl'] && $info['perm'] < AUTH_READ){
|
---|
183 | return false;
|
---|
184 | }
|
---|
185 |
|
---|
186 | //check pattern filter
|
---|
187 | if($opts['pattern'] && !@preg_match($opts['pattern'], $info['id'])){
|
---|
188 | return false;
|
---|
189 | }
|
---|
190 |
|
---|
191 | $info['file'] = basename($file);
|
---|
192 | $info['size'] = filesize($base.'/'.$file);
|
---|
193 | $info['mtime'] = filemtime($base.'/'.$file);
|
---|
194 | $info['writable'] = is_writable($base.'/'.$file);
|
---|
195 | if(preg_match("/\.(jpe?g|gif|png)$/",$file)){
|
---|
196 | $info['isimg'] = true;
|
---|
197 | $info['meta'] = new JpegMeta($base.'/'.$file);
|
---|
198 | }else{
|
---|
199 | $info['isimg'] = false;
|
---|
200 | }
|
---|
201 | if($opts['hash']){
|
---|
202 | $info['hash'] = md5(io_readFile(mediaFN($info['id']),false));
|
---|
203 | }
|
---|
204 |
|
---|
205 | $data[] = $info;
|
---|
206 |
|
---|
207 | return false;
|
---|
208 | }
|
---|
209 |
|
---|
210 | /**
|
---|
211 | * This function just lists documents (for RSS namespace export)
|
---|
212 | *
|
---|
213 | * @author Andreas Gohr <[email protected]>
|
---|
214 | */
|
---|
215 | function search_list(&$data,$base,$file,$type,$lvl,$opts){
|
---|
216 | //we do nothing with directories
|
---|
217 | if($type == 'd') return false;
|
---|
218 | //only search txt files
|
---|
219 | if(substr($file,-4) == '.txt'){
|
---|
220 | //check ACL
|
---|
221 | $id = pathID($file);
|
---|
222 | if(auth_quickaclcheck($id) < AUTH_READ){
|
---|
223 | return false;
|
---|
224 | }
|
---|
225 | $data[]['id'] = $id;
|
---|
226 | }
|
---|
227 | return false;
|
---|
228 | }
|
---|
229 |
|
---|
230 | /**
|
---|
231 | * Quicksearch for searching matching pagenames
|
---|
232 | *
|
---|
233 | * $opts['query'] is the search query
|
---|
234 | *
|
---|
235 | * @author Andreas Gohr <[email protected]>
|
---|
236 | */
|
---|
237 | function search_pagename(&$data,$base,$file,$type,$lvl,$opts){
|
---|
238 | //we do nothing with directories
|
---|
239 | if($type == 'd') return true;
|
---|
240 | //only search txt files
|
---|
241 | if(substr($file,-4) != '.txt') return true;
|
---|
242 |
|
---|
243 | //simple stringmatching
|
---|
244 | if (!empty($opts['query'])){
|
---|
245 | if(strpos($file,$opts['query']) !== false){
|
---|
246 | //check ACL
|
---|
247 | $id = pathID($file);
|
---|
248 | if(auth_quickaclcheck($id) < AUTH_READ){
|
---|
249 | return false;
|
---|
250 | }
|
---|
251 | $data[]['id'] = $id;
|
---|
252 | }
|
---|
253 | }
|
---|
254 | return true;
|
---|
255 | }
|
---|
256 |
|
---|
257 | /**
|
---|
258 | * Just lists all documents
|
---|
259 | *
|
---|
260 | * $opts['depth'] recursion level, 0 for all
|
---|
261 | * $opts['hash'] do md5 sum of content?
|
---|
262 | * $opts['skipacl'] list everything regardless of ACL
|
---|
263 | *
|
---|
264 | * @author Andreas Gohr <[email protected]>
|
---|
265 | */
|
---|
266 | function search_allpages(&$data,$base,$file,$type,$lvl,$opts){
|
---|
267 | //we do nothing with directories
|
---|
268 | if($type == 'd'){
|
---|
269 | if(!$opts['depth']) return true; // recurse forever
|
---|
270 | $parts = explode('/',ltrim($file,'/'));
|
---|
271 | if(count($parts) == $opts['depth']) return false; // depth reached
|
---|
272 | return true;
|
---|
273 | }
|
---|
274 |
|
---|
275 | //only search txt files
|
---|
276 | if(substr($file,-4) != '.txt') return true;
|
---|
277 |
|
---|
278 | $item['id'] = pathID($file);
|
---|
279 | if(!$opts['skipacl'] && auth_quickaclcheck($item['id']) < AUTH_READ){
|
---|
280 | return false;
|
---|
281 | }
|
---|
282 |
|
---|
283 | $item['rev'] = filemtime($base.'/'.$file);
|
---|
284 | $item['mtime'] = $item['rev'];
|
---|
285 | $item['size'] = filesize($base.'/'.$file);
|
---|
286 | if($opts['hash']){
|
---|
287 | $item['hash'] = md5(trim(rawWiki($item['id'])));
|
---|
288 | }
|
---|
289 |
|
---|
290 | $data[] = $item;
|
---|
291 | return true;
|
---|
292 | }
|
---|
293 |
|
---|
294 | /**
|
---|
295 | * Search for backlinks to a given page
|
---|
296 | *
|
---|
297 | * $opts['ns'] namespace of the page
|
---|
298 | * $opts['name'] name of the page without namespace
|
---|
299 | *
|
---|
300 | * @author Andreas Gohr <[email protected]>
|
---|
301 | * @deprecated Replaced by ft_backlinks()
|
---|
302 | */
|
---|
303 | function search_backlinks(&$data,$base,$file,$type,$lvl,$opts){
|
---|
304 | //we do nothing with directories
|
---|
305 | if($type == 'd') return true;
|
---|
306 | //only search txt files
|
---|
307 | if(substr($file,-4) != '.txt') return true;
|
---|
308 |
|
---|
309 | //absolute search id
|
---|
310 | $sid = cleanID($opts['ns'].':'.$opts['name']);
|
---|
311 |
|
---|
312 | //current id and namespace
|
---|
313 | $cid = pathID($file);
|
---|
314 | $cns = getNS($cid);
|
---|
315 |
|
---|
316 | //check ACL
|
---|
317 | if(auth_quickaclcheck($cid) < AUTH_READ){
|
---|
318 | return false;
|
---|
319 | }
|
---|
320 |
|
---|
321 | //fetch instructions
|
---|
322 | $instructions = p_cached_instructions($base.$file,true);
|
---|
323 | if(is_null($instructions)) return false;
|
---|
324 |
|
---|
325 | global $conf;
|
---|
326 | //check all links for match
|
---|
327 | foreach($instructions as $ins){
|
---|
328 | if($ins[0] == 'internallink' || ($conf['camelcase'] && $ins[0] == 'camelcaselink') ){
|
---|
329 | $mid = $ins[1][0];
|
---|
330 | resolve_pageid($cns,$mid,$exists); //exists is not used
|
---|
331 | if($mid == $sid){
|
---|
332 | //we have a match - finish
|
---|
333 | $data[]['id'] = $cid;
|
---|
334 | break;
|
---|
335 | }
|
---|
336 | }
|
---|
337 | }
|
---|
338 |
|
---|
339 | return false;
|
---|
340 | }
|
---|
341 |
|
---|
342 | /**
|
---|
343 | * Fulltextsearch
|
---|
344 | *
|
---|
345 | * $opts['query'] is the search query
|
---|
346 | *
|
---|
347 | * @author Andreas Gohr <[email protected]>
|
---|
348 | * @deprecated - fulltext indexer is used instead
|
---|
349 | */
|
---|
350 | function search_fulltext(&$data,$base,$file,$type,$lvl,$opts){
|
---|
351 | //we do nothing with directories
|
---|
352 | if($type == 'd') return true;
|
---|
353 | //only search txt files
|
---|
354 | if(substr($file,-4) != '.txt') return true;
|
---|
355 |
|
---|
356 | //check ACL
|
---|
357 | $id = pathID($file);
|
---|
358 | if(auth_quickaclcheck($id) < AUTH_READ){
|
---|
359 | return false;
|
---|
360 | }
|
---|
361 |
|
---|
362 | //create regexp from queries
|
---|
363 | $poswords = array();
|
---|
364 | $negwords = array();
|
---|
365 | $qpreg = preg_split('/\s+/',$opts['query']);
|
---|
366 |
|
---|
367 | foreach($qpreg as $word){
|
---|
368 | switch(substr($word,0,1)){
|
---|
369 | case '-':
|
---|
370 | if(strlen($word) > 1){ // catch single '-'
|
---|
371 | array_push($negwords,preg_quote(substr($word,1),'#'));
|
---|
372 | }
|
---|
373 | break;
|
---|
374 | case '+':
|
---|
375 | if(strlen($word) > 1){ // catch single '+'
|
---|
376 | array_push($poswords,preg_quote(substr($word,1),'#'));
|
---|
377 | }
|
---|
378 | break;
|
---|
379 | default:
|
---|
380 | array_push($poswords,preg_quote($word,'#'));
|
---|
381 | break;
|
---|
382 | }
|
---|
383 | }
|
---|
384 |
|
---|
385 | // a search without any posword is useless
|
---|
386 | if (!count($poswords)) return true;
|
---|
387 |
|
---|
388 | $reg = '^(?=.*?'.join(')(?=.*?',$poswords).')';
|
---|
389 | $reg .= count($negwords) ? '((?!'.join('|',$negwords).').)*$' : '.*$';
|
---|
390 | search_regex($data,$base,$file,$reg,$poswords);
|
---|
391 | return true;
|
---|
392 | }
|
---|
393 |
|
---|
394 | /**
|
---|
395 | * Reference search
|
---|
396 | * This fuction searches for existing references to a given media file
|
---|
397 | * and returns an array with the found pages. It doesn't pay any
|
---|
398 | * attention to ACL permissions to find every reference. The caller
|
---|
399 | * must check if the user has the appropriate rights to see the found
|
---|
400 | * page and eventually have to prevent the result from displaying.
|
---|
401 | *
|
---|
402 | * @param array $data Reference to the result data structure
|
---|
403 | * @param string $base Base usually $conf['datadir']
|
---|
404 | * @param string $file current file or directory relative to $base
|
---|
405 | * @param char $type Type either 'd' for directory or 'f' for file
|
---|
406 | * @param int $lvl Current recursion depht
|
---|
407 | * @param mixed $opts option array as given to search()
|
---|
408 | *
|
---|
409 | * $opts['query'] is the demanded media file name
|
---|
410 | *
|
---|
411 | * @author Andreas Gohr <[email protected]>
|
---|
412 | * @author Matthias Grimm <[email protected]>
|
---|
413 | */
|
---|
414 | function search_reference(&$data,$base,$file,$type,$lvl,$opts){
|
---|
415 | global $conf;
|
---|
416 |
|
---|
417 | //we do nothing with directories
|
---|
418 | if($type == 'd') return true;
|
---|
419 |
|
---|
420 | //only search txt files
|
---|
421 | if(substr($file,-4) != '.txt') return true;
|
---|
422 |
|
---|
423 | //we finish after 'cnt' references found. The return value
|
---|
424 | //'false' will skip subdirectories to speed search up.
|
---|
425 | $cnt = $conf['refshow'] > 0 ? $conf['refshow'] : 1;
|
---|
426 | if(count($data) >= $cnt) return false;
|
---|
427 |
|
---|
428 | $reg = '\{\{ *\:?'.$opts['query'].' *(\|.*)?\}\}';
|
---|
429 | search_regex($data,$base,$file,$reg,array($opts['query']));
|
---|
430 | return true;
|
---|
431 | }
|
---|
432 |
|
---|
433 | /* ------------- helper functions below -------------- */
|
---|
434 |
|
---|
435 | /**
|
---|
436 | * fulltext search helper
|
---|
437 | * searches a text file with a given regular expression
|
---|
438 | * no ACL checks are performed. This have to be done by
|
---|
439 | * the caller if necessary.
|
---|
440 | *
|
---|
441 | * @param array $data reference to array for results
|
---|
442 | * @param string $base base directory
|
---|
443 | * @param string $file file name to search in
|
---|
444 | * @param string $reg regular expression to search for
|
---|
445 | * @param array $words words that should be marked in the results
|
---|
446 | *
|
---|
447 | * @author Andreas Gohr <[email protected]>
|
---|
448 | * @author Matthias Grimm <[email protected]>
|
---|
449 | *
|
---|
450 | * @deprecated - fulltext indexer is used instead
|
---|
451 | */
|
---|
452 | function search_regex(&$data,$base,$file,$reg,$words){
|
---|
453 |
|
---|
454 | //get text
|
---|
455 | $text = io_readfile($base.'/'.$file);
|
---|
456 | //lowercase text (u modifier does not help with case)
|
---|
457 | $lctext = utf8_strtolower($text);
|
---|
458 |
|
---|
459 | //do the fulltext search
|
---|
460 | $matches = array();
|
---|
461 | if($cnt = preg_match_all('#'.$reg.'#usi',$lctext,$matches)){
|
---|
462 | //this is not the best way for snippet generation but the fastest I could find
|
---|
463 | $q = $words[0]; //use first word for snippet creation
|
---|
464 | $p = utf8_strpos($lctext,$q);
|
---|
465 | $f = $p - 100;
|
---|
466 | $l = utf8_strlen($q) + 200;
|
---|
467 | if($f < 0) $f = 0;
|
---|
468 | $snippet = '<span class="search_sep"> ... </span>'.
|
---|
469 | htmlspecialchars(utf8_substr($text,$f,$l)).
|
---|
470 | '<span class="search_sep"> ... </span>';
|
---|
471 | $mark = '('.join('|', $words).')';
|
---|
472 | $snippet = preg_replace('#'.$mark.'#si','<strong class="search_hit">\\1</strong>',$snippet);
|
---|
473 |
|
---|
474 | $data[] = array(
|
---|
475 | 'id' => pathID($file),
|
---|
476 | 'count' => preg_match_all('#'.$mark.'#usi',$lctext,$matches),
|
---|
477 | 'poswords' => join(' ',$words),
|
---|
478 | 'snippet' => $snippet,
|
---|
479 | );
|
---|
480 | }
|
---|
481 |
|
---|
482 | return true;
|
---|
483 | }
|
---|
484 |
|
---|
485 |
|
---|
486 | /**
|
---|
487 | * fulltext sort
|
---|
488 | *
|
---|
489 | * Callback sort function for use with usort to sort the data
|
---|
490 | * structure created by search_fulltext. Sorts descending by count
|
---|
491 | *
|
---|
492 | * @author Andreas Gohr <[email protected]>
|
---|
493 | */
|
---|
494 | function sort_search_fulltext($a,$b){
|
---|
495 | if($a['count'] > $b['count']){
|
---|
496 | return -1;
|
---|
497 | }elseif($a['count'] < $b['count']){
|
---|
498 | return 1;
|
---|
499 | }else{
|
---|
500 | return strcmp($a['id'],$b['id']);
|
---|
501 | }
|
---|
502 | }
|
---|
503 |
|
---|
504 | /**
|
---|
505 | * translates a document path to an ID
|
---|
506 | *
|
---|
507 | * @author Andreas Gohr <[email protected]>
|
---|
508 | * @todo move to pageutils
|
---|
509 | */
|
---|
510 | function pathID($path,$keeptxt=false){
|
---|
511 | $id = utf8_decodeFN($path);
|
---|
512 | $id = str_replace('/',':',$id);
|
---|
513 | if(!$keeptxt) $id = preg_replace('#\.txt$#','',$id);
|
---|
514 | $id = trim($id, ':');
|
---|
515 | return $id;
|
---|
516 | }
|
---|
517 |
|
---|
518 |
|
---|
519 | /**
|
---|
520 | * This is a very universal callback for the search() function, replacing
|
---|
521 | * many of the former individual functions at the cost of a more complex
|
---|
522 | * setup.
|
---|
523 | *
|
---|
524 | * How the function behaves, depends on the options passed in the $opts
|
---|
525 | * array, where the following settings can be used.
|
---|
526 | *
|
---|
527 | * depth int recursion depth. 0 for unlimited
|
---|
528 | * keeptxt bool keep .txt extension for IDs
|
---|
529 | * listfiles bool include files in listing
|
---|
530 | * listdirs bool include namespaces in listing
|
---|
531 | * pagesonly bool restrict files to pages
|
---|
532 | * skipacl bool do not check for READ permission
|
---|
533 | * sneakyacl bool don't recurse into nonreadable dirs
|
---|
534 | * hash bool create MD5 hash for files
|
---|
535 | * meta bool return file metadata
|
---|
536 | * filematch string match files against this regexp
|
---|
537 | * idmatch string match full ID against this regexp
|
---|
538 | * dirmatch string match directory against this regexp when adding
|
---|
539 | * nsmatch string match namespace against this regexp when adding
|
---|
540 | * recmatch string match directory against this regexp when recursing
|
---|
541 | * showmsg bool warn about non-ID files
|
---|
542 | * showhidden bool show hidden files too
|
---|
543 | * firsthead bool return first heading for pages
|
---|
544 | *
|
---|
545 | * @author Andreas Gohr <[email protected]>
|
---|
546 | */
|
---|
547 | function search_universal(&$data,$base,$file,$type,$lvl,$opts){
|
---|
548 | $item = array();
|
---|
549 | $return = true;
|
---|
550 |
|
---|
551 | // get ID and check if it is a valid one
|
---|
552 | $item['id'] = pathID($file,($type == 'd' || $opts['keeptxt']));
|
---|
553 | if($item['id'] != cleanID($item['id'])){
|
---|
554 | if($opts['showmsg'])
|
---|
555 | msg(hsc($item['id']).' is not a valid file name for DokuWiki - skipped',-1);
|
---|
556 | return false; // skip non-valid files
|
---|
557 | }
|
---|
558 | $item['ns'] = getNS($item['id']);
|
---|
559 |
|
---|
560 | if($type == 'd') {
|
---|
561 | // decide if to recursion into this directory is wanted
|
---|
562 | if(!$opts['depth']){
|
---|
563 | $return = true; // recurse forever
|
---|
564 | }else{
|
---|
565 | $depth = substr_count($file,'/');
|
---|
566 | if($depth >= $opts['depth']){
|
---|
567 | $return = false; // depth reached
|
---|
568 | }else{
|
---|
569 | $return = true;
|
---|
570 | }
|
---|
571 | }
|
---|
572 | if($return && !preg_match('/'.$opts['recmatch'].'/',$file)){
|
---|
573 | $return = false; // doesn't match
|
---|
574 | }
|
---|
575 | }
|
---|
576 |
|
---|
577 | // check ACL
|
---|
578 | if(!$opts['skipacl']){
|
---|
579 | if($type == 'd'){
|
---|
580 | $item['perm'] = auth_quickaclcheck($item['id'].':*');
|
---|
581 | }else{
|
---|
582 | $item['perm'] = auth_quickaclcheck($item['id']); //FIXME check namespace for media files
|
---|
583 | }
|
---|
584 | }else{
|
---|
585 | $item['perm'] = AUTH_DELETE;
|
---|
586 | }
|
---|
587 |
|
---|
588 | // are we done here maybe?
|
---|
589 | if($type == 'd'){
|
---|
590 | if(!$opts['listdirs']) return $return;
|
---|
591 | if(!$opts['skipacl'] && $opts['sneakyacl'] && $item['perm'] < AUTH_READ) return false; //neither list nor recurse
|
---|
592 | if($opts['dirmatch'] && !preg_match('/'.$opts['dirmatch'].'/',$file)) return $return;
|
---|
593 | if($opts['nsmatch'] && !preg_match('/'.$opts['nsmatch'].'/',$item['ns'])) return $return;
|
---|
594 | }else{
|
---|
595 | if(!$opts['listfiles']) return $return;
|
---|
596 | if(!$opts['skipacl'] && $item['perm'] < AUTH_READ) return $return;
|
---|
597 | if($opts['pagesonly'] && (substr($file,-4) != '.txt')) return $return;
|
---|
598 | if(!$opts['showhidden'] && isHiddenPage($item['id'])) return $return;
|
---|
599 | if($opts['filematch'] && !preg_match('/'.$opts['filematch'].'/',$file)) return $return;
|
---|
600 | if($opts['idmatch'] && !preg_match('/'.$opts['idmatch'].'/',$item['id'])) return $return;
|
---|
601 | }
|
---|
602 |
|
---|
603 | // still here? prepare the item
|
---|
604 | $item['type'] = $type;
|
---|
605 | $item['level'] = $lvl;
|
---|
606 | $item['open'] = $return;
|
---|
607 |
|
---|
608 | if($opts['meta']){
|
---|
609 | $item['file'] = basename($file);
|
---|
610 | $item['size'] = filesize($base.'/'.$file);
|
---|
611 | $item['mtime'] = filemtime($base.'/'.$file);
|
---|
612 | $item['rev'] = $item['mtime'];
|
---|
613 | $item['writable'] = is_writable($base.'/'.$file);
|
---|
614 | $item['executable'] = is_executable($base.'/'.$file);
|
---|
615 | }
|
---|
616 |
|
---|
617 | if($type == 'f'){
|
---|
618 | if($opts['hash']) $item['hash'] = md5(io_readFile($base.'/'.$file,false));
|
---|
619 | if($opts['firsthead']) $item['title'] = p_get_first_heading($item['id'],METADATA_DONT_RENDER);
|
---|
620 | }
|
---|
621 |
|
---|
622 | // finally add the item
|
---|
623 | $data[] = $item;
|
---|
624 | return $return;
|
---|
625 | }
|
---|
626 |
|
---|
627 | //Setup VIM: ex: et ts=4 :
|
---|