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
|
|
|
use dokuwiki\plugin\prosemirror\parser\SyntaxTreeBuilder; |
11
|
|
|
use dokuwiki\plugin\sentry\Event; |
|
|
|
|
12
|
|
|
|
13
|
|
|
if (!defined('DOKU_INC')) { |
14
|
|
|
die(); |
15
|
|
|
} |
16
|
|
|
|
17
|
|
|
class action_plugin_prosemirror_parser extends DokuWiki_Action_Plugin |
|
|
|
|
18
|
|
|
{ |
19
|
|
|
|
20
|
|
|
/** |
21
|
|
|
* Registers a callback function for a given event |
22
|
|
|
* |
23
|
|
|
* @param Doku_Event_Handler $controller DokuWiki's event controller object |
24
|
|
|
* |
25
|
|
|
* @return void |
26
|
|
|
*/ |
27
|
|
|
public function register(Doku_Event_Handler $controller) |
|
|
|
|
28
|
|
|
{ |
29
|
|
|
$controller->register_hook('ACTION_ACT_PREPROCESS', 'BEFORE', $this, 'handle_preprocess'); |
30
|
|
|
$controller->register_hook('COMMON_DRAFT_SAVE', 'BEFORE', $this, 'handle_draft'); |
31
|
|
|
} |
32
|
|
|
|
33
|
|
|
/** |
34
|
|
|
* Triggered by: COMMON_DRAFT_SAVE |
35
|
|
|
* |
36
|
|
|
* @param Doku_Event $event |
37
|
|
|
* @param $param |
38
|
|
|
*/ |
39
|
|
|
public function handle_draft(Doku_Event $event, $param) |
|
|
|
|
40
|
|
|
{ |
41
|
|
|
global $INPUT, $MSG; |
42
|
|
|
$unparsedJSON = $INPUT->post->str('prosemirror_json'); |
43
|
|
|
if (empty($unparsedJSON)) { |
44
|
|
|
return; |
45
|
|
|
} |
46
|
|
|
$syntax = $this->getSyntaxFromProsemirrorData($unparsedJSON); |
47
|
|
|
|
48
|
|
|
if (!empty($MSG) && $this->isAjax()) { |
49
|
|
|
echo '<div class="js-prosemirror-draft-errors" style="display: none;">'; |
50
|
|
|
html_msgarea(); |
|
|
|
|
51
|
|
|
echo '</div>'; |
52
|
|
|
} |
53
|
|
|
$event->data['text'] = $syntax; |
54
|
|
|
} |
55
|
|
|
|
56
|
|
|
/** |
57
|
|
|
* @return bool true if this is a ajax request, false if it is not |
58
|
|
|
*/ |
59
|
|
|
protected function isAjax() |
60
|
|
|
{ |
61
|
|
|
global $INPUT; |
62
|
|
|
return basename($INPUT->server->str('SCRIPT_NAME')) === 'ajax.php'; |
63
|
|
|
} |
64
|
|
|
|
65
|
|
|
/** |
66
|
|
|
* [Custom event handler which performs action] |
67
|
|
|
* |
68
|
|
|
* @param Doku_Event $event event object by reference |
69
|
|
|
* @param mixed $param [the parameters passed as fifth argument to register_hook() when this |
70
|
|
|
* handler was registered] |
71
|
|
|
* |
72
|
|
|
* @return void |
73
|
|
|
*/ |
74
|
|
|
public function handle_preprocess(Doku_Event $event, $param) |
75
|
|
|
{ |
76
|
|
|
global $TEXT, $INPUT; |
77
|
|
|
if ($INPUT->server->str('REQUEST_METHOD') !== 'POST' |
78
|
|
|
|| ($event->data !== 'save' && $event->data !== 'preview') |
79
|
|
|
|| !$INPUT->post->has('prosemirror_json') |
80
|
|
|
) { |
81
|
|
|
return; |
82
|
|
|
} |
83
|
|
|
$unparsedJSON = $INPUT->post->str('prosemirror_json'); |
84
|
|
|
$syntax = $this->getSyntaxFromProsemirrorData($unparsedJSON); |
85
|
|
|
if (!empty($syntax)) { |
86
|
|
|
$TEXT = $syntax; |
87
|
|
|
} |
88
|
|
|
} |
89
|
|
|
|
90
|
|
|
/** |
91
|
|
|
* Decode json and parse the data back into DokuWiki Syntax |
92
|
|
|
* |
93
|
|
|
* @param string $unparsedJSON the json produced by Prosemirror |
94
|
|
|
* |
95
|
|
|
* @return null|string DokuWiki syntax or null on error |
96
|
|
|
*/ |
97
|
|
|
protected function getSyntaxFromProsemirrorData($unparsedJSON) |
98
|
|
|
{ |
99
|
|
|
$prosemirrorData = json_decode($unparsedJSON, true); |
100
|
|
|
if ($prosemirrorData === null) { |
101
|
|
|
$errorMsg = 'Error decoding prosemirror data ' . json_last_error_msg(); |
102
|
|
|
|
103
|
|
|
/** @var helper_plugin_sentry $sentry */ |
104
|
|
|
$sentry = plugin_load('helper', 'sentry'); |
|
|
|
|
105
|
|
|
if ($sentry) { |
|
|
|
|
106
|
|
|
$event = new Event(['extra' => ['json' => $unparsedJSON]]); |
107
|
|
|
$exception = new ErrorException($errorMsg); // ToDo: create unique prosemirror exceptions? |
108
|
|
|
$event->addException($exception); |
109
|
|
|
$sentry->logEvent($event); |
110
|
|
|
|
111
|
|
|
$errorMsg .= ' Error has been logged to Sentry.'; |
112
|
|
|
} |
113
|
|
|
|
114
|
|
|
msg($errorMsg, -1); |
|
|
|
|
115
|
|
|
return null; |
116
|
|
|
} |
117
|
|
|
try { |
118
|
|
|
$rootNode = SyntaxTreeBuilder::parseDataIntoTree($prosemirrorData); |
119
|
|
|
} catch (Throwable $e) { |
120
|
|
|
$errorMsg = 'Parsing the data provided by the WYSIWYG editor failed with message: ' . hsc($e->getMessage()); |
|
|
|
|
121
|
|
|
/** @var helper_plugin_sentry $sentry */ |
122
|
|
|
$sentry = plugin_load('helper', 'sentry'); |
123
|
|
|
if ($sentry) { |
|
|
|
|
124
|
|
|
$sentry->logException($e); |
125
|
|
|
$errorMsg .= ' Error has been logged to Sentry.'; |
126
|
|
|
} |
127
|
|
|
|
128
|
|
|
msg($errorMsg, -1); |
129
|
|
|
return null; |
130
|
|
|
} |
131
|
|
|
$syntax = $rootNode->toSyntax(); |
132
|
|
|
return $syntax; |
133
|
|
|
} |
134
|
|
|
} |
135
|
|
|
|
136
|
|
|
// vim:ts=4:sw=4:et: |
137
|
|
|
|
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