Commit ef1b3d98 authored by Liang Ding's avatar Liang Ding

🎨 Fix #12489

parent e757b4de
...@@ -18,11 +18,18 @@ ...@@ -18,11 +18,18 @@
package org.b3log.solo.model; package org.b3log.solo.model;
import org.apache.commons.lang.StringUtils;
import org.b3log.latke.util.Strings;
import java.util.LinkedHashSet;
import java.util.Set;
import java.util.regex.Pattern;
/** /**
* This class defines all tag model relevant keys. * This class defines all tag model relevant keys.
* *
* @author <a href="http://88250.b3log.org">Liang Ding</a> * @author <a href="http://88250.b3log.org">Liang Ding</a>
* @version 1.0.0.7, Dec 3, 2010 * @version 1.1.0.0, Aug 27, 2018
*/ */
public final class Tag { public final class Tag {
...@@ -52,7 +59,97 @@ public final class Tag { ...@@ -52,7 +59,97 @@ public final class Tag {
public static final String TAG_PUBLISHED_REFERENCE_COUNT = "tagPublishedRefCount"; public static final String TAG_PUBLISHED_REFERENCE_COUNT = "tagPublishedRefCount";
/** /**
* Private default constructor. * Tag title pattern string.
*/
public static final String TAG_TITLE_PATTERN_STR = "[\\u4e00-\\u9fa5,\\w,&,\\+,\\-,\\.]+";
/**
* Tag title pattern.
*/
public static final Pattern TAG_TITLE_PATTERN = Pattern.compile(TAG_TITLE_PATTERN_STR);
/**
* Max tag count.
*/
public static final int MAX_TAG_COUNT = 4;
/**
* Formats the specified tags.
* <ul>
* <li>Trims every tag</li>
* <li>Deduplication</li>
* </ul>
*
* @param tagStr the specified tags
* @return formatted tags string
*/
public static String formatTags(final String tagStr) {
final String tagStr1 = tagStr.replaceAll("\\s+", "").replaceAll(",", ",").replaceAll("、", ",").
replaceAll(";", ",").replaceAll(";", ",");
String[] tagTitles = tagStr1.split(",");
tagTitles = Strings.trimAll(tagTitles);
// deduplication
final Set<String> titles = new LinkedHashSet<>();
for (final String tagTitle : tagTitles) {
if (!exists(titles, tagTitle)) {
titles.add(tagTitle);
}
}
tagTitles = titles.toArray(new String[0]);
int count = 0;
final StringBuilder tagsBuilder = new StringBuilder();
for (final String tagTitle : tagTitles) {
String title = tagTitle.trim();
if (StringUtils.isBlank(title)) {
continue;
}
if (StringUtils.length(title) > 12) {
continue;
}
if (!TAG_TITLE_PATTERN.matcher(title).matches()) {
continue;
}
tagsBuilder.append(title).append(",");
count++;
if (count >= MAX_TAG_COUNT) {
break;
}
}
if (tagsBuilder.length() > 0) {
tagsBuilder.deleteCharAt(tagsBuilder.length() - 1);
}
return tagsBuilder.toString();
}
/**
* Checks the specified title exists in the specified title set.
*
* @param titles the specified title set
* @param title the specified title to check
* @return {@code true} if exists, returns {@code false} otherwise
*/
private static boolean exists(final Set<String> titles, final String title) {
for (final String setTitle : titles) {
if (setTitle.equalsIgnoreCase(title)) {
return true;
}
}
return false;
}
/**
* Private constructor.
*/ */
private Tag() {} private Tag() {
}
} }
...@@ -52,7 +52,7 @@ import java.util.Set; ...@@ -52,7 +52,7 @@ import java.util.Set;
* Category console request processing. * Category console request processing.
* *
* @author <a href="http://88250.b3log.org">Liang Ding</a> * @author <a href="http://88250.b3log.org">Liang Ding</a>
* @version 1.1.1.2, Mar 3, 2018 * @version 1.1.1.3, Aug 27, 2018
* @since 2.0.0 * @since 2.0.0
*/ */
@RequestProcessor @RequestProcessor
...@@ -293,7 +293,10 @@ public class CategoryConsole { ...@@ -293,7 +293,10 @@ public class CategoryConsole {
try { try {
String tagsStr = requestJSONObject.optString(Category.CATEGORY_T_TAGS); String tagsStr = requestJSONObject.optString(Category.CATEGORY_T_TAGS);
tagsStr = tagsStr.replaceAll(",", ",").replaceAll("、", ","); tagsStr = Tag.formatTags(tagsStr);
if (StringUtils.isBlank(tagsStr)) {
throw new ServiceException(langPropsService.get("tagsEmptyLabel"));
}
final String[] tagTitles = tagsStr.split(","); final String[] tagTitles = tagsStr.split(",");
String addArticleWithTagFirstLabel = langPropsService.get("addArticleWithTagFirstLabel"); String addArticleWithTagFirstLabel = langPropsService.get("addArticleWithTagFirstLabel");
...@@ -423,7 +426,10 @@ public class CategoryConsole { ...@@ -423,7 +426,10 @@ public class CategoryConsole {
try { try {
String tagsStr = requestJSONObject.optString(Category.CATEGORY_T_TAGS); String tagsStr = requestJSONObject.optString(Category.CATEGORY_T_TAGS);
tagsStr = tagsStr.replaceAll(",", ",").replaceAll("、", ","); tagsStr = Tag.formatTags(tagsStr);
if (StringUtils.isBlank(tagsStr)) {
throw new ServiceException(langPropsService.get("tagsEmptyLabel"));
}
final String[] tagTitles = tagsStr.split(","); final String[] tagTitles = tagsStr.split(",");
String addArticleWithTagFirstLabel = langPropsService.get("addArticleWithTagFirstLabel"); String addArticleWithTagFirstLabel = langPropsService.get("addArticleWithTagFirstLabel");
......
...@@ -17,6 +17,7 @@ ...@@ -17,6 +17,7 @@
*/ */
package org.b3log.solo.service; package org.b3log.solo.service;
import org.apache.commons.lang.StringUtils;
import org.apache.commons.lang.time.DateFormatUtils; import org.apache.commons.lang.time.DateFormatUtils;
import org.apache.commons.lang.time.DateUtils; import org.apache.commons.lang.time.DateUtils;
import org.b3log.latke.Keys; import org.b3log.latke.Keys;
...@@ -54,7 +55,7 @@ import static org.b3log.solo.model.Article.*; ...@@ -54,7 +55,7 @@ import static org.b3log.solo.model.Article.*;
* Article management service. * Article management service.
* *
* @author <a href="http://88250.b3log.org">Liang Ding</a> * @author <a href="http://88250.b3log.org">Liang Ding</a>
* @version 1.2.2.10, Oct 3, 2017 * @version 1.2.2.11, Aug 27, 2018
* @since 0.3.5 * @since 0.3.5
*/ */
@Service @Service
...@@ -287,8 +288,12 @@ public class ArticleMgmtService { ...@@ -287,8 +288,12 @@ public class ArticleMgmtService {
try { try {
final JSONObject article = requestJSONObject.getJSONObject(ARTICLE); final JSONObject article = requestJSONObject.getJSONObject(ARTICLE);
final String tagsString = article.optString(Article.ARTICLE_TAGS_REF); String tagsString = article.optString(Article.ARTICLE_TAGS_REF);
article.put(Article.ARTICLE_TAGS_REF, tagsString.replaceAll(",", ",").replaceAll("、", ",")); tagsString = Tag.formatTags(tagsString);
if (StringUtils.isBlank(tagsString)) {
throw new ServiceException(langPropsService.get("tagsEmptyLabel"));
}
article.put(Article.ARTICLE_TAGS_REF, tagsString);
final String articleId = article.getString(Keys.OBJECT_ID); final String articleId = article.getString(Keys.OBJECT_ID);
// Set permalink // Set permalink
...@@ -471,7 +476,10 @@ public class ArticleMgmtService { ...@@ -471,7 +476,10 @@ public class ArticleMgmtService {
try { try {
// Step 1: Add tags // Step 1: Add tags
String tagsString = article.optString(Article.ARTICLE_TAGS_REF); String tagsString = article.optString(Article.ARTICLE_TAGS_REF);
tagsString = tagsString.replaceAll(",", ",").replaceAll("、", ","); tagsString = Tag.formatTags(tagsString);
if (StringUtils.isBlank(tagsString)) {
throw new ServiceException(langPropsService.get("tagsEmptyLabel"));
}
article.put(Article.ARTICLE_TAGS_REF, tagsString); article.put(Article.ARTICLE_TAGS_REF, tagsString);
final String[] tagTitles = tagsString.split(","); final String[] tagTitles = tagsString.split(",");
final JSONArray tags = tag(tagTitles, article); final JSONArray tags = tag(tagTitles, article);
......
...@@ -18,7 +18,7 @@ ...@@ -18,7 +18,7 @@
# #
# Description: Solo language configurations(en_US). # Description: Solo language configurations(en_US).
# Version: 2.21.0.1, Apr 5, 2018 # Version: 2.21.0.2, Aug 27, 2018
# Author: Liang Ding # Author: Liang Ding
# Author: Liyuan Li # Author: Liyuan Li
# Author: Dongxu Wang # Author: Dongxu Wang
...@@ -394,7 +394,7 @@ titleEmptyLabel=Title is empty ...@@ -394,7 +394,7 @@ titleEmptyLabel=Title is empty
contentEmptyLabel=Content is empty contentEmptyLabel=Content is empty
orderEmptyLabel=Order is empty orderEmptyLabel=Order is empty
abstractEmptyLabel=Abstract is empty abstractEmptyLabel=Abstract is empty
tagsEmptyLabel=Tags is empty tagsEmptyLabel=Tags is empty (do not include specific symbol)
addressEmptyLabel=Address is empty addressEmptyLabel=Address is empty
noAuthorizationURLLabel=Can not retrieve authorization URL from Google, please \ noAuthorizationURLLabel=Can not retrieve authorization URL from Google, please \
make sure the <em>Consumer Secret</em> you typed in and then try again. make sure the <em>Consumer Secret</em> you typed in and then try again.
......
...@@ -18,7 +18,7 @@ ...@@ -18,7 +18,7 @@
# #
# Description: Solo default language configurations(zh_CN). # Description: Solo default language configurations(zh_CN).
# Version: 2.21.0.1, Apr 5, 2018 # Version: 2.21.0.2, Aug 27, 2018
# Author: Liang Ding # Author: Liang Ding
# Author: Liyuan Li # Author: Liyuan Li
# Author: Dongxu Wang # Author: Dongxu Wang
...@@ -392,7 +392,7 @@ titleEmptyLabel=\u6807\u9898\u4E0D\u80FD\u4E3A\u7A7A\uFF01 ...@@ -392,7 +392,7 @@ titleEmptyLabel=\u6807\u9898\u4E0D\u80FD\u4E3A\u7A7A\uFF01
contentEmptyLabel=\u5185\u5BB9\u4E0D\u80FD\u4E3A\u7A7A\uFF01 contentEmptyLabel=\u5185\u5BB9\u4E0D\u80FD\u4E3A\u7A7A\uFF01
orderEmptyLabel=\u5E8F\u53F7\u4E0D\u80FD\u4E3A\u7A7A\uFF01 orderEmptyLabel=\u5E8F\u53F7\u4E0D\u80FD\u4E3A\u7A7A\uFF01
abstractEmptyLabel=\u6458\u8981\u4E0D\u80FD\u4E3A\u7A7A\uFF01 abstractEmptyLabel=\u6458\u8981\u4E0D\u80FD\u4E3A\u7A7A\uFF01
tagsEmptyLabel=\u6807\u7B7E\u4E0D\u80FD\u4E3A\u7A7A\uFF01 tagsEmptyLabel=\u6807\u7B7E\u4E0D\u80FD\u4E3A\u7A7A\uFF08\u8BF7\u52FF\u5305\u542B\u7B26\u53F7\uFF09\uFF01
addressEmptyLabel=\u5730\u5740\u4E0D\u80FD\u4E3A\u7A7A\uFF01 addressEmptyLabel=\u5730\u5740\u4E0D\u80FD\u4E3A\u7A7A\uFF01
noAuthorizationURLLabel=\u4ECE Google \u83B7\u53D6\u6388\u6743\u5730\u5740\u5931\u8D25\uFF0C\u8BF7\u786E\u8BA4\u60A8\u8F93\u5165\u7684 \ noAuthorizationURLLabel=\u4ECE Google \u83B7\u53D6\u6388\u6743\u5730\u5740\u5931\u8D25\uFF0C\u8BF7\u786E\u8BA4\u60A8\u8F93\u5165\u7684 \
<em>Consumer Secret</em> \u662F\u6B63\u786E\u7684\uFF0C\u7136\u540E\u8FDB\u884C\u91CD\u8BD5\u3002 <em>Consumer Secret</em> \u662F\u6B63\u786E\u7684\uFF0C\u7136\u540E\u8FDB\u884C\u91CD\u8BD5\u3002
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment