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 | protected $syntaxPatterns = []; |
||
22 | |||
23 | /** |
||
24 | * @return string Syntax mode type |
||
25 | */ |
||
26 | public function getType() { |
||
27 | return 'substition'; |
||
28 | } |
||
29 | |||
30 | /** |
||
31 | * @return int Sort order - Low numbers go before high numbers |
||
32 | */ |
||
33 | public function getSort() { |
||
34 | return 50; |
||
35 | } |
||
36 | |||
37 | /** |
||
38 | * Connect lookup pattern to lexer. |
||
39 | * |
||
40 | * @param string $mode Parser mode |
||
41 | */ |
||
42 | public function connectTo($mode) { |
||
43 | $serviceProvider = dokuwiki\plugin\issuelinks\classes\ServiceProvider::getInstance(); |
||
44 | $this->syntaxPatterns = $serviceProvider->getSyntaxKeys(); |
||
45 | |||
46 | foreach ($this->syntaxPatterns as $pattern => $class) { |
||
47 | $this->Lexer->addSpecialPattern("\[\[$pattern>.*?\]\]", $mode, 'plugin_issuelinks'); |
||
48 | } |
||
49 | |||
50 | } |
||
51 | |||
52 | /** |
||
53 | * Handle matches of the magicmatcher syntax |
||
54 | * |
||
55 | * @param string $match The match of the syntax |
||
56 | * @param int $state The state of the handler |
||
57 | * @param int $pos The position in the document |
||
58 | * @param Doku_Handler $handler The handler |
||
59 | * |
||
60 | * @return array Data for the renderer |
||
61 | * |
||
62 | * @throws Exception |
||
63 | */ |
||
64 | public function handle($match, $state, $pos, Doku_Handler $handler) { |
||
65 | list($pmServiceKey,$issueSyntax) = explode('>', trim($match,'[]')); |
||
66 | |||
67 | /** @var ServiceInterface $serviceClass */ |
||
68 | $serviceClass = $this->syntaxPatterns[$pmServiceKey]::getInstance(); |
||
69 | |||
70 | $issue = $serviceClass->parseIssueSyntax($issueSyntax); |
||
71 | |||
72 | if(null === $issue) { |
||
73 | return [$pmServiceKey, $issueSyntax]; |
||
74 | } |
||
75 | |||
76 | global $ID, $REV, $ACT; |
||
77 | $isLatest = empty($REV); |
||
78 | if (act_clean($ACT) === 'show' && $isLatest && page_exists($ID)) { |
||
79 | $this->saveLinkToDatabase($issue->getServiceName(), $issue->getProject(), $issue->getKey(), $issue->isMergeRequest()); |
||
0 ignored issues
–
show
Bug
introduced
by
![]() |
|||
80 | } |
||
81 | |||
82 | return array( |
||
83 | 'service' => $issue->getServiceName(), |
||
84 | 'project' => $issue->getProject(), |
||
85 | 'issueId' => $issue->getKey(), |
||
86 | 'isMergeRequest' => $issue->isMergeRequest(), |
||
87 | ); |
||
88 | } |
||
89 | |||
90 | /** |
||
91 | * Render xhtml output or metadata |
||
92 | * |
||
93 | * @param string $mode Renderer mode (supported modes: xhtml) |
||
94 | * @param Doku_Renderer $renderer The renderer |
||
95 | * @param array $data The data from the handler() function |
||
96 | * @return bool If rendering was successful. |
||
97 | */ |
||
98 | public function render($mode, Doku_Renderer $renderer, $data) { |
||
99 | if($mode !== 'xhtml' || count($data) === 2) { |
||
100 | $renderer->interwikilink(null, null, 'google.com', implode(' ', $data)); |
||
101 | return true; |
||
102 | } |
||
103 | |||
104 | /** @noinspection ExceptionsAnnotatingAndHandlingInspection We already checked for this in the handler */ |
||
105 | $issue = Issue::getInstance($data['service'], $data['project'], $data['issueId'], $data['isMergeRequest']); |
||
106 | $issue->getFromDB(); |
||
107 | $renderer->doc .= $issue->getIssueLinkHTML(); |
||
108 | return true; |
||
109 | } |
||
110 | |||
111 | /** |
||
112 | * @param string $project |
||
113 | * @param int $issue_id |
||
114 | * |
||
115 | * @throws InvalidArgumentException |
||
116 | */ |
||
117 | private function saveLinkToDatabase($pmServiceName, $project, $issue_id, $isMergeRequest) { |
||
118 | global $ID; |
||
119 | $currentRev = @filemtime(wikiFN($ID)); |
||
120 | |||
121 | /** @var helper_plugin_issuelinks_db $db_helper */ |
||
122 | $db_helper = $this->loadHelper('issuelinks_db'); |
||
123 | $db_helper->deleteAllIssuePageRevisions($ID, $pmServiceName, $project, $issue_id, $isMergeRequest, 'link'); |
||
124 | $db_helper->savePageRevIssues($ID, $currentRev, $pmServiceName, $project, $issue_id, $isMergeRequest, 'link'); |
||
125 | } |
||
126 | |||
127 | } |
||
128 | |||
129 | // vim:ts=4:sw=4:et: |
||
130 |