Passed
Push — master ( d5fcc5...3f9db4 )
by Andreas
02:52
created

action_plugin_prosemirror_parser::handle_draft()   B

Complexity

Conditions 4
Paths 4

Size

Total Lines 30
Code Lines 19

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 4
eloc 19
nc 4
nop 2
dl 0
loc 30
rs 8.5806
c 0
b 0
f 0
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;
1 ignored issue
show
Bug introduced by
The type dokuwiki\plugin\sentry\Event was not found. Maybe you did not declare it correctly or list all dependencies?

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:

filter:
    dependency_paths: ["lib/*"]

For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths

Loading history...
12
13
if (!defined('DOKU_INC')) {
14
    die();
15
}
16
17
class action_plugin_prosemirror_parser extends DokuWiki_Action_Plugin
1 ignored issue
show
Bug introduced by
The type DokuWiki_Action_Plugin was not found. Maybe you did not declare it correctly or list all dependencies?

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:

filter:
    dependency_paths: ["lib/*"]

For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths

Loading history...
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)
1 ignored issue
show
Bug introduced by
The type Doku_Event_Handler was not found. Maybe you did not declare it correctly or list all dependencies?

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:

filter:
    dependency_paths: ["lib/*"]

For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths

Loading history...
28
    {
29
        $controller->register_hook('ACTION_ACT_PREPROCESS', 'BEFORE', $this, 'handle_preprocess');
30
        $controller->register_hook('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)
1 ignored issue
show
Bug introduced by
The type Doku_Event was not found. Maybe you did not declare it correctly or list all dependencies?

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:

filter:
    dependency_paths: ["lib/*"]

For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths

Loading history...
40
    {
41
        global $INPUT;
42
        $unparsedJSON = $INPUT->post->str('prosemirror_json');
43
        if (empty($unparsedJSON)) {
44
            return;
45
        }
46
        try {
47
            $syntax = $this->getSyntaxFromProsemirrorData($unparsedJSON);
48
        } catch (\Throwable $e) {
49
            $event->preventDefault();
50
            $event->stopPropagation();
51
52
            $errorMsg = $e->getMessage();
53
54
            /** @var helper_plugin_sentry $sentry */
55
            $sentry = plugin_load('helper', 'sentry');
1 ignored issue
show
Bug introduced by
The function plugin_load was not found. Maybe you did not declare it correctly or list all dependencies? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

55
            $sentry = /** @scrutinizer ignore-call */ plugin_load('helper', 'sentry');
Loading history...
56
            if ($sentry) {
0 ignored issues
show
introduced by
$sentry is of type helper_plugin_sentry, thus it always evaluated to true. If $sentry can have other possible types, add them to action/parser.php:54
Loading history...
57
                $sentryEvent = new Event(['extra' => ['json' => $unparsedJSON]]);
58
                $sentryEvent->addException($e);
59
                $sentry->logEvent($sentryEvent);
60
61
                $errorMsg .= ' -- The error has been logged to Sentry.';
62
            }
63
64
            $event->data['errors'][] = $errorMsg;
65
            return;
66
        }
67
68
        $event->data['text'] = $syntax;
69
    }
70
71
    /**
72
     * [Custom event handler which performs action]
73
     *
74
     * Triggered by: ACTION_ACT_PREPROCESS
75
     *
76
     * @param Doku_Event $event  event object by reference
77
     * @param mixed      $param  [the parameters passed as fifth argument to register_hook() when this
78
     *                           handler was registered]
79
     *
80
     * @return void
81
     */
82
    public function handle_preprocess(Doku_Event $event, $param)
83
    {
84
        global $TEXT, $INPUT;
85
        if ($INPUT->server->str('REQUEST_METHOD') !== 'POST'
86
            || !in_array($event->data, ['save', 'preview'])
87
            || !$INPUT->post->has('prosemirror_json')
88
        ) {
89
            return;
90
        }
91
92
        $unparsedJSON = $INPUT->post->str('prosemirror_json');
93
        $syntax = $this->getSyntaxFromProsemirrorData($unparsedJSON);
94
        if (!empty($syntax)) {
95
            $TEXT = $syntax;
96
        }
97
    }
98
99
    /**
100
     * Decode json and parse the data back into DokuWiki Syntax
101
     *
102
     * @param string $unparsedJSON the json produced by Prosemirror
103
     *
104
     * @return null|string DokuWiki syntax or null on error
105
     */
106
    protected function getSyntaxFromProsemirrorData($unparsedJSON)
107
    {
108
        $prosemirrorData = json_decode($unparsedJSON, true);
109
        if ($prosemirrorData === null) {
110
            $errorMsg = 'Error decoding prosemirror json ' . json_last_error_msg();
111
            throw new RuntimeException($errorMsg);
112
        }
113
114
        $rootNode = SyntaxTreeBuilder::parseDataIntoTree($prosemirrorData);
115
        $syntax = $rootNode->toSyntax();
116
        return $syntax;
117
    }
118
}
119
120
// vim:ts=4:sw=4:et:
121