Skip to content

Commit a8993eb

Browse files
committed
WIP
1 parent af7fbd6 commit a8993eb

7 files changed

Lines changed: 68 additions & 10 deletions

File tree

Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,49 @@
1+
<?php
2+
3+
/* Icinga DB Web | (c) 2022 Icinga GmbH | GPLv2 */
4+
5+
namespace Icinga\Module\Icingadb\Hook;
6+
7+
use Exception;
8+
use Icinga\Application\Hook;
9+
use Icinga\Application\Logger;
10+
use Icinga\Module\Icingadb\Hook\Common\HookUtils;
11+
12+
abstract class CommentOutputHook
13+
{
14+
use HookUtils;
15+
16+
/**
17+
* Transform the given comment text
18+
*
19+
* Try to transform the comment output as efficient and fast as possible.
20+
* Especially list view performance may suffer otherwise.
21+
*
22+
* @param string $output A host's, service's, downtime's or an acknowledgement's comment
23+
*
24+
* @return string
25+
*/
26+
abstract public function transformComment(string $output): string;
27+
28+
29+
/**
30+
* Let all hooks process the given comment text
31+
*
32+
* @param string $output A host's, service's, downtime's or an acknowledgement's comment
33+
*
34+
* @return string
35+
*/
36+
final public static function processComment(string $output): string
37+
{
38+
foreach (Hook::all('Icingadb\\CommentOutput') as $hook) {
39+
/** @var self $hook */
40+
try {
41+
$output = $hook->transformComment($output);
42+
} catch (Exception $e) {
43+
Logger::error("Unable to process comment: %s\n%s", $e, $e->getTraceAsString());
44+
}
45+
}
46+
47+
return $output;
48+
}
49+
}

library/Icingadb/Widget/Detail/CommentDetail.php

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
77
use Icinga\Date\DateFormatter;
88
use Icinga\Module\Icingadb\Common\Auth;
99
use Icinga\Module\Icingadb\Common\Links;
10+
use Icinga\Module\Icingadb\Hook\CommentOutputHook;
1011
use Icinga\Module\Icingadb\Model\Comment;
1112
use Icinga\Module\Icingadb\Widget\MarkdownText;
1213
use Icinga\Module\Icingadb\Forms\Command\Object\DeleteCommentForm;
@@ -35,7 +36,7 @@ protected function createComment(): array
3536
{
3637
return [
3738
Html::tag('h2', t('Comment')),
38-
new MarkdownText($this->comment->text)
39+
new MarkdownText(CommentOutputHook::processComment($this->comment->text))
3940
];
4041
}
4142

library/Icingadb/Widget/Detail/DowntimeDetail.php

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@
1010
use Icinga\Module\Icingadb\Common\Database;
1111
use Icinga\Module\Icingadb\Common\HostLink;
1212
use Icinga\Module\Icingadb\Common\Links;
13+
use Icinga\Module\Icingadb\Hook\CommentOutputHook;
1314
use Icinga\Module\Icingadb\Widget\EmptyState;
1415
use Icinga\Module\Icingadb\Widget\MarkdownText;
1516
use Icinga\Module\Icingadb\Common\ServiceLink;
@@ -75,7 +76,7 @@ protected function assemble()
7576
Html::sprintf(
7677
t('%s commented: %s', '<username> ..: <comment>'),
7778
$this->downtime->author,
78-
new MarkdownText($this->downtime->comment)
79+
new MarkdownText(CommentOutputHook::processComment($this->downtime->comment))
7980
)
8081
]));
8182

