1
|
|
|
<?php |
2
|
|
|
/** |
3
|
|
|
* DokuWiki Plugin prosemirror (Action Component) |
4
|
|
|
* |
5
|
|
|
* @license GPL 2 http://www.gnu.org/licenses/gpl-2.0.html |
6
|
|
|
* @author Andreas Gohr <[email protected]> |
7
|
|
|
*/ |
8
|
|
|
|
9
|
|
|
// must be run within Dokuwiki |
10
|
|
|
|
11
|
|
|
use dokuwiki\plugin\prosemirror\parser\LinkNode; |
12
|
|
|
|
13
|
|
|
if (!defined('DOKU_INC')) { |
14
|
|
|
die(); |
15
|
|
|
} |
16
|
|
|
|
17
|
|
|
class action_plugin_prosemirror_ajax extends DokuWiki_Action_Plugin |
|
|
|
|
18
|
|
|
{ |
19
|
|
|
/** |
20
|
|
|
* Registers a callback function for a given event |
21
|
|
|
* |
22
|
|
|
* @param Doku_Event_Handler $controller DokuWiki's event controller object |
23
|
|
|
* |
24
|
|
|
* @return void |
25
|
|
|
*/ |
26
|
|
|
public function register(Doku_Event_Handler $controller) |
|
|
|
|
27
|
|
|
{ |
28
|
|
|
$controller->register_hook('AJAX_CALL_UNKNOWN', 'BEFORE', $this, 'handleAjax'); |
29
|
|
|
$controller->register_hook('AJAX_CALL_UNKNOWN', 'BEFORE', $this, 'switchEditors'); |
30
|
|
|
} |
31
|
|
|
|
32
|
|
|
/** |
33
|
|
|
* [Custom event handler which performs action] |
34
|
|
|
* |
35
|
|
|
* Event: AJAX_CALL_UNKNOWN |
36
|
|
|
* |
37
|
|
|
* @param Doku_Event $event event object by reference |
38
|
|
|
* @param mixed $param [the parameters passed as fifth argument to register_hook() when this |
39
|
|
|
* handler was registered] |
40
|
|
|
* |
41
|
|
|
* @return void |
42
|
|
|
*/ |
43
|
|
|
public function handleAjax(Doku_Event $event, $param) |
|
|
|
|
44
|
|
|
{ |
45
|
|
|
if ($event->data !== 'plugin_prosemirror') { |
46
|
|
|
return; |
47
|
|
|
} |
48
|
|
|
$event->preventDefault(); |
49
|
|
|
$event->stopPropagation(); |
50
|
|
|
|
51
|
|
|
global $INPUT, $ID; |
52
|
|
|
$ID = $INPUT->str('id'); |
53
|
|
|
$responseData = []; |
54
|
|
|
foreach ($INPUT->arr('actions') as $action) { |
55
|
|
|
switch ($action) { |
56
|
|
|
case 'resolveInternalLink': |
57
|
|
|
{ |
58
|
|
|
$inner = $INPUT->str('inner'); |
59
|
|
|
$responseData[$action] = $this->resolveInternalLink($inner, $ID); |
60
|
|
|
break; |
61
|
|
|
} |
62
|
|
|
case 'resolveInterWikiLink': |
63
|
|
|
{ |
64
|
|
|
$inner = $INPUT->str('inner'); |
65
|
|
|
list($shortcut, $reference) = explode('>', $inner); |
66
|
|
|
$responseData[$action] = $this->resolveInterWikiLink($shortcut, $reference); |
67
|
|
|
break; |
68
|
|
|
} |
69
|
|
|
case 'resolveMedia': |
70
|
|
|
{ |
71
|
|
|
$attrs = $INPUT->arr('attrs'); |
72
|
|
|
$responseData[$action] = [ |
73
|
|
|
'data-resolvedHtml' => \dokuwiki\plugin\prosemirror\parser\ImageNode::resolveMedia( |
74
|
|
|
$attrs['id'], |
75
|
|
|
$attrs['title'], |
76
|
|
|
$attrs['align'], |
77
|
|
|
$attrs['width'], |
78
|
|
|
$attrs['height'], |
79
|
|
|
$attrs['cache'], |
80
|
|
|
$attrs['linking'] |
81
|
|
|
) |
82
|
|
|
]; |
83
|
|
|
break; |
84
|
|
|
} |
85
|
|
|
case 'resolveImageTitle': |
86
|
|
|
{ |
87
|
|
|
$image = $INPUT->arr('image'); |
88
|
|
|
$responseData[$action] = []; |
89
|
|
|
$responseData[$action]['data-resolvedImage'] = LinkNode::resolveImageTitle( |
90
|
|
|
$ID, |
91
|
|
|
$image['id'], |
92
|
|
|
$image['title'], |
93
|
|
|
$image['align'], |
94
|
|
|
$image['width'], |
95
|
|
|
$image['height'], |
96
|
|
|
$image['cache'] |
97
|
|
|
); |
98
|
|
|
break; |
99
|
|
|
} |
100
|
|
|
default: |
101
|
|
|
{ |
102
|
|
|
dbglog('Unknown action: ' . $INPUT->str('action'), __FILE__ . ': ' . __LINE__); |
|
|
|
|
103
|
|
|
http_status(400, 'unknown action'); |
|
|
|
|
104
|
|
|
return; |
105
|
|
|
} |
106
|
|
|
} |
107
|
|
|
} |
108
|
|
|
|
109
|
|
|
echo json_encode($responseData); |
110
|
|
|
} |
111
|
|
|
|
112
|
|
|
protected function resolveInterWikiLink($shortcut, $reference) |
113
|
|
|
{ |
114
|
|
|
$xhtml_renderer = p_get_renderer('xhtml'); |
|
|
|
|
115
|
|
|
$xhtml_renderer->interwiki = getInterwiki(); |
|
|
|
|
116
|
|
|
$url = $xhtml_renderer->_resolveInterWiki($shortcut, $reference, $exits); |
|
|
|
|
117
|
|
|
return [ |
118
|
|
|
'url' => $url, |
119
|
|
|
'resolvedClass' => 'interwikilink interwiki iw_' . $shortcut, |
120
|
|
|
]; |
121
|
|
|
} |
122
|
|
|
|
123
|
|
|
protected function resolveInternalLink($inner, $curId) |
124
|
|
|
{ |
125
|
|
|
if ($inner[0] === '#') { |
126
|
|
|
return dokuwiki\plugin\prosemirror\parser\LocalLinkNode::resolveLocalLink($inner, $curId); |
127
|
|
|
} |
128
|
|
|
return \dokuwiki\plugin\prosemirror\parser\InternalLinkNode::resolveLink($inner, $curId); |
129
|
|
|
} |
130
|
|
|
|
131
|
|
|
/** |
132
|
|
|
* [Custom event handler which performs action] |
133
|
|
|
* |
134
|
|
|
* Event: AJAX_CALL_UNKNOWN |
135
|
|
|
* |
136
|
|
|
* @param Doku_Event $event event object by reference |
137
|
|
|
* @param mixed $param [the parameters passed as fifth argument to register_hook() when this |
138
|
|
|
* handler was registered] |
139
|
|
|
* |
140
|
|
|
* @return void |
141
|
|
|
*/ |
142
|
|
|
public function switchEditors(Doku_Event $event, $param) |
143
|
|
|
{ |
144
|
|
|
if ($event->data !== 'plugin_prosemirror_switch_editors') { |
145
|
|
|
return; |
146
|
|
|
} |
147
|
|
|
$event->preventDefault(); |
148
|
|
|
$event->stopPropagation(); |
149
|
|
|
|
150
|
|
|
global $INPUT; |
151
|
|
|
|
152
|
|
|
if ($INPUT->bool('getJSON')) { |
153
|
|
|
$text = $INPUT->str('data'); |
154
|
|
|
$instructions = p_get_instructions($text); |
|
|
|
|
155
|
|
|
try { |
156
|
|
|
$prosemirrorJSON = p_render('prosemirror', $instructions, $info); |
|
|
|
|
157
|
|
|
} catch (Throwable $e) { |
158
|
|
|
$errorMsg = 'Rendering the page\'s syntax for the WYSIWYG editor failed'; |
159
|
|
|
|
160
|
|
|
/** @var \helper_plugin_prosemirror $helper */ |
161
|
|
|
$helper = plugin_load('helper', 'prosemirror'); |
|
|
|
|
162
|
|
|
if ($helper->tryToLogErrorToSentry($e, ['text' => $text])) { |
163
|
|
|
$errorMsg .= ' -- The error has been logged to Sentry.'; |
164
|
|
|
} |
165
|
|
|
|
166
|
|
|
http_status(500); |
|
|
|
|
167
|
|
|
header('Content-Type: application/json'); |
168
|
|
|
echo json_encode(['error' => $errorMsg]); |
169
|
|
|
return; |
170
|
|
|
} |
171
|
|
|
$responseData = [ |
172
|
|
|
'json' => $prosemirrorJSON, |
173
|
|
|
]; |
174
|
|
|
} else { |
175
|
|
|
/** @var \helper_plugin_prosemirror $helper */ |
176
|
|
|
$helper = plugin_load('helper', 'prosemirror'); |
177
|
|
|
$json = $INPUT->str('data'); |
178
|
|
|
try { |
179
|
|
|
$syntax = $helper->getSyntaxFromProsemirrorData($json); |
180
|
|
|
} catch (Throwable $e) { |
181
|
|
|
$errorMsg = 'Parsing the data generated by Prosemirror failed with message: "'; |
182
|
|
|
$errorMsg .= $e->getMessage(); |
183
|
|
|
$errorMsg .= '"'; |
184
|
|
|
|
185
|
|
|
if ($helper->tryToLogErrorToSentry($e, ['json' => $json])) { |
186
|
|
|
$errorMsg .= ' -- The error has been logged to Sentry.'; |
187
|
|
|
} |
188
|
|
|
|
189
|
|
|
http_status(500); |
190
|
|
|
header('Content-Type: application/json'); |
191
|
|
|
echo json_encode(['error' => $errorMsg]); |
192
|
|
|
return; |
193
|
|
|
} |
194
|
|
|
$responseData = [ |
195
|
|
|
'text' => $syntax, |
196
|
|
|
]; |
197
|
|
|
} |
198
|
|
|
|
199
|
|
|
header('Content-Type: application/json'); |
200
|
|
|
echo json_encode($responseData); |
201
|
|
|
} |
202
|
|
|
} |
203
|
|
|
|
The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g.
excluded_paths: ["lib/*"]
, you can move it to the dependency path list as follows:For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths