Issues (109)

action/parser.php (2 issues)

1
<?php
2
3
/**
4
 * DokuWiki Plugin prosemirror (Action Component)
5
 *
6
 * @license GPL 2 http://www.gnu.org/licenses/gpl-2.0.html
7
 * @author  Andreas Gohr <[email protected]>
8
 */
9
10
use dokuwiki\Extension\ActionPlugin;
11
use dokuwiki\Extension\EventHandler;
12
use dokuwiki\Extension\Event;
0 ignored issues
show
This use statement conflicts with another class in this namespace, Event. Consider defining an alias.

Let?s assume that you have a directory layout like this:

.
|-- OtherDir
|   |-- Bar.php
|   `-- Foo.php
`-- SomeDir
    `-- Foo.php

and let?s assume the following content of Bar.php:

// Bar.php
namespace OtherDir;

use SomeDir\Foo; // This now conflicts the class OtherDir\Foo

If both files OtherDir/Foo.php and SomeDir/Foo.php are loaded in the same runtime, you will see a PHP error such as the following:

PHP Fatal error:  Cannot use SomeDir\Foo as Foo because the name is already in use in OtherDir/Foo.php

However, as OtherDir/Foo.php does not necessarily have to be loaded and the error is only triggered if it is loaded before OtherDir/Bar.php, this problem might go unnoticed for a while. In order to prevent this error from surfacing, you must import the namespace with a different alias:

// Bar.php
namespace OtherDir;

use SomeDir\Foo as SomeDirFoo; // There is no conflict anymore.
Loading history...
13
use dokuwiki\plugin\prosemirror\parser\SyntaxTreeBuilder;
14
15
class action_plugin_prosemirror_parser extends ActionPlugin
16
{
17
    /**
18
     * Registers a callback function for a given event
19
     *
20
     * @param EventHandler $controller DokuWiki's event controller object
21
     *
22
     * @return void
23
     */
24
    public function register(EventHandler $controller)
25
    {
26
        $controller->register_hook('ACTION_ACT_PREPROCESS', 'BEFORE', $this, 'handlePreprocess');
27
        $controller->register_hook('DRAFT_SAVE', 'BEFORE', $this, 'handleDraft');
28
    }
29
30
    /**
31
     * Triggered by: COMMON_DRAFT_SAVE
32
     *
33
     * @param Event $event
34
     * @param            $param
35
     */
36
    public function handleDraft(Event $event, $param)
37
    {
38
        global $INPUT;
39
        $unparsedJSON = $INPUT->post->str('prosemirror_json');
40
        if (empty($unparsedJSON)) {
41
            return;
42
        }
43
44
45
        /** @var \helper_plugin_prosemirror $helper */
46
        $helper = plugin_load('helper', 'prosemirror');
47
48
        try {
49
            $syntax = $helper->getSyntaxFromProsemirrorData($unparsedJSON);
50
        } catch (\Throwable $e) {
51
            $event->preventDefault();
52
            $event->stopPropagation();
53
54
            $errorMsg = $e->getMessage();
55
56
            if ($helper->tryToLogErrorToSentry($e, ['json' => $unparsedJSON])) {
57
                $errorMsg .= ' -- The error has been logged to Sentry.';
58
            }
59
60
            $event->data['errors'][] = $errorMsg;
61
            return;
62
        }
63
64
        $event->data['text'] = $syntax;
65
    }
66
67
    /**
68
     * [Custom event handler which performs action]
69
     *
70
     * Triggered by: ACTION_ACT_PREPROCESS
71
     *
72
     * @param Event $event event object by reference
73
     * @param mixed      $param  [the parameters passed as fifth argument to register_hook() when this
74
     *                           handler was registered]
75
     *
76
     * @return void
77
     */
78
    public function handlePreprocess(Event $event, $param)
79
    {
80
        global $TEXT, $INPUT;
81
        if (
82
            $INPUT->server->str('REQUEST_METHOD') !== 'POST'
83
            || !in_array($event->data, ['save', 'preview'])
84
            || !$INPUT->post->has('prosemirror_json')
85
            || !get_doku_pref('plugin_prosemirror_useWYSIWYG', false)
86
        ) {
87
            return;
88
        }
89
90
        $unparsedJSON = $INPUT->post->str('prosemirror_json');
91
92
        /** @var \helper_plugin_prosemirror $helper */
93
        $helper = plugin_load('helper', 'prosemirror');
94
        try {
95
            $syntax = $helper->getSyntaxFromProsemirrorData($unparsedJSON);
96
        } catch (\Throwable $e) {
97
            $event->preventDefault();
98
            $event->stopPropagation();
99
100
            $errorMsg = $e->getMessage();
101
102
            if ($helper->tryToLogErrorToSentry($e, ['json' => $unparsedJSON])) {
103
                $errorMsg .= ' -- The error has been logged to Sentry.';
104
            }
105
106
            msg($errorMsg, -1);
107
            return;
108
        }
109
        if ($syntax !== null) {
0 ignored issues
show
The condition $syntax !== null is always true.
Loading history...
110
            $TEXT = $syntax;
111
        }
112
    }
113
}
114
115
// vim:ts=4:sw=4:et:
116