library/Icingadb/Widget/Detail/EventDetail.php

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -7,11 +7,13 @@
77
use DateTime;
88
use DateTimeZone;
99
use Icinga\Date\DateFormatter;
10+
use Icinga\File\Ini\Dom\Comment;
1011
use Icinga\Module\Icingadb\Common\Auth;
1112
use Icinga\Module\Icingadb\Common\Database;
1213
use Icinga\Module\Icingadb\Common\HostLink;
1314
use Icinga\Module\Icingadb\Common\HostStates;
1415
use Icinga\Module\Icingadb\Common\Links;
16+
use Icinga\Module\Icingadb\Hook\CommentOutputHook;
1517
use Icinga\Module\Icingadb\Hook\ExtensionHook\ObjectDetailExtensionHook;
1618
use Icinga\Module\Icingadb\Widget\MarkdownText;
1719
use Icinga\Module\Icingadb\Common\ServiceLink;
@@ -253,7 +255,7 @@ protected function assembleDowntimeEvent(DowntimeHistory $downtime)
253255
{
254256
$commentInfo = [
255257
new HtmlElement('h2', null, Text::create(t('Comment'))),
256-
new MarkdownText($downtime->comment)
258+
new MarkdownText(CommentOutputHook::processComment($downtime->comment))
257259
];
258260

259261
$eventInfo = [new HtmlElement('h2', null, Text::create(t('Event Info')))];
@@ -352,7 +354,7 @@ protected function assembleCommentEvent(CommentHistory $comment)
352354
{
353355
$commentInfo = [
354356
new HtmlElement('h2', null, Text::create(t('Comment'))),
355-
new MarkdownText($comment->comment)
357+
new MarkdownText(CommentOutputHook::processComment($comment->comment))
356358
];
357359

358360
$eventInfo = [new HtmlElement('h2', null, Text::create(t('Event Info')))];
@@ -464,7 +466,7 @@ protected function assembleAcknowledgeEvent(AcknowledgementHistory $acknowledgem
464466
if ($acknowledgement->comment) {
465467
$commentInfo = [
466468
new HtmlElement('h2', null, Text::create(t('Comment'))),
467-
new MarkdownText($acknowledgement->comment)
469+
new MarkdownText(CommentOutputHook::processComment($acknowledgement->comment))
468470
];
469471
} elseif (! isset($acknowledgement->author)) {
470472
$commentInfo[] = new EmptyState(t('This acknowledgement was set before Icinga DB history recording'));

library/Icingadb/Widget/ItemList/BaseCommentListItem.php

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44

55
namespace Icinga\Module\Icingadb\Widget\ItemList;
66

7+
use Icinga\Module\Icingadb\Hook\CommentOutputHook;
78
use ipl\Html\Html;
89
use Icinga\Module\Icingadb\Common\HostLink;
910
use Icinga\Module\Icingadb\Common\Icons;
@@ -40,7 +41,7 @@ abstract class BaseCommentListItem extends BaseListItem
4041

4142
protected function assembleCaption(BaseHtmlElement $caption)
4243
{
43-
$markdownLine = new MarkdownLine($this->item->text);
44+
$markdownLine = new MarkdownLine(CommentOutputHook::processComment($this->item->text));
4445
$caption->getAttributes()->add($markdownLine->getAttributes());
4546
$caption->addFrom($markdownLine);
4647
}

library/Icingadb/Widget/ItemList/BaseDowntimeListItem.php

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@
1212
use Icinga\Module\Icingadb\Common\NoSubjectLink;
1313
use Icinga\Module\Icingadb\Common\ObjectLinkDisabled;
1414
use Icinga\Module\Icingadb\Common\ServiceLink;
15+
use Icinga\Module\Icingadb\Hook\CommentOutputHook;
1516
use Icinga\Module\Icingadb\Model\Downtime;
1617
use Icinga\Module\Icingadb\Widget\MarkdownLine;
1718
use ipl\Html\BaseHtmlElement;
@@ -106,7 +107,7 @@ protected function createProgress(): BaseHtmlElement
106107

107108
protected function assembleCaption(BaseHtmlElement $caption)
108109
{
109-
$markdownLine = new MarkdownLine($this->item->comment);
110+
$markdownLine = new MarkdownLine(CommentOutputHook::processComment($this->item->comment));
110111
$caption->getAttributes()->add($markdownLine->getAttributes());
111112
$caption->addHtml(
112113
new HtmlElement(

library/Icingadb/Widget/ItemList/BaseHistoryListItem.php

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@
99
use Icinga\Module\Icingadb\Common\HostStates;
1010
use Icinga\Module\Icingadb\Common\Icons;
1111
use Icinga\Module\Icingadb\Common\Links;
12+
use Icinga\Module\Icingadb\Hook\CommentOutputHook;
1213
use Icinga\Module\Icingadb\Widget\EmptyState;
1314
use Icinga\Module\Icingadb\Widget\MarkdownLine;
1415
use Icinga\Module\Icingadb\Common\NoSubjectLink;
@@ -54,7 +55,7 @@ protected function assembleCaption(BaseHtmlElement $caption)
5455
switch ($this->item->event_type) {
5556
case 'comment_add':
5657
case 'comment_remove':
57-
$markdownLine = new MarkdownLine($this->item->comment->comment);
58+
$markdownLine = new MarkdownLine(CommentOutputHook::processComment($this->item->comment->comment));
5859
$caption->getAttributes()->add($markdownLine->getAttributes());
5960
$caption->add([
6061
new Icon(Icons::USER),
@@ -65,7 +66,7 @@ protected function assembleCaption(BaseHtmlElement $caption)
6566
break;
6667
case 'downtime_end':
6768
case 'downtime_start':
68-
$markdownLine = new MarkdownLine($this->item->downtime->comment);
69+
$markdownLine = new MarkdownLine(CommentOutputHook::processComment($this->item->downtime->comment));
6970
$caption->getAttributes()->add($markdownLine->getAttributes());
7071
$caption->add([
7172
new Icon(Icons::USER),
@@ -106,7 +107,9 @@ protected function assembleCaption(BaseHtmlElement $caption)
106107
t('This acknowledgement was set before Icinga DB history recording')
107108
));
108109
} else {
109-
$markdownLine = new MarkdownLine($this->item->acknowledgement->comment);
110+
$markdownLine = new MarkdownLine(
111+
CommentOutputHook::processComment($this->item->acknowledgement->comment)
112+
);
110113
$caption->getAttributes()->add($markdownLine->getAttributes());
111114
$caption->add([
112115
new Icon(Icons::USER),

0 commit comments

Comments
 (0)