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