1 const MWBot
= require( 'mwbot' );
3 // TODO: Once we require Node 7 or later, we can use async-await.
7 * Get a logged-in instance of `MWBot` with edit token already set up.
8 * Default username, password and base URL is used unless specified.
11 * @param {string} username - Optional
12 * @param {string} password - Optional
13 * @param {string} baseUrl - Optional
14 * @return {Promise<MWBot>}
17 username
= browser
.options
.username
,
18 password
= browser
.options
.password
,
19 baseUrl
= browser
.options
.baseUrl
21 const bot
= new MWBot();
23 return bot
.loginGetEditToken( {
24 apiUrl
: `${baseUrl}/api.php`,
27 } ).then( function () {
33 * Shortcut for `MWBot#edit( .. )`.
34 * Default username, password and base URL is used unless specified
37 * @see <https://www.mediawiki.org/wiki/API:Edit>
38 * @param {string} title
39 * @param {string} content
40 * @param {string} username - Optional
41 * @param {string} password - Optional
42 * @param {baseUrl} baseUrl - Optional
43 * @return {Object} Promise for API action=edit response data.
47 username
= browser
.options
.username
,
48 password
= browser
.options
.password
,
49 baseUrl
= browser
.options
.baseUrl
51 return this.bot( username
, password
, baseUrl
)
52 .then( function ( bot
) {
53 return bot
.edit( title
, content
, `Created or updated page with "${content}"` );
58 * Shortcut for `MWBot#delete( .. )`.
61 * @see <https://www.mediawiki.org/wiki/API:Delete>
62 * @param {string} title
63 * @param {string} reason
64 * @return {Object} Promise for API action=delete response data.
66 delete( title
, reason
) {
68 .then( function ( bot
) {
69 return bot
.delete( title
, reason
);
74 * Shortcut for `MWBot#request( { acount: 'createaccount', .. } )`.
77 * @see <https://www.mediawiki.org/wiki/API:Account_creation>
78 * @param {string} username
79 * @param {string} password
80 * @return {Object} Promise for API action=createaccount response data.
82 createAccount( username
, password
) {
83 const bot
= new MWBot();
86 return bot
.loginGetCreateaccountToken( {
87 apiUrl
: `${browser.options.baseUrl}/api.php`,
88 username
: browser
.options
.username
,
89 password
: browser
.options
.password
90 } ).then( function () {
91 // Create the new account
93 action
: 'createaccount',
94 createreturnurl
: browser
.options
.baseUrl
,
95 createtoken
: bot
.createaccountToken
,
104 * Shortcut for `MWBot#request( { action: 'block', .. } )`.
107 * @see <https://www.mediawiki.org/wiki/API:Block>
108 * @param {string} [username] defaults to user making the request
109 * @param {string} [expiry] default is not set. For format see API docs
110 * @return {Object} Promise for API action=block response data.
112 blockUser( username
, expiry
) {
114 .then( function ( bot
) {
115 // block user. default = admin
116 return bot
.request( {
118 user
: username
|| browser
.options
.username
,
119 reason
: 'browser test',
120 token
: bot
.editToken
,
127 * Shortcut for `MWBot#request( { action: 'unblock', .. } )`.
130 * @see <https://www.mediawiki.org/wiki/API:Block>
131 * @param {string} [username] defaults to user making the request
132 * @return {Object} Promise for API action=unblock response data.
134 unblockUser( username
) {
136 .then( function ( bot
) {
137 // unblock user. default = admin
138 return bot
.request( {
140 user
: username
|| browser
.options
.username
,
141 reason
: 'browser test done',