Passed
Pull Request — master (#37)
by Michael
03:01
created

action_plugin_prosemirror_parser::handle_draft()   A

Complexity

Conditions 4
Paths 3

Size

Total Lines 15
Code Lines 10

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 4
eloc 10
nc 3
nop 2
dl 0
loc 15
rs 9.2
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('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)
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, $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();
1 ignored issue
show
Bug introduced by
The function html_msgarea 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

50
            /** @scrutinizer ignore-call */ 
51
            html_msgarea();
Loading history...
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');
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

104
            $sentry = /** @scrutinizer ignore-call */ plugin_load('helper', 'sentry');
Loading history...
105
            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:103
Loading history...
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);
1 ignored issue
show
Bug introduced by
The function msg 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

114
            /** @scrutinizer ignore-call */ 
115
            msg($errorMsg, -1);
Loading history...
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());
1 ignored issue
show
Bug introduced by
The function hsc 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

120
            $errorMsg = 'Parsing the data provided by the WYSIWYG editor failed with message: ' . /** @scrutinizer ignore-call */ hsc($e->getMessage());
Loading history...
121
            /** @var helper_plugin_sentry $sentry */
122
            $sentry = plugin_load('helper', 'sentry');
123
            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:121
Loading history...
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