Skip to content

EspoCRM – Open Source CRM Application

kb article full text search

Yuri Kuznetsov77fdd9e19497
diff --git a/application/Espo/Core/Upgrades/Migrations/V9_1/AfterUpgrade.php b/application/Espo/Core/Upgrades/Migrations/V9_1/AfterUpgrade.php
new file mode 100644
index 0000000000..3ac76c0cb9
--- /dev/null
+++ b/application/Espo/Core/Upgrades/Migrations/V9_1/AfterUpgrade.php
@@ -0,0 +1,74 @@
+<?php
+/************************************************************************
+ * This file is part of EspoCRM.
+ *
+ * EspoCRM – Open Source CRM application.
+ * Copyright (C) 2014-2025 Yurii Kuznietsov, Taras Machyshyn, Oleksii Avramenko
+ * Website: https://www.espocrm.com
+ *
+ * This program is free software: you can redistribute it and/or modify
+ * it under the terms of the GNU Affero General Public License as published by
+ * the Free Software Foundation, either version 3 of the License, or
+ * (at your option) any later version.
+ *
+ * This program is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
+ * GNU Affero General Public License for more details.
+ *
+ * You should have received a copy of the GNU Affero General Public License
+ * along with this program. If not, see <https://www.gnu.org/licenses/>.
+ *
+ * The interactive user interfaces in modified source and object code versions
+ * of this program must display Appropriate Legal Notices, as required under
+ * Section 5 of the GNU Affero General Public License version 3.
+ *
+ * In accordance with Section 7(b) of the GNU Affero General Public License version 3,
+ * these Appropriate Legal Notices must retain the display of the "EspoCRM" word.
+ ************************************************************************/
+
+namespace Espo\Core\Upgrades\Migrations\V9_1;
+
+use Espo\Core\ORM\Repository\Option\SaveOption;
+use Espo\Core\Upgrades\Migration\Script;
+use Espo\Modules\Crm\Entities\KnowledgeBaseArticle;
+use Espo\ORM\EntityManager;
+use Espo\Tools\Email\Util;
+
+class AfterUpgrade implements Script
+{
+ public function __construct(
+ private EntityManager $entityManager,
+ ) {}
+
+ public function run(): void
+ {
+ $this->processKbArticles();
+ }
+
+ private function processKbArticles(): void
+ {
+ if (!str_starts_with(php_sapi_name(), 'cli')) {
+ return;
+ }
+
+ $articles = $this->entityManager
+ ->getRDBRepositoryByClass(KnowledgeBaseArticle::class)
+ ->sth()
+ ->select([
+ 'id',
+ 'body',
+ 'bodyPlain',
+ ])
+ ->limit(0, 3000)
+ ->find();
+
+ foreach ($articles as $article) {
+ $plain = Util::stripHtml($article->getBody() ?? '') ?: null;
+
+ $article->set('bodyPlain', $plain);
+
+ $this->entityManager->saveEntity($article, [SaveOption::SKIP_HOOKS => true]);
+ }
+ }
+}
diff --git a/application/Espo/Entities/Email.php b/application/Espo/Entities/Email.php
index 0e61bdf50f..5ef9f00032 100644
--- a/application/Espo/Entities/Email.php
+++ b/application/Espo/Entities/Email.php
@@ -218,7 +218,7 @@ class Email extends Entity
return null;
}
- return EmailUtil::stripBodyPlainQuotePart($body) ?: null;
+ return EmailUtil::stripPlainTextQuotePart($body) ?: null;
}
public function getBodyPlain(): ?string
@@ -230,41 +230,7 @@ class Email extends Entity
/** @var string $body */
$body = $this->get('body') ?? '';
- $breaks = ["<br />", "<br>", "<br/>", "<br />", "&lt;br /&gt;", "&lt;br/&gt;", "&lt;br&gt;"];
-
- $body = str_ireplace($breaks, "\r\n", $body);
- $body = strip_tags($body);
-
- $reList = [
- '&(quot|#34);',
- '&(amp|#38);',
- '&(lt|#60);',
- '&(gt|#62);',
- '&(nbsp|#160);',
- '&(iexcl|#161);',
- '&(cent|#162);',
- '&(pound|#163);',
- '&(copy|#169);',
- '&(reg|#174);',
- ];
-
- $replaceList = [
- '',
- '&',
- '<',
- '>',
- ' ',
- '¡',
- '¢',
- '£',
- '©',
- '®',
- ];
-
- foreach ($reList as $i => $re) {
- /** @var string $body */
- $body = mb_ereg_replace($re, $replaceList[$i], $body, 'i');
- }
+ $body = EmailUtil::stripHtml($body);
if (!$body) {
return null;
diff --git a/application/Espo/Modules/Crm/Entities/KnowledgeBaseArticle.php b/application/Espo/Modules/Crm/Entities/KnowledgeBaseArticle.php
index 717bbf9ed5..2dec01a30d 100644
--- a/application/Espo/Modules/Crm/Entities/KnowledgeBaseArticle.php
+++ b/application/Espo/Modules/Crm/Entities/KnowledgeBaseArticle.php
@@ -117,4 +117,14 @@ class KnowledgeBaseArticle extends Entity
return $this;
}
+
+ public function getBody(): ?string
+ {
+ return $this->get('body');
+ }
+
+ public function setBody(?string $body): self
+ {
+ return $this->set('body', $body);
+ }
}
diff --git a/application/Espo/Modules/Crm/Hooks/KnowledgeBaseArticle/SetBodyPlain.php b/application/Espo/Modules/Crm/Hooks/KnowledgeBaseArticle/SetBodyPlain.php
new file mode 100644
index 0000000000..301daf736c
--- /dev/null
+++ b/application/Espo/Modules/Crm/Hooks/KnowledgeBaseArticle/SetBodyPlain.php
@@ -0,0 +1,65 @@
+<?php
+/************************************************************************
+ * This file is part of EspoCRM.
+ *
+ * EspoCRM – Open Source CRM application.
+ * Copyright (C) 2014-2025 Yurii Kuznietsov, Taras Machyshyn, Oleksii Avramenko
+ * Website: https://www.espocrm.com
+ *
+ * This program is free software: you can redistribute it and/or modify
+ * it under the terms of the GNU Affero General Public License as published by
+ * the Free Software Foundation, either version 3 of the License, or
+ * (at your option) any later version.
+ *
+ * This program is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
+ * GNU Affero General Public License for more details.
+ *
+ * You should have received a copy of the GNU Affero General Public License
+ * along with this program. If not, see <https://www.gnu.org/licenses/>.
+ *
+ * The interactive user interfaces in modified source and object code versions
+ * of this program must display Appropriate Legal Notices, as required under
+ * Section 5 of the GNU Affero General Public License version 3.
+ *
+ * In accordance with Section 7(b) of the GNU Affero General Public License version 3,
+ * these Appropriate Legal Notices must retain the display of the "EspoCRM" word.
+ ************************************************************************/
+
+namespace Espo\Modules\Crm\Hooks\KnowledgeBaseArticle;
+
+use Espo\Core\Hook\Hook\BeforeSave;
+use Espo\Modules\Crm\Entities\KnowledgeBaseArticle;
+use Espo\ORM\Entity;
+use Espo\ORM\Repository\Option\SaveOptions;
+use Espo\Tools\Email\Util as EmailUtil;
+
+/**
+ * @implements BeforeSave<KnowledgeBaseArticle>
+ */
+class SetBodyPlain implements BeforeSave
+{
+ private const ATTR_BODY = 'body';
+ private const ATTR_BODY_PLAIN = 'bodyPlain';
+
+ public function beforeSave(Entity $entity, SaveOptions $options): void
+ {
+ if (!$entity->isAttributeChanged(self::ATTR_BODY)) {
+ return;
+ }
+
+ $bodyPlain = $this->stripHtml($entity->getBody());
+
+ $entity->set(self::ATTR_BODY_PLAIN, $bodyPlain);
+ }
+
+ private function stripHtml(?string $body): ?string
+ {
+ if (!$body) {
+ return null;
+ }
+
+ return EmailUtil::stripHtml($body) ?: null;
+ }
+}
diff --git a/application/Espo/Modules/Crm/Resources/i18n/en_US/KnowledgeBaseArticle.json b/application/Espo/Modules/Crm/Resources/i18n/en_US/KnowledgeBaseArticle.json
index 51d807ed65..2fbf1e8bad 100644
--- a/application/Espo/Modules/Crm/Resources/i18n/en_US/KnowledgeBaseArticle.json
+++ b/application/Espo/Modules/Crm/Resources/i18n/en_US/KnowledgeBaseArticle.json
@@ -19,7 +19,8 @@
"body": "Body",
"categories": "Categories",
"language": "Language",
- "portals": "Portals"
+ "portals": "Portals",
+ "bodyPlain": "Body Plain"
},
"links": {
"cases": "Cases",
diff --git a/application/Espo/Modules/Crm/Resources/metadata/entityDefs/KnowledgeBaseArticle.json b/application/Espo/Modules/Crm/Resources/metadata/entityDefs/KnowledgeBaseArticle.json
index b6485f03e6..c6221470bc 100644
--- a/application/Espo/Modules/Crm/Resources/metadata/entityDefs/KnowledgeBaseArticle.json
+++ b/application/Espo/Modules/Crm/Resources/metadata/entityDefs/KnowledgeBaseArticle.json
@@ -101,6 +101,12 @@
},
"body": {
"type": "wysiwyg"
+ },
+ "bodyPlain": {
+ "type": "text",
+ "readOnly": true,
+ "directUpdateDisabled": true,
+ "fieldManagerParamList": []
}
},
"links": {
@@ -140,7 +146,12 @@
},
"collection": {
"orderBy": "order",
- "order": "asc"
+ "order": "asc",
+ "textFilterFields": [
+ "name",
+ "bodyPlain"
+ ],
+ "fullTextSearch": true
},
"optimisticConcurrencyControl": true
}
diff --git a/application/Espo/Tools/Email/Util.php b/application/Espo/Tools/Email/Util.php
index 150ff86636..bb060204f6 100644
--- a/application/Espo/Tools/Email/Util.php
+++ b/application/Espo/Tools/Email/Util.php
@@ -64,13 +64,72 @@ class Util
return $string;
}
- static public function stripBodyPlainQuotePart(string $body): string
+ /**
+ * Strip HTML.
+ *
+ * @since 9.1.0
+ */
+ static public function stripHtml(string $string): string
{
- if (!$body) {
+ $breaks = [
+ "<br />",
+ "<br>",
+ "<br/>",
+ "<br />",
+ "&lt;br /&gt;",
+ "&lt;br/&gt;",
+ "&lt;br&gt;",
+ ];
+
+ $string = str_ireplace($breaks, "\r\n", $string);
+ $string = strip_tags($string);
+
+ $reList = [
+ '&(quot|#34);',
+ '&(amp|#38);',
+ '&(lt|#60);',
+ '&(gt|#62);',
+ '&(nbsp|#160);',
+ '&(iexcl|#161);',
+ '&(cent|#162);',
+ '&(pound|#163);',
+ '&(copy|#169);',
+ '&(reg|#174);',
+ ];
+
+ $replaceList = [
+ '',
+ '&',
+ '<',
+ '>',
+ ' ',
+ '¡',
+ '¢',
+ '£',
+ '©',
+ '®',
+ ];
+
+ foreach ($reList as $i => $re) {
+ /** @var string $string */
+ $string = mb_ereg_replace($re, $replaceList[$i], $string, 'i');
+ }
+
+ return $string;
+ }
+
+ /**
+ * Strip a quote part in a plain text.
+ *
+ * @since 9.0.0
+ */
+ static public function stripPlainTextQuotePart(string $string): string
+ {
+ if (!$string) {
return '';
}
- $lines = preg_split("/\r\n|\n|\r/", $body);
+ $lines = preg_split("/\r\n|\n|\r/", $string);
if (!is_array($lines)) {
return '';