1b9d65e9dSAndreas Gohr<?php 28553d24dSAndreas Gohr 38553d24dSAndreas Gohruse dokuwiki\Extension\ActionPlugin; 48553d24dSAndreas Gohruse dokuwiki\Extension\EventHandler; 58553d24dSAndreas Gohruse dokuwiki\Extension\Event; 6*d4f83172SAndreas Gohr 7b9d65e9dSAndreas Gohr/** 8b9d65e9dSAndreas Gohr * DokuWiki Plugin safefnrecode (Action Component) 9b9d65e9dSAndreas Gohr * 10b9d65e9dSAndreas Gohr * @license GPL 2 http://www.gnu.org/licenses/gpl-2.0.html 11b9d65e9dSAndreas Gohr * @author Andreas Gohr <andi@splitbrain.org> 12b9d65e9dSAndreas Gohr */ 138553d24dSAndreas Gohrclass action_plugin_safefnrecode extends ActionPlugin 14661d78e8SAndreas Gohr{ 15661d78e8SAndreas Gohr /** @inheritdoc */ 168553d24dSAndreas Gohr public function register(EventHandler $controller) 17661d78e8SAndreas Gohr { 18661d78e8SAndreas Gohr $controller->register_hook('INDEXER_TASKS_RUN', 'BEFORE', $this, 'handleIndexerTasksRun'); 19b9d65e9dSAndreas Gohr } 20b9d65e9dSAndreas Gohr 21661d78e8SAndreas Gohr /** 22661d78e8SAndreas Gohr * Handle indexer event 23661d78e8SAndreas Gohr * 24661d78e8SAndreas Gohr * @param Doku_Event $event 25661d78e8SAndreas Gohr * @param $param 26661d78e8SAndreas Gohr */ 278553d24dSAndreas Gohr public function handleIndexerTasksRun(Event $event, $param) 28661d78e8SAndreas Gohr { 29b9d65e9dSAndreas Gohr global $conf; 30b9d65e9dSAndreas Gohr if ($conf['fnencode'] != 'safe') return; 31b9d65e9dSAndreas Gohr 32b9d65e9dSAndreas Gohr if (!file_exists($conf['datadir'] . '_safefn.recoded')) { 33b9d65e9dSAndreas Gohr $this->recode($conf['datadir']); 34b9d65e9dSAndreas Gohr touch($conf['datadir'] . '_safefn.recoded'); 35b9d65e9dSAndreas Gohr } 36b9d65e9dSAndreas Gohr 37b9d65e9dSAndreas Gohr if (!file_exists($conf['olddir'] . '_safefn.recoded')) { 38b9d65e9dSAndreas Gohr $this->recode($conf['olddir']); 39b9d65e9dSAndreas Gohr touch($conf['olddir'] . '_safefn.recoded'); 40b9d65e9dSAndreas Gohr } 41b9d65e9dSAndreas Gohr 42b9d65e9dSAndreas Gohr if (!file_exists($conf['metadir'] . '_safefn.recoded')) { 43b9d65e9dSAndreas Gohr $this->recode($conf['metadir']); 44b9d65e9dSAndreas Gohr touch($conf['metadir'] . '_safefn.recoded'); 45b9d65e9dSAndreas Gohr } 46b9d65e9dSAndreas Gohr 47b9d65e9dSAndreas Gohr if (!file_exists($conf['mediadir'] . '_safefn.recoded')) { 48f7f0e633SMyron Turner $this->recode($conf['mediadir']); 49f7f0e633SMyron Turner touch($conf['mediadir'] . '_safefn.recoded'); 50b9d65e9dSAndreas Gohr } 51b9d65e9dSAndreas Gohr } 52b9d65e9dSAndreas Gohr 53b9d65e9dSAndreas Gohr /** 54b9d65e9dSAndreas Gohr * Recursive function to rename all safe encoded files to use the new 55b9d65e9dSAndreas Gohr * square bracket post indicator 56b9d65e9dSAndreas Gohr */ 57661d78e8SAndreas Gohr private function recode($dir) 58661d78e8SAndreas Gohr { 59b9d65e9dSAndreas Gohr $dh = opendir($dir); 60b9d65e9dSAndreas Gohr if (!$dh) return; 61b9d65e9dSAndreas Gohr while (($file = readdir($dh)) !== false) { 62b9d65e9dSAndreas Gohr if ($file == '.' || $file == '..') continue; # cur and upper dir 63b9d65e9dSAndreas Gohr if (is_dir("$dir/$file")) $this->recode("$dir/$file"); #recurse 64f7f0e633SMyron Turner if (strpos($file, '%') === false) continue; # no encoding used 65903e5bc8SAndreas Gohr $new = preg_replace('/(%[^\]]*?)\./', '\1]', $file); # new post indicator 66aa235a70SAndreas Gohr if (preg_match('/%[^\]]+$/', $new)) $new .= ']'; # fix end FS#2122 67b9d65e9dSAndreas Gohr rename("$dir/$file", "$dir/$new"); # rename it 68b9d65e9dSAndreas Gohr } 69b9d65e9dSAndreas Gohr closedir($dh); 70b9d65e9dSAndreas Gohr } 71b9d65e9dSAndreas Gohr} 72