/
ApiRequest.js
199 lines (178 loc) · 5.52 KB
/
ApiRequest.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
'use strict';
const apiUtil = require( '../api-util.js' ),
preq = require( 'preq' ),
cxUtil = require( '../util.js' ),
Title = require( 'mediawiki-title' ).Title,
cxutil = require( '../util.js' ),
languageDomainNameMapping = require( './../language-domain-mapping.json' ),
HTTPError = cxUtil.HTTPError;
class MWApiRequest {
/**
* @param {Object} config Configuration options
* @cfg {Object} context Application context
* @cfg {string} [sourceLanguage] Source language
* @cfg {string} [targetLanguage] target language
*/
constructor( config ) {
this.context = config.context;
// Source and target languages
this.sourceLanguage = config.sourceLanguage;
this.targetLanguage = config.targetLanguage;
apiUtil.setupApiTemplates( config.context );
}
/**
* Calls the MW API with the supplied query as its body
*
* @param {string} domain The domain to issue the request to
* @param {Object} query An object with all the query parameters for the MW API
* @param {string} method The HTTP method to use - get or post
* @return {Promise} A promise resolving as the response object from the MW API
*/
mwRequest( domain, query, method ) {
var request;
query = query || {};
query.continue = query.continue || '';
query.format = 'json';
request = this.context.mwapi_tpl.expand( {
request: {
params: {
domain: domain,
origin: '*'
},
headers: {
'user-agent': this.context.conf.user_agent
}
}
} );
if ( method === 'get' ) {
request.query = query;
} else if ( method === 'post' ) {
request.body = query;
request.headers[ 'content-type' ] = 'application/x-www-form-urlencoded';
}
return preq[ method ]( request ).then( ( response ) => {
if ( response.status < 200 || response.status > 399 ) {
// there was an error when calling the upstream service, propagate that
throw new HTTPError( {
status: response.status,
type: 'api_error',
title: 'MW API error',
detail: response.body
} );
}
return response.body;
} );
}
/**
* Calls the MW API with the supplied query as its body
*
* @param {string} domain The domain to issue the request to
* @param {Object} query An object with all the query parameters for the MW API
* @return {Promise} A promise resolving as the response object from the MW API
*/
mwPost( domain, query ) {
return this.mwRequest( domain, query, 'post' );
}
/**
* Calls the MW API with the supplied query as URL params
*
* @param {string} domain The domain to issue the request to
* @param {Object} query An object with all the query parameters for the MW API
* @return {Promise} A promise resolving as the response object from the MW API
*/
mwGet( domain, query ) {
return this.mwRequest( domain, query, 'get' );
}
getDomain( language ) {
return this.getSiteCode( language ) + '.wikipedia.org';
}
/**
* Resolve non-standard wikimedia site codes
* @param {string} language Language code
* @return {string} Wikipedia site code corresponding to the language code.
*/
getSiteCode( language ) {
return languageDomainNameMapping[ language ] || language;
}
/**
* Fetch the site information for a given language
* @param {string} language
* @return {Promise}
*/
getSiteInfo( language ) {
var query,
domain = this.getDomain( language );
if ( MWApiRequest.siteInfoCache[ domain ] ) {
return MWApiRequest.siteInfoCache[ domain ];
}
query = {
action: 'query',
meta: 'siteinfo',
siprop: 'general|namespaces|namespacealiases|specialpagealiases',
format: 'json',
formatversion: 2
};
MWApiRequest.siteInfoCache[ domain ] = this.mwGet( domain, query )
.then( ( res ) => res.query );
return MWApiRequest.siteInfoCache[ domain ];
}
htmlToWikiText( html, language ) {
var restReq, path, domain = this.getDomain( language );
if ( cxUtil.isPlainText( html ) ) {
// Does not contain HTML elements. Save api call.
return Promise.resolve( html );
}
restReq = {
method: 'post',
body: { html },
headers: {
// See https://www.mediawiki.org/wiki/Specs/HTML/1.5.0
accept: 'text/html; charset=utf-8; profile="https://www.mediawiki.org/wiki/Specs/HTML/1.5.0"'
}
};
path = '/transform/html/to/wikitext';
return apiUtil.restApiGet( this.context, domain, path, restReq )
.then( ( response ) => response.body );
}
wikitextToHTML( wikitext, language ) {
var restReq, path, domain = this.getDomain( language );
if ( !wikitext || !wikitext.trim() ) {
// Save api call.
return Promise.resolve( wikitext );
}
restReq = {
method: 'post',
body: {
wikitext,
body_only: true, // eslint-disable-line
},
headers: {
// See https://www.mediawiki.org/wiki/Specs/HTML/1.5.0
accept: 'text/html; charset=utf-8; profile="https://www.mediawiki.org/wiki/Specs/HTML/1.5.0"'
}
};
path = '/transform/wikitext/to/html';
return apiUtil.restApiGet( this.context, domain, path, restReq )
.then( ( response ) => response.body );
}
}
/**
* Normalize the title of the response
*
* @param {string} title Title
* @param {string} language language
* @return {Promise} Promise resolved with the normalized title
*/
MWApiRequest.prototype.normalizeTitle = cxutil.async( function* ( title, language ) {
var titleObj, siteInfo;
siteInfo = yield this.getSiteInfo( language );
// Remove prefixes like './'
title = title.replace( /^\.*\//, '' );
titleObj = Title.newFromText( title, siteInfo );
if ( !titleObj ) {
return title;
}
return titleObj.getPrefixedDBKey();
} );
MWApiRequest.siteInfoCache = new Map();
module.exports = MWApiRequest;