Completed
Push — master ( 4ffd57...6a646c )
by Michael
02:30
created

syntax.php (1 issue)

Labels
Severity
1
<?php
2
/**
3
 * DokuWiki Plugin issuelink (Syntax Component)
4
 *
5
 * @license GPL 2 http://www.gnu.org/licenses/gpl-2.0.html
6
 * @author  Andreas Gohr, Michael Große <[email protected]>
7
 */
8
9
use dokuwiki\plugin\issuelinks\classes\Issue;
10
use dokuwiki\plugin\issuelinks\services\ServiceInterface;
11
12
class syntax_plugin_issuelinks extends DokuWiki_Syntax_Plugin
13
{
14
15
    protected $syntaxPatterns = [];
16
17
    /**
18
     * @return string Syntax mode type
19
     */
20
    public function getType()
21
    {
22
        return 'substition';
23
    }
24
25
    /**
26
     * @return int Sort order - Low numbers go before high numbers
27
     */
28
    public function getSort()
29
    {
30
        return 50;
31
    }
32
33
    /**
34
     * Connect lookup pattern to lexer.
35
     *
36
     * @param string $mode Parser mode
37
     */
38
    public function connectTo($mode)
39
    {
40
        $serviceProvider = dokuwiki\plugin\issuelinks\classes\ServiceProvider::getInstance();
41
        $this->syntaxPatterns = $serviceProvider->getSyntaxKeys();
42
43
        foreach ($this->syntaxPatterns as $pattern => $class) {
44
            $this->Lexer->addSpecialPattern("\[\[$pattern>.*?\]\]", $mode, 'plugin_issuelinks');
45
        }
46
    }
47
48
    /**
49
     * Handle matches of the issuelinks syntax
50
     *
51
     * @param string       $match   The match of the syntax
52
     * @param int          $state   The state of the handler
53
     * @param int          $pos     The position in the document
54
     * @param Doku_Handler $handler The handler
55
     *
56
     * @return array Data for the renderer
57
     *
58
     * @throws Exception
59
     */
60
    public function handle($match, $state, $pos, Doku_Handler $handler)
61
    {
62
        list($pmServiceKey, $issueSyntax) = explode('>', trim($match, '[]'));
63
64
        /** @var ServiceInterface $serviceClass */
65
        $serviceClass = $this->syntaxPatterns[$pmServiceKey]::getInstance();
66
67
        $issue = $serviceClass->parseIssueSyntax($issueSyntax);
68
69
        if (null === $issue) {
70
            return [$pmServiceKey, $issueSyntax];
71
        }
72
73
        global $ID, $REV, $ACT;
74
        $isLatest = empty($REV);
75
        if (act_clean($ACT) === 'show' && $isLatest && page_exists($ID)) {
76
            $this->saveLinkToDatabase(
77
                $issue->getServiceName(),
78
                $issue->getProject(),
79
                $issue->getKey(),
0 ignored issues
show
It seems like $issue->getKey() can also be of type string; however, parameter $issue_id of syntax_plugin_issuelinks::saveLinkToDatabase() does only seem to accept integer, maybe add an additional type check? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-type  annotation

79
                /** @scrutinizer ignore-type */ $issue->getKey(),
Loading history...
80
                $issue->isMergeRequest()
81
            );
82
        }
83
84
        return [
85
            'service' => $issue->getServiceName(),
86
            'project' => $issue->getProject(),
87
            'issueId' => $issue->getKey(),
88
            'isMergeRequest' => $issue->isMergeRequest(),
89
        ];
90
    }
91
92
    /**
93
     * @param string $project
94
     * @param int    $issue_id
95
     *
96
     * @throws InvalidArgumentException
97
     */
98
    private function saveLinkToDatabase($pmServiceName, $project, $issue_id, $isMergeRequest)
99
    {
100
        global $ID;
101
        $currentRev = @filemtime(wikiFN($ID));
102
103
        /** @var helper_plugin_issuelinks_db $db_helper */
104
        $db_helper = $this->loadHelper('issuelinks_db');
105
        $db_helper->deleteAllIssuePageRevisions($ID, $pmServiceName, $project, $issue_id, $isMergeRequest, 'link');
106
        $db_helper->savePageRevIssues($ID, $currentRev, $pmServiceName, $project, $issue_id, $isMergeRequest, 'link');
107
    }
108
109
    /**
110
     * Render xhtml output or metadata
111
     *
112
     * @param string        $mode     Renderer mode (supported modes: xhtml)
113
     * @param Doku_Renderer $renderer The renderer
114
     * @param array         $data     The data from the handler() function
115
     *
116
     * @return bool If rendering was successful.
117
     */
118
    public function render($mode, Doku_Renderer $renderer, $data)
119
    {
120
        if ($mode !== 'xhtml' || count($data) === 2) {
121
            $renderer->interwikilink(null, null, 'google.com', implode(' ', $data));
122
            return true;
123
        }
124
125
        /** @noinspection ExceptionsAnnotatingAndHandlingInspection We already checked for this in the handler */
126
        $issue = Issue::getInstance($data['service'], $data['project'], $data['issueId'], $data['isMergeRequest']);
127
        $issue->getFromDB();
128
        $renderer->doc .= $issue->getIssueLinkHTML();
129
        return true;
130
    }
131
}
132
133
// vim:ts=4:sw=4:et:
134