-
Notifications
You must be signed in to change notification settings - Fork 4
/
site.js
397 lines (376 loc) · 20.3 KB
/
site.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
import { Plug } from '/mindtouch-http.js/plug.js';
import { Settings } from './lib/settings.js';
import { utility } from './lib/utility.js';
import { modelParser } from './lib/modelParser.js';
import { searchModel } from './models/search.model.js';
import { siteTagsModelGet, siteTagsModelPost } from './models/siteTags.model.js';
import { reportLogsModel } from './models/reportLogs.model.js';
import { logUrlModel } from './models/logUrl.model.js';
import { siteActivityModel } from './models/siteActivity.model.js';
import { searchAnalyticsModel } from './models/searchAnalytics.model.js';
import { searchAnalyticsQueryModel } from './models/searchAnalyticsQuery.model.js';
import { siteRolesModel } from './models/siteRoles.model.js';
import { localizationsModel } from './models/localizations.model.js';
function _buildSearchConstraints(params) {
let constraints = [];
if('path' in params) {
let path = params.path;
if(path.substr(0, 1) === '/') {
path = path.substr(1);
}
constraints.push('+path.ancestor:' + utility.searchEscape(path));
}
if('tags' in params) {
let tags = params.tags;
if(typeof tags === 'string' && (tags)) {
tags = tags.split(',');
}
tags.forEach((tag) => {
constraints.push('+tag:"' + utility.searchEscape(tag) + '"');
});
}
if('type' in params) {
let types = params.type;
if(typeof types === 'string' && (types)) {
types = types.split(',');
}
types.forEach((type) => {
constraints.push('+type:' + utility.searchEscape(type));
});
}
if('namespaces' in params) {
let namespaces = params.namespaces;
if(typeof namespaces === 'string') {
namespaces = namespaces.split(',');
}
namespaces.forEach((ns) => {
constraints.push(`+namespace:${utility.searchEscape(ns)}`);
});
}
return constraints.length > 0 ? '+(' + constraints.join(' ') + ')' : '';
}
function _getBatchTagsTemplate(data) {
var postBatchTagsTemplate = '<?xml version="1.0"?><tags>';
if(Array.isArray(data.add) && data.add.length > 0) {
data.add.forEach((elm) => {
let tagStr = `<tag.add value="${utility.escapeHTML(elm.name)}">`;
elm.pageids.forEach((id) => {
tagStr += `<page id="${id}"></page>`;
});
tagStr += '</tag.add>';
postBatchTagsTemplate = postBatchTagsTemplate + tagStr;
});
}
if(Array.isArray(data.remove) && data.remove.length > 0) {
data.remove.forEach((elm) => {
let tagStr = `<tag.remove value="${utility.escapeHTML(elm.name)}">`;
elm.pageids.forEach((id) => {
tagStr += `<page id="${id}"></page>`;
});
tagStr += '</tag.remove>';
postBatchTagsTemplate = postBatchTagsTemplate + tagStr;
});
}
postBatchTagsTemplate = `${postBatchTagsTemplate}</tags>`;
return postBatchTagsTemplate;
}
/**
* A class for administering aspects of a MindTouch site.
*/
export class Site {
/**
* Construct a Site object.
* @param {Settings} [settings] - The {@link Settings} information to use in construction. If not supplied, the default settings are used.
*/
constructor(settings = new Settings()) {
this.plug = new Plug(settings.host, settings.plugConfig).at('@api', 'deki', 'site');
}
/**
* Get the available site activity logs.
* @returns {Promise.<reportLogsModel>} - A Promise that, when resolved, yields a {@link reportLogsModel} containing the available logs for site activity.
*/
getSiteActivityLogs() {
return this.plug.at('activity', 'logs').get().then((r) => r.json()).then(modelParser.createParser(reportLogsModel));
}
/**
* Get the available search query logs.
* @returns {Promise.<reportLogsModel>} - A Promise that, when resolved, yields a {@link reportLogsModel} containing the available logs for search query.
*/
getSearchQueryLogs() {
return this.plug.at('query', 'logs').get().then((r) => r.json()).then(modelParser.createParser(reportLogsModel));
}
/**
* Get the localized string corresponding to the supplied resource key.
* @param {Object} options - Options to direct the fetching of the localized string.
* @param {String} options.key - The key that identifies the string to fetch.
* @param {String} [options.lang] - A language code used to fetch the string in a specific language. If not supplied, the current system language will be used.
* @returns {Promise.<String>} - A Promise that, when resolved, yields the fetched string.
*/
getResourceString(options = {}) {
if(!('key' in options)) {
return Promise.reject('No resource key was supplied');
}
let locPlug = this.plug.at('localization', options.key);
if('lang' in options) {
locPlug = locPlug.withParam('lang', options.lang);
}
return locPlug.get().then((r) => r.text());
}
/**
* Fetch a batch of translated resource strings.
* @param {Object} options Options to direct the fetching of the translated strings.
* @param {Array} options.keys An array of resource keys to fetch the translations for.
* @param {String} [options.lang] Optional language code to use for resource localization.
* @returns {Promise} A promise that, when resolved, yields a localizationsModel containing the requested translations.
*/
getResourceStrings({ keys, lang } = {}) {
if(!keys || !Array.isArray(keys)) {
return Promise.reject(new Error('The keys parameter must be supplied, and it must be an array.'));
}
const params = { resources: keys.join(',') };
if(lang) {
if(typeof lang !== 'string') {
return Promise.reject(new Error('The lang parameter must be a string'));
}
params.lang = lang;
}
return this.plug.at('localizations').withParams(params).get().then((r) => r.json()).then(modelParser.createParser(localizationsModel));
}
/**
* Get the available search query log url.
* @param {String} logName - Name of log to retrive URL from.
* @returns {Promise.<availableLogsModel>} - A Promise that, when resolved, yields a {@link availableLogsModel} containing log url.
*/
getSearchQueryLogUrl(logName) {
if(typeof logName === 'undefined' || logName.length === 0) {
return Promise.reject(new Error('Attempting to get log url without required name'));
}
return this.plug.at('query', 'logs', logName, 'url').get().then((r) => r.json()).then(modelParser.createParser(logUrlModel));
}
/**
* Get the available site activity log url.
* @param {String} logName - Name of log to retrive URL from.
* @returns {Promise.<availableLogsModel>} - A Promise that, when resolved, yields a {@link logUrlModel} containing log url.
*/
getSiteActivityLogUrl(logName) {
if(typeof logName === 'undefined' || logName.length === 0) {
return Promise.reject(new Error('Attempting to get log url without required name'));
}
return this.plug.at('activity', 'logs', logName, 'url').get().then((r) => r.json()).then(modelParser.createParser(logUrlModel));
}
/**
* Get tags list.
* @param {String} [params] - Parameters to send along to the API.
* @returns {Promise.<Object>} - A Promise that will be resolved with the tags data, or rejected with an error specifying the reason for rejection.
*/
getTags(params = {}) {
const siteTagsModelParser = modelParser.createParser(siteTagsModelGet);
return this.plug.at('tags').withParams(params).get().then((r) => r.json()).then(siteTagsModelParser);
}
/**
* Post tags list for each page that each tag in contained in.
* @param {Object} [params] - Options to direct the fetching of the localized tags.
* @param {Array} [params.add] - A tag array containing all the pages containing this tag where they need to be added.
* @param {Array} [params.remove] - A tag array containing all the pages containing this tag where they need to be removed.
* @returns {Promise.<Object>} - A Promise that will be resolved with the tags data, or rejected with an error specifying the reason for rejection.
*/
setTags(params = {}) {
const XMLBatchData = _getBatchTagsTemplate(params);
const siteTagsModelParser = modelParser.createParser(siteTagsModelPost);
return this.plug.at('tags').post(XMLBatchData, 'application/xml').then((r) => r.json()).then(siteTagsModelParser);
}
/**
* Perform a search across the site.
* This function takes a single parameter with the following options.
* @param {Number} [limit=10] - Limit search results to the specified number of items per paginated page.
* @param {Number} [offset=10] - The index in the total query results at which to begin the returned result set.
* @param {String|Array} [tags=''] - A comma-separated list or array of tags to constrain search results to items containing one of the tags.
* @param {String|Array} [type=''] - Type or types to filter the results in a comma delimited list or an array. Valid types: `wiki`, `document`, `image`, `binary`
* @param {String} [q=''] - Search keywords or advanced search syntax.
* @param {String} [path=''] - A page path to constrain the search results to items located under the specified path.
* @param {String|Array} [namespace='main'] - A comma-separated list or array of namespaces to filter the results by. Valid namespaces: 'main', 'template', 'user'.
* @param {String} [sessionid=null] - An identifier to know that the query is grouped with the previous query.
* @param {Boolean} [recommendations=true] - `true` to include recommended search results based off site configuration. `false` to suppress them.
* @returns {Promise.<searchModel>} - A Promise that, when resolved, yields the results from the search in a {@link searchModel}.
*/
search({ limit = 10, offset = 0, q = '', path = '', recommendations = true, tags = '', type = '', namespaces = 'main', sessionid = null } = {}) {
const constraint = {};
if(path !== '' && path !== '/') {
constraint.path = path;
}
if(tags !== '') {
constraint.tags = tags;
}
if(type !== '') {
constraint.type = type;
}
constraint.namespaces = namespaces;
const searchParams = {
limit,
offset,
sortBy: '-rank',
q,
summarypath: encodeURI(path),
constraint: _buildSearchConstraints(constraint),
recommendations
};
if(sessionid) {
searchParams.sessionid = sessionid;
}
return this.plug.at('query').withParams(searchParams).get().then((r) => r.json()).then(modelParser.createParser(searchModel));
}
/**
* Search the site index
* @param {Object} options - The options to direct the search operation.
* @param {String} options.q The search string
* @param {Number|String} [options.limit=100] The maximum number of items to retrieve. Must be a positive number or 'all' to retrieve all items.
* @param {Number} [options.offset=0] Number of items to skip. Must be a positive number or 0 to not skip any.
* @param {String} [options.sortBy='-score'] Sort field. Prefix value with '-' to sort descending.
* @param {String} options.constraintString The pre-built constraint string to use. If not supplied, it will be built from the options.constraints object. If both options.constraintString and options.constraints are supplied, this parameter will take precedence.
* @param {Object} [options.constraints={}] Addidional search constraints
* @param {String} options.constraints.type The article type to filter from the results.
* @param {String} options.constraints.path The path to use for path.ancestor in the search constraint.
* @param {Array} options.constraints.tags An array of tags to only consider when returning page results.
* @param {Array} options.constraints.namespaces An array of namespaces to limit the results by.
* @param {Boolean} [options.verbose=true] Show verbose page xml
* @param {String} [options.parser='bestguess'] - The parser to use for the query. Must be one of "bestguess", "term", "filename", "lucene"
* @returns {Promise.<Object>} - A Promise that will be resolved with the search results, or rejected with an error specifying the reason for rejection.
*/
searchIndex({ q = '', limit = 100, offset = 0, sortBy = '-score', constraintString = null, constraints = {}, verbose = true, parser = 'bestguess', format = 'xml' } = {}) {
if(typeof limit === 'string') {
if(limit !== 'all') {
return Promise.reject(new Error('The limit for index searching must be a number or "all"'));
}
}
const searchParams = {
q,
limit,
offset,
sortby: sortBy,
constraint: constraintString || _buildSearchConstraints(constraints),
verbose,
parser,
format
};
return this.plug.at('search').withParams(searchParams).get().then((r) => r.json()).then(modelParser.createParser(searchModel));
}
/**
* Get the analytics for search on the site
* @param {Object} options - The paramaters to pass through with the request
* @param {String} [options.start] - The start date (YYYYMMDDHHMMSS)
* @param {String} [options.end] - The end date (YYYYMMDDHHMMSS)
* @param {String} [options.queryFilters] - the stem queries you want to return results for
* @param {String} [options.userFilter] - The user type you want to filter by (Anonymous, Community, Pro)
* @param {String} [options.groupIds] - Filter all search data by a set of comma separated group ids
* @param {String} [options.bucket] - The time you want to bucket results into
* @param {String} [options.origin] - The source of the search query (mt-web, mt-api, etc)
* @param {String} [options.webWidgetEmbedId] - the embed id for the source web widget
* @param {String} [options.sortBy] - Sort table data by this field (e.g. clicks, position) (default: clicks)
* @param {String} [options.sortOrder] - Sort direction to be used with sortby (e.g. asc, desc) (default: desc)
* @param {Number} [options.limit] - Number of clicked results to return results for (between 1 and 1000 inclusive) (default: 100)
* @returns {Promise.<Object>} - A Promise that will be resolved with the search analytics data, or rejected with an error specifiying the reason for rejection.
*/
getSearchAnalytics({ start = null, end = null, queryFilters = null, userFilter = null, groupIds = null, bucket = null, origin = null, webWidgetEmbedId = null, sortBy = null, sortOrder = null, limit = null }) {
const searchParams = {
start,
end,
queryFilters,
userFilter,
groupids: groupIds,
bucket,
originFilter: origin,
web_widget_embed_id: webWidgetEmbedId, // eslint-disable-line camelcase
sortby: sortBy,
sortorder: sortOrder,
limit
};
return this.plug.at('search', 'analytics').withParams(utility.cleanParams(searchParams)).get().then((r) => r.json()).then(modelParser.createParser(searchAnalyticsModel));
}
/**
* Get the search analytics for the given period for a particular query
* @param {Object} options - The paramaters to pass through with the request
* @param {String} [options.query] - Query to generate analytics for
* @param {String} [options.start] - The start date (YYYYMMDDHHMMSS)
* @param {String} [options.end] - The end date (YYYYMMDDHHMMSS)
* @param {String} [options.userFilter] - The user type you want to filter by (Anonymous, Community, Pro)
* @param {String} [options.groupIds] - Filter all search data by a set of comma separated group ids
* @param {String} [options.bucket] - The time you want to bucket results into (e.g. day, month) (default: month)
* @param {String} [options.origin] - The source of the search query (mt-web, mt-api, etc)
* @param {String} [options.webWidgetEmbedId] - the embed id for the source web widget
* @param {String} [options.sortBy] - Sort table data by this field (e.g. clicks, position) (default: clicks)
* @param {String} [options.sortOrder] - Sort direction to be used with sortby (e.g. asc, desc) (default: desc)
* @param {Number} [options.limit] - Number of clicked results to return results for (between 1 and 1000 inclusive) (default: 100)
* @returns {Promise.<Object>} - A Promise that will be resolved with the search analytics data, or rejected with an error specifiying the reason for rejection.
*/
getSearchAnalyticsQuery({ query, start = null, end = null, userFilter = null, groupIds = null, bucket = null, origin = null, webWidgetEmbedId = null, sortBy = null, sortOrder = null, limit = null }) {
const searchParams = {
query,
start,
end,
userFilter,
groupids: groupIds,
bucket,
originFilter: origin,
web_widget_embed_id: webWidgetEmbedId, // eslint-disable-line camelcase
sortby: sortBy,
sortorder: sortOrder,
limit
};
return this.plug.at('search', 'analytics', 'query').withParams(utility.cleanParams(searchParams)).get().then((r) => r.json()).then(modelParser.createParser(searchAnalyticsQueryModel));
}
/**
* Get the activity stats for the site.
* @param {Date} [since] Start date for report.
* @returns {Promise.<Object>} - A Promise that will be resolved with the activity data, or rejected with an error specifying the reason for rejection.
*/
getActivity(since = null) {
let activityPlug = this.plug.at('activity');
if(since !== null) {
if(!(since instanceof Date)) {
return Promise.reject(new Error('The `since` parameter must be of type Date.'));
}
// Create a date string of the format `yyyyMMddHHmmss`
const sinceString = utility.getApiDateString(since);
activityPlug = activityPlug.withParam('since', sinceString);
}
return activityPlug.get().then((r) => r.json()).then(modelParser.createParser(siteActivityModel));
}
/**
* Retrieve list of defined roles
* @returns {Promise.<Object>} - A Promise that will be resolved with the roles info, or rejected with an error specifying the reason for rejection.
*/
getRoles() {
return this.plug.at('roles').get().then((r) => r.json()).then(modelParser.createParser(siteRolesModel));
}
/**
* Send feedback to the site owner.
* @param {Object} feedbackData The data to send as the feedback.
* @param {String} feedbackData.comment The comment body.
* @param {String} [feedbackData.title] The title/subject for the feedback.
* @param {Object} [feedbackData.metadata] Additional data to accompany the feedback submission.
* @returns {Promise} A Promise that, when resolved, indicates a successful feedback submission.
*/
sendFeedback({ comment, title, metadata = {} } = {}) {
if(typeof comment !== 'string') {
return Promise.reject(new Error('The `comment` parameter must be supplied, and must be a string.'));
}
let feedbackXml = '<feedback>';
feedbackXml += `<body>${comment}</body>`;
if(title) {
if(typeof title !== 'string') {
return Promise.reject(new Error('The title parameter must be a string.'));
}
feedbackXml += `<title>${title}</title>`;
}
feedbackXml += '<metadata>';
if(typeof metadata !== 'object') {
return Promise.reject(new Error('The `metadata` parameter must be an object.'));
}
Object.keys(metadata).forEach((key) => {
feedbackXml += `<${key}>${metadata[key].toString()}</${key}>`;
});
feedbackXml += '</metadata>';
feedbackXml += '</feedback>';
return this.plug.at('feedback').post(feedbackXml, utility.xmlRequestType);
}
}