-
Notifications
You must be signed in to change notification settings - Fork 8
/
Copy pathwikibase.api.js
169 lines (147 loc) · 4.01 KB
/
wikibase.api.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
'use strict';
const MWBot = require( 'mwbot' );
const request = require( 'request' );
class WikibaseApi {
/**
* Initialize the API
*
* @param {string} [cpPosIndex] The value of the cpPosIndex browser cookie.
* Optional, but strongly recommended to have chronology protection.
* @param {string} [mwUser] Override mwUser argument.
* Implemented due to the removal of mwUser from browser.options,
* the replacement for browser.config.
* @param {string} [mwPwd] Override mwPwd argument.
* Implemented due to the removal of mwPwd from browser.options,
* the replacement for browser.config.
* @return {Promise<MWBot>} resolving with MWBot
*/
async initialize( cpPosIndex, mwUser, mwPwd ) {
const config = Object.assign( browser.config || {}, browser.options || {} );
const jar = request.jar();
if ( cpPosIndex ) {
const cookie = request.cookie( `cpPosIndex=${cpPosIndex}` );
jar.setCookie( cookie, config.baseUrl );
}
const bot = new MWBot(
{
apiUrl: `${config.baseUrl}/api.php`
},
{
jar: jar
}
);
await bot.loginGetEditToken( {
username: mwUser || config.mwUser,
password: mwPwd || config.mwPwd
} );
this.bot = bot;
return bot;
}
/**
* @return {Promise<MWBot>} resolving with MWBot
*/
getBot() {
if ( !this.bot ) {
console.trace( 'WARNING: WikibaseApi not initialized' );
return this.initialize();
}
return Promise.resolve( this.bot );
}
/**
* Create an item
*
* @param {string|Object} [label] Optional English label of the item or object containing all labels
* @param {Object} [data] Optional data to populate the item with
* @return {Promise<string>} resolving with the id of the created item
*/
async createItem( label, data ) {
const itemData = {};
let labels = {};
if ( typeof label === 'object' ) {
labels = label;
} else if ( label ) {
labels = {
en: {
language: 'en',
value: label
}
};
}
Object.assign( itemData, { labels }, data );
const bot = await this.getBot();
const response = await bot.request( {
action: 'wbeditentity',
new: 'item',
data: JSON.stringify( itemData ),
token: bot.editToken
} );
return response.entity.id;
}
/**
* Create a property
*
* @param {string} datatype The datatype of the property
* @param {Object} [data] Optional data to populate the property with
* @return {Promise<string>} resolving with the id of the created property
*/
async createProperty( datatype, data ) {
let propertyData = {};
propertyData = Object.assign( {}, { datatype }, data );
const bot = await this.getBot();
const response = await bot.request( {
action: 'wbeditentity',
new: 'property',
data: JSON.stringify( propertyData ),
token: bot.editToken
} );
return response.entity.id;
}
/**
* @param {string} id The id of the entity
* @return {Promise<Object>} resolving with the requested entity
*/
async getEntity( id ) {
const bot = await this.getBot();
const response = await bot.request( {
ids: id,
action: 'wbgetentities',
token: bot.editToken
} );
return response.entities[ id ];
}
/**
* @param {string} entityId The id of the entity
* @return {Promise<Object>}
*/
async protectEntity( entityId ) {
const bot = await this.getBot();
const getEntitiesResponse = await bot.request( {
action: 'wbgetentities',
format: 'json',
ids: entityId,
props: 'info'
} );
const entityTitle = getEntitiesResponse.entities[ entityId ].title;
return bot.request( {
action: 'protect',
title: entityTitle,
protections: 'edit=sysop',
token: bot.editToken
} );
}
/**
* @param {string} datatype
* @return {Promise<string>} resolving with the id of the property
*/
async getProperty( datatype ) {
const envName = `WIKIBASE_PROPERTY_${datatype.toUpperCase()}`;
if ( envName in process.env ) {
return process.env[ envName ];
} else {
const propertyId = await this.createProperty( datatype );
process.env[ envName ] = propertyId;
return propertyId;
}
}
}
module.exports = new WikibaseApi();