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