Press n or j to go to the next uncovered block, b, p or k for the previous block.
| 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 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 | 130x 130x 130x 130x 130x 130x 130x 130x 130x 130x 130x 130x 130x 130x 130x 130x 130x 130x 130x 130x 130x 130x 130x 213x 213x 213x 213x 130x 130x 130x 130x 130x 130x 130x 130x 29x 130x 130x 130x 130x 130x 130x 130x 130x 8x 130x 130x 130x 130x 130x 130x 130x 130x 130x 130x 130x 130x 5x 5x 5x 5x 5x 5x 5x 5x 5x 5x 5x 5x 5x 5x 5x 130x 130x 130x 130x 130x 130x 130x 130x 130x 130x 130x 130x 130x 130x 130x 30x 30x 30x 30x 30x 30x 30x 30x 30x 30x 30x 30x 30x 30x 30x 27x 27x 27x 27x 27x 27x 27x 30x 30x 30x 130x 130x 130x 130x 130x 130x 130x 130x 130x 130x 130x 130x 130x 130x 130x 130x 130x 23x 23x 23x 23x 23x 23x 23x 23x 23x 23x 23x 23x 23x 23x 23x 23x 23x 23x 130x 130x 130x 130x 130x 130x 130x 130x 130x 130x 130x 130x 130x 130x 130x 130x 130x 121x 121x 121x 121x 121x 121x 121x 121x 121x 121x 121x 121x 121x 121x 121x 121x 121x 121x 130x 130x 130x 130x 130x 130x 130x 130x 130x 130x 130x 130x 130x 130x 130x 130x 130x 130x 130x 130x 28x 28x 28x 28x 28x 28x 28x 28x 28x 28x 28x 28x 28x 28x 28x 28x 28x 28x 28x 28x 28x 28x 28x 28x 130x 130x 130x 130x 130x 130x 130x 130x 130x 130x 130x 130x 130x 130x 130x 130x 130x 130x 130x 130x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 130x 130x 130x 130x 130x 130x 130x 130x 130x 130x 130x 130x 130x 130x 130x 14x 14x 14x 14x 14x 14x 14x 14x 14x 14x 14x 14x 14x 14x 14x 13x 12x 12x 1x 1x 1x 14x 14x 14x 130x 130x 130x 130x 130x 130x 130x 130x 130x 130x 130x 130x 130x 130x 130x 14x 14x 14x 14x 14x 14x 14x 14x 14x 14x 14x 14x 14x 14x 14x 14x 14x 130x 130x 130x 130x 130x 130x 130x 130x 130x 130x 130x 130x 130x 130x 5x 5x 5x 5x 5x 5x 5x 5x 5x 5x 5x 5x 5x 5x 5x 1x 1x 5x 5x 5x 5x 5x 4x 4x 4x 4x 4x 4x 4x 4x 4x 5x 5x 5x 130x 130x 130x 130x 130x 130x 130x 130x 130x 130x 130x 130x 3x 3x 3x 3x 3x 3x 3x 3x 3x 3x 3x 3x 3x 3x 3x 3x 130x 130x 130x 130x 130x 130x 130x 130x 130x 130x 130x 130x 130x 130x 130x 10x 10x 10x 10x 10x 10x 10x 10x 10x 10x 10x 10x 10x 1x 1x 10x 10x 10x 10x 10x 9x 9x 10x 10x 10x 130x 130x 130x 130x 130x 130x 130x 130x 130x 130x 130x 130x 130x 130x 130x 19x 19x 19x 19x 19x 19x 19x 19x 19x 19x 19x 19x 19x 19x 19x 19x 130x 130x 130x 130x 130x 130x 130x 130x 130x 130x 130x 130x 130x 130x 11x 11x 11x 11x 11x 11x 11x 11x 11x 11x 11x 11x 11x 11x 11x 9x 11x 11x 11x 130x 130x 130x 130x 130x 130x 130x 130x 130x 130x 130x 130x 5x 5x 5x 5x 5x 5x 5x 5x 5x 5x 5x 5x 5x 5x 5x 5x 130x 130x 130x 130x 130x 130x 130x 130x 130x 130x 130x 130x 130x 130x 130x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 130x 130x 130x 130x 130x 130x 130x 130x 130x 130x 130x 130x 130x 130x 130x 130x 130x 8x 8x 8x 8x 8x 8x 8x 8x 8x 8x 8x 8x 8x 8x 8x 8x 8x 8x 8x 7x 7x 7x 8x 8x 8x 8x 8x 8x 130x 130x 130x 130x 130x 130x 130x 130x 130x 130x 130x 130x 130x 130x 130x 130x 3x 3x 3x 3x 3x 3x 3x 3x 3x 3x 3x 3x 3x 3x 3x 3x 3x 3x 130x 130x 130x 130x 130x 130x 130x 130x 130x 130x 130x 130x 130x 130x 130x 130x 130x 21x 21x 21x 21x 21x 21x 21x 21x 21x 21x 21x 21x 21x 21x 21x 21x 21x 21x 21x 12x 12x 12x 12x 12x 12x 12x 21x 21x 21x 21x 130x 130x | /* eslint-disable camelcase */
/**
* WikiLambda Vue editor: API calls util
*
* All mw.API calls are wrapped in a native Promise to make them easier to test
* and to make them more predictable.
*
* @copyright 2020– Abstract Wikipedia team; see AUTHORS.txt
* @license MIT
*/
'use strict';
const ApiError = require( '../store/classes/ApiError.js' );
const Constants = require( '../Constants.js' );
const { hybridToCanonical } = require( './schemata.js' );
const apiUtils = {
/**
* Creates a new mw.Api object
*
* @return {mw.Api|mw.ForeignApi}
*/
newApi: function () {
const foreignUrl = mw.config.get( 'wgWikifunctionsBaseUrl' );
return foreignUrl ?
new mw.ForeignApi( `${ foreignUrl }/w/api.php`, { anonymous: true } ) :
new mw.Api();
},
/**
* Creates a new mw.ForeignApi object pointing at Wikidata.
*
* @return {mw.ForeignApi}
*/
newWikidataApi: function () {
return new mw.ForeignApi( `${ Constants.WIKIDATA_BASE_URL }/w/api.php`, { anonymous: true } );
},
/**
* Creates a new mw.ForeignApi object pointing at Wikimedia Commons.
*
* @return {mw.ForeignApi}
*/
newCommonsApi: function () {
return new mw.ForeignApi( `${ Constants.COMMONS_BASE_URL }/w/api.php`, { anonymous: true } );
},
/**
* Calls the wikilambdaload_zlanguages internal API
* to map language codes (BCP47 / MediaWiki) to ZLanguage ZIDs.
*
* @param {Object} payload
* @param {string[]} payload.codes Array of language codes
* @param {AbortSignal} payload.signal The AbortSignal to cancel the request
* @return {Promise}
*/
fetchLanguageZids: function ( payload ) {
const api = apiUtils.newApi();
return new Promise( ( resolve, reject ) => {
api.get( {
action: 'query',
list: 'wikilambdaload_zlanguages',
format: 'json',
formatversion: '2',
wikilambdaload_zlanguages_codes: payload.codes
}, {
signal: payload.signal
} )
.then( ( data ) => resolve( data.query.wikilambdaload_zlanguages ) )
.catch( ( ...args ) => reject( ApiError.fromMwApiRejection( ...args ) ) );
} );
},
/**
* Calls the wikilambda_function_call internal API
* https://www.mediawiki.org/wiki/Extension:WikiLambda/API#wikilambda_function_call
*
* Needs error handling.
*
* @param {Object} payload
* @param {Object} payload.functionCall
* @param {string} payload.language
* @param {AbortSignal} payload.signal The AbortSignal to cancel the request
* @return {Promise}
*/
performFunctionCall: function ( payload ) {
const api = apiUtils.newApi();
const canonicalJson = JSON.stringify( hybridToCanonical( payload.functionCall ) );
return new Promise( ( resolve, reject ) => {
api.post( {
action: 'wikilambda_function_call',
format: 'json',
formatversion: '2',
wikilambda_function_call_zobject: canonicalJson,
uselang: payload.language
}, {
// The AbortSignal to cancel the request
signal: payload.signal
} )
.then( ( data ) => {
const maybeNormalResponse = JSON.parse( data.wikilambda_function_call.data );
const response = hybridToCanonical( maybeNormalResponse );
resolve( {
response,
result: response[ Constants.Z_RESPONSEENVELOPE_VALUE ],
metadata: response[ Constants.Z_RESPONSEENVELOPE_METADATA ]
} );
} )
.catch( ( ...args ) => reject( ApiError.fromMwApiRejection( ...args ) ) );
} );
},
/**
* Calls the wikilambda_edit internal API
* https://www.mediawiki.org/wiki/Extension:WikiLambda/API#wikilambda_edit
*
* Needs error handling.
*
* @param {Object} payload
* @param {Object} payload.zobject The canonical ZObject to update
* @param {string} payload.zid The zid of the object to update or undefined if new object
* @param {string} payload.summary The update summary
* @param {string} payload.language The user language code
* @param {AbortSignal} payload.signal The AbortSignal to cancel the request
* @return {Promise}
*/
saveZObject: function ( payload ) {
// Should never work with foreign API
const api = new mw.Api();
return new Promise( ( resolve, reject ) => {
api.postWithEditToken( {
action: 'wikilambda_edit',
format: 'json',
formatversion: '2',
summary: payload.summary || '',
zid: payload.zid,
zobject: JSON.stringify( payload.zobject ),
uselang: payload.language
}, {
signal: payload.signal
} )
.then( ( data ) => resolve( data.wikilambda_edit ) )
.catch( ( ...args ) => reject( ApiError.fromMwApiRejection( ...args ) ) );
} );
},
/**
* Calls the wikilambdaload_zobjects internal API
* https://www.mediawiki.org/wiki/Extension:WikiLambda/API#wikilambdaload_zobjects
*
* Doesn't need error handling.
*
* @param {Object} payload
* @param {string} payload.zids The zids to request, separated by pipes. E.g. 'Z1|Z2'
* @param {string|undefined} payload.revisions The revisions to request, separated by pipes. E.g. '100|101'
* @param {string|undefined} payload.language The preferred language code or none
* @param {boolean|undefined} payload.dependencies Whether to fetch their dependencies too
* @param {AbortSignal} payload.signal The AbortSignal to cancel the request
* @return {Promise}
*/
fetchZObjects: function ( payload ) {
const api = apiUtils.newApi();
return new Promise( ( resolve, reject ) => {
api.get( {
action: 'query',
list: 'wikilambdaload_zobjects',
format: 'json',
formatversion: '2',
wikilambdaload_zids: payload.zids,
wikilambdaload_revisions: payload.revisions,
wikilambdaload_language: payload.language,
wikilambdaload_get_dependencies: payload.dependencies ? 'true' : 'false'
}, {
signal: payload.signal
} )
.then( ( data ) => resolve( data.query.wikilambdaload_zobjects ) )
.catch( ( ...args ) => reject( ApiError.fromMwApiRejection( ...args ) ) );
} );
},
/**
* Calls the wikilambdasearch_labels internal API
* https://www.mediawiki.org/wiki/Extension:WikiLambda/API#wikilambdasearch_labels
*
* Doesn't need error handling.
*
* @param {Object} payload
* @param {string} payload.input Substring to search by
* @param {Array|undefined} payload.types Types of objects to retrieve, separated by pipes
* @param {Array|undefined} payload.returnTypes Return types to retrieve, separated by pipes
* @param {string} payload.language The user language code
* @param {number} payload.searchContinue When more results are available, use this to continue
* @param {boolean} payload.exact Search by exact match
* @param {number} payload.limit The maximum number of results to return
* @param {AbortSignal} payload.signal The AbortSignal to cancel the request
* @return {Promise<Object>|undefined} Promise resolving to an object with 'labels' and 'continue'
*/
searchLabels: function ( payload ) {
const api = apiUtils.newApi();
return new Promise( ( resolve, reject ) => {
api.get( {
action: 'query',
list: 'wikilambdasearch_labels',
format: 'json',
formatversion: '2',
wikilambdasearch_search: payload.input,
wikilambdasearch_type: payload.types ? payload.types.join( '|' ) : undefined,
wikilambdasearch_return_type: payload.returnTypes ? payload.returnTypes.join( '|' ) : undefined,
wikilambdasearch_language: payload.language,
wikilambdasearch_exact: payload.exact || undefined,
wikilambdasearch_limit: payload.limit,
wikilambdasearch_continue: payload.searchContinue
}, {
signal: payload.signal
} )
.then( ( data ) => resolve( {
labels: data.query ? data.query.wikilambdasearch_labels : [],
searchContinue: data.continue ? Number( data.continue.wikilambdasearch_continue ) : null
} ) )
.catch( ( ...args ) => reject( ApiError.fromMwApiRejection( ...args ) ) );
} );
},
/**
* Calls the wikilambdasearch_functions internal API
* https://www.mediawiki.org/wiki/Extension:WikiLambda/API#wikilambdasearch_functions
*
* Doesn't need error handling.
*
* @param {Object} payload
* @param {string} payload.search Substring to search by
* @param {string} payload.language The user language code
* @param {boolean} payload.renderable
* @param {Array} payload.inputTypes
* @param {string} payload.outputType
* @param {number} payload.limit The maximum number of results to return
* @param {number} payload.searchContinue When more results are available, use this to continue
* @param {AbortSignal} payload.signal The AbortSignal to cancel the request
* @return {Promise<Object>|undefined} Promise resolving to an object with 'objects' and 'continue'
*/
searchFunctions: function ( payload ) {
const api = apiUtils.newApi();
return new Promise( ( resolve, reject ) => {
api.get( {
action: 'query',
list: 'wikilambdasearch_functions',
format: 'json',
formatversion: '2',
wikilambdasearch_functions_search: payload.search,
wikilambdasearch_functions_language: payload.language,
wikilambdasearch_functions_renderable: payload.renderable,
wikilambdasearch_functions_input_types: payload.inputTypes,
wikilambdasearch_functions_output_type: payload.outputType,
wikilambdasearch_functions_limit: payload.limit,
wikilambdasearch_functions_continue: payload.searchContinue
}, {
signal: payload.signal
} )
.then( ( data ) => resolve( {
objects: data.query ? data.query.wikilambdasearch_functions : [],
searchContinue: data.continue ?
Number( data.continue.wikilambdasearch_functions_continue ) :
null
} ) )
.catch( ( ...args ) => reject( ApiError.fromMwApiRejection( ...args ) ) );
} );
},
/**
* Calls the wikilambda_perform_test internal API
* https://www.mediawiki.org/wiki/Extension:WikiLambda/API#wikilambda_perform_test
*
* @param {Object} payload
* @param {string} payload.functionZid Zid of the function to test
* @param {Array} payload.implementations List of implementations to test
* @param {Array} payload.testers List of tests to run
* @param {string} payload.language The user language code
* @param {AbortSignal} payload.signal The AbortSignal to cancel the request
* @return {Promise}
*/
performTests: function ( payload ) {
const api = apiUtils.newApi();
return new Promise( ( resolve, reject ) => {
api.get( {
action: 'wikilambda_perform_test',
format: 'json',
formatversion: '2',
wikilambda_perform_test_zfunction: payload.functionZid,
wikilambda_perform_test_zimplementations: payload.implementations.join( '|' ),
wikilambda_perform_test_ztesters: payload.testers.join( '|' ),
uselang: payload.language
}, {
signal: payload.signal
} )
.then( ( data ) => {
if ( data.query.wikilambda_perform_test ) {
return resolve( data.query.wikilambda_perform_test );
}
// Successful call, but has no data:
// Check for warnings or build undefined error
return reject( ApiError.fromMwApiSuccess( data ) );
} )
.catch( ( ...args ) => reject( ApiError.fromMwApiRejection( ...args ) ) );
} );
},
/**
* Calls the wikilambdafn_search internal API
* https://www.mediawiki.org/wiki/Extension:WikiLambda/API#wikilambdafn_search
*
* Doesn't need error handling.
*
* @param {Object} payload
* @param {string} payload.functionZid Zid of the function to test
* @param {string} payload.type What type of object to fetch (Z20 or Z14)
* @param {AbortSignal} payload.signal The AbortSignal to cancel the request
* @return {Promise}
*/
fetchFunctionObjects: function ( payload ) {
const api = apiUtils.newApi();
return new Promise( ( resolve, reject ) => {
api.get( {
action: 'query',
list: 'wikilambdafn_search',
format: 'json',
formatversion: '2',
wikilambdafn_zfunction_id: payload.functionZid,
wikilambdafn_type: payload.type,
wikilambdafn_limit: Constants.API_LIMIT_MAX
}, {
signal: payload.signal
} )
.then( ( data ) => resolve( data.query.wikilambdafn_search ) )
.catch( ( ...args ) => reject( ApiError.fromMwApiRejection( ...args ) ) );
} );
},
/**
* Calls the Commons Action API to search for media files by keyword.
* Uses generator=search in File (namespace 6).
* https://www.mediawiki.org/wiki/API:Search
*
* @param {Object} payload
* @param {string} payload.search - Search term
* @param {number|null} [payload.searchContinue] - Offset for pagination
* @param {AbortSignal} [payload.signal] - Optional AbortSignal to cancel the request
* @return {Promise}
*/
searchCommonsMedia: function ( payload ) {
const api = apiUtils.newCommonsApi();
const params = {
action: 'query',
format: 'json',
formatversion: '2',
generator: 'search',
gsrsearch: payload.search,
gsrnamespace: '6',
gsrlimit: '10',
prop: 'imageinfo|pageimages',
iiprop: 'url|mime',
piprop: 'thumbnail',
pithumbsize: '120'
};
if ( payload.searchContinue ) {
params.gsroffset = payload.searchContinue;
}
return new Promise( ( resolve, reject ) => {
api.get( params, {
signal: payload.signal
} )
.then( ( data ) => {
const rawPages = data.query ? data.query.pages : [];
// generator=search returns an object keyed by pageid; normalise to array
const pages = Array.isArray( rawPages ) ?
rawPages :
Object.values( rawPages );
// Sort by index so results appear in search-relevance order
pages.sort( ( a, b ) => ( a.index || 0 ) - ( b.index || 0 ) );
const searchContinue = data.continue ? data.continue.gsroffset : null;
resolve( { pages, searchContinue } );
} )
.catch( ( ...args ) => reject( ApiError.fromMwApiRejection( ...args ) ) );
} );
},
/**
* Calls the Commons Action API to fetch media metadata for specific page IDs.
* Used to load display data (title, thumbnail) for a known M-ID.
*
* @param {Object} payload
* @param {string} payload.ids - Page IDs joined by pipes (numeric, no "M" prefix)
* @param {AbortSignal} [payload.signal] - Optional AbortSignal to cancel the request
* @return {Promise<Object>}
*/
fetchCommonsMediaByIds: function ( payload ) {
const api = apiUtils.newCommonsApi();
return new Promise( ( resolve, reject ) => {
api.get( {
action: 'query',
format: 'json',
formatversion: '2',
pageids: payload.ids,
prop: 'imageinfo',
iiprop: 'url',
iiurlwidth: 250
}, {
signal: payload.signal
} )
.then( ( data ) => resolve( data ) )
.catch( ( ...args ) => reject( ApiError.fromMwApiRejection( ...args ) ) );
} );
},
/**
* Calls the wbsearchentities Wikidata Action API
* https://www.wikidata.org/w/api.php?action=help&modules=wbsearchentities
*
* @param {Object} payload
* @param {string} payload.language user language code
* @param {string} payload.type type of Wikidata entity
* @param {string} payload.search search term
* @param {number} payload.searchContinue When more results are available, use this to continue
* @param {AbortSignal} [payload.signal] Optional AbortSignal to cancel the request
* @return {Promise}
*/
searchWikidataEntities: function ( payload ) {
const api = apiUtils.newWikidataApi();
const params = {
action: 'wbsearchentities',
format: 'json',
formatversion: '2',
language: payload.language,
uselang: payload.language,
search: payload.search,
type: payload.type,
limit: '10',
props: 'url'
};
if ( payload.searchContinue ) {
params.continue = payload.searchContinue;
}
return new Promise( ( resolve, reject ) => {
api.get( params, {
signal: payload.signal
} )
.then( ( data ) => resolve( {
search: data.search ? data.search : [],
searchContinue: data[ 'search-continue' ] ? Number( data[ 'search-continue' ] ) : null
} ) )
.catch( ( ...args ) => reject( ApiError.fromMwApiRejection( ...args ) ) );
} );
},
/**
* Calls the wbgetentities Wikidata Action API
* https://www.wikidata.org/w/api.php?action=help&modules=wbgetentities
*
* @param {Object} payload
* @param {string} payload.language user language code
* @param {string} payload.ids entity Ids to fetch, separated by pipes and max 50 (API limit)
* @param {string} [payload.props] wbgetentities `props` value to restrict the response (e.g.
* 'labels'); when omitted, the API returns the full entity payload.
* @param {AbortSignal} [payload.signal] Optional AbortSignal to cancel the request
* @return {Promise}
*/
fetchWikidataEntities: function ( payload ) {
const api = apiUtils.newWikidataApi();
return new Promise( ( resolve, reject ) => {
api.get( {
action: 'wbgetentities',
format: 'json',
formatversion: '2',
languages: payload.language,
languagefallback: true,
ids: payload.ids,
props: payload.props || undefined
}, {
signal: payload.signal
} )
.then( ( data ) => resolve( data ) )
.catch( ( ...args ) => reject( ApiError.fromMwApiRejection( ...args ) ) );
} );
},
/**
* Calls the wikifunctions_html_sanitiser internal API
* https://www.mediawiki.org/wiki/Extension:WikiLambda/API#wikifunctions_html_sanitiser
*
* Sanitises HTML fragments for safe user-facing display.
*
* @param {Object} payload
* @param {string} payload.html The HTML string to sanitise
* @param {AbortSignal} [payload.signal] Optional AbortSignal to cancel the request
* @return {Promise<string>} Promise resolving to the sanitised HTML string
*/
sanitiseHtmlFragment: function ( payload ) {
// Always local, never the foreign API: postWithEditToken's CSRF-token fetch is
// refused cross-origin (T426024). The sanitiser endpoint is registered on every
// wiki where WikiLambda loads, so this works in both repo and client modes.
const api = new mw.Api();
return new Promise( ( resolve, reject ) => {
api.postWithEditToken( {
action: 'wikifunctions_html_sanitiser',
format: 'json',
formatversion: '2',
html: payload.html
}, {
signal: payload.signal
} )
.then( ( data ) => resolve( {
html: data.wikifunctions_html_sanitiser ? data.wikifunctions_html_sanitiser.value : ''
} ) )
.catch( ( ...args ) => reject( ApiError.fromMwApiRejection( ...args ) ) );
} );
},
/**
* Calls mediawiki query Action API go get basic page info given a list of Ids
* https://www.mediawiki.org/w/api.php?action=help&modules=query%2Binfo
*
* @param {Object} payload
* @param {Array} payload.titles page Ids to fetch
* @param {AbortSignal} [payload.signal] Optional AbortSignal to cancel the request
* @return {Promise<Array>}
*/
fetchPageInfo: function ( payload ) {
// Should never work with foreign API
const api = new mw.Api();
return new Promise( ( resolve, reject ) => {
api.get( {
action: 'query',
prop: 'info',
titles: payload.titles,
format: 'json',
formatversion: '2'
}, {
signal: payload.signal
} )
.then( ( data ) => resolve( data.query.pages ) )
.catch( ( ...args ) => reject( ApiError.fromMwApiRejection( ...args ) ) );
} );
},
/**
* Calls the action=edit API for saving abstract wiki content.
*
* Needs error handling.
*
* @param {Object} payload
* @param {Object} payload.content
* @param {string} payload.title
* @param {string} payload.summary
* @param {AbortSignal} payload.signal The AbortSignal to cancel the request
* @return {Promise}
*/
saveAbstractWikiContent: function ( payload ) {
// Should never work with foreign API
const api = new mw.Api();
return new Promise( ( resolve, reject ) => {
api.postWithEditToken( {
action: 'edit',
format: 'json',
formatversion: '2',
summary: payload.summary || '',
title: payload.title,
text: JSON.stringify( payload.content, null, 4 )
}, {
signal: payload.signal
} )
.then( ( data ) => resolve( data.edit ) )
.catch( ( ...args ) => reject( ApiError.fromMwApiRejection( ...args ) ) );
} );
},
/**
* Calls the action=abstractwiki_fetch_section API for retrieving the current
* stored fragments for a given topic/section/language.
*
* Needs error handling.
*
* @param {Object} payload
* @param {string} payload.topic
* @param {string} payload.section
* @param {string} payload.language
* @param {Array|undefined} payload.fragments
* @param {AbortSignal} payload.signal The AbortSignal to cancel the request
* @return {Promise}
*/
fetchAbstractWikiSection: function ( payload ) {
// Should never work with foreign API
const api = new mw.Api();
const params = {
action: 'abstractwiki_fetch_section',
format: 'json',
formatversion: '2',
abstractwiki_fetch_section_topic: payload.topic,
abstractwiki_fetch_section_section: payload.section,
abstractwiki_fetch_section_language: payload.language,
abstractwiki_fetch_section_fragments: payload.fragments
};
const options = { signal: payload.signal };
// Fetching a section's persisted fragments is an idempotent read served as
// a cacheable GET, like the sibling abstractwiki_run_fragment API. Supplying
// unsaved fragments flips the API to its POST-only render path.
// We get csrf token, manually set it as abstractwiki_fetch_section_token,
// because postWithToken would add the token parameter without the prefix
const request = payload.fragments === undefined ?
api.get( params, options ) :
api.getToken( 'csrf' ).then( ( token ) => {
params.abstractwiki_fetch_section_token = token;
return api.post( params, options );
} );
return new Promise( ( resolve, reject ) => {
request
.then( ( data ) => resolve( data.abstractwiki_fetch_section ) )
.catch( ( ...args ) => reject( ApiError.fromMwApiRejection( ...args ) ) );
} );
},
/**
* Calls the action=abstractwiki_run_fragment API for generating the sanitized
* preview of a given Abstract Wiki content fragment.
*
* Needs error handling.
*
* @param {Object} payload
* @param {string} payload.qid
* @param {string} payload.language
* @param {Object} payload.fragment
* @param {AbortSignal} payload.signal The AbortSignal to cancel the request
* @return {Promise}
*/
runAbstractWikiFragment: function ( payload ) {
// Should never work with foreign API
const api = new mw.Api();
const canonicalJson = JSON.stringify( hybridToCanonical( payload.fragment ) );
return new Promise( ( resolve, reject ) => {
api.get( {
action: 'abstractwiki_run_fragment',
format: 'json',
formatversion: '2',
abstractwiki_run_fragment_qid: payload.qid,
abstractwiki_run_fragment_language: payload.language,
abstractwiki_run_fragment_fragment: canonicalJson
}, {
signal: payload.signal
} )
.then( ( data ) => resolve( data.abstractwiki_run_fragment ) )
.catch( ( ...args ) => reject( ApiError.fromMwApiRejection( ...args ) ) );
} );
},
/**
* Calls the wikilambdafn_usage query API to get how many pages use a given
* Function, and how many wikis they are on.
*
* The property is empty for a Function that nothing uses yet, so fall back to
* zeroes rather than making the caller handle a missing object.
*
* @param {Object} payload
* @param {string} payload.zid The Function's ZID
* @param {AbortSignal} [payload.signal] Optional AbortSignal to cancel the request
* @return {Promise<{pages: number, wikis: number, pagesLimited: boolean}>}
* pagesLimited is true when the real page count is higher than the API counts to,
* so pages is a floor rather than an exact figure.
*/
fetchFunctionUsage: function ( payload ) {
// Only the repo stores usage, so this must never work with a foreign API
const api = new mw.Api();
return new Promise( ( resolve, reject ) => {
api.get( {
action: 'query',
prop: 'wikilambdafn_usage',
titles: payload.zid,
format: 'json',
formatversion: '2',
// The counts are the same for everyone and are cached server-side anyway,
// so let the CDN and the browser absorb repeat views. The Action API only
// sends public cache headers when one of these is set.
maxage: 300,
smaxage: 300
}, {
signal: payload.signal
} )
.then( ( data ) => {
const page = data.query.pages[ 0 ] || {};
const usage = page.wikilambdafn_usage || {};
resolve( {
pages: usage.pages || 0,
wikis: usage.wikis || 0,
pagesLimited: !!usage.pagesLimited
} );
} )
.catch( ( ...args ) => reject( ApiError.fromMwApiRejection( ...args ) ) );
} );
}
};
module.exports = apiUtils;
|