~fabiocbalbuquerque/sahana-agasti/web-services

« back to all changes in this revision

Viewing changes to web/wiki/lib/plugins/safefnrecode/action.php

  • Committer: Chad Heuschober
  • Date: 2011-06-06 13:37:45 UTC
  • mfrom: (1.1.1244 trunk)
  • mto: This revision was merged to the branch mainline in revision 17.
  • Revision ID: chad.heuschober@mail.cuny.edu-20110606133745-850mdvnjtv392zta
Pulled in most recent batch of changes from the cuny sps trunk.

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
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
 
10
if (!defined('DOKU_INC')) die();
 
11
 
 
12
require_once DOKU_PLUGIN.'action.php';
 
13
 
 
14
class 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: