forked from glensc/dokuwiki-plugin-pageredirect
-
Notifications
You must be signed in to change notification settings - Fork 0
/
action.php
129 lines (108 loc) · 3.79 KB
/
action.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
<?php
/**
* DokuWiki Plugin pageredirect (Action Component)
*
* @license GPL 2 http://www.gnu.org/licenses/gpl-2.0.html
* @author Elan Ruusamäe <glen@delfi.ee>
* @author David Lorentsen <zyberdog@quakenet.org>
*/
// must be run within Dokuwiki
if(!defined('DOKU_INC')) die();
class action_plugin_pageredirect extends DokuWiki_Action_Plugin {
/**
* Registers a callback function for a given event
*
* @param Doku_Event_Handler $controller DokuWiki's event controller object
* @return void
*/
public function register(Doku_Event_Handler $controller) {
/* @see action_plugin_pageredirect::handle_dokuwiki_started() */
$controller->register_hook('DOKUWIKI_STARTED', 'BEFORE', $this, 'handle_dokuwiki_started');
/* @see action_plugin_pageredirect::handle_parser_metadata_render() */
$controller->register_hook('PARSER_METADATA_RENDER','BEFORE', $this, 'handle_parser_metadata_render');
// This plugin goes first
// After PR#555
/* @see action_plugin_pageredirect::handle_tpl_act_render() */
$controller->register_hook('TPL_ACT_RENDER', 'BEFORE', $this, 'handle_tpl_act_render', null, -PHP_INT_MAX);
// Before PR#555, i.e 2013-12-08 release
if (isset($controller->_hook)) {
$hooks =& $controller->_hooks[TPL_ACT_RENDER_BEFORE];
if ($hooks[0][0] != $this) {
array_unshift($hooks, array_pop($hooks));
}
}
}
public function handle_dokuwiki_started(&$event, $param) {
global $ID, $ACT, $REV;
if (($ACT != 'show' && $ACT != '') || $REV) {
return;
}
$page = p_get_metadata($ID,'relation isreplacedby');
// return if no redirection data
if (empty($page)) {
return;
}
if (isset($_GET['redirect'])) {
// return if redirection is temporarily disabled,
// or we have been redirected 5 times in a row
if ($_GET['redirect'] == 'no' || $_GET['redirect'] > 4) {
return;
} elseif ($_GET['redirect'] > 0) {
$redirect = $_GET['redirect'] + 1;
} else {
$redirect = 1;
}
} else {
$redirect = 1;
}
// verify metadata currency
if (@filemtime(metaFN($ID,'.meta')) < @filemtime(wikiFN($ID))) {
return;
}
// preserve #section from $page
list($page, $section) = explode('#', $page, 2);
if (isset($section)) {
$section = '#' . $section;
} else {
$section = '';
}
// prepare link for internal redirects, keep external targets
if (!preg_match('#^https?://#i', $page)) {
$page = wl($page, array('redirect' => $redirect), TRUE, '&');
if (!headers_sent() && $this->getConf('show_note')) {
// remember to show note about being redirected from another page
session_start();
$_SESSION[DOKU_COOKIE]['redirect'] = $ID;
}
}
// redirect
header("HTTP/1.1 301 Moved Permanently");
header("Location: ".$page.$section.$_SERVER['QUERY_STRING']);
exit();
}
public function handle_tpl_act_render(&$event, $param) {
global $ID, $ACT;
if ($ACT != 'show' && $ACT != '') {
return;
}
if (!$this->getConf('show_note')) {
return;
}
if (isset($_GET['redirect']) && $_GET['redirect'] > 0 && $_GET['redirect'] < 6) {
if (isset($_SESSION[DOKU_COOKIE]['redirect']) && $_SESSION[DOKU_COOKIE]['redirect'] != '') {
// we were redirected from another page, show it!
$page = cleanID($_SESSION[DOKU_COOKIE]['redirect']);
$title = hsc(useHeading('navigation') && p_get_first_heading($page) ? p_get_first_heading($page) : $page);
echo '<div class="noteredirect">'.sprintf($this->getLang('redirected_from'), '<a href="'.wl(':'.$page, array('redirect' => 'no'), TRUE, '&').'" class="wikilink1" title="'.$page.'">'.$title.'</a>').'</div><br/>';
unset($_SESSION[DOKU_COOKIE]['redirect']);
return true;
}
}
return true;
}
public function handle_parser_metadata_render(&$event, $param) {
if (isset($event->data->meta['relation'])) {
unset($event->data->meta['relation']['isreplacedby']);
}
}
}