1<?php
2
3use dokuwiki\Extension\Plugin;
4use dokuwiki\plugin\prosemirror\parser\SyntaxTreeBuilder;
5use dokuwiki\plugin\sentry\Event;
6
7/**
8 * DokuWiki Plugin prosemirror (Helper Component)
9 *
10 * @license GPL 2 http://www.gnu.org/licenses/gpl-2.0.html
11 * @author  Andreas Gohr <gohr@cosmocode.de>
12 */
13class helper_plugin_prosemirror extends Plugin
14{
15    /**
16     * Decode json and parse the data back into DokuWiki Syntax
17     *
18     * @param string $unparsedJSON the json produced by Prosemirror
19     *
20     * @return null|string DokuWiki syntax or null on error
21     */
22    public function getSyntaxFromProsemirrorData($unparsedJSON)
23    {
24        $prosemirrorData = json_decode($unparsedJSON, true);
25        if ($prosemirrorData === null) {
26            $errorMsg = 'Error decoding prosemirror json ' . json_last_error_msg();
27            throw new RuntimeException($errorMsg);
28        }
29
30        $rootNode = SyntaxTreeBuilder::parseDataIntoTree($prosemirrorData);
31        $syntax = $rootNode->toSyntax();
32        return $syntax;
33    }
34
35    /**
36     * Try to log an error to sentry if the sentry plugin exists
37     *
38     * @param Throwable $exception
39     * @param array     $extraData associative array for sentries `extra` field
40     *
41     * @return bool true if the exception has been logged to sentry, false otherwise
42     */
43    public function tryToLogErrorToSentry(Throwable $exception, array $extraData = [])
44    {
45        global $ID;
46
47        /** @var helper_plugin_sentry $sentry */
48        $sentry = plugin_load('helper', 'sentry');
49        if (!$sentry) {
50            return false;
51        }
52        $sentryEvent = new Event([
53            'extra' => $extraData,
54            'tags' => [
55                'plugin' => 'prosemirror',
56                'id' => $ID,
57            ],
58        ]);
59        $sentryEvent->addException($exception);
60
61        $sentry->logEvent($sentryEvent);
62        return true;
63    }
64}
65// vim:ts=4:sw=4:et:
66