Completed
Push — master ( 68e15a...5a472b )
by Michael
03:04
created

InternalLinkNode::resolveLink()   B

Complexity

Conditions 4
Paths 8

Size

Total Lines 26
Code Lines 19

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 4
eloc 19
nc 8
nop 2
dl 0
loc 26
rs 8.5806
c 0
b 0
f 0
1
<?php
2
3
namespace dokuwiki\plugin\prosemirror\parser;
4
5
class InternalLinkNode extends LinkNode
6
{
7
    public function toSyntax()
8
    {
9
        return $this->getDefaultLinkSyntax($this->attrs['data-inner']);
10
    }
11
12
    public static function render(\renderer_plugin_prosemirror $renderer, $originalId, $name)
13
    {
14
        global $ID;
15
        $additionalAttributes = [];
16
17
        $resolvedAttributes = self::resolveLink($originalId, $ID);
18
        $additionalAttributes['data-resolvedTitle'] = $resolvedAttributes['id'];
19
        $additionalAttributes['data-resolvedID'] = $resolvedAttributes['id'];
20
        if (!is_array($name)) {
21
            $additionalAttributes['data-resolvedName'] = $name ?: $resolvedAttributes['heading'];
22
            if ($resolvedAttributes['exists']) {
23
                $class = 'wikilink1';
24
            } else {
25
                $class = 'wikilink2';
26
            }
27
            $additionalAttributes['data-resolvedClass'] = $class;
28
        }
29
30
        self::renderToJSON(
31
            $renderer,
32
            'internallink',
33
            $originalId,
34
            $name,
35
            $additionalAttributes
36
        );
37
    }
38
39
    public static function resolveLink($inner, $curId) {
40
        $params = '';
41
        $parts = explode('?', $inner, 2);
42
        $resolvedPageId = $parts[0];
43
        if (count($parts) === 2) {
44
            $params = $parts[1];
45
        }
46
        $ns = getNS($curId);
1 ignored issue
show
Bug introduced by
The function getNS 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

46
        $ns = /** @scrutinizer ignore-call */ getNS($curId);
Loading history...
47
        $xhtml_renderer = p_get_renderer('xhtml');
1 ignored issue
show
Bug introduced by
The function p_get_renderer 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

47
        $xhtml_renderer = /** @scrutinizer ignore-call */ p_get_renderer('xhtml');
Loading history...
48
        $default = $xhtml_renderer->_simpleTitle($parts[0]);
49
        resolve_pageid($ns, $resolvedPageId, $exists);
1 ignored issue
show
Comprehensibility Best Practice introduced by
The variable $exists seems to be never defined.
Loading history...
Bug introduced by
The function resolve_pageid 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

49
        /** @scrutinizer ignore-call */ 
50
        resolve_pageid($ns, $resolvedPageId, $exists);
Loading history...
50
51
        if (useHeading('content')) {
1 ignored issue
show
Bug introduced by
The function useHeading 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

51
        if (/** @scrutinizer ignore-call */ useHeading('content')) {
Loading history...
52
            $heading = p_get_first_heading($resolvedPageId);
1 ignored issue
show
Bug introduced by
The function p_get_first_heading 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

52
            $heading = /** @scrutinizer ignore-call */ p_get_first_heading($resolvedPageId);
Loading history...
53
        }
54
        if (empty($heading)) {
55
            $heading = $default;
56
        }
57
58
        $url = wl($resolvedPageId, $params);
1 ignored issue
show
Bug introduced by
The function wl 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

58
        $url = /** @scrutinizer ignore-call */ wl($resolvedPageId, $params);
Loading history...
59
60
        return [
61
            'id' => $resolvedPageId,
62
            'exists' => $exists,
63
            'heading' => $heading,
64
            'url' => $url,
65
        ];
66
    }
67
68
    protected static function getLinkTitle($title, $default, $id)
69
    {
70
        if (null === $title || trim($title) == '') {
71
            if (useHeading('content') && $id) {
1 ignored issue
show
Bug introduced by
The function useHeading 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

71
            if (/** @scrutinizer ignore-call */ useHeading('content') && $id) {
Loading history...
72
                $heading = p_get_first_heading($id);
1 ignored issue
show
Bug introduced by
The function p_get_first_heading 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

72
                $heading = /** @scrutinizer ignore-call */ p_get_first_heading($id);
Loading history...
73
                if (!blank($heading)) {
1 ignored issue
show
Bug introduced by
The function blank 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

73
                if (!/** @scrutinizer ignore-call */ blank($heading)) {
Loading history...
74
                    return hsc($heading);
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

74
                    return /** @scrutinizer ignore-call */ hsc($heading);
Loading history...
75
                }
76
            }
77
            return hsc($default);
78
        }
79
        return hsc($title);
80
    }
81
}
82