wikiploy 1.8.0 → 1.8.1

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -1,10 +1,8 @@
1
1
  # Building your project
2
2
 
3
- There are various ways to build scripts. You can use browserify, webpack, TypeScript compiler etc... or even don't build at all. This is all fine, but not all solution are equally well suite for the wiki world and choosing the right solution takes time.
3
+ There are various ways to build scripts. You can use [browserify](https://browserify.org/), webpack, TypeScript compiler, etc., or even choose not to build at all. Wikiploy doesn't force the use of any specific tool (you are free to choose whatever you are comfortable with). However, not all solutions are equally well-suited for the wiki world, and choosing the right solution takes time.
4
4
 
5
- So here is a recommended project setup to save you some time :)
6
-
7
- Some of this is based on a real script. So if you want you can take a look at [wikibot-jsbot-core](https://github.com/Eccenux/wikibot-jsbot-core). Also you might want to take a look at a template repository for Wikiploy: [wikiploy-rollout-example](https://github.com/Eccenux/wikiploy-rollout-example).
5
+ Let me save you some time. Below is a recommended project setup based on some of my real scripts and years of experience building gadgets. If you're interested, you can take a look at a real-world script: [wikibot-jsbot-core](https://github.com/Eccenux/wikibot-jsbot-core) (it's a bit experimental, but you might find some interesting constructs there). For a more stable example, you might want to explore the template repository for Wikiploy: [wikiploy-rollout-example](https://github.com/Eccenux/wikiploy-rollout-example).
8
6
 
9
7
  ## Recommended setup for building scripts
10
8
 
@@ -13,18 +11,20 @@ Example `.gitignore` for your project:
13
11
  /node_modules
14
12
  *.lnk
15
13
  *.priv.*
16
- bot.config.js
14
+ bot.config.*
17
15
  ```
18
16
 
19
17
  Your `package.json` (crucial part being `scripts`):
20
18
  ```js
21
19
  {
22
- "name": "wiki-gadget-yourname",
20
+ "name": "wiki-gadget-yourgadetname",
23
21
  "type": "commonjs",
24
22
  "scripts": {
25
23
  "test": "mocha",
26
- "build": "browserify src/main.js -o yourname.js",
24
+ "build": "browserify src/main.js -o dist/yourGadetName.js",
25
+ "deploy-dev": "node wikiploy-dev.mjs",
27
26
  "deploy": "node wikiploy.mjs",
27
+ "rollout-dev": "npm run build && npm run deploy-dev"
28
28
  "rollout": "npm run build && npm run deploy"
29
29
  },
30
30
  "devDependencies": {
@@ -39,24 +39,49 @@ Your `package.json` (crucial part being `scripts`):
39
39
  }
40
40
  ```
41
41
 
42
- Your `main.js` could look something like this:
42
+ Your `src/main.js` could look something like this:
43
43
  ```js
44
44
  var { MyGadget } = require("./MyGadget");
45
45
 
46
- // bot instance
46
+ // instance
47
47
  var gadget = new MyGadget();
48
48
 
49
+ // init elements
49
50
  $(function(){
50
51
  gadget.init();
51
52
  })
52
53
  ```
53
54
 
54
- ## Visual Studio Code setup
55
+ Your `src/MyGadget.js` could look something like this:
56
+ ```js
57
+ function MyGadget() {
58
+ /** Initialize things when DOM is ready. */
59
+ this.init = function() {
60
+ console.log("init done");
61
+ }
62
+ }
63
+
64
+ module.exports = { MyGadget };
65
+ ```
55
66
 
56
- [VSC](https://code.visualstudio.com/) is a free editor that over years became almost like an IDE.
67
+ Remember to install tools after changing `package.json`:
68
+ ```bash
69
+ npm i
70
+ ```
57
71
 
58
- VSCode helper to install a set of extensions `.vscode/extensions.json`:
72
+ And now you should already be able to build the script:
73
+ ```bash
74
+ npm run build
59
75
  ```
76
+
77
+ Now we just need to setup things so that we don't have to build and deploy things manually.
78
+
79
+ ## Visual Studio Code setup
80
+
81
+ [VSC](https://code.visualstudio.com/) is a free editor that has evolved over the years to become almost like an IDE. It is great for developing JS scripts.
82
+
83
+ Add `.vscode/extensions.json` to help install a set of **extensions**:
84
+ ```js
60
85
  {
61
86
  "recommendations": [
62
87
  "gsppvo.vscode-commandbar",
@@ -66,21 +91,154 @@ VSCode helper to install a set of extensions `.vscode/extensions.json`:
66
91
  }
67
92
  ```
68
93
 
69
- VSC settings `.vscode/settings.json`:
70
- ```
94
+ VSC **settings** `.vscode/settings.json`:
95
+ ```js
71
96
  {
72
97
  "search.exclude": {
73
98
  "package-lock.json": true,
74
99
  },
75
- "editor.detectIndentation": false,
76
- "editor.useTabStops": true,
77
- "editor.insertSpaces": false,
100
+ "editor.detectIndentation": false,
101
+ "editor.useTabStops": true,
102
+ "editor.insertSpaces": false,
78
103
  }
79
104
  ```
80
105
 
81
- ## Basic script and dst
106
+ Using tabs vs spaces is obviously your preference. People tend to have conflicting opinions on that. The simple truth is that a tab is one byte, and 4 spaces are 4 bytes, but use whatever works for your project. Just remember to enforce it at the project level so that the style is consistent.
82
107
 
83
- Your minimal `wikiploy.mjs`:
108
+ VSC **tasks** `.vscode/tasks.json`:
109
+ ```js
110
+ {
111
+ "version": "2.0.0",
112
+ "tasks": [
113
+ {
114
+ "type": "npm",
115
+ "script": "test",
116
+ "group": "test",
117
+ "problemMatcher": [],
118
+ "label": "Run tests",
119
+ "detail": "mocha"
120
+ },
121
+ {
122
+ "type": "npm",
123
+ "script": "deploy-dev",
124
+ "group": "none",
125
+ "problemMatcher": [],
126
+ "label": "Run dev deploy",
127
+ "detail": "Run deploy script (wikiploy)"
128
+ },
129
+ {
130
+ "type": "npm",
131
+ "script": "deploy",
132
+ "group": "none",
133
+ "problemMatcher": [],
134
+ "label": "Run deploy",
135
+ "detail": "Run deploy script (wikiploy)"
136
+ },
137
+ {
138
+ "type": "npm",
139
+ "script": "rollout-dev",
140
+ "group": "none",
141
+ "problemMatcher": [],
142
+ "label": "Run dev rollout",
143
+ "detail": "Run rollout script (browserify & wikiploy)"
144
+ },
145
+ {
146
+ "type": "npm",
147
+ "script": "rollout",
148
+ "group": "none",
149
+ "problemMatcher": [],
150
+ "label": "Run rollout",
151
+ "detail": "Run rollout script (browserify & wikiploy)"
152
+ },
153
+ {
154
+ "type": "npm",
155
+ "script": "install",
156
+ "group": "none",
157
+ "problemMatcher": [],
158
+ "label": "npm install",
159
+ "detail": "install dependencies from package"
160
+ }
161
+ ]
162
+ }
163
+ ```
164
+
165
+ When you use tasks, VSC runs an interactive terminal. This makes some scripts work better:
166
+ - Any scripts that use colors in the terminal will work with tasks.
167
+ - An interactive shell is required for the `userPrompt` helper (see *Deploy script*).
168
+ - Text progress (like in npm install) looks much better with tasks.
169
+
170
+ VCS **commandbar** `.vscode/commandbar.json`:
171
+ ```js
172
+ // Remember to change VSC settings (UI): in `files.associations` add `commandbar.json: jsonc`
173
+ {
174
+ "skipTerminateQuickPick": true,
175
+ "skipSwitchToOutput": false,
176
+ "skipErrorMessage": true,
177
+ "commands": [
178
+ // "commandType": "script" runs npm script directly
179
+ {
180
+ "text": "test",
181
+ "tooltip": "Run mocha tests.",
182
+ "color": "lightgreen",
183
+ "commandType": "script",
184
+ "command": "test",
185
+ "priority": 0
186
+ },
187
+
188
+ {
189
+ "text": "build",
190
+ "tooltip": "Run the browserify build.",
191
+ "color": "lightgreen",
192
+ "commandType": "script",
193
+ "command": "build",
194
+ "priority": 0
195
+ },
196
+
197
+ // running deployment as a task (so that prompts work)
198
+ // note that "Run dev deploy" is a label from tasks.json
199
+ {
200
+ "text": "wikiploy-dev",
201
+ "tooltip": "Deploy pre-built script.",
202
+ "color": "lightgreen",
203
+ "commandType": "palette",
204
+ "command": "workbench.action.tasks.runTask|Run dev deploy",
205
+ "priority": 0
206
+ },
207
+
208
+ // this our one-click rollout
209
+ {
210
+ "text": "build & deploy-dev",
211
+ "tooltip": "Build (browserify) and deploy (wikiploy).",
212
+ "color": "lightgreen",
213
+ "commandType": "palette",
214
+ "command": "workbench.action.tasks.runTask|Run dev rollout",
215
+ "priority": 0
216
+ }
217
+ ]
218
+ }
219
+ ```
220
+
221
+ Note: You should also change VSC settings (UI) under `files.associations` to add `commandbar.json: jsonc` (JSON with comments). This allows comments in JSON, making it more understandable. Otherwise, VSC will complain about inline comments.
222
+
223
+ Also, note: Above, we only added dev tasks to the command bar. This is just for brevity. The same commands can be added for other tasks.
224
+
225
+ ## Deploy script
226
+
227
+ Your **bot password** configuration `bot.config.mjs`:
228
+ ```js
229
+ /**
230
+ Bot with edit&create rights.
231
+
232
+ https://test.wikipedia.org/wiki/Special:BotPasswords/Wikiploy
233
+
234
+ The new password to log in with Nux@Wikiploy.
235
+ Where "Nux" is a Wikipedia username and "Wikiploy" is a bot name choosen in [[Special:BotPasswords]].
236
+ */
237
+ export const username = 'Nux@Wikiploy';
238
+ export const password = '0abc...fckgw';
239
+ ```
240
+
241
+ Your minimal **developer deployment script** `wikiploy-dev.mjs`:
84
242
  ```js
85
243
  import {DeployConfig, WikiployLite, userPrompt} from 'wikiploy';
86
244
 
@@ -92,8 +250,7 @@ ployBot.site = "en.wikipedia.org";
92
250
 
93
251
  // run asynchronously to be able to wait for results
94
252
  (async () => {
95
- // Note! The `userPrompt` requires an interactive terminal.
96
- // In VSC you can use tasks to get an interactive terminal and still run wikiploy from your commandbar.
253
+ // custom summary from a prompt
97
254
  const summary = await userPrompt('Summary of changes (empty for default summary):');
98
255
  if (typeof summary === 'string' && summary.length) {
99
256
  ployBot.summary = () => {
@@ -104,11 +261,17 @@ ployBot.site = "en.wikipedia.org";
104
261
  // push out file(s) to wiki
105
262
  const configs = [];
106
263
  configs.push(new DeployConfig({
107
- src: 'test.js',
108
- dst: '~/test-wikiploy--test.js',
264
+ src: 'dist/yourGadetName.js',
265
+ dst: '~/yourGadetName.js',
109
266
  nowiki: true,
110
267
  }));
268
+ configs.push(new DeployConfig({
269
+ src: 'dist/yourGadetName.css',
270
+ dst: '~/yourGadetName.css',
271
+ }));
272
+
111
273
  await ployBot.deploy(configs);
274
+
112
275
  })().catch(err => {
113
276
  console.error(err);
114
277
  process.exit(1);
@@ -116,22 +279,221 @@ ployBot.site = "en.wikipedia.org";
116
279
  ```
117
280
 
118
281
  Note that `~` will be `User:SomeName` so the user space of a currently logged in user (you user space).
119
- You can omit `dst` and it will default to `dst: '~/${src}'`.
282
+ You can omit `dst` and it will default to `dst: '~/${src}'`. More about this basic code and `dst` in the [Wikiploy rollout example](https://github.com/Eccenux/wikiploy-rollout-example/).
283
+
284
+ Config for your **main deployment script** `wikiploy.mjs`:
285
+ ```js
286
+ // [...] (same sa -dev)
287
+
288
+ // push out file(s) to wiki
289
+ // dev version
290
+ const configs = [];
291
+ configs.push(new DeployConfig({
292
+ src: 'dist/yourGadetName.js',
293
+ dst: '~/yourGadetName.js',
294
+ nowiki: true,
295
+ }));
296
+ configs.push(new DeployConfig({
297
+ src: 'dist/yourGadetName.css',
298
+ dst: '~/yourGadetName.css',
299
+ }));
300
+ // gadget
301
+ configs.push(new DeployConfig({
302
+ src: 'dist/yourGadetName.js',
303
+ dst: 'MediaWiki:Gadget-yourGadetName.js',
304
+ nowiki: true,
305
+ }));
306
+ configs.push(new DeployConfig({
307
+ src: 'dist/yourGadetName.css',
308
+ dst: 'MediaWiki:Gadget-yourGadetName.css',
309
+ }));
310
+
311
+ await ployBot.deploy(configs);
312
+
313
+ })().catch(err => {
314
+ console.error(err);
315
+ process.exit(1);
316
+ });
317
+ ```
318
+
319
+ Note: Above is only the config part of the script.
120
320
 
121
- More about this basic code and `dst` in the [Wikiploy rollout example](https://github.com/Eccenux/wikiploy-rollout-example/).
321
+ We update both dev and release version above. This will make an empty edit in a dev script if you already deployed it.
322
+
323
+ Though we only deploy to `site = "en.wikipedia.org"` you could use other configs to deploy to many wikis:
324
+ ```js
325
+ function addConfig(configs, site) {
326
+ configs.push(new DeployConfig({
327
+ src: 'dist/yourGadetName.js',
328
+ dst: 'MediaWiki:Gadget-yourGadetName.js',
329
+ site,
330
+ nowiki: true,
331
+ }));
332
+ configs.push(new DeployConfig({
333
+ src: 'dist/yourGadetName.css',
334
+ dst: 'MediaWiki:Gadget-yourGadetName.css',
335
+ site,
336
+ }));
337
+ }
338
+ addConfig(configs, 'pl.wikipedia.org');
339
+ addConfig(configs, 'pl.wikisource.org');
340
+ ```
341
+ The script doesn't have any limits. You do need interface-admin rights to deploy to `MediaWiki:Gadget-` namespace. So you might need to use `~/` instead to deploy to your user-space.
122
342
 
123
343
  ## Exporting stuff
124
344
 
125
- Your `main.js` could look something like this:
345
+ Note that *browserify* will wrap your code with a function automatically, so you don't have to worry about leaking variables to the global scope (you should still remember to define variables with `var`, `let`, or `const`).
346
+
347
+ If you want to ***intentionally* expose** a variable, there are two ways to do that:
348
+
349
+ 1. Use the `window` object (only if you have to).
350
+ 2. Use *userjs* hooks (`mw.hook`).
351
+
352
+ Exposing things in `main.js` could look something like this:
126
353
  ```js
127
354
  var { MyGadget } = require("./MyGadget");
128
355
 
129
356
  // bot instance
130
357
  var gadget = new MyGadget();
131
358
 
359
+ // expose for external usage (*not* recommended)
360
+ window.yourGadetName = gadget;
361
+
362
+ // hook when object is ready
363
+ mw.hook('userjs.yourGadetName.loaded').fire(gadget);
364
+
132
365
  $(function(){
133
- gadget.init();
134
- })
366
+ // load Mediwiki core dependency
367
+ // (in this case util is for `mw.util.addPortletLink`)
368
+ mw.loader.using(["mediawiki.util"]).then( function() {
369
+ gadget.init();
370
+
371
+ // hook when initial elements are ready
372
+ mw.hook('userjs.yourGadetName.ready').fire(gadget);
373
+ });
374
+ });
375
+ ```
376
+
377
+ As you can see, hooks are better because you can keep the name of the variable short. It's also much easier to make sure each dependency is loaded so other devs can use your hooks in other gadgets, and your users can customize the script.
378
+
379
+ User options:
380
+ ```js
381
+ mw.hook('userjs.yourGadetName.loaded').add(function (gadget) {
382
+ gadget.options.createTool = false;
383
+ });
384
+ importScript('User:Nux/yourGadetName.js');
385
+ ```
386
+ A more advanced customization:
387
+ ```js
388
+ mw.hook('userjs.yourGadetName.ready').add(function (gadget) {
389
+ var $tool = $('#some-unique-gadet-tool a');
390
+ $tool.text($tool.text() + ' & plugin');
391
+ });
392
+ ```
393
+
394
+ ## Using classes
395
+
396
+ JS has classes for a long time. They don't work in IE and you cannot use them in default gadgets on Wikipedia. If you are building a default gadget you might want to add [babeljs](https://babeljs.io/) as a build step.
397
+ ```bash
398
+ # install
399
+ npm install --save-dev babelify @babel/core
400
+ # build
401
+ browserify script.js -t babelify --outfile bundle.js
402
+ ```
403
+
404
+ If the gadget is intended for advanced users it is safe to assume they won't use IE. So for most scripts you can probably skip babelify. But as of 2023 you will need `requiresES6` option in the gadget definition.
405
+
406
+ ```
407
+ yourClassyGadetName [ResourceLoader | requiresES6 | dependencies = mediawiki.util] | yourGadetName.js | yourGadetName.css
408
+ ```
409
+
410
+
411
+ Example `MyGadget.js` with a link in the toolbar:
412
+ ```js
413
+ class MyGadget {
414
+ constructor() {
415
+ this.options = {
416
+ createTool: true,
417
+ };
418
+ }
419
+
420
+ /** Initialize things when DOM is ready. */
421
+ init() {
422
+ // Example link in the tools sidebar
423
+ if (this.options.createTool) {
424
+ var portletId = mw.config.get('skin') === 'timeless' ? 'p-pagemisc' : 'p-tb';
425
+ var linkLabel = 'My gadget dialog';
426
+ var itemId = 'some-unique-gadget-tool';
427
+ var item = mw.util.addPortletLink(portletId, '#', linkLabel, itemId);
428
+ $(item).on('click', function (evt) {
429
+ evt.preventDefault();
430
+ gadget.openDialog();
431
+ });
432
+ }
433
+ }
434
+
435
+ /** Open some dialog. */
436
+ openDialog() {
437
+ // Open a dialog window here
438
+ }
439
+ }
440
+
441
+ module.exports = { MyGadget };
442
+ ```
443
+
444
+ This MyGadget class will work with the `main.js` example from the section above.
445
+
446
+ ## Appendix: Unit testing
447
+
448
+ Unit testing is important for the stability of tools. Not everything needs to be unit tested, but if you perform any kind of parsing or other complicated tasks, it is a good habit to add automatic tests.
449
+
450
+ There are many testing frameworks, but I mostly use Mocha/Chai. This is mainly because the [Chai assert API](https://www.chaijs.com/api/assert/) is very functional.
451
+
452
+ Below is an example test of `imdb.redir()` function. The test file `test/imdb.test.js`:
453
+ ```js
454
+ /* global require, describe, it */
455
+ const { assert } = require('chai');
456
+ const imdb = require('../src/imdb');
457
+
458
+ describe('imdb', function () {
459
+ describe('imdb redir', function () {
460
+ // test function specific to *imdb redir*
461
+ function test(text, expected) {
462
+ // wrap
463
+ let result = imdb.redir(text);
464
+ if (result !== expected) {
465
+ console.log({text, result, expected});
466
+ }
467
+ assert.equal(result, expected);
468
+ }
469
+ // actual test
470
+ it('should resolve redirs', function () {
471
+ test(`[[IMDb.com]]`, `[[IMDb]]`);
472
+ test(`[[IMDb.com|abc]]`, `[[IMDb|abc]]`);
473
+ });
474
+ });
475
+ });
476
+ ```
477
+ You don't have to define a `test` function, but having a function like that can help test many different cases quickly.
478
+
479
+ Visual Studio Code will let you run or even debug each `it` function separately, thanks to the *Vscode Test Explorer* extension.
480
+
481
+ ## Appendix: Wiki2git
482
+
483
+ So now that we have everthing setup we need code right? If you already have a gadget you might to import it from Wikipedia.
484
+
485
+ ```bash
486
+ # install
487
+ npm i -g wiki-to-git
488
+
489
+ # get metadata and insert commits to `src/NavFrame.js`
490
+ wiki2git-load --site pl.wikipedia.org -p "MediaWiki:Gadget-NavFrame.js"
491
+ wiki2git-commit --site pl.wikipedia.org --repo "gadget-NavFrame" -o "src/NavFrame.js"
492
+
493
+ # get metadata and insert commits to `src/NavFrame.css`
494
+ wiki2git-load --site pl.wikipedia.org -p "MediaWiki:Gadget-NavFrame.css"
495
+ wiki2git-commit --site pl.wikipedia.org --repo "gadget-NavFrame" -o "src/NavFrame.css"
135
496
  ```
136
497
 
137
- Note that browserify will wrap your code with a function automatically. So you don't have to worry about leaking variables to global scope.
498
+ Note that in the example above `gadget-NavFrame` is a repository folder.
499
+ This can be a repo that already exists or something that you've already setup.
package/README.md CHANGED
@@ -1,13 +1,13 @@
1
1
  Wikiploy
2
2
  ==========================
3
3
 
4
- Wikiploy is a one-click solution to deploy JS and CSS to Wikipedia.
4
+ Wikiploy is a one-click solution to deploy JS and CSS to Wikipedia.
5
5
 
6
- After initial setup you can quickly build and deploy your user scripts and gadgets. Thought this was designed to work with Wikipedia you should be able to deploy to any [MediaWiki](https://www.mediawiki.org/) based wiki. You can even deploy to multiple websites with one-click (to whatever you have access to).
6
+ After the initial setup, you can quickly build and deploy your user scripts and gadgets. Though this was designed to work with Wikipedia, you should be able to deploy to any [MediaWiki](https://www.mediawiki.org/)-based wiki. You can even deploy to multiple websites with just one click on your commandbar. Your only limitation is your access level to the wikis.
7
7
 
8
8
  See also:
9
9
 
10
- - [README: building your project](README.building your project.md) recommendation on how to build JS and CSS for your gadgets.
10
+ - [README: building your project](https://github.com/Eccenux/Wikiploy/blob/main/README.building%20your%20project.md) recommendation on how to build JS and CSS for your gadgets (includes unit testing setup).
11
11
  - [Wikipedia:Wikiploy on pl.wiki](https://pl.wikipedia.org/wiki/Wikipedia:Wikiploy) for Polish description.
12
12
  - (more links on the bottom)
13
13
 
@@ -15,7 +15,14 @@ See also:
15
15
 
16
16
  ### nowiki (v1.7)
17
17
 
18
- Note! It is recommended that you use `nowiki: true` for all JS files. The `nowiki` property is a new option in `DeployConfig` since Wikiploy v1.7.
18
+ The `nowiki` property is a new option in `DeployConfig` since Wikiploy v1.7. It is now recommended to use `nowiki: true` for all JS files.
19
+ ```js
20
+ configs.push(new DeployConfig({
21
+ src: 'dist/test.js',
22
+ dst: '~/test.js',
23
+ nowiki: true,
24
+ }));
25
+ ```
19
26
 
20
27
  JavaScript page is still a wiki page... Kind of. It can be added to a category or link to other pages. To avoid this use the nowiki option.
21
28
 
@@ -23,7 +30,9 @@ Don't add this option to CSS though. It won't work correctly.
23
30
 
24
31
  ### userPrompt (v1.8)
25
32
 
26
- Prompt for summary so that you don't forget to change it. This is a bit more tricky to setup, but can still work as a one-click build (see [README: building your project](README.building your project.md)).
33
+ Use the `userPrompt` helper function to prompt for a summary in your Wikiploy script. This is only a helper. You can still set up a static summary, but a prompt helps to ensure you don't forget to change the summary.
34
+
35
+ Note that when using `userPrompt` you have to use an interactive terminal. This might be a bit more tricky to set up but can still function as a one-click build from a commandbar (see [README: building your project](https://github.com/Eccenux/Wikiploy/blob/main/README.building%20your%20project.md)).
27
36
 
28
37
  ## Wikiploy types
29
38
 
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "wikiploy",
3
- "version": "1.8.0",
3
+ "version": "1.8.1",
4
4
  "description": "User scripts and gadgets deployment for MediaWiki (Wikipedia).",
5
5
  "main": "src/index.js",
6
6
  "type": "module",