Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

KB - Filters out templates which are inside a parent directory with the name "parts" or inside the path "kb/parts" #1893

Merged
merged 4 commits into from
Nov 28, 2023
Merged
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
43 changes: 41 additions & 2 deletions src/main/java/sirius/biz/tycho/kb/SynchronizeArticlesTask.java
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@
import sirius.pasta.tagliatelle.Template;
import sirius.pasta.tagliatelle.rendering.GlobalRenderContext;

import java.io.File;
import java.time.LocalDate;
import java.util.Arrays;
import java.util.List;
Expand All @@ -36,7 +37,10 @@
/**
* Scans all available articles and turns them into {@link KnowledgeBaseEntry knowledge base entries}.
* <p>
* Note that this also deletes outdated entries or empty chapters.
* Note:
* - the task also deletes outdated entries or empty chapters.
* - the task ignores everything in the "kb/parts" directory as this is meant for shared components.
* - the task ignores every template which has a parent directory called "parts" as this is meant for article components.
*/
@Register(framework = KnowledgeBase.FRAMEWORK_KNOWLEDGE_BASE)
public class SynchronizeArticlesTask implements EndOfDayTask {
Expand Down Expand Up @@ -93,7 +97,10 @@ private void synchronizeArticles() {
String syncId = keyGenerator.generateId();
Sirius.getClasspath()
.find(Pattern.compile("(default/|customizations/[^/]+/)?kb/.*\\.pasta"))
.forEach(matcher -> updateArticle(cleanupTemplatePath(matcher.group(0)), syncId));
.map(matcher -> cleanupTemplatePath(matcher.group(0)))
.filter(templatePath -> !isTemplateInsidePartsDirectory(templatePath))
.filter(templatePath -> !isTemplateAPartOfAnArticle(templatePath))
.forEach(templatePath -> updateArticle(templatePath, syncId));

elastic.refresh(KnowledgeBaseEntry.class);
cleanupOldEntries(syncId);
Expand All @@ -106,6 +113,38 @@ private void synchronizeArticles() {
knowledgeBase.resetLanguages();
}

/**
* Determines if the given template is part of an article.
* <p>
* Note:
* - Every template which is inside a parent directory called "parts" is considered a component of an article.
* <p>
* Example:
* kb/integration/document/parts
* The main document article is located at /document. Its components are located in /document/parts.
*
* @param templatePath the template path to check
* @return <tt>true</tt> if the template is part of an article, <tt>false</tt> otherwise
*/
private static boolean isTemplateAPartOfAnArticle(String templatePath) {
String parentDir = new File(templatePath).getParentFile().getName();
return "parts".equals(parentDir);
}

/**
* Determines if the given template path is located inside the "kb/parts" directory.
* <p>
* Note:
* - The kb/parts directory (and sub-dirs) only contains article components which are not articles on their own.
* - The directory is meant for shared components which are used by multiple articles.
*
* @param templatePath the template path to check
* @return <tt>true</tt> if the template path is located inside the "kb/parts" directory, <tt>false</tt> otherwise
*/
private static boolean isTemplateInsidePartsDirectory(String templatePath) {
return templatePath.contains("kb/parts/");
}

private String cleanupTemplatePath(String templatePath) {
if (templatePath.startsWith("default/")) {
return "/" + templatePath.substring(8);
Expand Down