summaryrefslogtreecommitdiff
path: root/inc/search.php
diff options
context:
space:
mode:
Diffstat (limited to 'inc/search.php')
-rw-r--r--inc/search.php30
1 files changed, 19 insertions, 11 deletions
diff --git a/inc/search.php b/inc/search.php
index 1cecfd5ec..cc3e79006 100644
--- a/inc/search.php
+++ b/inc/search.php
@@ -16,12 +16,13 @@ if(!defined('DOKU_INC')) die('meh.');
*
* @param array ref $data The results of the search are stored here
* @param string $base Where to start the search
- * @param callback $func Callback (function name or arayy with object,method)
+ * @param callback $func Callback (function name or array with object,method)
* @param string $dir Current directory beyond $base
* @param int $lvl Recursion Level
+ * @param mixed $sort 'natural' to use natural order sorting (default); 'date' to sort by filemtime; leave empty to skip sorting.
* @author Andreas Gohr <andi@splitbrain.org>
*/
-function search(&$data,$base,$func,$opts,$dir='',$lvl=1,$sort=false){
+function search(&$data,$base,$func,$opts,$dir='',$lvl=1,$sort='natural'){
$dirs = array();
$files = array();
$filepaths = array();
@@ -39,17 +40,19 @@ function search(&$data,$base,$func,$opts,$dir='',$lvl=1,$sort=false){
$filepaths[] = $base.'/'.$dir.'/'.$file;
}
closedir($dh);
- if ($sort == 'date') {
- @array_multisort(array_map('filemtime', $filepaths), SORT_NUMERIC, SORT_DESC, $files);
- } else {
- sort($files);
+ if (!empty($sort)) {
+ if ($sort == 'date') {
+ @array_multisort(array_map('filemtime', $filepaths), SORT_NUMERIC, SORT_DESC, $files);
+ } else /* natural */ {
+ natsort($files);
+ }
+ natsort($dirs);
}
- sort($dirs);
//give directories to userfunction then recurse
foreach($dirs as $dir){
if (call_user_func_array($func, array(&$data,$base,$dir,'d',$lvl,$opts))){
- search($data,$base,$func,$opts,$dir,$lvl+1);
+ search($data,$base,$func,$opts,$dir,$lvl+1,$sort);
}
}
//now handle the files
@@ -247,11 +250,16 @@ function search_pagename(&$data,$base,$file,$type,$lvl,$opts){
* @author Andreas Gohr <andi@splitbrain.org>
*/
function search_allpages(&$data,$base,$file,$type,$lvl,$opts){
+ if(isset($opts['depth']) && $opts['depth']){
+ $parts = explode('/',ltrim($file,'/'));
+ if(($type == 'd' && count($parts) > $opts['depth'])
+ || ($type != 'd' && count($parts) > $opts['depth'] + 1)){
+ return false; // depth reached
+ }
+ }
+
//we do nothing with directories
if($type == 'd'){
- if(!$opts['depth']) return true; // recurse forever
- $parts = explode('/',ltrim($file,'/'));
- if(count($parts) == $opts['depth']) return false; // depth reached
return true;
}