| 1 |
|
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
|
| 6 |
|
| 7 |
|
| 8 |
|
| 9 |
|
| 10 |
|
| 11 |
|
| 12 |
|
| 13 |
|
| 14 |
|
| 15 |
|
| 16 |
|
| 17 |
|
| 18 |
|
| 19 |
ve.init.mw.ApiResponseCache = function VeInitMwApiResponseCache( api ) { |
| 20 |
|
| 21 |
OO.EventEmitter.call( this ); |
| 22 |
|
| 23 |
this.api = api || new mw.Api(); |
| 24 |
|
| 25 |
|
| 26 |
this.deferreds = {}; |
| 27 |
|
| 28 |
|
| 29 |
|
| 30 |
this.cacheValues = {}; |
| 31 |
|
| 32 |
|
| 33 |
this.queue = []; |
| 34 |
|
| 35 |
this.schedule = ve.debounce( this.processQueue.bind( this ), 0 ); |
| 36 |
}; |
| 37 |
|
| 38 |
|
| 39 |
|
| 40 |
OO.mixinClass( ve.init.mw.ApiResponseCache, OO.EventEmitter ); |
| 41 |
|
| 42 |
|
| 43 |
|
| 44 |
|
| 45 |
|
| 46 |
|
| 47 |
|
| 48 |
|
| 49 |
|
| 50 |
|
| 51 |
|
| 52 |
ve.init.mw.ApiResponseCache.static.processPage = null; |
| 53 |
|
| 54 |
|
| 55 |
|
| 56 |
|
| 57 |
|
| 58 |
ve.init.mw.ApiResponseCache.static.normalizeTitle = function ( title ) { |
| 59 |
const titleObj = mw.Title.newFromText( title ); |
| 60 |
if ( !titleObj ) { |
| 61 |
return title; |
| 62 |
} |
| 63 |
return titleObj.getPrefixedText(); |
| 64 |
}; |
| 65 |
|
| 66 |
|
| 67 |
|
| 68 |
|
| 69 |
|
| 70 |
|
| 71 |
|
| 72 |
|
| 73 |
|
| 74 |
|
| 75 |
|
| 76 |
ve.init.mw.ApiResponseCache.prototype.get = function ( title ) { |
| 77 |
if ( typeof title !== 'string' ) { |
| 78 |
|
| 79 |
|
| 80 |
|
| 81 |
return ve.createDeferred().reject().promise(); |
| 82 |
} |
| 83 |
title = this.constructor.static.normalizeTitle( title ); |
| 84 |
if ( !Object.prototype.hasOwnProperty.call( this.deferreds, title ) ) { |
| 85 |
this.deferreds[ title ] = ve.createDeferred(); |
| 86 |
this.queue.push( title ); |
| 87 |
this.schedule(); |
| 88 |
} |
| 89 |
return this.deferreds[ title ].promise(); |
| 90 |
}; |
| 91 |
|
| 92 |
|
| 93 |
|
| 94 |
|
| 95 |
|
| 96 |
|
| 97 |
|
| 98 |
|
| 99 |
ve.init.mw.ApiResponseCache.prototype.getCached = function ( name ) { |
| 100 |
if ( Object.prototype.hasOwnProperty.call( this.cacheValues, name ) ) { |
| 101 |
return this.cacheValues[ name ]; |
| 102 |
} |
| 103 |
}; |
| 104 |
|
| 105 |
|
| 106 |
|
| 107 |
|
| 108 |
|
| 109 |
|
| 110 |
|
| 111 |
|
| 112 |
|
| 113 |
|
| 114 |
|
| 115 |
|
| 116 |
|
| 117 |
|
| 118 |
ve.init.mw.ApiResponseCache.prototype.set = function ( entries ) { |
| 119 |
for ( const name in entries ) { |
| 120 |
if ( !Object.prototype.hasOwnProperty.call( this.deferreds, name ) ) { |
| 121 |
this.deferreds[ name ] = ve.createDeferred(); |
| 122 |
} |
| 123 |
if ( this.deferreds[ name ].state() === 'pending' ) { |
| 124 |
this.deferreds[ name ].resolve( entries[ name ] ); |
| 125 |
this.cacheValues[ name ] = entries[ name ]; |
| 126 |
} |
| 127 |
} |
| 128 |
this.emit( 'add', Object.keys( entries ) ); |
| 129 |
}; |
| 130 |
|
| 131 |
|
| 132 |
|
| 133 |
|
| 134 |
|
| 135 |
|
| 136 |
|
| 137 |
|
| 138 |
ve.init.mw.ApiResponseCache.prototype.getRequestPromise = null; |
| 139 |
|
| 140 |
|
| 141 |
|
| 142 |
|
| 143 |
|
| 144 |
|
| 145 |
|
| 146 |
ve.init.mw.ApiResponseCache.prototype.processQueue = function () { |
| 147 |
const rejectSubqueue = ( rejectQueue ) => { |
| 148 |
for ( let i = 0, len = rejectQueue.length; i < len; i++ ) { |
| 149 |
this.deferreds[ rejectQueue[ i ] ].reject(); |
| 150 |
} |
| 151 |
}; |
| 152 |
|
| 153 |
const processResult = ( data ) => { |
| 154 |
const pages = ( data.query && data.query.pages ) || data.pages, |
| 155 |
processed = {}; |
| 156 |
|
| 157 |
const mappedTitles = []; |
| 158 |
[ 'redirects', 'normalized', 'converted' ].forEach( ( map ) => { |
| 159 |
ve.batchPush( mappedTitles, ( data.query && data.query[ map ] ) || [] ); |
| 160 |
} ); |
| 161 |
|
| 162 |
if ( pages ) { |
| 163 |
let page, processedPage; |
| 164 |
for ( const pageid in pages ) { |
| 165 |
page = pages[ pageid ]; |
| 166 |
processedPage = this.constructor.static.processPage( page ); |
| 167 |
if ( processedPage !== undefined ) { |
| 168 |
processed[ page.title ] = processedPage; |
| 169 |
} |
| 170 |
} |
| 171 |
for ( let i = 0; i < mappedTitles.length; i++ ) { |
| 172 |
|
| 173 |
if ( mappedTitles[ i ].to === page.title ) { |
| 174 |
const from = mappedTitles[ i ].fromencoded === '' ? |
| 175 |
decodeURIComponent( mappedTitles[ i ].from ) : |
| 176 |
mappedTitles[ i ].from; |
| 177 |
processed[ from ] = processedPage; |
| 178 |
break; |
| 179 |
} |
| 180 |
} |
| 181 |
this.set( processed ); |
| 182 |
} |
| 183 |
}; |
| 184 |
|
| 185 |
const queue = this.queue; |
| 186 |
this.queue = []; |
| 187 |
while ( queue.length ) { |
| 188 |
const subqueue = queue.splice( 0, 50 ).map( this.constructor.static.normalizeTitle ); |
| 189 |
this.getRequestPromise( subqueue ) |
| 190 |
.then( processResult ) |
| 191 |
|
| 192 |
|
| 193 |
|
| 194 |
|
| 195 |
.then( rejectSubqueue.bind( null, subqueue ) ); |
| 196 |
} |
| 197 |
}; |