Skip to content
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
49 changes: 49 additions & 0 deletions library/Icingadb/Hook/CommentOutputHook.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
<?php

/* Icinga DB Web | (c) 2022 Icinga GmbH | GPLv2 */

namespace Icinga\Module\Icingadb\Hook;

use Exception;
use Icinga\Application\Hook;
use Icinga\Application\Logger;
use Icinga\Module\Icingadb\Hook\Common\HookUtils;

abstract class CommentOutputHook
{
use HookUtils;

/**
* Transform the given comment text
*
* Try to transform the comment output as efficient and fast as possible.
* Especially list view performance may suffer otherwise.
*
* @param string $output A host's, service's, downtime's or an acknowledgement's comment
*
* @return string
*/
abstract public function transformComment(string $output): string;


/**
* Let all hooks process the given comment text
*
* @param string $output A host's, service's, downtime's or an acknowledgement's comment
*
* @return string
*/
final public static function processComment(string $output): string
{
foreach (Hook::all('Icingadb\\CommentOutput') as $hook) {
/** @var self $hook */
try {
$output = $hook->transformComment($output);
} catch (Exception $e) {
Logger::error("Unable to process comment: %s\n%s", $e, $e->getTraceAsString());
}
}

return $output;
}
}
3 changes: 2 additions & 1 deletion library/Icingadb/Widget/Detail/CommentDetail.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
use Icinga\Date\DateFormatter;
use Icinga\Module\Icingadb\Common\Auth;
use Icinga\Module\Icingadb\Common\Links;
use Icinga\Module\Icingadb\Hook\CommentOutputHook;
use Icinga\Module\Icingadb\Model\Comment;
use Icinga\Module\Icingadb\Widget\MarkdownText;
use Icinga\Module\Icingadb\Forms\Command\Object\DeleteCommentForm;
Expand Down Expand Up @@ -35,7 +36,7 @@ protected function createComment(): array
{
return [
Html::tag('h2', t('Comment')),
new MarkdownText($this->comment->text)
new MarkdownText(CommentOutputHook::processComment($this->comment->text))
];
}

Expand Down
3 changes: 2 additions & 1 deletion library/Icingadb/Widget/Detail/DowntimeDetail.php
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
use Icinga\Module\Icingadb\Common\Database;
use Icinga\Module\Icingadb\Common\HostLink;
use Icinga\Module\Icingadb\Common\Links;
use Icinga\Module\Icingadb\Hook\CommentOutputHook;
use Icinga\Module\Icingadb\Widget\EmptyState;
use Icinga\Module\Icingadb\Widget\MarkdownText;
use Icinga\Module\Icingadb\Common\ServiceLink;
Expand Down Expand Up @@ -75,7 +76,7 @@ protected function assemble()
Html::sprintf(
t('%s commented: %s', '<username> ..: <comment>'),
$this->downtime->author,
new MarkdownText($this->downtime->comment)
new MarkdownText(CommentOutputHook::processComment($this->downtime->comment))
)
]));

