| 1 | module.exports = ( function () { |
| 2 | |
| 3 | /** |
| 4 | * @classdesc Interact with the REST API of another MediaWiki site. mw.ForeignRest creates |
| 5 | * an object like {@link mw.Rest}, but automatically handles everything required to communicate |
| 6 | * with another MediaWiki wiki via cross-origin requests (CORS). |
| 7 | * |
| 8 | * The foreign wiki must be configured to accept requests from the current wiki. |
| 9 | * For details, see [$wgCrossSiteAJAXdomains](https://www.mediawiki.org/wiki/Manual:$wgCrossSiteAJAXdomains) |
| 10 | * and [$wgRestAllowCrossOriginCookieAuth](https://www.mediawiki.org/wiki/Manual:$wgRestAllowCrossOriginCookieAuth). |
| 11 | * ``` |
| 12 | * const api = new mw.ForeignRest( 'https://commons.wikimedia.org/w/rest.php' ); |
| 13 | * api.get( '/page/Main_Page/html' ) |
| 14 | * .done( function ( data ) { |
| 15 | * console.log( data ); |
| 16 | * } ); |
| 17 | * ``` |
| 18 | * |
| 19 | * Authentication-related MediaWiki extensions may extend this class to ensure that the user |
| 20 | * authenticated on the current wiki will be automatically authenticated on the foreign one. These |
| 21 | * extension modules should be registered using the ResourceLoaderForeignApiModules hook. See |
| 22 | * CentralAuth for a practical example. The general pattern to extend and override the name is: |
| 23 | * ``` |
| 24 | * function MyForeignRest() {}; |
| 25 | * OO.inheritClass( MyForeignRest, mw.ForeignRest ); |
| 26 | * mw.ForeignRest = MyForeignRest; |
| 27 | * ``` |
| 28 | * |
| 29 | * @class mw.ForeignRest |
| 30 | * @extends mw.Rest |
| 31 | * @since 1.36 |
| 32 | * |
| 33 | * @constructor |
| 34 | * @description Create an instance of `mw.ForeignRest`. |
| 35 | * @param {string} url URL pointing to another wiki's `rest.php` endpoint. |
| 36 | * @param {mw.ForeignApi} foreignActionApi |
| 37 | * @param {Object} [options] See {@link mw.Rest}. |
| 38 | * @param {boolean} [options.anonymous=false] Perform all requests anonymously. Use this option if |
| 39 | * the target wiki may otherwise not accept cross-origin requests, or if you don't need to |
| 40 | * perform write actions or read restricted information and want to avoid the overhead. |
| 41 | * |
| 42 | * @author Petr Pchelko |
| 43 | */ |
| 44 | function CoreForeignRest( url, foreignActionApi, options ) { |
| 45 | this.apiUrl = url; |
| 46 | this.anonymous = options && options.anonymous; |
| 47 | this.foreignActionApi = foreignActionApi; |
| 48 | |
| 49 | options = $.extend( /* deep= */ true, |
| 50 | { |
| 51 | ajax: { |
| 52 | url: this.apiUrl, |
| 53 | xhrFields: { |
| 54 | withCredentials: !this.anonymous |
| 55 | } |
| 56 | } |
| 57 | }, |
| 58 | options |
| 59 | ); |
| 60 | |
| 61 | // Call parent constructor |
| 62 | CoreForeignRest.super.call( this, options ); |
| 63 | } |
| 64 | |
| 65 | OO.inheritClass( CoreForeignRest, mw.Rest ); |
| 66 | |
| 67 | return CoreForeignRest; |
| 68 | }() ); |