You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
 
 
 
 

72 lines
2.3 KiB

  1. <?php
  2. use dokuwiki\Extension\ActionPlugin;
  3. use dokuwiki\Extension\EventHandler;
  4. use dokuwiki\Extension\Event;
  5. /**
  6. * DokuWiki Plugin safefnrecode (Action Component)
  7. *
  8. * @license GPL 2 http://www.gnu.org/licenses/gpl-2.0.html
  9. * @author Andreas Gohr <andi@splitbrain.org>
  10. */
  11. class action_plugin_safefnrecode extends ActionPlugin
  12. {
  13. /** @inheritdoc */
  14. public function register(EventHandler $controller)
  15. {
  16. $controller->register_hook('INDEXER_TASKS_RUN', 'BEFORE', $this, 'handleIndexerTasksRun');
  17. }
  18. /**
  19. * Handle indexer event
  20. *
  21. * @param Event $event
  22. * @param $param
  23. */
  24. public function handleIndexerTasksRun(Event $event, $param)
  25. {
  26. global $conf;
  27. if ($conf['fnencode'] != 'safe') return;
  28. if (!file_exists($conf['datadir'] . '_safefn.recoded')) {
  29. $this->recode($conf['datadir']);
  30. touch($conf['datadir'] . '_safefn.recoded');
  31. }
  32. if (!file_exists($conf['olddir'] . '_safefn.recoded')) {
  33. $this->recode($conf['olddir']);
  34. touch($conf['olddir'] . '_safefn.recoded');
  35. }
  36. if (!file_exists($conf['metadir'] . '_safefn.recoded')) {
  37. $this->recode($conf['metadir']);
  38. touch($conf['metadir'] . '_safefn.recoded');
  39. }
  40. if (!file_exists($conf['mediadir'] . '_safefn.recoded')) {
  41. $this->recode($conf['mediadir']);
  42. touch($conf['mediadir'] . '_safefn.recoded');
  43. }
  44. }
  45. /**
  46. * Recursive function to rename all safe encoded files to use the new
  47. * square bracket post indicator
  48. */
  49. private function recode($dir)
  50. {
  51. $dh = opendir($dir);
  52. if (!$dh) return;
  53. while (($file = readdir($dh)) !== false) {
  54. if ($file == '.' || $file == '..') continue; # cur and upper dir
  55. if (is_dir("$dir/$file")) $this->recode("$dir/$file"); #recurse
  56. if (strpos($file, '%') === false) continue; # no encoding used
  57. $new = preg_replace('/(%[^\]]*?)\./', '\1]', $file); # new post indicator
  58. if (preg_match('/%[^\]]+$/', $new)) $new .= ']'; # fix end FS#2122
  59. rename("$dir/$file", "$dir/$new"); # rename it
  60. }
  61. closedir($dh);
  62. }
  63. }