Expand Down
8 changes: 5 additions & 3 deletions library/Icingadb/Widget/Detail/EventDetail.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,11 +7,13 @@
use DateTime;
use DateTimeZone;
use Icinga\Date\DateFormatter;
use Icinga\File\Ini\Dom\Comment;
use Icinga\Module\Icingadb\Common\Auth;
use Icinga\Module\Icingadb\Common\Database;
use Icinga\Module\Icingadb\Common\HostLink;
use Icinga\Module\Icingadb\Common\HostStates;
use Icinga\Module\Icingadb\Common\Links;
use Icinga\Module\Icingadb\Hook\CommentOutputHook;
use Icinga\Module\Icingadb\Hook\ExtensionHook\ObjectDetailExtensionHook;
use Icinga\Module\Icingadb\Widget\MarkdownText;
use Icinga\Module\Icingadb\Common\ServiceLink;
Expand Down Expand Up @@ -253,7 +255,7 @@ protected function assembleDowntimeEvent(DowntimeHistory $downtime)
{
$commentInfo = [
new HtmlElement('h2', null, Text::create(t('Comment'))),
new MarkdownText($downtime->comment)
new MarkdownText(CommentOutputHook::processComment($downtime->comment))
];

$eventInfo = [new HtmlElement('h2', null, Text::create(t('Event Info')))];
Expand Down Expand Up @@ -352,7 +354,7 @@ protected function assembleCommentEvent(CommentHistory $comment)
{
$commentInfo = [
new HtmlElement('h2', null, Text::create(t('Comment'))),
new MarkdownText($comment->comment)
new MarkdownText(CommentOutputHook::processComment($comment->comment))
];

$eventInfo = [new HtmlElement('h2', null, Text::create(t('Event Info')))];
Expand Down Expand Up @@ -464,7 +466,7 @@ protected function assembleAcknowledgeEvent(AcknowledgementHistory $acknowledgem
if ($acknowledgement->comment) {
$commentInfo = [
new HtmlElement('h2', null, Text::create(t('Comment'))),
new MarkdownText($acknowledgement->comment)
new MarkdownText(CommentOutputHook::processComment($acknowledgement->comment))
];
} elseif (! isset($acknowledgement->author)) {
$commentInfo[] = new EmptyState(t('This acknowledgement was set before Icinga DB history recording'));
Expand Down
3 changes: 2 additions & 1 deletion library/Icingadb/Widget/ItemList/BaseCommentListItem.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@

namespace Icinga\Module\Icingadb\Widget\ItemList;

use Icinga\Module\Icingadb\Hook\CommentOutputHook;
use ipl\Html\Html;
use Icinga\Module\Icingadb\Common\HostLink;
use Icinga\Module\Icingadb\Common\Icons;
Expand Down Expand Up @@ -40,7 +41,7 @@ abstract class BaseCommentListItem extends BaseListItem

protected function assembleCaption(BaseHtmlElement $caption)
{
$markdownLine = new MarkdownLine($this->item->text);
$markdownLine = new MarkdownLine(CommentOutputHook::processComment($this->item->text));
$caption->getAttributes()->add($markdownLine->getAttributes());
$caption->addFrom($markdownLine);
}
Expand Down
3 changes: 2 additions & 1 deletion library/Icingadb/Widget/ItemList/BaseDowntimeListItem.php
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
use Icinga\Module\Icingadb\Common\NoSubjectLink;
use Icinga\Module\Icingadb\Common\ObjectLinkDisabled;
use Icinga\Module\Icingadb\Common\ServiceLink;
use Icinga\Module\Icingadb\Hook\CommentOutputHook;
use Icinga\Module\Icingadb\Model\Downtime;
use Icinga\Module\Icingadb\Widget\MarkdownLine;
use ipl\Html\BaseHtmlElement;
Expand Down Expand Up @@ -106,7 +107,7 @@ protected function createProgress(): BaseHtmlElement

protected function assembleCaption(BaseHtmlElement $caption)
{
$markdownLine = new MarkdownLine($this->item->comment);
$markdownLine = new MarkdownLine(CommentOutputHook::processComment($this->item->comment));
$caption->getAttributes()->add($markdownLine->getAttributes());
$caption->addHtml(
new HtmlElement(
Expand Down
9 changes: 6 additions & 3 deletions library/Icingadb/Widget/ItemList/BaseHistoryListItem.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
use Icinga\Module\Icingadb\Common\HostStates;
use Icinga\Module\Icingadb\Common\Icons;
use Icinga\Module\Icingadb\Common\Links;
use Icinga\Module\Icingadb\Hook\CommentOutputHook;
use Icinga\Module\Icingadb\Widget\EmptyState;
use Icinga\Module\Icingadb\Widget\MarkdownLine;
use Icinga\Module\Icingadb\Common\NoSubjectLink;
Expand Down Expand Up @@ -54,7 +55,7 @@ protected function assembleCaption(BaseHtmlElement $caption)
switch ($this->item->event_type) {
case 'comment_add':
case 'comment_remove':
$markdownLine = new MarkdownLine($this->item->comment->comment);
$markdownLine = new MarkdownLine(CommentOutputHook::processComment($this->item->comment->comment));
$caption->getAttributes()->add($markdownLine->getAttributes());
$caption->add([
new Icon(Icons::USER),
Expand All @@ -65,7 +66,7 @@ protected function assembleCaption(BaseHtmlElement $caption)
break;
case 'downtime_end':
case 'downtime_start':
$markdownLine = new MarkdownLine($this->item->downtime->comment);
$markdownLine = new MarkdownLine(CommentOutputHook::processComment($this->item->downtime->comment));
$caption->getAttributes()->add($markdownLine->getAttributes());
$caption->add([
new Icon(Icons::USER),
Expand Down Expand Up @@ -106,7 +107,9 @@ protected function assembleCaption(BaseHtmlElement $caption)
t('This acknowledgement was set before Icinga DB history recording')
));
} else {
$markdownLine = new MarkdownLine($this->item->acknowledgement->comment);
$markdownLine = new MarkdownLine(
CommentOutputHook::processComment($this->item->acknowledgement->comment)
);
$caption->getAttributes()->add($markdownLine->getAttributes());
$caption->add([
new Icon(Icons::USER),
Expand Down