| 1 |
|
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
|
| 6 |
|
| 7 |
( function () { |
| 8 |
|
| 9 |
|
| 10 |
|
| 11 |
|
| 12 |
|
| 13 |
|
| 14 |
|
| 15 |
|
| 16 |
|
| 17 |
|
| 18 |
|
| 19 |
|
| 20 |
|
| 21 |
mw.widgets.MediaResourceProvider = function MwWidgetsMediaResourceProvider( apiurl, config ) { |
| 22 |
config = config || {}; |
| 23 |
|
| 24 |
|
| 25 |
mw.widgets.MediaResourceProvider.super.call( this, apiurl, config ); |
| 26 |
|
| 27 |
|
| 28 |
this.scriptDirUrl = config.scriptDirUrl; |
| 29 |
this.isLocal = config.local !== undefined; |
| 30 |
|
| 31 |
if ( this.isLocal ) { |
| 32 |
this.setAPIurl( mw.util.wikiScript( 'api' ) ); |
| 33 |
} else { |
| 34 |
|
| 35 |
|
| 36 |
this.setAPIurl( this.getAPIurl() || ( this.scriptDirUrl + '/api.php' ) ); |
| 37 |
} |
| 38 |
|
| 39 |
this.siteInfoPromise = null; |
| 40 |
this.thumbSizes = []; |
| 41 |
this.imageSizes = []; |
| 42 |
}; |
| 43 |
|
| 44 |
|
| 45 |
OO.inheritClass( mw.widgets.MediaResourceProvider, mw.widgets.APIResultsProvider ); |
| 46 |
|
| 47 |
|
| 48 |
|
| 49 |
|
| 50 |
|
| 51 |
|
| 52 |
mw.widgets.MediaResourceProvider.prototype.getStaticParams = function () { |
| 53 |
return Object.assign( |
| 54 |
{}, |
| 55 |
|
| 56 |
mw.widgets.MediaResourceProvider.super.prototype.getStaticParams.call( this ), |
| 57 |
{ |
| 58 |
action: 'query', |
| 59 |
iiprop: 'dimensions|url|mediatype|extmetadata|timestamp|user', |
| 60 |
iiextmetadatalanguage: this.getLang(), |
| 61 |
prop: 'imageinfo' |
| 62 |
} |
| 63 |
); |
| 64 |
}; |
| 65 |
|
| 66 |
|
| 67 |
|
| 68 |
|
| 69 |
|
| 70 |
|
| 71 |
|
| 72 |
|
| 73 |
|
| 74 |
|
| 75 |
mw.widgets.MediaResourceProvider.prototype.loadSiteInfo = function () { |
| 76 |
|
| 77 |
if ( !this.siteInfoPromise ) { |
| 78 |
this.siteInfoPromise = new mw.Api().get( { |
| 79 |
action: 'query', |
| 80 |
meta: 'siteinfo' |
| 81 |
} ) |
| 82 |
.then( ( data ) => { |
| 83 |
this.setImageSizes( data.query.general.imagelimits || [] ); |
| 84 |
this.setThumbSizes( data.query.general.thumblimits || [] ); |
| 85 |
this.setUserParams( { |
| 86 |
|
| 87 |
iiurlwidth: this.getStandardWidth() |
| 88 |
} ); |
| 89 |
} ); |
| 90 |
} |
| 91 |
return this.siteInfoPromise; |
| 92 |
}; |
| 93 |
|
| 94 |
|
| 95 |
|
| 96 |
|
| 97 |
|
| 98 |
|
| 99 |
|
| 100 |
|
| 101 |
mw.widgets.MediaResourceProvider.prototype.getResults = function ( howMany ) { |
| 102 |
let xhr, |
| 103 |
aborted = false; |
| 104 |
|
| 105 |
return this.loadSiteInfo() |
| 106 |
.then( () => { |
| 107 |
if ( aborted ) { |
| 108 |
return $.Deferred().reject(); |
| 109 |
} |
| 110 |
xhr = this.fetchAPIresults( howMany ); |
| 111 |
return xhr; |
| 112 |
} ) |
| 113 |
.then( |
| 114 |
( results ) => { |
| 115 |
if ( !results || results.length === 0 ) { |
| 116 |
this.toggleDepleted( true ); |
| 117 |
return []; |
| 118 |
} |
| 119 |
return results; |
| 120 |
}, |
| 121 |
|
| 122 |
() => { |
| 123 |
this.toggleDepleted( true ); |
| 124 |
return $.Deferred().resolve( [] ); |
| 125 |
} |
| 126 |
) |
| 127 |
.promise( { abort: function () { |
| 128 |
aborted = true; |
| 129 |
if ( xhr ) { |
| 130 |
xhr.abort(); |
| 131 |
} |
| 132 |
} } ); |
| 133 |
}; |
| 134 |
|
| 135 |
|
| 136 |
|
| 137 |
|
| 138 |
|
| 139 |
|
| 140 |
|
| 141 |
mw.widgets.MediaResourceProvider.prototype.getContinueData = function () { |
| 142 |
return {}; |
| 143 |
}; |
| 144 |
|
| 145 |
|
| 146 |
|
| 147 |
|
| 148 |
|
| 149 |
|
| 150 |
mw.widgets.MediaResourceProvider.prototype.setContinue = function () { |
| 151 |
}; |
| 152 |
|
| 153 |
|
| 154 |
|
| 155 |
|
| 156 |
|
| 157 |
|
| 158 |
|
| 159 |
mw.widgets.MediaResourceProvider.prototype.sort = function ( results ) { |
| 160 |
return results; |
| 161 |
}; |
| 162 |
|
| 163 |
|
| 164 |
|
| 165 |
|
| 166 |
|
| 167 |
|
| 168 |
|
| 169 |
|
| 170 |
mw.widgets.MediaResourceProvider.prototype.fetchAPIresults = function ( howMany ) { |
| 171 |
if ( !this.isValid() ) { |
| 172 |
return $.Deferred().reject().promise( { abort: function () {} } ); |
| 173 |
} |
| 174 |
|
| 175 |
const api = this.isLocal ? new mw.Api() : new mw.ForeignApi( this.getAPIurl(), { anonymous: true } ); |
| 176 |
const xhr = api.get( Object.assign( {}, this.getStaticParams(), this.getUserParams(), this.getContinueData( howMany ) ) ); |
| 177 |
return xhr |
| 178 |
.then( ( data ) => { |
| 179 |
const results = []; |
| 180 |
|
| 181 |
if ( data.error ) { |
| 182 |
this.toggleDepleted( true ); |
| 183 |
return []; |
| 184 |
} |
| 185 |
|
| 186 |
if ( data.continue ) { |
| 187 |
|
| 188 |
this.setContinue( data.continue ); |
| 189 |
} else { |
| 190 |
|
| 191 |
this.toggleDepleted( true ); |
| 192 |
} |
| 193 |
|
| 194 |
|
| 195 |
|
| 196 |
if ( data.query ) { |
| 197 |
const raw = data.query.pages; |
| 198 |
if ( raw ) { |
| 199 |
|
| 200 |
for ( const page in raw ) { |
| 201 |
if ( !raw[ page ].imageinfo ) { |
| 202 |
|
| 203 |
|
| 204 |
|
| 205 |
|
| 206 |
|
| 207 |
continue; |
| 208 |
} |
| 209 |
const newObj = raw[ page ].imageinfo[ 0 ]; |
| 210 |
newObj.title = raw[ page ].title; |
| 211 |
newObj.index = raw[ page ].index; |
| 212 |
results.push( newObj ); |
| 213 |
} |
| 214 |
} |
| 215 |
} |
| 216 |
return this.sort( results ); |
| 217 |
} ) |
| 218 |
.promise( { abort: xhr.abort } ); |
| 219 |
}; |
| 220 |
|
| 221 |
|
| 222 |
|
| 223 |
|
| 224 |
|
| 225 |
|
| 226 |
mw.widgets.MediaResourceProvider.prototype.setName = function ( name ) { |
| 227 |
this.name = name; |
| 228 |
}; |
| 229 |
|
| 230 |
|
| 231 |
|
| 232 |
|
| 233 |
|
| 234 |
|
| 235 |
mw.widgets.MediaResourceProvider.prototype.getName = function () { |
| 236 |
return this.name; |
| 237 |
}; |
| 238 |
|
| 239 |
|
| 240 |
|
| 241 |
|
| 242 |
|
| 243 |
|
| 244 |
mw.widgets.MediaResourceProvider.prototype.getStandardWidth = function () { |
| 245 |
return ( this.thumbSizes && this.thumbSizes[ this.thumbSizes.length - 1 ] ) || |
| 246 |
( this.imageSizes && this.imageSizes[ 0 ] ) || |
| 247 |
|
| 248 |
300; |
| 249 |
}; |
| 250 |
|
| 251 |
|
| 252 |
|
| 253 |
|
| 254 |
|
| 255 |
|
| 256 |
mw.widgets.MediaResourceProvider.prototype.getFetchProp = function () { |
| 257 |
return this.fetchProp; |
| 258 |
}; |
| 259 |
|
| 260 |
|
| 261 |
|
| 262 |
|
| 263 |
|
| 264 |
|
| 265 |
mw.widgets.MediaResourceProvider.prototype.setFetchProp = function ( prop ) { |
| 266 |
this.fetchProp = prop; |
| 267 |
}; |
| 268 |
|
| 269 |
|
| 270 |
|
| 271 |
|
| 272 |
|
| 273 |
|
| 274 |
mw.widgets.MediaResourceProvider.prototype.setThumbSizes = function ( sizes ) { |
| 275 |
this.thumbSizes = sizes; |
| 276 |
}; |
| 277 |
|
| 278 |
|
| 279 |
|
| 280 |
|
| 281 |
|
| 282 |
|
| 283 |
mw.widgets.MediaResourceProvider.prototype.setImageSizes = function ( sizes ) { |
| 284 |
this.imageSizes = sizes; |
| 285 |
}; |
| 286 |
|
| 287 |
|
| 288 |
|
| 289 |
|
| 290 |
|
| 291 |
|
| 292 |
mw.widgets.MediaResourceProvider.prototype.getThumbSizes = function () { |
| 293 |
return this.thumbSizes; |
| 294 |
}; |
| 295 |
|
| 296 |
|
| 297 |
|
| 298 |
|
| 299 |
|
| 300 |
|
| 301 |
mw.widgets.MediaResourceProvider.prototype.getImageSizes = function () { |
| 302 |
return this.imageSizes; |
| 303 |
}; |
| 304 |
|
| 305 |
|
| 306 |
|
| 307 |
|
| 308 |
|
| 309 |
|
| 310 |
mw.widgets.MediaResourceProvider.prototype.isValid = function () { |
| 311 |
return this.isLocal || |
| 312 |
|
| 313 |
|
| 314 |
this.apiurl !== undefined || |
| 315 |
this.scriptDirUrl !== undefined; |
| 316 |
}; |
| 317 |
}() ); |