1<?php
2
3if(!defined('DOKU_INC')) die();
4
5if(class_exists("helper_plugin_redirect", TRUE)) {
6
7    class action_plugin_fixredirectlinks extends DokuWiki_Action_Plugin {
8
9        /**
10         * Register its handlers with the DokuWiki's event controller
11         */
12        public function register(Doku_Event_Handler $controller) {
13
14            $controller->register_hook('RENDERER_CONTENT_POSTPROCESS', 'AFTER', $this, 'fix_redirect_links');
15
16        }
17
18        /**
19         * @param Doku_Event $event  event object by reference
20         * @param mixed      $param  empty
21         * @param string     $advise the advise the hook receives
22         */
23        public function fix_redirect_links (&$event, $param=null) {
24            global $INFO;
25
26            $redirHelper = new helper_plugin_redirect();
27
28            libxml_use_internal_errors(TRUE);
29            $domDoc = new DOMDocument('1.0', 'UTF-8');
30            $domDoc->loadHTML(mb_convert_encoding($event->data[1], 'HTML-ENTITIES', "UTF-8"), LIBXML_HTML_NODEFDTD);
31
32            $aTags = $domDoc->getElementsByTagName("a");
33            $nb = $aTags->length;
34            for($pos=0; $pos<$nb; $pos++) {
35                $node = $aTags[$pos];
36                $classes = $node->getAttribute("class");
37                if(preg_match('/wikilink2/i', $classes)){
38                    $title = $node->getAttribute("title");
39
40                    $redirects = $redirHelper->getRedirectURL($title);
41
42                    if($redirects) {
43                        $url = parse_url($redirects);
44                        $id = "";
45                        if($url["query"]) {
46                            $query = parse_str($url["query"]);
47                            if($query["id"]) $id = $query["id"];
48                        }
49                        if($url["path"]) {
50                            $path = $url["path"];
51                            $dokuRel =  trim(DOKU_REL, "/");
52                            $cleanPath = ltrim(preg_replace(["/$dokuRel/i", '/\/doku.php/i'], ["", ""], $path), "/");
53                            if($cleanPath) {
54                                $cleanPath = str_replace("/", ":", $cleanPath);
55                                $id = $cleanPath;
56                            }
57                        }
58                        if(page_exists($id)) {
59                            $newClasses = preg_replace('/wikilink2/i', "wikilink1", $classes);
60                            $node->removeAttribute('class');
61
62                            if($id == $INFO['id']) {
63                                $newClasses .= " active";
64                            }
65                            $node->setAttribute("class", $newClasses);
66                        }
67                    }
68
69                }
70            }
71            $body = $domDoc->getElementsByTagName('body')->item(0);
72            $event->data[1] = "";
73            foreach ($body->childNodes as $childNode) {
74                $event->data[1] .= $domDoc->saveHTML($childNode);
75            }
76        }
77    }
78}