Merge "Remove register_globals and magic_quotes_* checks"
[lhc/web/wiklou.git] / resources / src / mediawiki / mediawiki.ForeignStructuredUpload.BookletLayout.js
1 /*global moment */
2 ( function ( $, mw ) {
3
4 /**
5 * mw.ForeignStructuredUpload.BookletLayout encapsulates the process
6 * of uploading a file to MediaWiki using the mw.ForeignStructuredUpload model.
7 *
8 * var uploadDialog = new mw.Upload.Dialog( {
9 * bookletClass: mw.ForeignStructuredUpload.BookletLayout,
10 * booklet: {
11 * target: 'local'
12 * }
13 * } );
14 * var windowManager = new OO.ui.WindowManager();
15 * $( 'body' ).append( windowManager.$element );
16 * windowManager.addWindows( [ uploadDialog ] );
17 *
18 * @class mw.ForeignStructuredUpload.BookletLayout
19 * @uses mw.ForeignStructuredUpload
20 * @extends mw.Upload.BookletLayout
21 * @cfg {string} [target] Used to choose the target repository.
22 * If nothing is passed, the {@link mw.ForeignUpload#property-target default} is used.
23 */
24 mw.ForeignStructuredUpload.BookletLayout = function ( config ) {
25 config = config || {};
26 // Parent constructor
27 mw.ForeignStructuredUpload.BookletLayout.parent.call( this, config );
28
29 this.target = config.target;
30 };
31
32 /* Setup */
33
34 OO.inheritClass( mw.ForeignStructuredUpload.BookletLayout, mw.Upload.BookletLayout );
35
36 /* Uploading */
37
38 /**
39 * @inheritdoc
40 */
41 mw.ForeignStructuredUpload.BookletLayout.prototype.initialize = function () {
42 var booklet = this;
43 return mw.ForeignStructuredUpload.BookletLayout.parent.prototype.initialize.call( this ).then(
44 function () {
45 // Point the CategorySelector to the right wiki
46 return booklet.upload.getApi().then(
47 function ( api ) {
48 // If this is a ForeignApi, it will have a apiUrl, otherwise we don't need to do anything
49 if ( api.apiUrl ) {
50 // Can't reuse the same object, CategorySelector calls #abort on its mw.Api instance
51 booklet.categoriesWidget.api = new mw.ForeignApi( api.apiUrl );
52 }
53 return $.Deferred().resolve();
54 },
55 function () {
56 return $.Deferred().resolve();
57 }
58 );
59 },
60 function () {
61 return $.Deferred().resolve();
62 }
63 );
64 };
65
66 /**
67 * Returns a {@link mw.ForeignStructuredUpload mw.ForeignStructuredUpload}
68 * with the {@link #cfg-target target} specified in config.
69 *
70 * @protected
71 * @return {mw.Upload}
72 */
73 mw.ForeignStructuredUpload.BookletLayout.prototype.createUpload = function () {
74 return new mw.ForeignStructuredUpload( this.target );
75 };
76
77 /* Form renderers */
78
79 /**
80 * @inheritdoc
81 */
82 mw.ForeignStructuredUpload.BookletLayout.prototype.renderUploadForm = function () {
83 var fieldset, $ownWorkMessage, $notOwnWorkMessage,
84 ownWorkMessage, notOwnWorkMessage, notOwnWorkLocal,
85 validTargets = mw.config.get( 'wgForeignUploadTargets' ),
86 target = this.target || validTargets[ 0 ] || 'local',
87 layout = this;
88
89 // foreign-structured-upload-form-label-own-work-message-local
90 // foreign-structured-upload-form-label-own-work-message-shared
91 ownWorkMessage = mw.message( 'foreign-structured-upload-form-label-own-work-message-' + target );
92 // foreign-structured-upload-form-label-not-own-work-message-local
93 // foreign-structured-upload-form-label-not-own-work-message-shared
94 notOwnWorkMessage = mw.message( 'foreign-structured-upload-form-label-not-own-work-message-' + target );
95 // foreign-structured-upload-form-label-not-own-work-local-local
96 // foreign-structured-upload-form-label-not-own-work-local-shared
97 notOwnWorkLocal = mw.message( 'foreign-structured-upload-form-label-not-own-work-local-' + target );
98
99 if ( !ownWorkMessage.exists() ) {
100 ownWorkMessage = mw.message( 'foreign-structured-upload-form-label-own-work-message-default' );
101 }
102 if ( !notOwnWorkMessage.exists() ) {
103 notOwnWorkMessage = mw.message( 'foreign-structured-upload-form-label-not-own-work-message-default' );
104 }
105 if ( !notOwnWorkLocal.exists() ) {
106 notOwnWorkLocal = mw.message( 'foreign-structured-upload-form-label-not-own-work-local-default' );
107 }
108
109 $ownWorkMessage = $( '<p>' ).append( ownWorkMessage.parseDom() )
110 .addClass( 'mw-foreignStructuredUpload-bookletLayout-license' );
111 $notOwnWorkMessage = $( '<div>' ).append(
112 $( '<p>' ).append( notOwnWorkMessage.parseDom() ),
113 $( '<p>' ).append( notOwnWorkLocal.parseDom() )
114 );
115 $ownWorkMessage.add( $notOwnWorkMessage ).find( 'a' ).attr( 'target', '_blank' );
116
117 this.selectFileWidget = new OO.ui.SelectFileWidget();
118 this.messageLabel = new OO.ui.LabelWidget( {
119 label: $notOwnWorkMessage
120 } );
121 this.ownWorkCheckbox = new OO.ui.CheckboxInputWidget().on( 'change', function ( on ) {
122 layout.messageLabel.toggle( !on );
123 } );
124
125 fieldset = new OO.ui.FieldsetLayout();
126 fieldset.addItems( [
127 new OO.ui.FieldLayout( this.selectFileWidget, {
128 align: 'top',
129 label: mw.msg( 'upload-form-label-select-file' )
130 } ),
131 new OO.ui.FieldLayout( this.ownWorkCheckbox, {
132 align: 'inline',
133 label: $( '<div>' ).append(
134 $( '<p>' ).text( mw.msg( 'foreign-structured-upload-form-label-own-work' ) ),
135 $ownWorkMessage
136 )
137 } ),
138 new OO.ui.FieldLayout( this.messageLabel, {
139 align: 'top'
140 } )
141 ] );
142 this.uploadForm = new OO.ui.FormLayout( { items: [ fieldset ] } );
143
144 // Validation
145 this.selectFileWidget.on( 'change', this.onUploadFormChange.bind( this ) );
146 this.ownWorkCheckbox.on( 'change', this.onUploadFormChange.bind( this ) );
147
148 this.selectFileWidget.on( 'change', function () {
149 var file = layout.getFile();
150
151 // Set the date to lastModified once we have the file
152 if ( layout.getDateFromLastModified( file ) !== undefined ) {
153 layout.dateWidget.setValue( layout.getDateFromLastModified( file ) );
154 }
155
156 // Check if we have EXIF data and set to that where available
157 layout.getDateFromExif( file ).done( function ( date ) {
158 layout.dateWidget.setValue( date );
159 } );
160 } );
161
162 return this.uploadForm;
163 };
164
165 /**
166 * @inheritdoc
167 */
168 mw.ForeignStructuredUpload.BookletLayout.prototype.onUploadFormChange = function () {
169 var file = this.selectFileWidget.getValue(),
170 ownWork = this.ownWorkCheckbox.isSelected(),
171 valid = !!file && ownWork;
172 this.emit( 'uploadValid', valid );
173 };
174
175 /**
176 * @inheritdoc
177 */
178 mw.ForeignStructuredUpload.BookletLayout.prototype.renderInfoForm = function () {
179 var fieldset;
180
181 this.filenameWidget = new OO.ui.TextInputWidget( {
182 required: true,
183 validate: /.+/
184 } );
185 this.descriptionWidget = new OO.ui.TextInputWidget( {
186 required: true,
187 validate: /\S+/,
188 multiline: true,
189 autosize: true
190 } );
191 this.categoriesWidget = new mw.widgets.CategorySelector( {
192 // Can't be done here because we don't know the target wiki yet... done in #initialize.
193 // api: new mw.ForeignApi( ... ),
194 $overlay: this.$overlay
195 } );
196 this.dateWidget = new mw.widgets.DateInputWidget( {
197 $overlay: this.$overlay,
198 required: true,
199 mustBeBefore: moment().add( 1, 'day' ).locale( 'en' ).format( 'YYYY-MM-DD' ) // Tomorrow
200 } );
201
202 fieldset = new OO.ui.FieldsetLayout( {
203 label: mw.msg( 'upload-form-label-infoform-title' )
204 } );
205 fieldset.addItems( [
206 new OO.ui.FieldLayout( this.filenameWidget, {
207 label: mw.msg( 'upload-form-label-infoform-name' ),
208 align: 'top',
209 help: mw.msg( 'upload-form-label-infoform-name-tooltip' )
210 } ),
211 new OO.ui.FieldLayout( this.descriptionWidget, {
212 label: mw.msg( 'upload-form-label-infoform-description' ),
213 align: 'top',
214 help: mw.msg( 'upload-form-label-infoform-description-tooltip' )
215 } ),
216 new OO.ui.FieldLayout( this.categoriesWidget, {
217 label: mw.msg( 'foreign-structured-upload-form-label-infoform-categories' ),
218 align: 'top'
219 } ),
220 new OO.ui.FieldLayout( this.dateWidget, {
221 label: mw.msg( 'foreign-structured-upload-form-label-infoform-date' ),
222 align: 'top'
223 } )
224 ] );
225 this.infoForm = new OO.ui.FormLayout( { items: [ fieldset ] } );
226
227 // Validation
228 this.filenameWidget.on( 'change', this.onInfoFormChange.bind( this ) );
229 this.descriptionWidget.on( 'change', this.onInfoFormChange.bind( this ) );
230 this.dateWidget.on( 'change', this.onInfoFormChange.bind( this ) );
231
232 return this.infoForm;
233 };
234
235 /**
236 * @inheritdoc
237 */
238 mw.ForeignStructuredUpload.BookletLayout.prototype.onInfoFormChange = function () {
239 var layout = this;
240 $.when(
241 this.filenameWidget.getValidity(),
242 this.descriptionWidget.getValidity(),
243 this.dateWidget.getValidity()
244 ).done( function () {
245 layout.emit( 'infoValid', true );
246 } ).fail( function () {
247 layout.emit( 'infoValid', false );
248 } );
249 };
250
251 /* Getters */
252
253 /**
254 * @inheritdoc
255 */
256 mw.ForeignStructuredUpload.BookletLayout.prototype.getText = function () {
257 var language = mw.config.get( 'wgContentLanguage' );
258 this.upload.clearDescriptions();
259 this.upload.addDescription( language, this.descriptionWidget.getValue() );
260 this.upload.setDate( this.dateWidget.getValue() );
261 this.upload.clearCategories();
262 this.upload.addCategories( this.categoriesWidget.getItemsData() );
263 return this.upload.getText();
264 };
265
266 /**
267 * Get original date from EXIF data
268 *
269 * @param {Object} file
270 * @return {jQuery.Promise} Promise resolved with the EXIF date
271 */
272 mw.ForeignStructuredUpload.BookletLayout.prototype.getDateFromExif = function ( file ) {
273 var fileReader,
274 deferred = $.Deferred();
275
276 if ( file && file.type === 'image/jpeg' ) {
277 fileReader = new FileReader();
278 fileReader.onload = function () {
279 var fileStr, arr, i, metadata;
280
281 if ( typeof fileReader.result === 'string' ) {
282 fileStr = fileReader.result;
283 } else {
284 // Array buffer; convert to binary string for the library.
285 arr = new Uint8Array( fileReader.result );
286 fileStr = '';
287 for ( i = 0; i < arr.byteLength; i++ ) {
288 fileStr += String.fromCharCode( arr[ i ] );
289 }
290 }
291
292 try {
293 metadata = mw.libs.jpegmeta( this.result, file.name );
294 } catch ( e ) {
295 metadata = null;
296 }
297
298 if ( metadata !== null && metadata.exif !== undefined && metadata.exif.DateTimeOriginal ) {
299 deferred.resolve( moment( metadata.exif.DateTimeOriginal, 'YYYY:MM:DD' ).format( 'YYYY-MM-DD' ) );
300 } else {
301 deferred.reject();
302 }
303 };
304
305 if ( 'readAsBinaryString' in fileReader ) {
306 fileReader.readAsBinaryString( file );
307 } else if ( 'readAsArrayBuffer' in fileReader ) {
308 fileReader.readAsArrayBuffer( file );
309 } else {
310 // We should never get here
311 deferred.reject();
312 throw new Error( 'Cannot read thumbnail as binary string or array buffer.' );
313 }
314 }
315
316 return deferred.promise();
317 };
318
319 /**
320 * Get last modified date from file
321 *
322 * @param {Object} file
323 * @return {Object} Last modified date from file
324 */
325 mw.ForeignStructuredUpload.BookletLayout.prototype.getDateFromLastModified = function ( file ) {
326 if ( file && file.lastModified ) {
327 return moment( file.lastModified ).format( 'YYYY-MM-DD' );
328 }
329 };
330
331 /* Setters */
332
333 /**
334 * @inheritdoc
335 */
336 mw.ForeignStructuredUpload.BookletLayout.prototype.clear = function () {
337 mw.ForeignStructuredUpload.BookletLayout.parent.prototype.clear.call( this );
338
339 this.ownWorkCheckbox.setSelected( false );
340 this.categoriesWidget.setItemsFromData( [] );
341 this.dateWidget.setValue( '' ).setValidityFlag( true );
342 };
343
344 }( jQuery, mediaWiki ) );