cosmocode /
dokuwiki-plugin-prosemirror
| 1 | <?php |
||
| 2 | |||
| 3 | use dokuwiki\Extension\Plugin; |
||
| 4 | use dokuwiki\plugin\prosemirror\parser\SyntaxTreeBuilder; |
||
| 5 | use dokuwiki\plugin\sentry\Event; |
||
|
0 ignored issues
–
show
|
|||
| 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 <[email protected]> |
||
| 12 | */ |
||
| 13 | class 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) { |
||
|
0 ignored issues
–
show
|
|||
| 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 |
Let?s assume that you have a directory layout like this:
. |-- OtherDir | |-- Bar.php | `-- Foo.php `-- SomeDir `-- Foo.phpand let?s assume the following content of
Bar.php:If both files
OtherDir/Foo.phpandSomeDir/Foo.phpare 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.phpHowever, as
OtherDir/Foo.phpdoes not necessarily have to be loaded and the error is only triggered if it is loaded beforeOtherDir/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: