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
// must be run within Dokuwiki
10
if (!defined('DOKU_INC')) {
11
    die();
12
}
13
14
15
use dokuwiki\plugin\issuelinks\classes\Issue;
16
use dokuwiki\plugin\issuelinks\services\ServiceInterface;
17
18
19
class syntax_plugin_issuelinks extends DokuWiki_Syntax_Plugin
20
{
21
22
    protected $syntaxPatterns = [];
23
24
    /**
25
     * @return string Syntax mode type
26
     */
27
    public function getType()
28
    {
29
        return 'substition';
30
    }
31
32
    /**
33
     * @return int Sort order - Low numbers go before high numbers
34
     */
35
    public function getSort()
36
    {
37
        return 50;
38
    }
39
40
    /**
41
     * Connect lookup pattern to lexer.
42
     *
43
     * @param string $mode Parser mode
44
     */
45
    public function connectTo($mode)
46
    {
47
        $serviceProvider = dokuwiki\plugin\issuelinks\classes\ServiceProvider::getInstance();
48
        $this->syntaxPatterns = $serviceProvider->getSyntaxKeys();
49
50
        foreach ($this->syntaxPatterns as $pattern => $class) {
51
            $this->Lexer->addSpecialPattern("\[\[$pattern>.*?\]\]", $mode, 'plugin_issuelinks');
52
        }
53
54
    }
55
56
    /**
57
     * Handle matches of the magicmatcher syntax
58
     *
59
     * @param string       $match   The match of the syntax
60
     * @param int          $state   The state of the handler
61
     * @param int          $pos     The position in the document
62
     * @param Doku_Handler $handler The handler
63
     *
64
     * @return array Data for the renderer
65
     *
66
     * @throws Exception
67
     */
68
    public function handle($match, $state, $pos, Doku_Handler $handler)
69
    {
70
        list($pmServiceKey, $issueSyntax) = explode('>', trim($match, '[]'));
71
72
        /** @var ServiceInterface $serviceClass */
73
        $serviceClass = $this->syntaxPatterns[$pmServiceKey]::getInstance();
74
75
        $issue = $serviceClass->parseIssueSyntax($issueSyntax);
76
77
        if (null === $issue) {
78
            return [$pmServiceKey, $issueSyntax];
79
        }
80
81
        global $ID, $REV, $ACT;
82
        $isLatest = empty($REV);
83
        if (act_clean($ACT) === 'show' && $isLatest && page_exists($ID)) {
84
            $this->saveLinkToDatabase($issue->getServiceName(), $issue->getProject(), $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

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