| 1 |
( function () { |
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
|
| 6 |
|
| 7 |
|
| 8 |
|
| 9 |
|
| 10 |
|
| 11 |
|
| 12 |
|
| 13 |
|
| 14 |
|
| 15 |
|
| 16 |
|
| 17 |
|
| 18 |
|
| 19 |
|
| 20 |
|
| 21 |
|
| 22 |
function ForeignStructuredUpload( target, apiconfig ) { |
| 23 |
this.date = undefined; |
| 24 |
this.descriptions = []; |
| 25 |
this.categories = []; |
| 26 |
|
| 27 |
|
| 28 |
|
| 29 |
this.config = require( './config.json' ).UploadDialog; |
| 30 |
|
| 31 |
mw.ForeignUpload.call( this, target, apiconfig ); |
| 32 |
} |
| 33 |
|
| 34 |
OO.inheritClass( ForeignStructuredUpload, mw.ForeignUpload ); |
| 35 |
|
| 36 |
|
| 37 |
|
| 38 |
|
| 39 |
|
| 40 |
|
| 41 |
|
| 42 |
ForeignStructuredUpload.prototype.loadConfig = function () { |
| 43 |
const upload = this; |
| 44 |
|
| 45 |
if ( this.configPromise ) { |
| 46 |
return this.configPromise; |
| 47 |
} |
| 48 |
|
| 49 |
if ( this.target === 'local' ) { |
| 50 |
const deferred = $.Deferred(); |
| 51 |
setTimeout( () => { |
| 52 |
|
| 53 |
|
| 54 |
deferred.resolve( upload.config ); |
| 55 |
} ); |
| 56 |
this.configPromise = deferred.promise(); |
| 57 |
} else { |
| 58 |
this.configPromise = this.apiPromise.then( |
| 59 |
|
| 60 |
( api ) => api.get( { |
| 61 |
action: 'query', |
| 62 |
meta: 'siteinfo', |
| 63 |
siprop: 'uploaddialog', |
| 64 |
|
| 65 |
formatversion: 2 |
| 66 |
} ).then( ( resp ) => { |
| 67 |
|
| 68 |
if ( resp.query && resp.query.uploaddialog ) { |
| 69 |
upload.config = resp.query.uploaddialog; |
| 70 |
return upload.config; |
| 71 |
} else { |
| 72 |
return $.Deferred().reject( 'upload-foreign-cant-load-config' ); |
| 73 |
} |
| 74 |
}, () => $.Deferred().reject( 'upload-foreign-cant-load-config' ) ) |
| 75 |
); |
| 76 |
} |
| 77 |
|
| 78 |
return this.configPromise; |
| 79 |
}; |
| 80 |
|
| 81 |
|
| 82 |
|
| 83 |
|
| 84 |
|
| 85 |
|
| 86 |
ForeignStructuredUpload.prototype.addCategories = function ( categories ) { |
| 87 |
this.categories.push( ...categories ); |
| 88 |
}; |
| 89 |
|
| 90 |
|
| 91 |
|
| 92 |
|
| 93 |
ForeignStructuredUpload.prototype.clearCategories = function () { |
| 94 |
this.categories = []; |
| 95 |
}; |
| 96 |
|
| 97 |
|
| 98 |
|
| 99 |
|
| 100 |
|
| 101 |
|
| 102 |
|
| 103 |
ForeignStructuredUpload.prototype.addDescription = function ( language, description ) { |
| 104 |
this.descriptions.push( { |
| 105 |
language: language, |
| 106 |
text: description |
| 107 |
} ); |
| 108 |
}; |
| 109 |
|
| 110 |
|
| 111 |
|
| 112 |
|
| 113 |
ForeignStructuredUpload.prototype.clearDescriptions = function () { |
| 114 |
this.descriptions = []; |
| 115 |
}; |
| 116 |
|
| 117 |
|
| 118 |
|
| 119 |
|
| 120 |
|
| 121 |
|
| 122 |
ForeignStructuredUpload.prototype.setDate = function ( date ) { |
| 123 |
this.date = date; |
| 124 |
}; |
| 125 |
|
| 126 |
|
| 127 |
|
| 128 |
|
| 129 |
|
| 130 |
|
| 131 |
|
| 132 |
ForeignStructuredUpload.prototype.getText = function () { |
| 133 |
return this.config.format.filepage |
| 134 |
|
| 135 |
.replace( '$DESCRIPTION', this.getDescriptions() ) |
| 136 |
.replace( '$DATE', this.getDate() ) |
| 137 |
.replace( '$SOURCE', this.getSource() ) |
| 138 |
.replace( '$AUTHOR', this.getUser() ) |
| 139 |
.replace( '$LICENSE', this.getLicense() ) |
| 140 |
.replace( '$CATEGORIES', this.getCategories() ); |
| 141 |
}; |
| 142 |
|
| 143 |
|
| 144 |
|
| 145 |
|
| 146 |
ForeignStructuredUpload.prototype.getComment = function () { |
| 147 |
const |
| 148 |
isLocal = this.target === 'local', |
| 149 |
comment = typeof this.config.comment === 'string' ? |
| 150 |
this.config.comment : |
| 151 |
this.config.comment[ isLocal ? 'local' : 'foreign' ], |
| 152 |
pagename = mw.config.get( 'wgPageName' ); |
| 153 |
return comment |
| 154 |
.replace( '$PAGENAME', pagename.replace( /_/g, ' ' ) ) |
| 155 |
.replace( '$HOST', location.host ); |
| 156 |
}; |
| 157 |
|
| 158 |
|
| 159 |
|
| 160 |
|
| 161 |
|
| 162 |
|
| 163 |
|
| 164 |
ForeignStructuredUpload.prototype.getDate = function () { |
| 165 |
if ( !this.date ) { |
| 166 |
return ''; |
| 167 |
} |
| 168 |
|
| 169 |
return this.date.toString(); |
| 170 |
}; |
| 171 |
|
| 172 |
|
| 173 |
|
| 174 |
|
| 175 |
|
| 176 |
|
| 177 |
|
| 178 |
|
| 179 |
ForeignStructuredUpload.prototype.getDescriptions = function () { |
| 180 |
const upload = this; |
| 181 |
return this.descriptions.map( ( desc ) => upload.config.format.description |
| 182 |
.replace( '$LANGUAGE', desc.language ) |
| 183 |
.replace( '$TEXT', desc.text ) ).join( '\n' ); |
| 184 |
}; |
| 185 |
|
| 186 |
|
| 187 |
|
| 188 |
|
| 189 |
|
| 190 |
|
| 191 |
|
| 192 |
|
| 193 |
ForeignStructuredUpload.prototype.getCategories = function () { |
| 194 |
if ( this.categories.length === 0 ) { |
| 195 |
return this.config.format.uncategorized; |
| 196 |
} |
| 197 |
|
| 198 |
return this.categories.map( ( cat ) => '[[Category:' + cat + ']]' ).join( '\n' ); |
| 199 |
}; |
| 200 |
|
| 201 |
|
| 202 |
|
| 203 |
|
| 204 |
|
| 205 |
|
| 206 |
|
| 207 |
ForeignStructuredUpload.prototype.getLicense = function () { |
| 208 |
return this.config.format.license; |
| 209 |
}; |
| 210 |
|
| 211 |
|
| 212 |
|
| 213 |
|
| 214 |
|
| 215 |
|
| 216 |
|
| 217 |
ForeignStructuredUpload.prototype.getSource = function () { |
| 218 |
return this.config.format.ownwork; |
| 219 |
}; |
| 220 |
|
| 221 |
|
| 222 |
|
| 223 |
|
| 224 |
|
| 225 |
|
| 226 |
|
| 227 |
ForeignStructuredUpload.prototype.getUser = function () { |
| 228 |
|
| 229 |
const namespace = 'User'; |
| 230 |
let username = mw.config.get( 'wgUserName' ); |
| 231 |
if ( !username ) { |
| 232 |
|
| 233 |
|
| 234 |
|
| 235 |
|
| 236 |
username = '{{subst:REVISIONUSER}}'; |
| 237 |
} |
| 238 |
return '[[' + namespace + ':' + username + '|' + username + ']]'; |
| 239 |
}; |
| 240 |
|
| 241 |
mw.ForeignStructuredUpload = ForeignStructuredUpload; |
| 242 |
}() ); |