action.php 2.2 KB

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