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

Enable logographic detection #2056

Merged
merged 1 commit into from
Mar 2, 2025
Merged
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
36 changes: 25 additions & 11 deletions controllers/scriptStorage.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,9 @@ var statusError = require('../libs/debug').statusError;
var isSecured = require('../libs/debug').isSecured;
var uaOUJS = require('../libs/debug').uaOUJS;

var rLogographic = require('../libs/debug').rLogographic;
var logographicDivisor = require('../libs/debug').logographicDivisor;

//

//--- Dependency inclusions
Expand Down Expand Up @@ -1198,8 +1201,6 @@ exports.storeScript = function (aUser, aMeta, aBuf, aUpdate, aCallback) {
var userName = findMeta(aMeta, 'OpenUserJS.author.0.value') || aUser.name;
var scriptName = null;
var scriptDescription = null;
var thisName = null;
var thisDescription = null;
var hasInvalidKey = null;

async.series([
Expand Down Expand Up @@ -1250,8 +1251,7 @@ exports.storeScript = function (aUser, aMeta, aBuf, aUpdate, aCallback) {
// Check for non-localized presence
name.forEach(function (aElement, aIndex, aArray) {
if (!name[aIndex].key) {
thisName = aElement.value;
scriptName = cleanFilename(thisName, '');
scriptName = aElement.value;
}
});

Expand Down Expand Up @@ -1311,8 +1311,7 @@ exports.storeScript = function (aUser, aMeta, aBuf, aUpdate, aCallback) {
if (description) {
description.forEach(function (aElement, aIndex, aArray) {
if (!description[aIndex].key) {
thisDescription = aElement.value;
scriptDescription = cleanFilename(thisDescription, '');
scriptDescription = aElement.value;
}
});
}
Expand Down Expand Up @@ -2058,6 +2057,8 @@ exports.storeScript = function (aUser, aMeta, aBuf, aUpdate, aCallback) {
var s3 = null;
var now = null;

var storeDescriptionLength = null;

if (aRemoved) {
aCallback(new statusError({
message: 'Script removed permanently.',
Expand All @@ -2079,11 +2080,18 @@ exports.storeScript = function (aUser, aMeta, aBuf, aUpdate, aCallback) {
} else if (!aScript) {
// New script
now = new Date();


storeDescriptionLength = settings.scriptSearchQueryStoreMaxDescription;
storeDescriptionLength = rLogographic.test(scriptDescription)
? parseInt(storeDescriptionLength / logographicDivisor)
: storeDescriptionLength;

aScript = new Script({
name: thisName,
name: scriptName,
_description: (
thisDescription
? thisDescription.substr(0, settings.scriptSearchQueryStoreMaxDescription).trim()
scriptDescription
? scriptDescription.substr(0, storeDescriptionLength).trim()
: ''
),
author: aUser.name,
Expand Down Expand Up @@ -2123,9 +2131,15 @@ exports.storeScript = function (aUser, aMeta, aBuf, aUpdate, aCallback) {
}), null);
return;
}

storeDescriptionLength = settings.scriptSearchQueryStoreMaxDescription;
storeDescriptionLength = rLogographic.test(scriptDescription)
? parseInt(storeDescriptionLength / logographicDivisor)
: storeDescriptionLength;

aScript._description = (
thisDescription
? thisDescription.substr(0, settings.scriptSearchQueryStoreMaxDescription).trim()
scriptDescription
? scriptDescription.substr(0, storeDescriptionLength).trim()
: ''
);
aScript.meta = aMeta;
Expand Down
5 changes: 5 additions & 0 deletions libs/debug.js
Original file line number Diff line number Diff line change
Expand Up @@ -77,6 +77,11 @@ uaOUJS = pkg.org + '/' + pkg.version
+ 'OUJS/20131106 ' + pkg.name + '/' + hash;
exports.uaOUJS = uaOUJS;

// NOTE: Requires DB migration for changing below settings
exports.rLogographic = /[\p{sc=Han}\p{sc=Hiragana}\p{sc=Katakana}\p{sc=Hangul}]/u;
exports.logographicDivisor = 4;

// /NOTE:

// ES6+ in use to eliminate extra property
class statusError extends Error {
Expand Down
17 changes: 15 additions & 2 deletions libs/modelParser.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,9 @@ var isDev = require('../libs/debug').isDev;
var isDbg = require('../libs/debug').isDbg;
var statusError = require('../libs/debug').statusError;

var rLogographic = require('../libs/debug').rLogographic;
var logographicDivisor = require('../libs/debug').logographicDivisor;

//

//--- Dependency inclusions
Expand All @@ -31,6 +34,7 @@ var isFQUrl = require('../libs/helpers').isFQUrl;
var isSameOrigin = require('../libs/helpers').isSameOrigin;
var patternHasSameOrigin = require('../libs/helpers').patternHasSameOrigin;
var scriptStorageLib = require('../libs/scriptStorage').invalidKey;
var settings = require('../models/settings.json');


//--- Configuration inclusions
Expand Down Expand Up @@ -316,6 +320,9 @@ var parseScript = function (aScript) {

var matches = null;

var logographic = null;
var storeDescriptionLength = null;

if (!aScript) {
return;
}
Expand All @@ -339,8 +346,14 @@ var parseScript = function (aScript) {
}
});

if (script.description && script._description && script.description.length > script._description.length) {
script.hasLongDescription = true;
logographic = rLogographic.test(script.description);

if (script.description && script._description
&& script.description.length && script.description.length > logographicDivisor
&& script.description.length > (logographic
? parseInt(script._description.length / logographicDivisor)
: script._description.length)) {
script.hasLongDescription = true;
}
}

Expand Down