| 1 |
const Model = require( './Model.js' ); |
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
|
| 6 |
|
| 7 |
|
| 8 |
|
| 9 |
|
| 10 |
|
| 11 |
|
| 12 |
|
| 13 |
|
| 14 |
|
| 15 |
|
| 16 |
|
| 17 |
|
| 18 |
function SourceHandler( config ) { |
| 19 |
config = config || {}; |
| 20 |
|
| 21 |
|
| 22 |
OO.EventEmitter.call( this ); |
| 23 |
|
| 24 |
this.apiCache = {}; |
| 25 |
this.templateSourceCodePromise = null; |
| 26 |
this.templateSourceCodeParams = []; |
| 27 |
|
| 28 |
|
| 29 |
this.setParentPage( config.parentPage ); |
| 30 |
this.setPageSubLevel( config.isPageSubLevel ); |
| 31 |
this.setFullPageName( config.fullPageName ); |
| 32 |
} |
| 33 |
|
| 34 |
|
| 35 |
|
| 36 |
OO.mixinClass( SourceHandler, OO.EventEmitter ); |
| 37 |
|
| 38 |
|
| 39 |
|
| 40 |
|
| 41 |
|
| 42 |
|
| 43 |
|
| 44 |
|
| 45 |
SourceHandler.prototype.getApi = function ( page, getTemplateData ) { |
| 46 |
const type = getTemplateData ? 'templatedata' : 'query', |
| 47 |
api = new mw.Api(), |
| 48 |
baseConfig = { |
| 49 |
action: type, |
| 50 |
titles: page, |
| 51 |
redirects: getTemplateData ? 1 : 0 |
| 52 |
}; |
| 53 |
|
| 54 |
let config; |
| 55 |
if ( type === 'query' ) { |
| 56 |
config = Object.assign( baseConfig, { |
| 57 |
prop: 'revisions', |
| 58 |
rvprop: 'content', |
| 59 |
indexpageids: '1' |
| 60 |
} ); |
| 61 |
} |
| 62 |
|
| 63 |
|
| 64 |
if ( !this.apiCache[ page ] || !this.apiCache[ page ][ type ] ) { |
| 65 |
this.apiCache[ page ] = this.apiCache[ page ] || {}; |
| 66 |
this.apiCache[ page ][ type ] = api.get( config ); |
| 67 |
} |
| 68 |
return this.apiCache[ page ][ type ]; |
| 69 |
}; |
| 70 |
|
| 71 |
|
| 72 |
|
| 73 |
|
| 74 |
|
| 75 |
|
| 76 |
|
| 77 |
|
| 78 |
SourceHandler.prototype.buildModel = function ( wikitext ) { |
| 79 |
const templateDataString = this.findModelInString( wikitext ); |
| 80 |
|
| 81 |
let tdObject = null; |
| 82 |
if ( templateDataString !== null ) { |
| 83 |
try { |
| 84 |
tdObject = JSON.parse( templateDataString ); |
| 85 |
} catch ( err ) { |
| 86 |
|
| 87 |
return $.Deferred().reject(); |
| 88 |
} |
| 89 |
} |
| 90 |
|
| 91 |
|
| 92 |
|
| 93 |
return this.getParametersFromTemplateSource( wikitext ) |
| 94 |
|
| 95 |
.then( ( templateSourceCodeParams ) => Model.static.newFromObject( |
| 96 |
tdObject, |
| 97 |
templateSourceCodeParams |
| 98 |
) ); |
| 99 |
}; |
| 100 |
|
| 101 |
|
| 102 |
|
| 103 |
|
| 104 |
|
| 105 |
|
| 106 |
|
| 107 |
|
| 108 |
|
| 109 |
|
| 110 |
|
| 111 |
|
| 112 |
|
| 113 |
|
| 114 |
|
| 115 |
|
| 116 |
SourceHandler.prototype.getParametersFromTemplateSource = function ( wikitext ) { |
| 117 |
let params = []; |
| 118 |
if ( !this.templateSourceCodePromise ) { |
| 119 |
|
| 120 |
if ( wikitext ) { |
| 121 |
params = this.extractParametersFromTemplateCode( wikitext ); |
| 122 |
} |
| 123 |
|
| 124 |
if ( params.length > 0 ) { |
| 125 |
|
| 126 |
this.templateSourceCodePromise = $.Deferred().resolve( params ); |
| 127 |
} else if ( this.isPageSubLevel() && this.getParentPage() ) { |
| 128 |
|
| 129 |
this.templateSourceCodePromise = this.getApi( this.getParentPage() ).then( |
| 130 |
( resp ) => { |
| 131 |
let pageContent = ''; |
| 132 |
|
| 133 |
|
| 134 |
|
| 135 |
|
| 136 |
if ( |
| 137 |
resp.query.pages[ resp.query.pageids[ 0 ] ].revisions && |
| 138 |
resp.query.pages[ resp.query.pageids[ 0 ] ].revisions[ 0 ] |
| 139 |
) { |
| 140 |
pageContent = resp.query.pages[ resp.query.pageids[ 0 ] ].revisions[ 0 ][ '*' ]; |
| 141 |
} |
| 142 |
return this.extractParametersFromTemplateCode( pageContent ); |
| 143 |
}, |
| 144 |
|
| 145 |
() => $.Deferred().resolve( [] ) |
| 146 |
|
| 147 |
); |
| 148 |
} else { |
| 149 |
|
| 150 |
this.templateSourceCodePromise = $.Deferred().resolve( [] ); |
| 151 |
} |
| 152 |
} |
| 153 |
|
| 154 |
return this.templateSourceCodePromise; |
| 155 |
}; |
| 156 |
|
| 157 |
|
| 158 |
|
| 159 |
|
| 160 |
|
| 161 |
|
| 162 |
|
| 163 |
|
| 164 |
|
| 165 |
SourceHandler.prototype.extractParametersFromTemplateCode = function ( templateCode ) { |
| 166 |
const paramNames = [], |
| 167 |
normalizedParamNames = [], |
| 168 |
|
| 169 |
paramExtractor = /{{{+([^\n#={|}]*?)([<|]|}}})/mg; |
| 170 |
|
| 171 |
|
| 172 |
templateCode = templateCode.replace( /<!--[\s\S]*?-->/g, '' ) |
| 173 |
.replace( /<nowiki\s*>[\s\S]*?<\/nowiki\s*>/g, '' ) |
| 174 |
.replace( /<pre\s*>[\s\S]*?<\/pre\s*>/g, '' ); |
| 175 |
|
| 176 |
let matches; |
| 177 |
while ( ( matches = paramExtractor.exec( templateCode ) ) !== null ) { |
| 178 |
|
| 179 |
const normalizedParamName = matches[ 1 ].replace( /[-_ ]+/, ' ' ).trim().toLowerCase(); |
| 180 |
if ( !normalizedParamName || normalizedParamNames.indexOf( normalizedParamName ) !== -1 ) { |
| 181 |
continue; |
| 182 |
} |
| 183 |
if ( paramNames.indexOf( matches[ 1 ] ) === -1 ) { |
| 184 |
normalizedParamNames.push( normalizedParamName ); |
| 185 |
paramNames.push( matches[ 1 ].trim() ); |
| 186 |
} |
| 187 |
} |
| 188 |
|
| 189 |
return paramNames; |
| 190 |
}; |
| 191 |
|
| 192 |
|
| 193 |
|
| 194 |
|
| 195 |
|
| 196 |
|
| 197 |
|
| 198 |
|
| 199 |
SourceHandler.prototype.findModelInString = function ( templateDataString ) { |
| 200 |
const parts = templateDataString.match( |
| 201 |
/<templatedata>([\s\S]*?)<\/templatedata>/i |
| 202 |
); |
| 203 |
|
| 204 |
if ( parts && parts[ 1 ] && parts[ 1 ].trim().length > 0 ) { |
| 205 |
return parts[ 1 ].trim(); |
| 206 |
} else { |
| 207 |
return null; |
| 208 |
} |
| 209 |
}; |
| 210 |
|
| 211 |
|
| 212 |
|
| 213 |
|
| 214 |
|
| 215 |
|
| 216 |
SourceHandler.prototype.setPageSubLevel = function ( isSubLevel ) { |
| 217 |
this.subLevel = !!isSubLevel; |
| 218 |
}; |
| 219 |
|
| 220 |
|
| 221 |
|
| 222 |
|
| 223 |
|
| 224 |
|
| 225 |
SourceHandler.prototype.isPageSubLevel = function () { |
| 226 |
return this.subLevel; |
| 227 |
}; |
| 228 |
|
| 229 |
|
| 230 |
|
| 231 |
|
| 232 |
|
| 233 |
|
| 234 |
SourceHandler.prototype.setFullPageName = function ( pageName ) { |
| 235 |
this.fullPageName = pageName || ''; |
| 236 |
}; |
| 237 |
|
| 238 |
|
| 239 |
|
| 240 |
|
| 241 |
|
| 242 |
|
| 243 |
SourceHandler.prototype.getFullPageName = function () { |
| 244 |
return this.fullPageName; |
| 245 |
}; |
| 246 |
|
| 247 |
|
| 248 |
|
| 249 |
|
| 250 |
|
| 251 |
|
| 252 |
SourceHandler.prototype.setParentPage = function ( parent ) { |
| 253 |
this.parentPage = parent || ''; |
| 254 |
}; |
| 255 |
|
| 256 |
|
| 257 |
|
| 258 |
|
| 259 |
|
| 260 |
|
| 261 |
SourceHandler.prototype.getParentPage = function () { |
| 262 |
return this.parentPage; |
| 263 |
}; |
| 264 |
|
| 265 |
|
| 266 |
|
| 267 |
|
| 268 |
|
| 269 |
|
| 270 |
SourceHandler.prototype.setTemplateSourceCodeParams = function ( params ) { |
| 271 |
this.templateSourceCodeParams = params; |
| 272 |
}; |
| 273 |
|
| 274 |
|
| 275 |
|
| 276 |
|
| 277 |
|
| 278 |
|
| 279 |
SourceHandler.prototype.getTemplateSourceCodeParams = function () { |
| 280 |
return this.templateSourceCodeParams; |
| 281 |
}; |
| 282 |
|
| 283 |
module.exports = SourceHandler; |