-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathcms.js
More file actions
247 lines (186 loc) · 5.52 KB
/
cms.js
File metadata and controls
247 lines (186 loc) · 5.52 KB
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
/* eslint-disable no-tabs */
'use strict';
const DataWriter = require(__dirname + '/dataWriter.js');
const { Utils, Log } = require('larvitutils');
const topLogPrefix = 'larvitcms: ./cms.js: ';
class Cms {
constructor(options) {
this.options = options || {};
if (!options.db) throw new Error('Missing required option "db"');
if (!options.lUtils) options.lUtils = new Utils();
if (!this.options.log) {
this.options.log = new Log();
}
this.log = this.options.log;
for (const key of Object.keys(this.options)) {
this[key] = this.options[key];
}
this.dataWriter = new DataWriter({
log: this.log,
db: this.db,
});
};
async runDbMigrations() {
await this.dataWriter.runDbMigrations();
}
async getPages(options) {
const logPrefix = topLogPrefix + 'getPages() - ';
const tmpPages = {};
const dbFields = [];
const pages = [];
let sql;
options = options || {};
this.log.debug(logPrefix + 'Called with options: "' + JSON.stringify(options) + '"');
// Make sure options that should be arrays actually are arrays
// This will simplify our lives in the SQL builder below
if (options.langs !== undefined && !(options.langs instanceof Array)) {
options.langs = [options.langs];
}
if (options.uuids !== undefined && !(options.ids instanceof Array)) {
options.uuids = [options.uuids];
}
if (options.slugs !== undefined && !(options.slugs instanceof Array)) {
options.slugs = [options.slugs];
}
// Make sure there is an invalid ID in the id list if it is empty
// Since the most logical thing to do is replying with an empty set
if (options.uuids instanceof Array && options.uuids.length === 0) {
options.uuids.push('');
}
if (options.limit === undefined) {
options.limit = 10;
}
sql = 'SELECT pgd.*, p.*\n';
sql += 'FROM cms_pages p\n';
sql += ' LEFT JOIN cms_pagesData pgd ON pgd.pageUuid = p.uuid\n';
sql += 'WHERE 1 + 1\n';
// Only get post contents with selected languages
if (options.langs !== undefined) {
sql += ' AND pgd.lang IN (';
for (let i = 0; options.langs[i] !== undefined; i++) {
sql += '?,';
dbFields.push(options.langs[i]);
}
sql = sql.substring(0, sql.length - 1) + ')\n';
}
// Only get posts with the current slugs
if (options.slugs !== undefined) {
sql += ' AND p.uuid IN (SELECT pageUuid FROM cms_pagesData WHERE slug IN (';
for (let i = 0; options.slugs[i] !== undefined; i++) {
sql += '?,';
dbFields.push(options.slugs[i]);
}
sql = sql.substring(0, sql.length - 1) + '))\n';
}
// Only get posts with given ids
if (options.uuids !== undefined) {
sql += ' AND p.uuid IN (';
for (let i = 0; options.uuids[i] !== undefined; i++) {
const buffer = this.lUtils.uuidToBuffer(options.uuids[i]);
if (buffer === false) {
const e = new Error('Invalid page uuid supplied');
log.warn(logPrefix + e.message);
throw e;
}
sql += '?,';
dbFields.push(buffer);
}
sql = sql.substring(0, sql.length - 1) + ')\n';
}
if (options.published === true) {
sql += ' AND p.published = 1\n';
} else if (options.published === false) {
sql += ' AND p.published = 0\n';
}
sql += 'ORDER BY p.published DESC, p.name\n';
if (options.limit !== false) {
sql += 'LIMIT ' + Number(options.limit) + '\n';
if (options.offset !== undefined) {
sql += ' OFFSET ' + Number(options.offset);
}
}
const { rows } = await this.db.query(sql, dbFields);
for (let i = 0; rows[i] !== undefined; i++) {
const uuid = this.lUtils.formatUuid(rows[i].uuid);
if (tmpPages[uuid] === undefined) {
tmpPages[uuid] = {
uuid: uuid,
name: rows[i].name,
published: Boolean(rows[i].published),
template: rows[i].template,
langs: {},
};
}
tmpPages[uuid].langs[rows[i].lang] = {
htmlTitle: rows[i].htmlTitle,
body1: rows[i].body1,
body2: rows[i].body2,
body3: rows[i].body3,
body4: rows[i].body4,
body5: rows[i].body5,
slug: rows[i].slug,
};
}
for (const pageUuid in tmpPages) {
pages.push(tmpPages[pageUuid]);
}
return pages;
};
async getSnippets(options) {
options = options || {};
if (options.onlyNames) {
const { rows } = await this.db.query('SELECT DISTINCT name FROM cms_snippets ORDER BY name;');
return rows;
}
const dbFields = [];
let sql = 'SELECT * FROM cms_snippets\n';
sql += 'WHERE 1 + 1\n';
if (options.names !== undefined) {
if (typeof options.names === 'string') {
options.names = [options.names];
}
if (options.names.length === 0) {
options.names = [''];
}
sql += ' AND name IN (';
for (let i = 0; options.names[i] !== undefined; i++) {
sql += '?,';
dbFields.push(options.names[i]);
}
sql = sql.substring(0, sql.length - 1) + ')\n';
}
sql += 'ORDER BY name, lang';
const { rows } = await this.db.query(sql, dbFields);
const snippets = [];
let snippet;
let prevName;
for (let i = 0; rows[i] !== undefined; i++) {
if (prevName !== rows[i].name) {
if (snippet) {
snippets.push(snippet);
}
snippet = {name: rows[i].name, langs: {}};
}
prevName = rows[i].name;
snippet.langs[rows[i].lang] = rows[i].body;
}
// Add the last one
if (snippet) {
snippets.push(snippet);
}
return snippets;
}
async rmPage(uuid) {
await this.dataWriter.rmPage(uuid);
}
async rmSnippet(name) {
await this.dataWriter.rmSnippet(name);
}
async savePage(data) {
await this.dataWriter.savePage(data);
};
async saveSnippet(data) {
await this.dataWriter.saveSnippet(data);
}
}
module.exports = exports = Cms;