-
Notifications
You must be signed in to change notification settings - Fork 12
/
ltData.js
227 lines (222 loc) · 7.53 KB
/
ltData.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
import angular from 'angular';
import deepmerge from 'deepmerge';
import LatLng from './LatLng';
data.$inject = ['$http', '$httpParamSerializer', '$parse', '$sce', '$q', 'limitToFilter'];
export default function data($http, $httpParamSerializer, $parse, $sce, $q, limitToFilter) {
const maxTitlesPerRequest = 50;
return {
getCoordinates,
getCategoriesForPrefix,
getFileDetails,
getFiles,
getFilesForUser,
getFilesForCategory
};
function getCoordinates(titles) {
if (angular.isString(titles)) {
titles = titles.split('|');
}
if (titles.length > maxTitlesPerRequest) {
return getCoordinatesChunkByChunk(titles);
}
const params = {
prop: 'coordinates',
titles: titles.join('|').replace(/_/g, ' ')
};
return $query(params).then(data => {
const pages = (data && data.query && data.query.pages) || {};
const coordsGetter = $parse('{lat: coordinates[0].lat, lng: coordinates[0].lon}');
return Object.keys(pages).map(pageid => {
const page = pages[pageid];
return {
pageid: parseInt(pageid),
file: page.title,
url: `https://commons.wikimedia.org/wiki/${page.title}`,
imageUrl(width) {
const file = this.file.replace(/^File:/, '');
const url = `https://commons.wikimedia.org/wiki/Special:FilePath/${file}`;
if (width >= 2048) {
return url;
} else if (width >= 1280) {
return `${url}?width=2048`;
} else {
return `${url}?width=1024`;
}
},
coordinates: new LatLng('Location', coordsGetter(page)),
objectLocation: new LatLng('Object location', {})
};
});
});
}
function getCoordinatesChunkByChunk(titles) {
const t = Object.assign([], titles);
const requests = [];
while (t.length) {
requests.push(t.splice(0, Math.min(maxTitlesPerRequest, t.length)));
}
const coordinatesPromises = requests.map(getCoordinates);
return $q.all(coordinatesPromises).then(flatten);
function flatten(array) {
return [].concat(...array);
}
}
function getFileDetails(pageid) {
const params = {
prop: 'categories|imageinfo|revisions',
clshow: '!hidden',
pageids: pageid,
iiprop: 'url|extmetadata',
iiextmetadatafilter: 'ImageDescription',
rvprop: 'content'
};
return $query(params).then(data => {
const page = (data && data.query && data.query.pages && data.query.pages[pageid]) || {};
const categories = ((page && page.categories) || []).map(category =>
category.title.replace(/^Category:/, '')
);
const descriptionGetter = $parse('imageinfo[0].extmetadata.ImageDescription.value');
const urlGetter = $parse('imageinfo[0].descriptionurl');
return {
categories,
description: $sce.trustAsHtml(descriptionGetter(page)),
url: urlGetter(page),
objectLocation: extractObjectLocation(page)
};
});
function extractObjectLocation(page) {
try {
const wikitext = page.revisions[0]['*'];
const locDeg = wikitext.match(
/\{\{Object location( dec)?\|([0-9]+)\|([0-9]+)\|([0-9.]+)\|([NS])\|([0-9]+)\|([0-9]+)\|([0-9.]+)\|([WE])/i
);
const loc = wikitext.match(/\{\{Object location( dec)?\s*\|\s*([0-9.]+)\s*\|\s*([0-9.]+)/i);
let lat;
let lng;
if (locDeg) {
lat = parseInt(locDeg[2]) + parseInt(locDeg[3]) / 60 + parseFloat(locDeg[4]) / 3600;
lat *= locDeg[5] === 'N' ? 1 : -1;
lng = parseInt(locDeg[6]) + parseInt(locDeg[7]) / 60 + parseFloat(locDeg[8]) / 3600;
lng *= locDeg[9] === 'E' ? 1 : -1;
} else if (loc) {
lat = parseFloat(loc[2]);
lng = parseFloat(loc[3]);
}
return new LatLng('Object location', {lat, lng});
} catch (e) {
return new LatLng('Object location', {});
}
}
}
function getCategoriesForPrefix(prefix) {
const params = {
list: 'allpages',
apnamespace: 14,
aplimit: 30,
apfrom: prefix,
apprefix: prefix
};
return $query(params).then(data =>
data.query.allpages.map(i => i.title.replace(/^Category:/, ''))
);
}
function getFiles({files, user, userLimit, userStart, userEnd, category, categoryDepth}) {
return $q((resolve, reject) => {
if (files) {
resolve(files);
} else if (user) {
getFilesForUser(user, userLimit, userStart, userEnd).then(resolve);
} else if (category) {
getFilesForCategory(category, categoryDepth).then(resolve);
} else {
reject();
}
});
}
function getFilesForUser(user, userLimit, userStart, userEnd) {
const params = {
list: 'usercontribs',
ucuser: user,
ucnamespace: 6,
ucshow: 'new',
uclimit: userLimit || 'max',
ucstart: userEnd, // sic! (due to ucdir)
ucend: userStart, // sic! (due to ucdir)
ucdir: 'older',
ucprop: 'title'
};
const shouldContinue = data =>
data.continue && (!userLimit || data.query.usercontribs.length < userLimit);
return $query(params, {}, shouldContinue)
.then(data => data.query.usercontribs.map(i => i.title))
.then(usercontribs => (userLimit ? limitToFilter(usercontribs, userLimit) : usercontribs));
}
function getFilesForCategory(cat, depth = 3) {
cat = cat.replace(/^Category:/, '');
return successRace([getFilesForCategory1(cat, depth), getFilesForCategory2(cat, depth)]);
}
function getFilesForCategory1(cat, depth) {
const params = {
lang: 'commons',
cat: cat.replace(/^Category:/, ''),
type: 6, // File:
depth: depth,
json: 1
};
return $http
.get('https://tools.wmflabs.org/cats-php/', {params})
.then(d => d.data.map(f => `File:${f}`));
}
function getFilesForCategory2(cat, depth) {
const params = {
action: 'query',
lang: 'commons',
query: cat,
querydepth: depth,
flaws: 'ALL',
format: 'json'
};
return $http.get('/render/tlgbe/tlgwsgi.py', {params, transformResponse}).then(d => {
const exceptions = d.data.filter(x => !!x.exception).map(x => x.exception);
if (exceptions.length) {
throw new Error(exceptions[0]);
}
return d.data.filter(x => !!x.page).map(x => x.page.page_title);
});
function transformResponse(value) {
// tlgwsgi returns one JSON object per line w/o commas in between
const array = `[${value.replace(/\n/g, ',').replace(/,$/, '')}]`;
return JSON.parse(array);
}
}
function $query(query, previousResults = {}, shouldContinue = data => !!data.continue) {
const data = $httpParamSerializer(query);
const params = {
action: 'query',
format: 'json',
origin: '*'
};
return $http
.post('https://commons.wikimedia.org/w/api.php', data, {params})
.then(d => d.data)
.then(data => deepmerge(previousResults, data, {arrayMerge: (x, y) => [].concat(...x, ...y)}))
.then(
data =>
shouldContinue(data)
? $query(
Object.assign(query, {continue: undefined}, data.continue),
Object.assign(data, {continue: undefined}),
shouldContinue
)
: data
);
}
function successRace(promises) {
return $q((resolve, reject) => {
// resolve first successful one
promises.forEach(promise => promise.then(resolve));
// reject when all fail
$q.all(promises).catch(reject);
});
}
}