Passed
Pull Request — master (#37)
by Michael
02:25
created

handle_preprocess()   B

Complexity

Conditions 6
Paths 3

Size

Total Lines 13
Code Lines 9

Duplication

Lines 0
Ratio 0 %

Importance

Changes 3
Bugs 1 Features 0
Metric Value
cc 6
eloc 9
nc 3
nop 2
dl 0
loc 13
rs 8.8571
c 3
b 1
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
     * @param Doku_Event $event  event object by reference
75
     * @param mixed      $param  [the parameters passed as fifth argument to register_hook() when this
76
     *                           handler was registered]
77
     *
78
     * @return void
79
     */
80
    public function handle_preprocess(Doku_Event $event, $param)
81
    {
82
        global $TEXT, $INPUT;
83
        if ($INPUT->server->str('REQUEST_METHOD') !== 'POST'
84
            || ($event->data !== 'save' && $event->data !== 'preview')
85
            || !$INPUT->post->has('prosemirror_json')
86
        ) {
87
            return;
88
        }
89
        $unparsedJSON = $INPUT->post->str('prosemirror_json');
90
        $syntax = $this->getSyntaxFromProsemirrorData($unparsedJSON);
91
        if (!empty($syntax)) {
92
            $TEXT = $syntax;
93
        }
94
    }
95
96
    /**
97
     * Decode json and parse the data back into DokuWiki Syntax
98
     *
99
     * @param string $unparsedJSON the json produced by Prosemirror
100
     *
101
     * @return null|string DokuWiki syntax or null on error
102
     */
103
    protected function getSyntaxFromProsemirrorData($unparsedJSON)
104
    {
105
        $prosemirrorData = json_decode($unparsedJSON, true);
106
        if ($prosemirrorData === null) {
107
            $errorMsg = 'Error decoding prosemirror json ' . json_last_error_msg();
108
            throw new RuntimeException($errorMsg);
109
        }
110
111
        $rootNode = SyntaxTreeBuilder::parseDataIntoTree($prosemirrorData);
112
        $syntax = $rootNode->toSyntax();
113
        return $syntax;
114
    }
115
}
116
117
// vim:ts=4:sw=4:et:
118