| 1 | /* |
| 2 | * This file is part of the MediaWiki extension MultimediaViewer. |
| 3 | * |
| 4 | * MultimediaViewer is free software: you can redistribute it and/or modify |
| 5 | * it under the terms of the GNU General Public License as published by |
| 6 | * the Free Software Foundation, either version 2 of the License, or |
| 7 | * (at your option) any later version. |
| 8 | * |
| 9 | * MultimediaViewer is distributed in the hope that it will be useful, |
| 10 | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 11 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 12 | * GNU General Public License for more details. |
| 13 | * |
| 14 | * You should have received a copy of the GNU General Public License |
| 15 | * along with MultimediaViewer. If not, see <http://www.gnu.org/licenses/>. |
| 16 | */ |
| 17 | |
| 18 | /** |
| 19 | * Base class for API-based data providers. |
| 20 | * |
| 21 | * @abstract |
| 22 | */ |
| 23 | class Api { |
| 24 | /** |
| 25 | * @param {mw.Api} api |
| 26 | * @param {Object} [options] |
| 27 | * @param {number} [options.maxage] cache expiration time, in seconds |
| 28 | * Will be used for both client-side cache (maxage) and reverse proxies (s-maxage) |
| 29 | */ |
| 30 | constructor( api, options ) { |
| 31 | /** |
| 32 | * API object for dependency injection. |
| 33 | * |
| 34 | * @property {mw.Api} |
| 35 | */ |
| 36 | this.api = api; |
| 37 | |
| 38 | /** |
| 39 | * Options object; the exact format and meaning is unspecified and could be different |
| 40 | * from subclass to subclass. |
| 41 | * |
| 42 | * @property {Object} |
| 43 | */ |
| 44 | this.options = options || {}; |
| 45 | |
| 46 | /** |
| 47 | * API call cache. |
| 48 | * |
| 49 | * @property {Object.<string, jQuery.Promise>} cache |
| 50 | * @protected |
| 51 | */ |
| 52 | this.cache = {}; |
| 53 | } |
| 54 | |
| 55 | /** |
| 56 | * Wraps a caching layer around a function returning a promise; if getCachedPromise has been |
| 57 | * called with the same key already, it will return the previous result. |
| 58 | * |
| 59 | * Since it is the promise and not the API response that gets cached, this method can ensure |
| 60 | * that there are no race conditions and multiple calls to the same resource: even if the |
| 61 | * request is still in progress, separate calls (with the same key) to getCachedPromise will |
| 62 | * share on the same promise object. |
| 63 | * The promise is cached even if it is rejected, so if the API request fails, all later calls |
| 64 | * to getCachedPromise will fail immediately without retrying the request. |
| 65 | * |
| 66 | * @param {string} key cache key |
| 67 | * @param {function(): jQuery.Promise} getPromise a function to get the promise on cache miss |
| 68 | * @return {jQuery.Promise} |
| 69 | */ |
| 70 | getCachedPromise( key, getPromise ) { |
| 71 | if ( !this.cache[ key ] ) { |
| 72 | this.cache[ key ] = getPromise(); |
| 73 | this.cache[ key ].fail( ( error ) => { |
| 74 | // constructor.name is usually not reliable in inherited classes, but OOjs fixes that |
| 75 | mw.log( `${ this.constructor.name } provider failed to load: `, error ); |
| 76 | } ); |
| 77 | } |
| 78 | return this.cache[ key ]; |
| 79 | } |
| 80 | |
| 81 | /** |
| 82 | * Calls mw.Api.get, with caching parameters. |
| 83 | * |
| 84 | * @param {Object} params Parameters to the API query. |
| 85 | * @param {Object} [ajaxOptions] ajaxOptions argument for mw.Api.get |
| 86 | * @param {number|null} [maxage] Cache the call for this many seconds. |
| 87 | * Sets both the maxage (client-side) and smaxage (proxy-side) caching parameters. |
| 88 | * Null means no caching. Undefined means the default caching period is used. |
| 89 | * @return {jQuery.Promise} the return value from mw.Api.get |
| 90 | */ |
| 91 | apiGetWithMaxAge( params, ajaxOptions, maxage ) { |
| 92 | if ( maxage === undefined ) { |
| 93 | maxage = this.options.maxage; |
| 94 | } |
| 95 | if ( maxage ) { |
| 96 | params.maxage = params.smaxage = maxage; |
| 97 | } |
| 98 | |
| 99 | return this.api.get( params, ajaxOptions ); |
| 100 | } |
| 101 | |
| 102 | /** |
| 103 | * Pulls an error message out of an API response. |
| 104 | * |
| 105 | * @param {Object} data |
| 106 | * @param {Object} data.error |
| 107 | * @param {string} data.error.code |
| 108 | * @param {string} data.error.info |
| 109 | * @return {string} From data.error.code + ': ' + data.error.info, or 'unknown error' |
| 110 | */ |
| 111 | getErrorMessage( data ) { |
| 112 | const errorCode = data.error && data.error.code; |
| 113 | let errorMessage = data.error && data.error.info || 'unknown error'; |
| 114 | if ( errorCode ) { |
| 115 | errorMessage = `${ errorCode }: ${ errorMessage }`; |
| 116 | } |
| 117 | return errorMessage; |
| 118 | } |
| 119 | |
| 120 | /** |
| 121 | * Returns a promise with the specified page from the API result. |
| 122 | * This is intended to be used as a .then() callback for action=query&prop=(...) APIs. |
| 123 | * |
| 124 | * @param {Object} data |
| 125 | * @return {jQuery.Promise} when successful, the first argument will be the page data, |
| 126 | * when unsuccessful, it will be an error message. The second argument is always |
| 127 | * the full API response. |
| 128 | */ |
| 129 | getQueryPage( data ) { |
| 130 | if ( data && |
| 131 | data.query && |
| 132 | Array.isArray( data.query.pages ) && |
| 133 | data.query.pages.length === 1 |
| 134 | ) { |
| 135 | // pages is an array and the first element is always the requested title |
| 136 | return $.Deferred().resolve( data.query.pages[ 0 ], data ); |
| 137 | } |
| 138 | |
| 139 | // If we got to this point either the pages array is missing completely, or the |
| 140 | // first element is not the requested page. Neither is supposed to happen |
| 141 | // (if the page simply did not exist, there would still be a record for it). |
| 142 | return $.Deferred().reject( this.getErrorMessage( data ), data ); |
| 143 | } |
| 144 | } |
| 145 | |
| 146 | module.exports = Api; |