xref: /dokuwiki/lib/plugins/safefnrecode/action.php (revision f7f0e633f735842dfb466878eef440245a9bd3e1)
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
9b9d65e9dSAndreas Gohr// must be run within Dokuwiki
10b9d65e9dSAndreas Gohrif (!defined('DOKU_INC')) die();
11b9d65e9dSAndreas Gohr
12b9d65e9dSAndreas Gohrrequire_once DOKU_PLUGIN.'action.php';
13b9d65e9dSAndreas Gohr
14b9d65e9dSAndreas Gohrclass action_plugin_safefnrecode extends DokuWiki_Action_Plugin {
15b9d65e9dSAndreas Gohr
16b9d65e9dSAndreas Gohr    public function register(Doku_Event_Handler &$controller) {
17b9d65e9dSAndreas Gohr
18b9d65e9dSAndreas Gohr       $controller->register_hook('INDEXER_TASKS_RUN', 'BEFORE', $this, 'handle_indexer_tasks_run');
19b9d65e9dSAndreas Gohr
20b9d65e9dSAndreas Gohr    }
21b9d65e9dSAndreas Gohr
22b9d65e9dSAndreas Gohr    public function handle_indexer_tasks_run(Doku_Event &$event, $param) {
23b9d65e9dSAndreas Gohr        global $conf;
24b9d65e9dSAndreas Gohr        if($conf['fnencode'] != 'safe') return;
25b9d65e9dSAndreas Gohr
26b9d65e9dSAndreas Gohr        if(!file_exists($conf['datadir'].'_safefn.recoded')){
27b9d65e9dSAndreas Gohr            $this->recode($conf['datadir']);
28b9d65e9dSAndreas Gohr            touch($conf['datadir'].'_safefn.recoded');
29b9d65e9dSAndreas Gohr        }
30b9d65e9dSAndreas Gohr
31b9d65e9dSAndreas Gohr        if(!file_exists($conf['olddir'].'_safefn.recoded')){
32b9d65e9dSAndreas Gohr            $this->recode($conf['olddir']);
33b9d65e9dSAndreas Gohr            touch($conf['olddir'].'_safefn.recoded');
34b9d65e9dSAndreas Gohr        }
35b9d65e9dSAndreas Gohr
36b9d65e9dSAndreas Gohr        if(!file_exists($conf['metadir'].'_safefn.recoded')){
37b9d65e9dSAndreas Gohr            $this->recode($conf['metadir']);
38b9d65e9dSAndreas Gohr            touch($conf['metadir'].'_safefn.recoded');
39b9d65e9dSAndreas Gohr        }
40b9d65e9dSAndreas Gohr
41b9d65e9dSAndreas Gohr        if(!file_exists($conf['mediadir'].'_safefn.recoded')){
42*f7f0e633SMyron Turner            $this->recode($conf['mediadir']);
43*f7f0e633SMyron Turner            touch($conf['mediadir'].'_safefn.recoded');
44b9d65e9dSAndreas Gohr        }
45b9d65e9dSAndreas Gohr
46b9d65e9dSAndreas Gohr    }
47b9d65e9dSAndreas Gohr
48b9d65e9dSAndreas Gohr    /**
49b9d65e9dSAndreas Gohr     * Recursive function to rename all safe encoded files to use the new
50b9d65e9dSAndreas Gohr     * square bracket post indicator
51b9d65e9dSAndreas Gohr     */
52b9d65e9dSAndreas Gohr    private function recode($dir){
53b9d65e9dSAndreas Gohr        $dh = opendir($dir);
54b9d65e9dSAndreas Gohr        if(!$dh) return;
55b9d65e9dSAndreas Gohr        while (($file = readdir($dh)) !== false) {
56b9d65e9dSAndreas Gohr            if($file == '.' || $file == '..') continue;           # cur and upper dir
57b9d65e9dSAndreas Gohr            if(is_dir("$dir/$file")) $this->recode("$dir/$file"); #recurse
58*f7f0e633SMyron Turner            if(strpos($file,'%') === false) continue;             # no encoding used
59903e5bc8SAndreas Gohr            $new = preg_replace('/(%[^\]]*?)\./','\1]',$file);    # new post indicator
60aa235a70SAndreas Gohr            if(preg_match('/%[^\]]+$/',$new)) $new .= ']';        # fix end FS#2122
61b9d65e9dSAndreas Gohr            rename("$dir/$file","$dir/$new");                     # rename it
62b9d65e9dSAndreas Gohr        }
63b9d65e9dSAndreas Gohr        closedir($dh);
64b9d65e9dSAndreas Gohr    }
65b9d65e9dSAndreas Gohr
66b9d65e9dSAndreas Gohr}
67b9d65e9dSAndreas Gohr
68b9d65e9dSAndreas Gohr// vim:ts=4:sw=4:et:
69