-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathproveit.js
executable file
·1582 lines (1419 loc) · 49.4 KB
/
proveit.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/**
* ProveIt is a reference manager for Wikipedia and any other MediaWiki wiki
* Documentation at https://www.mediawiki.org/wiki/ProveIt
*
* Copyright 2008-2011 Georgia Tech Research Corporation, Atlanta, GA 30332-0415, ALL RIGHTS RESERVED
* Copyright 2011- Matthew Flaschen
* Rewritten, internationalized, improved and maintained by Sophivorus since 2014
*
* ProveIt is available under the GNU Free Documentation License (http://www.gnu.org/copyleft/fdl.html),
* the Creative Commons Attribution/Share-Alike License 3.0 (http://creativecommons.org/licenses/by-sa/3.0/)
* and the GNU General Public License 2 (http://www.gnu.org/licenses/gpl-2.0.html)
*
* <nowiki>
*/
window.ProveIt = {
/**
* Template data of the templates
* Populated on ProveIt.realInit()
*
* @type {Object} Map from template name to template data
*/
templateData: {},
/**
* Initialization script
*/
init: function () {
// Remove any previous instance
$( '#proveit' ).remove();
// Only continue on wikitext pages
var contentModel = mw.config.get( 'wgPageContentModel' );
if ( contentModel !== 'wikitext' ) {
return;
}
// Only continue on supported namespaces
var namespace = mw.config.get( 'wgNamespaceNumber' ),
namespaces = mw.config.get( 'proveit-namespaces' );
if ( namespaces && namespaces.indexOf( namespace ) === -1 ) {
return;
}
// Only continue on wikitext editors
if ( ProveIt.getEditor() === 'visualeditor' ) {
return;
}
// Add the basic GUI
ProveIt.buildGUI();
// Remove ProveIt when switching out from the source editor
mw.hook( 've.deactivationComplete' ).add( function () {
$( '#proveit' ).remove();
} );
// When previewing, re-add the ProveIt tag (T154357)
if ( mw.config.get( 'wgAction' ) === 'submit' ) {
var currentSummary = $( '#wpSummary' ).val(),
proveitSummary = mw.config.get( 'proveit-summary' );
if ( proveitSummary && currentSummary.indexOf( proveitSummary ) > -1 ) {
ProveIt.addTag();
}
}
},
/**
* Build the basic GUI and add it to the DOM
*/
buildGUI: function () {
// Define the basic elements
var $gui = $( '<div>' ).attr( 'id', 'proveit' ).addClass( 'notheme' ),
$header = $( '<div>' ).attr( 'id', 'proveit-header' ),
$body = $( '<div>' ).attr( 'id', 'proveit-body' ),
$footer = $( '<div>' ).attr( 'id', 'proveit-footer' ),
$logo = $( '<span>' ).attr( 'id', 'proveit-logo' ),
$logoText = $( '<span>' ).attr( 'id', 'proveit-logo-text' ).text( 'P' ),
$logoLeftBracket = $( '<span>' ).addClass( 'proveit-logo-bracket' ).text( '[' ),
$logoRightBracket = $( '<span>' ).addClass( 'proveit-logo-bracket' ).text( ']' );
// Put everything together and add it to the DOM
$logo.append( $logoLeftBracket, $logoText, $logoRightBracket );
$header.append( $logo );
$gui.append( $header, $body, $footer );
$( 'body' ).append( $gui );
// Make the GUI draggable
$gui.draggable( {
handle: $header,
containment: 'window',
start: function () {
$gui.css( { right: 'auto', bottom: 'auto' } );
}
} );
// Toggle the GUI when the logo is clicked
var minimized = true;
$logo.on( 'click', function () {
if ( minimized ) {
minimized = false;
$( '#proveit-logo-text' ).text( 'ProveIt' );
$( '#proveit-header button, #proveit-body, #proveit-footer' ).show();
if ( $.isEmptyObject( ProveIt.templateData ) ) {
ProveIt.realInit();
} else if ( $( '#proveit-list' ).length ) {
ProveIt.buildList(); // Make sure the list is updated
}
} else {
minimized = true;
$( '#proveit-logo-text' ).text( 'P' );
$( '#proveit-header button, #proveit-body, #proveit-footer' ).hide();
}
$gui.css( { top: 'auto', left: 'auto', right: 0, bottom: 0 } ); // Reset the position of the gadget
} );
},
/**
* Get the template data, redirects and interface messages, then build the reference list
*/
realInit: function () {
$( '#proveit-logo-text' ).text( '.' ); // Start loading
// Get the list of template names and prepend the namespace
var templates = mw.config.get( 'proveit-templates' ) ? mw.config.get( 'proveit-templates' ) : [],
formattedNamespaces = mw.config.get( 'wgFormattedNamespaces' ),
templateNamespace = formattedNamespaces[ 10 ],
titles = [];
templates.forEach( function ( templateName ) {
titles.push( templateNamespace + ':' + templateName );
} );
// Get the template data
var api = new mw.Api();
api.get( {
titles: titles.join( '|' ),
action: 'templatedata',
redirects: true,
includeMissingTitles: true,
format: 'json',
formatversion: 2
} ).done( function ( data ) {
$( '#proveit-logo-text' ).text( '..' ); // Still loading
// Extract and set the template data
var templateData, templateTitle, templateName;
for ( var id in data.pages ) {
templateData = data.pages[ id ];
if ( 'missing' in templateData ) {
continue;
}
templateTitle = templateData.title;
templateName = templateTitle.substring( templateTitle.indexOf( ':' ) + 1 ); // Remove the namespace
ProveIt.templateData[ templateName ] = templateData;
}
// Get all the redirects to the citaton templates
api.get( {
titles: titles.join( '|' ),
action: 'query',
prop: 'redirects',
rdlimit: 'max',
rdnamespace: 10,
format: 'json',
formatversion: 2
} ).done( function ( data ) {
$( '#proveit-logo-text' ).text( '...' ); // Still loading
// Map the redirects to the cannonical names
var redirects, redirectTitle, redirectName;
data.query.pages.forEach( function ( templateData ) {
templateTitle = templateData.title;
templateName = templateTitle.substring( templateTitle.indexOf( ':' ) + 1 ); // Remove the namespace
if ( 'redirects' in templateData ) {
redirects = templateData.redirects;
redirects.forEach( function ( redirect ) {
redirectTitle = redirect.title;
redirectName = redirectTitle.substring( redirectTitle.indexOf( ':' ) + 1 ); // Remove the namespace
ProveIt.templateData[ redirectName ] = templateName;
} );
}
} );
// Get the latest English messages
$.get( '//gerrit.wikimedia.org/r/plugins/gitiles/mediawiki/gadgets/ProveIt/+/master/i18n/en.json?format=text', function ( data ) {
var englishMessages = JSON.parse( ProveIt.decodeBase64( data ) );
delete englishMessages[ '@metadata' ];
// Get the latest translations to the preferred user language
var userLanguage = mw.config.get( 'wgUserLanguage' );
$.get( '//gerrit.wikimedia.org/r/plugins/gitiles/mediawiki/gadgets/ProveIt/+/master/i18n/' + userLanguage + '.json?format=text' ).always( function ( data, status ) {
$( '#proveit-logo-text' ).text( 'ProveIt' ); // Finish loading
var translatedMessages = {};
if ( status === 'success' ) {
translatedMessages = JSON.parse( ProveIt.decodeBase64( data ) );
delete translatedMessages[ '@metadata' ];
}
// Merge and set the messages
var messages = $.extend( {}, englishMessages, translatedMessages );
mw.messages.set( messages );
// Finally, build the list
ProveIt.buildList();
} );
} );
} );
} );
},
/**
* Build the reference list and add it to the GUI
*/
buildList: function () {
var $list = $( '<ol>' ).attr( 'id', 'proveit-list' ),
$item, $span, $link;
// Build a list item for each reference
var wikitext = ProveIt.getWikitext(),
references = ProveIt.getReferences( wikitext );
references.forEach( function ( reference, index ) {
$item = $( '<li>' ).addClass( 'proveit-item' );
$item.on( 'click', reference, function ( event ) {
var reference = event.data;
ProveIt.highlight( reference );
ProveIt.buildForm( reference );
} );
// Add the number
$span = $( '<span>' ).addClass( 'proveit-number' ).text( index + 1 );
$item.append( $span );
// Add the arrow and letters
$link = $( '<a>' ).addClass( 'proveit-arrow' ).text( '↑' );
$link.on( 'click', reference, ProveIt.highlight );
$item.append( $link );
// Add the letters
if ( reference.citations.length ) {
$link = $( '<a>' ).addClass( 'proveit-letter' ).text( 'a' );
$link.on( 'click', reference, ProveIt.highlight );
$item.append( $link );
reference.citations.forEach( function ( citation, i ) {
var letter = String.fromCharCode( 98 + i ); // 97 is the ASCII code for 'b'
$link = $( '<a>' ).addClass( 'proveit-letter' ).text( letter );
$link.on( 'click', citation, ProveIt.highlight );
$item.append( $link );
} );
}
// Add the reference template, if any
if ( reference.template.name ) {
$span = $( '<span>' ).addClass( 'proveit-template' ).text( reference.template.name );
$item.append( $span );
}
// Add the reference snippet
$item.append( reference.snippet );
// Add to the list
$list.append( $item );
} );
// Build a list item for each template
// First remove the references from the wikitext to avoid matching the templates again
references.forEach( function ( reference ) {
wikitext = wikitext.replace( reference.wikitext, '' );
} );
var templates = ProveIt.getTemplates( wikitext );
templates.forEach( function ( template ) {
$item = $( '<li>' ).addClass( 'proveit-item' );
$item.on( 'click', template, function ( event ) {
var template = event.data;
ProveIt.highlight( template );
ProveIt.buildForm( template );
} );
$link = $( '<a>' ).addClass( 'proveit-arrow' ).text( '↓' );
$link.on( 'click', template, ProveIt.highlight );
$item.append( $link );
// Add the template name
$span = $( '<span>' ).addClass( 'proveit-template' ).text( template.name );
$item.append( $span );
// Add the template snippet
$item.append( template.snippet );
// Add to the list
$list.append( $item );
} );
if ( references.length || templates.length ) {
// Add the list to the GUI and make sure we're at the top
$( '#proveit-body' ).html( $list ).scrollTop( 0 );
} else {
var $div = $( '<div>' ).attr( 'id', 'proveit-no-references-message' ).text( mw.message( 'proveit-no-references' ) );
$( '#proveit-body' ).html( $div );
}
// Build the footer
var $footer = $( '#proveit-footer' );
$footer.empty();
if ( references.length || templates.length ) {
var $normalizeButton = $( '<button>' ).attr( 'id', 'proveit-normalize-button' ).text( mw.message( 'proveit-normalize-button' ) );
$footer.append( $normalizeButton );
$normalizeButton.on( 'click', function () {
var warning = mw.config.get( 'proveit-normalize-warning' );
if ( warning ) {
var $warning = $( '<div>' + warning + '</div>' );
OO.ui.confirm( $warning ).done( function ( confirm ) {
if ( confirm ) {
$( this ).remove();
ProveIt.normalizeAll( references, templates );
}
} );
} else {
$( this ).remove();
ProveIt.normalizeAll( references, templates );
}
} );
var $filterReferences = $( '<input>' ).attr( 'placeholder', mw.message( 'proveit-filter-references' ) );
$footer.prepend( $filterReferences );
$filterReferences.on( 'keyup', function () {
var filter = $( this ).val().toLowerCase();
$( 'li', $list ).show().filter( function () {
return $( this ).text().toLowerCase().indexOf( filter ) === -1;
} ).hide();
} );
}
// Build the header
var $header = $( '#proveit-header' ),
$addReferenceButton = $( '<button>' ).text( mw.message( 'proveit-add-reference-button' ) ).addClass( 'progressive' ),
$addBibliographyButton = $( '<button>' ).text( mw.message( 'proveit-add-bibliography-button' ) );
$( 'button', $header ).remove();
$header.prepend( $addReferenceButton, $addBibliographyButton );
// Bind events
$addReferenceButton.on( 'click', function () {
var templateName = mw.cookie.get( 'proveit-last-template' ), // Remember the last choice
wikitext = templateName ? '<ref>{{' + templateName + '}}</ref>' : '<ref></ref>',
reference = new ProveIt.Reference( wikitext );
ProveIt.buildForm( reference );
} );
$addBibliographyButton.on( 'click', function () {
var templateName = mw.cookie.get( 'proveit-last-template' ), // Remember the last choice
wikitext = templateName ? '{{' + templateName + '}}' : '',
template = new ProveIt.Template( wikitext );
ProveIt.buildForm( template );
} );
},
/**
* Normalize all references
*
* @param {ProveIt.Reference[]} references Array of Reference objects
* @param {ProveIt.Template[]} templates Array of Template objects
*/
normalizeAll: function ( references, templates ) {
mw.notify( mw.message( 'proveit-normalize-message' ) );
setTimeout( function () {
references.forEach( function ( reference ) {
ProveIt.buildForm( reference ); // There's no current way to avoid going through the interface, but the user doesn't notice
ProveIt.update( reference );
} );
templates.forEach( function ( template ) {
ProveIt.buildForm( template );
ProveIt.update( template );
} );
ProveIt.buildList();
}, 100 );
},
/**
* Build the form and add it to the GUI
*
* @param {ProveIt.Reference|ProveIt.Template} object Reference or Template object to fill the form
*/
buildForm: function ( object ) {
var $form = $( '<div>' ).attr( 'id', 'proveit-form' ); // Yea it's not a <form>, for easier styling
// Add the form to the GUI and make sure we're at the top
$( '#proveit-body' ).html( $form ).scrollTop( 0 );
// Build the header
var $header = $( '#proveit-header' ),
$backButton = $( '<button>' ).text( mw.message( 'proveit-back-button' ) );
$( 'button', $header ).remove();
$header.prepend( $backButton );
$backButton.on( 'click', ProveIt.buildList );
// Build the footer
var $footer = $( '#proveit-footer' ),
$insertButton = $( '<button>' ).attr( 'id', 'proveit-insert-button' ).text( mw.message( 'proveit-insert-button' ) ).on( 'click', object, ProveIt.insert ).addClass( 'progressive' ),
$updateButton = $( '<button>' ).attr( 'id', 'proveit-update-button' ).text( mw.message( 'proveit-update-button' ) ).on( 'click', object, ProveIt.update ).addClass( 'progressive' ),
$removeButton = $( '<button>' ).attr( 'id', 'proveit-remove-button' ).text( mw.message( 'proveit-remove-button' ) ).on( 'click', object, ProveIt.remove );
$footer.empty();
// Add the Insert button or the Remove and Update buttons
if ( ProveIt.getWikitext().indexOf( object.wikitext ) === -1 ) {
$footer.append( $insertButton );
} else {
$footer.append( $removeButton, $updateButton );
}
// Add the relevant fields and buttons
if ( object instanceof ProveIt.Reference ) {
ProveIt.buildReferenceFields( object );
ProveIt.buildTemplateFields( object.template );
} else {
ProveIt.buildTemplateFields( object );
}
},
/**
* Build the reference fields and add them to the form
*
* @param {ProveIt.Reference} reference Reference object to fill the fields
*/
buildReferenceFields: function ( reference ) {
var $fields = $( '<div>' ).attr( 'id', 'proveit-reference-fields' ),
$label, $input, $div;
// Add the reference name field
$label = $( '<label>' ).text( mw.message( 'proveit-reference-name-label' ) );
$input = $( '<input>' ).attr( 'id', 'proveit-reference-name' ).val( reference.name );
$div = $( '<div>' ).append( $label, $input );
$fields.append( $div );
// Add the reference group field
$label = $( '<label>' ).text( mw.message( 'proveit-reference-group-label' ) );
$input = $( '<input>' ).attr( 'id', 'proveit-reference-group' ).val( reference.group );
$div = $( '<div>' ).append( $label, $input );
$fields.append( $div );
// Add the reference content field
$label = $( '<label>' ).text( mw.message( 'proveit-reference-content-label' ) );
$input = $( '<textarea>' ).attr( 'id', 'proveit-reference-content' ).val( reference.content );
$div = $( '<div>' ).append( $label, $input );
$fields.append( $div );
// When the reference content changes, update the template fields
$input.on( 'change', function () {
var content = $( this ).val(),
dummy = new ProveIt.Reference( '<ref>' + content + '</ref>' );
ProveIt.buildTemplateFields( dummy.template );
} );
// Add the fields to the form
$( '#proveit-reference-fields' ).remove();
$( '#proveit-form' ).prepend( $fields );
// Add the footer buttons
var $buttons = $( '<span>' ).attr( 'id', 'proveit-reference-buttons' ),
$citeButton = $( '<button>' ).attr( 'id', 'proveit-cite-button' ).text( mw.message( 'proveit-cite-button' ) ).on( 'click', reference, reference.cite );
$buttons.append( $citeButton );
$( '#proveit-reference-buttons' ).remove();
$( '#proveit-footer' ).prepend( $buttons );
},
/**
* Build the fields for the template parameters and add them to the reference form
*
* @param {ProveIt.Template} template Template object to fill the fields, if any
*/
buildTemplateFields: function ( template ) {
var $fields = $( '<div>' ).attr( 'id', 'proveit-template-fields' ),
$label, $input, $option, $button, $div;
// Add the template select menu
$label = $( '<label>' ).text( mw.message( 'proveit-reference-template-label' ) );
$input = $( '<select>' ).attr( 'id', 'proveit-template-select' );
$div = $( '<div>' ).append( $label, $input );
$fields.append( $div );
// Add the empty option
$option = $( '<option>' ).text( mw.message( 'proveit-no-template' ) ).val( '' );
$input.append( $option );
// Add an option for each template
var templateNames = Object.keys( ProveIt.templateData ).sort();
templateNames.forEach( function ( templateName ) {
if ( typeof ProveIt.templateData[ templateName ] === 'string' ) {
return;
}
$option = $( '<option>' ).text( templateName ).val( templateName );
if ( template.name === templateName ) {
$option.prop( 'selected', true );
}
$input.append( $option );
} );
// When the template select changes, update the template fields
$input.on( 'change', template, function ( event ) {
var template = event.data;
template.name = $( this ).val();
template.data = template.getData();
template.params = template.getParams();
template.paramOrder = template.getParamOrder();
mw.cookie.set( 'proveit-last-template', template.name ); // Remember the new choice
ProveIt.buildTemplateFields( template );
} );
if ( 'maps' in template.data && 'citoid' in template.data.maps ) {
// Add the Citoid field
$button = $( '<button>' ).text( mw.message( 'proveit-citoid-load' ) );
$label = $( '<label>' ).text( mw.message( 'proveit-citoid-label' ) ).attr( 'data-tooltip', mw.message( 'proveit-citoid-tooltip' ) );
$input = $( '<input>' ).attr( 'placeholder', mw.message( 'proveit-citoid-placeholder' ) );
$div = $( '<div>' ).append( $button, $label, $input );
$fields.append( $div );
// When the Citoid button is clicked, try to extract the reference data automatically via the Citoid service
$button.on( 'click', function () {
var $button = $( this ),
query = $button.siblings( 'input' ).val();
// We need a query
if ( !query ) {
return;
}
// Show the loading message
$button.text( mw.message( 'proveit-citoid-loading' ) ).prop( 'disabled', true );
// Get the data
var contentLanguage = mw.config.get( 'wgContentLanguage' );
$.get( '//' + contentLanguage + '.wikipedia.org/api/rest_v1/data/citation/mediawiki/' + encodeURIComponent( query ) ).done( function ( data ) {
// Recursive helper function
function setParamValue( paramName, paramValue ) {
if ( typeof paramName === 'string' && typeof paramValue === 'string' ) {
$( '.proveit-template-param [name="' + paramName + '"]' ).val( paramValue );
} else if ( paramName instanceof Array && paramValue instanceof Array ) {
for ( var i in paramName ) {
setParamValue( paramName[ i ], paramValue[ i ] );
}
}
}
// Fill the template fields
var citoidMap = template.data.maps.citoid,
citoidData = data[ 0 ],
paramName, paramValue;
for ( var citoidKey in citoidData ) {
paramName = citoidMap[ citoidKey ];
paramValue = citoidData[ citoidKey ];
setParamValue( paramName, paramValue );
}
// Reset the button
$button.text( mw.message( 'proveit-citoid-load' ) ).prop( 'disabled', false );
// Update the reference content too
if ( $( '#proveit-reference-content' ).length ) {
var content = $( '#proveit-reference-content' ).val(),
dummy = new ProveIt.Reference( '<ref>' + content + '</ref>' );
content = dummy.buildContent();
$( '#proveit-reference-content' ).val( content );
}
// @todo For some reason this isn't firing
} ).fail( function () {
$button.text( mw.message( 'proveit-citoid-error' ) );
setTimeout( function () {
$button.text( mw.message( 'proveit-citoid-load' ) ).prop( 'disabled', false );
}, 3000 );
} );
} );
}
// Add a field for each parameter
var userLanguage = mw.config.get( 'wgUserLanguage' ),
contentLanguage = mw.config.get( 'wgContentLanguage' ),
paramData, labelText, labelTooltip, inputValue, inputPlaceholder;
template.paramOrder.forEach( function ( inputName ) {
// Reset defaults
paramData = {
label: null,
description: null,
required: false,
suggested: false,
deprecated: false
};
labelText = inputName;
labelTooltip = null;
inputValue = null;
inputPlaceholder = null;
// Override with template data
if ( 'params' in template.data && inputName in template.data.params ) {
paramData = template.data.params[ inputName ];
}
if ( paramData.label ) {
if ( userLanguage in paramData.label ) {
labelText = paramData.label[ userLanguage ];
} else if ( contentLanguage in paramData.label ) {
labelText = paramData.label[ contentLanguage ];
}
}
if ( paramData.description ) {
if ( userLanguage in paramData.description ) {
labelTooltip = paramData.description[ userLanguage ];
} else if ( contentLanguage in paramData.description ) {
labelTooltip = paramData.description[ contentLanguage ];
}
}
// Extract the parameter value
if ( inputName in template.params ) {
inputValue = template.params[ inputName ];
} else if ( paramData.aliases ) {
paramData.aliases.forEach( function ( paramAlias ) {
if ( paramAlias in template.params ) {
inputValue = template.params[ paramAlias ];
return;
}
} );
}
// Build the label, input and div
$label = $( '<label>' ).text( labelText );
if ( labelTooltip ) {
$label.attr( 'data-tooltip', labelTooltip );
}
$input = paramData.type === 'content' ? $( '<textarea>' ) : $( '<input>' );
$input.val( inputValue ).attr( {
name: inputName,
placeholder: inputPlaceholder
} );
$div = $( '<div>' ).addClass( 'proveit-template-param' ).append( $label, $input );
// If the parameter is of the page type, search the wiki
if ( paramData.type === 'wiki-page-name' ) {
$input.attr( 'list', inputName + '-list' );
var $list = $( '<datalist>' ).attr( 'id', inputName + '-list' );
$div.prepend( $list );
$input.on( 'keyup', function () {
var search = $( this ).val();
new mw.Api().get( {
action: 'opensearch',
search: search,
limit: 5,
redirects: 'resolve',
format: 'json',
formatversion: 2
} ).done( function ( data ) {
$list.empty();
var titles = data[ 1 ];
titles.forEach( function ( title ) {
var $option = $( '<option>' ).val( title );
$list.append( $option );
} );
} );
} );
}
// If the parameter is of the URL type, add the Archive button
if ( paramData.type === 'url' ) {
$button = $( '<button>' ).text( mw.message( 'proveit-archive-button' ) );
$div.prepend( $button );
$button.on( 'click', $input, function ( event ) {
var url = event.data.val().trim();
if ( !url ) {
return;
}
var $button = $( this );
$button.text( mw.message( 'proveit-archive-fetching' ) ).prop( 'disabled', true );
$.getJSON( 'https://archive.org/wayback/available?url=' + encodeURIComponent( url ) ).done( function ( data ) {
if ( data.archived_snapshots.closest ) {
var snapshot = data.archived_snapshots.closest;
var archive = snapshot.url.replace( /^http:\/\//, 'https://' );
OO.ui.alert( archive, { size: 'large', title: mw.message( 'proveit-archive-title' ).text() } );
} else {
OO.ui.alert( mw.message( 'proveit-archive-no-url' ).text() );
}
} ).fail( function () {
OO.ui.alert( mw.message( 'proveit-archive-error' ).text() );
} ).always( function () {
$button.text( mw.message( 'proveit-archive-button' ) ).prop( 'disabled', false );
} );
} );
}
// If the parameter is of the date type, add the Today button
if ( paramData.type === 'date' ) {
$button = $( '<button>' ).text( mw.message( 'proveit-today-button' ) );
$div.prepend( $button );
$button.on( 'click', $input, function ( event ) {
var input = event.data,
date = new Date(),
yyyy = date.getFullYear(),
mm = ( '0' + ( date.getMonth() + 1 ) ).slice( -2 ),
dd = ( '0' + date.getDate() ).slice( -2 );
input.val( yyyy + '-' + mm + '-' + dd );
} );
}
// Mark the div according to the parameter status
if ( paramData.required ) {
$div.addClass( 'proveit-required' );
} else if ( paramData.suggested ) {
$div.addClass( 'proveit-suggested' );
} else if ( paramData.deprecated ) {
$div.addClass( 'proveit-deprecated' );
} else {
$div.addClass( 'proveit-optional' );
}
// Hide all optional and deprecated parameters, unless they are filled
if ( !inputValue && ( $div.hasClass( 'proveit-optional' ) || $div.hasClass( 'proveit-deprecated' ) ) ) {
$div.hide();
}
// Add the div to the table
$fields.append( $div );
} );
// Some reference templates may have no template data
if ( !template.data || 'notemplatedata' in template.data ) {
$div = $( '<div>' ).attr( 'id', 'proveit-no-template-data-message' ).text( mw.message( 'proveit-no-template-data' ) );
$fields.append( $div );
}
// Add the fields to the form
$( '#proveit-template-fields' ).remove();
$( '#proveit-form' ).append( $fields );
// Add the footer buttons
var $buttons = $( '<span>' ).attr( 'id', 'proveit-template-buttons' ),
$filterFields = $( '<input>' ).attr( 'placeholder', mw.message( 'proveit-filter-fields' ) ),
$showAllButton = $( '<button>' ).attr( 'id', 'proveit-show-all-button' ).text( mw.message( 'proveit-show-all-button' ) );
if ( template.paramOrder.length ) {
$buttons.append( $filterFields );
}
if ( $( '.proveit-required, .proveit-suggested' ).length && $( '.proveit-deprecated, .proveit-optional' ).length ) {
$buttons.append( $showAllButton );
} else {
$( '.proveit-deprecated, .proveit-optional' ).show();
}
$( '#proveit-template-buttons' ).remove();
$( '#proveit-footer' ).prepend( $buttons );
// Bind events
$showAllButton.on( 'click', function () {
$( '.proveit-deprecated, .proveit-optional' ).show();
$( this ).remove();
} );
$filterFields.on( 'keyup', function () {
var filter = $( this ).val().toLowerCase();
$( 'div', $fields ).show().filter( function () {
return $( this ).text().toLowerCase().indexOf( filter ) === -1;
} ).hide();
$( '#proveit-show-all-button' ).remove();
} );
// When a template parameter changes, update the reference content
if ( $( '#proveit-reference-content' ).length ) {
$( 'select, input, textarea', '#proveit-template-fields' ).on( 'change', function () {
var content = $( '#proveit-reference-content' ).val(),
dummy = new ProveIt.Reference( '<ref>' + content + '</ref>' );
content = dummy.buildContent();
$( '#proveit-reference-content' ).val( content );
} );
}
},
/**
* Parse the given wikitext in search for references and return an array of Reference objects
*
* @param {string} wikitext
* @return {ProveIt.Reference[]} Array of Reference objects
*/
getReferences: function ( wikitext ) {
var references = [],
matches = wikitext.match( /<\s*ref[^>]*>[^<]*<\s*\/\s*ref\s*>/ig );
if ( matches ) {
matches.forEach( function ( match ) {
var reference = new ProveIt.Reference( match );
references.push( reference );
} );
}
return references;
},
/**
* Parse the given wikitext in search for templates and return an array of Template objects
*
* @param {string} wikitext
* @return {ProveIt.Template[]} Array of Template objects
*/
getTemplates: function ( wikitext ) {
var templates = [],
templateName, templateRegex, templateMatch, templateWikitext, templateStart, templateEnd, templateDepth, template;
for ( templateName in ProveIt.templateData ) {
templateRegex = new RegExp( '{{\\s*' + templateName + '\\s*[|}]', 'ig' );
while ( ( templateMatch = templateRegex.exec( wikitext ) ) !== null ) {
templateWikitext = templateMatch[ 0 ];
templateStart = templateMatch.index;
// Figure out the templateEnd by searching for the closing "}}"
// knowing that there may be subtemplates, like so:
// {{Cite book |title=Foo |year={{BC|123}} |author=Bar}}
templateEnd = wikitext.length;
templateDepth = 0;
for ( var i = templateStart; i < templateEnd; i++ ) {
if ( wikitext[ i ] + wikitext[ i + 1 ] === '{{' ) {
templateDepth++;
i++; // We speed up the loop to avoid multiple matches when two or more templates are found together
} else if ( wikitext[ i ] + wikitext[ i + 1 ] === '}}' ) {
templateDepth--;
i++;
}
if ( templateDepth === 0 ) {
templateEnd = i + 1;
break;
}
}
templateWikitext = wikitext.substring( templateStart, templateEnd );
template = new ProveIt.Template( templateWikitext );
templates.push( template );
}
}
return templates;
},
/**
* Add the ProveIt revision tag
*/
addTag: function () {
var tag = mw.config.get( 'proveit-tag' );
if ( !tag ) {
return; // No tag defined
}
switch ( ProveIt.getEditor() ) {
case 'core':
case 'wikieditor':
case 'codemirror':
var $tagInput = $( '#wpChangeTags' );
// Don't add it twice
if ( !$tagInput.data( 'proveit' ) ) {
if ( $tagInput.length ) {
$tagInput.val( $tagInput.val() + ',' + tag );
} else {
$tagInput = $( '<input>' ).attr( {
id: 'wpChangeTags',
type: 'hidden',
name: 'wpChangeTags',
value: tag
} );
$( '#editform' ).prepend( $tagInput );
}
$tagInput.data( 'proveit', true );
}
break;
case '2017':
ve.init.target.saveFields.wpChangeTags = function () {
return tag;
};
break;
}
},
/**
* Add the ProveIt edit summary
*/
addSummary: function () {
var proveitSummary = mw.config.get( 'proveit-summary' );
if ( !proveitSummary ) {
return; // No summary defined
}
proveitSummary += ' #proveit'; // For tracking via https://hashtags.wmcloud.org/
switch ( ProveIt.getEditor() ) {
case 'core':
case 'wikieditor':
case 'codemirror':
var $summaryTextarea = $( '#wpSummary' ),
currentSummary = $summaryTextarea.val();
if ( !currentSummary ) {
$summaryTextarea.val( proveitSummary );
}
break;
case '2017':
$( document ).on( 'focus', '.ve-ui-mwSaveDialog-summary textarea', function () {
var $summaryTextarea = $( this ),
currentSummary = $summaryTextarea.val();
if ( !currentSummary ) {
$summaryTextarea.val( proveitSummary );
}
} );
break;
}
},
/**
* Insert the given object in the wikitext
*
* @param {jQuery.Event|ProveIt.Reference|ProveIt.Template|ProveIt.Citation} object Reference, template or citation, or a jQuery event containing one
*/
insert: function ( object ) {
if ( object instanceof $.Event ) {
object = object.data;
}
var wikitext = object.buildWikitext();
if ( object instanceof ProveIt.Citation ) {
object.index = $( '#wpTextbox1' ).textSelection( 'getCaretPosition' );
}
$( '#wpTextbox1' ).textSelection( 'encapsulateSelection', {
peri: wikitext,
replace: true
} );
if ( object instanceof ProveIt.Reference ) {
var reference = new ProveIt.Reference( wikitext );
ProveIt.buildForm( reference ); // Changes the Insert button for Update
}
if ( object instanceof ProveIt.Template ) {
var template = new ProveIt.Template( wikitext );
ProveIt.buildForm( template ); // Changes the Insert button for Update
}
ProveIt.addTag();
ProveIt.addSummary();
},
/**
* Update the given object in the wikitext
*
* @param {jQuery.Event|ProveIt.Reference|ProveIt.Template|ProveIt.Citation} object Reference, template or citation, or a jQuery event containing one
*/
update: function ( object ) {
if ( object instanceof $.Event ) {
object = object.data;
}
var wikitext = object.buildWikitext();
// If the object is a reference, update the citations too
if ( object instanceof ProveIt.Reference ) {
object.citations.forEach( function ( citation ) {
ProveIt.update( citation );
} );
}
ProveIt.replace( object.wikitext, wikitext );
object.wikitext = wikitext;
ProveIt.highlight( object );
ProveIt.addTag();
ProveIt.addSummary();
},
/**
* Remove the given object from the wikitext
*
* @param {jQuery.Event|ProveIt.Reference|ProveIt.Template|ProveIt.Citation} object Reference, template or citation, or a jQuery event containing one
*/
remove: function ( object ) {
if ( object instanceof $.Event ) {
object = object.data;
}
// If the object is a reference, remove the citations too
if ( object instanceof ProveIt.Reference && object.citations.length && confirm( mw.message( 'proveit-confirm-remove' ) ) ) {
object.citations.forEach( function ( citation ) {
ProveIt.remove( citation );
} );
}
ProveIt.replace( object.wikitext, '' );
ProveIt.addTag();
ProveIt.addSummary();
ProveIt.buildList();
},
/**
* Highlight the given object in the wikitext
*
* @param {jQuery.Event|ProveIt.Reference|ProveIt.Template|ProveIt.Citation} object Reference, template or citation, or a jQuery event containing one
*/
highlight: function ( object ) {
if ( object instanceof $.Event ) {
object.stopPropagation();
object = object.data;
}
var wikitext = ProveIt.getWikitext(),