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