[dokuwiki] Patch submission : allow user-defined namespaces/pages order in the index

Hi everybody,

Just subscribed the mailing list so I can submit a patch to you.

The goal of the patch is that user can choose the order in which 
namespaces and pages are displayed in the index.

The behavior is very simple :
 - To define namespaces order : place in the namespace in which namespaces 
order shall be changed a file named ".user_order_dirs"
In this file, place in the order you want it to appear the list of 
namespaces - i.e. directories -  (one per line). Non-existing namespaces 
will be ignored, remaining namespaces will be added alphabetically at the 
end of the user list.
Repeat this process in all concerned namespaces.
 - To define pages order : place in the namespace in which pages order 
shall be changed a file named ".user_order_files"
In this file, place in the order you want it to appear the list of pages - 
i.e. files -  (one per line, and with the ".txt" extension). Non-existing 
pages will be ignored, remaining pages will be added alphabetically at the 
end of the user list.
Repeat this process in all concerned namespaces.

Now, please find here below the patch itself as a unified diff (notice it 
only modified "inc/search.php")

-----------------------------------------------------------------------------------------------------------------------------------------------------------
--- inc/search.php      2009-06-05 13:06:56.000000000 +0200
+++ inc/search.php      2009-06-16 17:05:16.000000000 +0200
@@ -38,9 +38,18 @@ 
     $files[] = $dir.'/'.$file; 
   } 
   closedir($dh); 
-  sort($files); 
-  sort($dirs); 
- 
+  if(file_exists($base.'/'.$dir.'/.user_order_files')){ //if a user sort 
order exists for files
+    user_sort_order($files,file($base.'/'.$dir.'/.user_order_files')); 
//run the sorting function
+  }
+  else{
+    sort($files);
+  }
+  if(file_exists($base.'/'.$dir.'/.user_order_dirs')){ //if a user sort 
order exists for dirs
+    user_sort_order($dirs,file($base.'/'.$dir.'/.user_order_dirs')); 
//run the sorting function
+  }
+  else{
+    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))){
@@ -63,6 +72,36 @@
 }

 /**
+ * Apply user sort order when exists
+ *
+ * This function applies user sort order (of dirs or files)
+ *
+ * @param   array ref $items     List of items to sort
+ * @param   array     $sort_list User defined sort list
+ * @author  Matthieu Rioteau <matthieu.rioteau@xxxxxxx>
+ */
+function user_sort_order(&$items,$sort_list){
+  $temp_sort = array(); //temporary the user-sorted items
+
+  foreach($sort_list as $sort_list_item){ //for each item defined in the 
user sort list
+    foreach($items as $item_id => $item){ //for each item present in the 
"to sort" list
+      $item_trim=trim(substr($item,-strlen(strrchr($item,"/"))),"/ 
\t\n\r\0"); //remove extra characters and path of item (only keep name)
+      $sort_list_item_trim=trim($sort_list_item,"/ \t\n\r\0"); //remove 
extra characters
+      if(strcasecmp($item_trim,$sort_list_item_trim) == 0){ //if the user 
list item is present in the "to sort" list
+        $temp_sort[]=$item; //store it in the temporary sorted list...
+       unset($items[$item_id]); //...and remove it from the to sort 
list...
+       continue; //...and directly go to next item of user sort list
+      }
+    }
+    unset($item_id); //clear reference
+    unset($item); //clear reference
+  }
+  unset($sort_list_item); //clear reference
+  sort($items); //sort remaining (i.e. not present in user sort order) 
items from lowest to highest
+  $items=array_values(array_merge($temp_sort,$items)); //merge 
user-sorted and auto-sorted items ans clean array
+}
+
+/**
  * The following functions are userfunctions to use with the search
  * function above. This function is called for every found file or
  * directory. When a directory is given to the function it has to
-----------------------------------------------------------------------------------------------------------------------------------------------------------

Hope this patch will be useful.
I think it should be something simple to create a GUI plugin to manage 
mare friendly this plugin, but I've not time enough at now to do so.
Feel free to contact me if information is missing.

Thanks for everybody's work in Dokuwiki, hope this contribution have 
sense.

Kind regards,
Matthieu

-- 
DokuWiki mailing list - more info at
http://wiki.splitbrain.org/wiki:mailinglist

Other related posts: