forked from zioth/dokuwiki_fastwiki
-
Notifications
You must be signed in to change notification settings - Fork 0
/
action.php
259 lines (227 loc) · 9.07 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
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
<?php
if (!defined('DOKU_INC'))
die();
/**
* Fastwiki plugin, used for inline section editing, and loading of do= actions without a page refresh.
*
* @see http://dokuwiki.org/plugin:fastwiki
* @license GPL 2 http://www.gnu.org/licenses/gpl-2.0.html
* @author Eli Fenton
*/
class action_plugin_fastwiki extends DokuWiki_Action_Plugin {
protected $m_inPartial = false;
protected $m_no_content = false;
protected $m_preload_head = '====47hsjwycv782nwncv8b920m8bv72jmdm3929bno3b3====';
/**
* Register callback functions
*
* @param {Doku_Event_Handler} $controller DokuWiki's event controller object
*/
public function register(Doku_Event_Handler $controller) {
$controller->register_hook('DOKUWIKI_STARTED', 'BEFORE', $this, 'handle_start');
$controller->register_hook('DOKUWIKI_STARTED', 'AFTER', $this, 'override_loadskin');
$controller->register_hook('ACTION_ACT_PREPROCESS', 'BEFORE', $this, 'handle_action_before');
$controller->register_hook('ACTION_ACT_PREPROCESS', 'AFTER', $this, 'handle_action');
$controller->register_hook('TPL_ACT_RENDER', 'BEFORE', $this, 'pre_render');
$controller->register_hook('TPL_ACT_UNKNOWN', 'BEFORE', $this, 'unknown_action');
$controller->register_hook('ACTION_SHOW_REDIRECT', 'BEFORE', $this, 'handle_redirect');
$controller->register_hook('ACTION_HEADERS_SEND', 'BEFORE', $this, 'block_headers');
}
/**
* Start processing the request. This happens after doku init.
*
* @param {Doku_Event} $event - The DokuWiki event object.
* @param {mixed} $param - The fifth argument to register_hook().
*/
public function handle_start(Doku_Event &$event, $param) {
global $conf, $INPUT;
if ($INPUT->str('partial') == '1') {
$this->m_inPartial = true;
// Because so much is declared in global scope in doku.php, it's impossible to call tpl_content() without
// rendering the whole template. This hack loads a blank template, so we only render the page's inner content.
$conf['template'] = '../plugins/fastwiki/tplblank';
}
else {
global $lang, $JSINFO;
$JSINFO['fastwiki'] = array(
// Configuration
'secedit' => $this->getConf('secedit'),
'preview' => $this->getConf('preview'),
'fastpages' => $this->getConf('fastpages'),
'save' => $this->getConf('save'),
'fastshow' => $this->getConf('fastshow'),
'fastshow_same_ns' => $this->getConf('fastshow_same_ns'),
'fastshow_include' => $this->getConf('fastshow_include'),
'fastshow_exclude' => $this->getConf('fastshow_exclude'),
'preload' => $this->getConf('preload'),
'preload_head' => $this->m_preload_head,
'preload_batchsize'=> $this->getConf('preload_batchsize'),
'preload_per_page' => $this->getConf('preload_per_page'),
// Needed for the initialization of the partial edit page.
'locktime' => $conf['locktime'] - 60,
'usedraft' => $conf['usedraft'] ? $conf['usedraft'] : '0',
// Miscellaneous
'text_btn_show' => $lang['btn_show'],
'templatename' => $conf['template']
);
}
}
/**
* The Loadskin plugin changes $conf['template'] in multiple places. Make sure we cover them all.
*
* @param {Doku_Event} $event - The DokuWiki event object.
* @param {mixed} $param - The fifth argument to register_hook().
*/
public function override_loadskin(Doku_Event &$event, $param) {
global $conf;
if ($this->m_inPartial)
$conf['template'] = '../plugins/fastwiki/tplblank';
}
/**
* Define special actions.
*
* @param {Doku_Event} $event - The DokuWiki event object.
* @param {mixed} $param - The fifth argument to register_hook().
*/
public function unknown_action(Doku_Event &$event, $param) {
if ($event->data == 'fastwiki_preload')
$event->preventDefault();
}
/**
* Don't output headers while proxying preload pages.
*
* @param {Doku_Event} $event - The DokuWiki event object.
* @param {mixed} $param - The fifth argument to register_hook().
*/
public function block_headers(Doku_Event &$event, $param) {
global $INPUT;
if ($INPUT->str('fastwiki_preload_proxy'))
$event->preventDefault();
}
/**
* Hook into the pre-processor for the action handler to catch subscribe sub-actions before the action name changes.
*
* @param {Doku_Event} $event - The DokuWiki event object.
* @param {mixed} $param - The fifth argument to register_hook().
*/
public function handle_action_before(Doku_Event &$event, $param) {
if (!$this->m_inPartial)
return;
global $ACT, $INPUT;
// For partials, we don't want output from subscribe actions -- just success/error messages.
if ($ACT == 'subscribe' && $INPUT->str('sub_action'))
$this->m_no_content = true;
// Preload page content.
else if ($this->getConf('preload') && $ACT == 'fastwiki_preload') {
$event->preventDefault();
$maxpages = $this->getConf('preload_batchsize');
$pages = split(',', $INPUT->str('fastwiki_preload_pages'));
$count = min($maxpages, count($pages));
$requests = array();
$cookies = array();
foreach ($_COOKIE as $name=>$value)
array_push($cookies, $name . '=' . addslashes($value));
for ($x=0; $x<$count; $x++) {
$newid = cleanID($pages[$x]);
// ACL must be exactly the same.
if (page_exists($newid) && (auth_quickaclcheck($ID) == auth_quickaclcheck($newid))) {
// Because there's no way to call doku recursively, curl is the only way to get a fresh context.
// Without a fresh context, there's no easy way to get action plugins to run or TOC to render properly.
$ch = curl_init(DOKU_URL.'doku.php');
curl_setopt($ch, CURLOPT_POST, 1);
curl_setopt($ch, CURLOPT_POSTFIELDS, "id={$newid}&partial=1&fastwiki_preload_proxy=1");
curl_setopt($ch, CURLOPT_COOKIE, join('; ', $cookies));
curl_setopt($ch, CURLOPT_FOLLOWLOCATION, 0); // Ignore redirects
curl_setopt($ch, CURLOPT_HEADER, 0);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
array_push($requests, array($ch, $newid));
}
}
// Request URLs with multiple threads.
// TODO: This currently hangs. Enable the array_push above, and remove curl_exec, to test.
if (count($requests) > 0) {
$multicurl = curl_multi_init();
foreach ($requests as $req)
curl_multi_add_handle($multicurl, $req[0]);
$active = null;
// Strange loop becuase php 5.3.18 broke curl_multi_select
do {
do {
$mrc = curl_multi_exec($multicurl, $active);
} while ($mrc == CURLM_CALL_MULTI_PERFORM);
// Wait 10ms to fix a bug where multi_select returns -1 forever.
usleep(10000);
} while(curl_multi_select($multicurl) === -1);
while ($active && $mrc == CURLM_OK) {
if (curl_multi_select($multicurl) != -1) {
do {
$mrc = curl_multi_exec($multicurl, $active);
} while ($mrc == CURLM_CALL_MULTI_PERFORM);
}
}
foreach ($requests as $idx=>$req) {
if ($idx > 0)
print $this->m_preload_head;
print $req[1] . "\n";
echo curl_multi_getcontent($req[0]);
curl_multi_remove_handle($multicurl, $req[0]);
}
curl_multi_close($multicurl);
}
}
}
/**
* Handle the "partial" action, using the blank template to deliver nothing but the inner page content.
*
* @param {Doku_Event} $event - The DokuWiki event object.
* @param {mixed} $param - The fifth argument to register_hook().
*/
public function handle_action(Doku_Event &$event, $param) {
if (!$this->m_inPartial)
return;
global $ACT, $INPUT, $ID;
// Compare permissions between the current page and the passed-in id.
$compareid = $INPUT->str('fastwiki_compareid');
if ($compareid && (auth_quickaclcheck($ID) != auth_quickaclcheck($compareid)))
echo 'PERMISSION_CHANGE';
// Some partials only want an error message.
else if (!$this->m_no_content) {
if ($ACT == 'show')
tpl_toc();
// Section save. This won't work, unless I return new "range" inputs for all sections.
// $secedit = $ACT == 'show' && $INPUT->str('target') == 'section' && ($INPUT->str('prefix') || $INPUT->str('suffix'));
// if ($secedit)
// $this->render_text($INPUT->str('wikitext')); //+++ render_text isn't outputting anything.
// else
tpl_content(false);
}
// Output error messages.
html_msgarea();
}
/**
* The template is about to render the main content area. Plop in a marker div so the javascript can
* figure out where the main content area is. NOTE: Templates that don't wrap tpl_content()
* in an HTML tag won't work with this plugin.
*
* @param {Doku_Event} $event - The DokuWiki event object.
* @param {mixed} $param - The fifth argument to register_hook().
*/
public function pre_render(Doku_Event &$event, $param) {
if (!$this->m_inPartial)
print '<div class="plugin_fastwiki_marker" style="display:none"></div>';
}
/**
* Some actions normally redirect. Block that for partials.
*
* @param {Doku_Event} $event - The DokuWiki event object.
* @param {mixed} $param - The fifth argument to register_hook().
*/
function handle_redirect(Doku_Event &$event, $param) {
global $ACT;
if ($this->m_inPartial && ($event->data['preact'] == 'subscribe') || ($event->data['preact'] == 'save')) {
// Undo the action override, which sets $ACT to 'show.'
$ACT = $event->data['preact'];
$event->preventDefault();
}
}
}