-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathcategoryScan.mjs
More file actions
118 lines (113 loc) · 2.93 KB
/
categoryScan.mjs
File metadata and controls
118 lines (113 loc) · 2.93 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
import axios from "axios";
import * as util from "./util.mjs";
import * as c from "./config.mjs";
function getPages(con, allPages, toDo, depth, namespace) {
return new Promise((resolve, reject) => {
const categoriesString = toDo.join('","');
let promise = con
.promise()
.query(
`SELECT DISTINCT page_title, page_namespace FROM page, categorylinks, linktarget WHERE
page_id=cl_from AND lt_id=cl_target_id AND lt_namespace=14 AND lt_title IN ("${categoriesString}")`
);
promise
.then(([results, fields]) => {
toDo = [];
for (let result of results) {
if (result.page_namespace == namespace) {
allPages.push(result.page_title.toString("utf8").replace(/_/g, " "));
}
if (result.page_namespace == 14) {
toDo.push(result.page_title.toString("utf8"));
}
}
if (toDo.length > 0 && depth != 0) {
getPages(con, allPages, toDo, depth - 1, namespace)
.then(pages => {
resolve(pages);
})
.catch(err => {
console.log(err);
reject(err);
});
} else {
resolve(allPages);
}
})
.catch(err => {
console.log(err);
reject(err);
});
});
}
function categoryScan(req, res) {
let data = req.query;
if (!("category" in data)) {
res.status(500).json({
error: {
code: "param-missing",
info: 'The required parameter "category" was missing.',
},
});
}
if (!("servername" in data)) {
res.status(500).json({
error: {
code: "param-missing",
info: 'The required parameter "servername" was missing.',
},
});
}
const category = data.category.replace(/ /g, "_");
const servername = data.servername;
let depth;
if ("depth" in data) {
depth = parseInt(data.depth);
} else {
depth = 0;
}
let namespace;
if ("namespace" in data) {
namespace = parseInt(data.namespace);
} else {
namespace = 0;
}
let qs = {
action: "query",
meta: "siteinfo",
siprop: "general",
format: "json",
origin: "*",
};
axios
.get(`https://${servername}/w/api.php`, { params: qs, headers: c.headers })
.then(response => {
const site = response.data.query.general.wikiid;
const con = util.createSQLconnectionReplica(site);
getPages(con, [], [category], depth, namespace)
.then(pages => {
res.json([...new Set(pages)]);
})
.catch(err => {
console.log(err);
res.status(500).json({
error: {
code: "unknown",
info: err,
},
});
})
.finally(() => {
con.destroy();
});
})
.catch(err => {
res.status(500).json({
error: {
code: "unknown",
info: err,
},
});
});
}
export { categoryScan };