Skip to content

Commit 66ef9fd

Browse files
authored
Enable logographic detection (#2056)
* Trim up some of the data a bit who don't know how to use SEO properly. i.e. possible spamming/poisoning * Bug fix using incorrect identifier. Post #2048 NOTE(s): * I tried for HOURS to get these settings into settings.json *(hence why the require is left in one file)* however RegExp just wouldn't have it no matter what I tried. Existing scripts worked, new scripts did not. If anyone has an idea PRs are welcome to move its location to settings.json. * DB migration will happen when I get around to it as this is very complicated since script source has to be reread but at least new scripts will have it.
1 parent 4e31414 commit 66ef9fd

3 files changed

Lines changed: 45 additions & 13 deletions

File tree

controllers/scriptStorage.js

Lines changed: 25 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,9 @@ var statusError = require('../libs/debug').statusError;
88
var isSecured = require('../libs/debug').isSecured;
99
var uaOUJS = require('../libs/debug').uaOUJS;
1010

11+
var rLogographic = require('../libs/debug').rLogographic;
12+
var logographicDivisor = require('../libs/debug').logographicDivisor;
13+
1114
//
1215

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

12051206
async.series([
@@ -1250,8 +1251,7 @@ exports.storeScript = function (aUser, aMeta, aBuf, aUpdate, aCallback) {
12501251
// Check for non-localized presence
12511252
name.forEach(function (aElement, aIndex, aArray) {
12521253
if (!name[aIndex].key) {
1253-
thisName = aElement.value;
1254-
scriptName = cleanFilename(thisName, '');
1254+
scriptName = aElement.value;
12551255
}
12561256
});
12571257

@@ -1311,8 +1311,7 @@ exports.storeScript = function (aUser, aMeta, aBuf, aUpdate, aCallback) {
13111311
if (description) {
13121312
description.forEach(function (aElement, aIndex, aArray) {
13131313
if (!description[aIndex].key) {
1314-
thisDescription = aElement.value;
1315-
scriptDescription = cleanFilename(thisDescription, '');
1314+
scriptDescription = aElement.value;
13161315
}
13171316
});
13181317
}
@@ -2058,6 +2057,8 @@ exports.storeScript = function (aUser, aMeta, aBuf, aUpdate, aCallback) {
20582057
var s3 = null;
20592058
var now = null;
20602059

2060+
var storeDescriptionLength = null;
2061+
20612062
if (aRemoved) {
20622063
aCallback(new statusError({
20632064
message: 'Script removed permanently.',
@@ -2079,11 +2080,18 @@ exports.storeScript = function (aUser, aMeta, aBuf, aUpdate, aCallback) {
20792080
} else if (!aScript) {
20802081
// New script
20812082
now = new Date();
2083+
2084+
2085+
storeDescriptionLength = settings.scriptSearchQueryStoreMaxDescription;
2086+
storeDescriptionLength = rLogographic.test(scriptDescription)
2087+
? parseInt(storeDescriptionLength / logographicDivisor)
2088+
: storeDescriptionLength;
2089+
20822090
aScript = new Script({
2083-
name: thisName,
2091+
name: scriptName,
20842092
_description: (
2085-
thisDescription
2086-
? thisDescription.substr(0, settings.scriptSearchQueryStoreMaxDescription).trim()
2093+
scriptDescription
2094+
? scriptDescription.substr(0, storeDescriptionLength).trim()
20872095
: ''
20882096
),
20892097
author: aUser.name,
@@ -2123,9 +2131,15 @@ exports.storeScript = function (aUser, aMeta, aBuf, aUpdate, aCallback) {
21232131
}), null);
21242132
return;
21252133
}
2134+
2135+
storeDescriptionLength = settings.scriptSearchQueryStoreMaxDescription;
2136+
storeDescriptionLength = rLogographic.test(scriptDescription)
2137+
? parseInt(storeDescriptionLength / logographicDivisor)
2138+
: storeDescriptionLength;
2139+
21262140
aScript._description = (
2127-
thisDescription
2128-
? thisDescription.substr(0, settings.scriptSearchQueryStoreMaxDescription).trim()
2141+
scriptDescription
2142+
? scriptDescription.substr(0, storeDescriptionLength).trim()
21292143
: ''
21302144
);
21312145
aScript.meta = aMeta;

libs/debug.js

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -77,6 +77,11 @@ uaOUJS = pkg.org + '/' + pkg.version
7777
+ 'OUJS/20131106 ' + pkg.name + '/' + hash;
7878
exports.uaOUJS = uaOUJS;
7979

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

8186
// ES6+ in use to eliminate extra property
8287
class statusError extends Error {

libs/modelParser.js

Lines changed: 15 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,9 @@ var isDev = require('../libs/debug').isDev;
66
var isDbg = require('../libs/debug').isDbg;
77
var statusError = require('../libs/debug').statusError;
88

9+
var rLogographic = require('../libs/debug').rLogographic;
10+
var logographicDivisor = require('../libs/debug').logographicDivisor;
11+
912
//
1013

1114
//--- Dependency inclusions
@@ -31,6 +34,7 @@ var isFQUrl = require('../libs/helpers').isFQUrl;
3134
var isSameOrigin = require('../libs/helpers').isSameOrigin;
3235
var patternHasSameOrigin = require('../libs/helpers').patternHasSameOrigin;
3336
var scriptStorageLib = require('../libs/scriptStorage').invalidKey;
37+
var settings = require('../models/settings.json');
3438

3539

3640
//--- Configuration inclusions
@@ -316,6 +320,9 @@ var parseScript = function (aScript) {
316320

317321
var matches = null;
318322

323+
var logographic = null;
324+
var storeDescriptionLength = null;
325+
319326
if (!aScript) {
320327
return;
321328
}
@@ -339,8 +346,14 @@ var parseScript = function (aScript) {
339346
}
340347
});
341348

342-
if (script.description && script._description && script.description.length > script._description.length) {
343-
script.hasLongDescription = true;
349+
logographic = rLogographic.test(script.description);
350+
351+
if (script.description && script._description
352+
&& script.description.length && script.description.length > logographicDivisor
353+
&& script.description.length > (logographic
354+
? parseInt(script._description.length / logographicDivisor)
355+
: script._description.length)) {
356+
script.hasLongDescription = true;
344357
}
345358
}
346359

0 commit comments

Comments
 (0)