MediaWiki:Reports.js

/** * Flexible form script for VSTF Wiki reports * * @author Lil' Miss Rarity - original author * @author Joeytje50       - i18n fixes and dropdown * @author Jr Mime         - pop-up layout, variables * @author VegaDark        - vstf adaption * @author Cqm             - Major cleanup/rewrite * * @license: CC-BY-NC-SA */   var c = mw.config.get([            'wgNamespaceNumber',            'wgPageName',            'wgServer',            'wgUserName',            'wgUserLanguage'        ]), options = { /* 			//BEGIN EXAMPLE example: { page: 'Page name the form is for', buttonText: 'Text for button to open form', form: 'HTML form for reporting users. Each input/textarea should have an id. any optional inputs should be marked with the `optional` class. If any attributes need URI encoding, the relevant inputs should have the `data-encode` attribute set to `true`.', // this is where the input ids in the form are matched to numbers // for use in the summary/submitted text formParams: { '$1': 'foo', '$2': 'bar' },               submitText: 'Text to submit to the page. Any form parameters can be inserted via the key names in `formParams`', summary: 'Text used for the edit summary. Any form parameters can be inserted via the key names in `formParams`', sectionTitle: 'Text used as the section title. Any form parameters can be inserted via the key names in `formParams`' },           // END EXAMPLE */
 * (function ($, mw) {

profile: { page: 'Report:User_profile_headers', buttonText: 'Report Profile', form: '' + ' ' +                           ' ' +                                ' Wiki Name ' + ' ' +                               '' + ' ' +                               ' ' +                                ' Wiki URL ' + ' ' +                               ' http:// ' + '' + ' .wikia.com ' + ' ' +                               ' User ' + ' ' +                               '' +                                ' ' +                                ' ' +                                ' Reason ' +                                ' ' +                                ' ' +                                ' ' +                                ' ' +                            ' ' +                        ' ' +                    ' ',                formParams: {                    '$1': 'wikiurl',                    '$2': 'wikiname',                    '$3': 'user',                    '$4': 'comment'                },                submitText: '*Wiki: http://$1.wikia.com\n' +                    '*Profile: http://$1.wikia.com/wiki/User:$3?useskin=wikia\n' + '*Reason: $4\n' + '*Signature: ~', summary: 'New profile report ($2, $3)', sectionTitle: '$2' },

vandalism: { page: 'Report:Vandalism', buttonText: 'Report vandalism', form: '' + ' ' +                           ' ' +                                ' Wiki Name ' + ' ' +                               '' + ' ' +                               ' ' +                                ' Wiki URL ' + ' ' +                               ' http:// ' + '' + ' .wikia.com ' + ' ' +                               ' Vandal ' + ' ' +                               '' + ' ' +                               ' ' +                                ' Reason ' + ' ' +                               ' ' + ' ' +                               ' ' +                            ' ' +                        ' ' +                    ' ',                formParams: { '$1': 'wikiname', '$2': 'wikiurl', '$3': 'user', '$4': 'comment' },               submitText: '*Wiki: http://$2.wikia.com\n' + '*Vandal: http://$2.wikia.com/wiki/Special:Contributions/$3\n' + '*Reason: $4\n' + '*Signature: ~', summary: 'New vandalism report ($1, $3)', sectionTitle: '$3 at $1' },			spam: { page: 'Report:Spam', buttonText: 'Report spam', form: '' + ' ' +                           ' ' +                                ' Wiki Name ' + ' ' +                               '' + ' ' +                               ' ' +                                ' Wiki URL ' + ' ' +                               ' http:// ' + '' + ' .wikia.com ' + ' ' +                               ' Spammer ' + ' ' +                               '' + ' ' +                               ' ' +                                ' Reason ' + ' ' +                               ' ' + ' ' +                               ' ' +                            ' ' +                        ' ' +                    ' ',                formParams: { '$1': 'wikiname', '$2': 'wikiurl', '$3': 'user', '$4': 'comment' },               submitText: '*Wiki: http://$2.wikia.com\n' + '*Spammer: http://$2.wikia.com/wiki/Special:Contributions/$3\n' + '*Reason: $4\n' + '*Signature: ~', summary: 'New spam report ($1, $3)', sectionTitle: '$3 at $1' },			phalanx: { page: 'Report:Spam_filter_problems', buttonText: 'Report False Positives', form: '' + ' ' +                           ' ' +                                ' Wiki Name ' + ' ' +                               '<input id="wikiname" type="text" placeholder="VSTF Wiki" style="width:400px"/>' + ' ' +                               ' ' +                                ' Wiki URL & Page ' + ' ' +                               ' http:// ' + '<input id="wikiurl" type="text" placeholder="Name BETWEEN http:// AND .wikia.com IE.: vstf" style="width:200px"/>' + ' .wikia.com/wiki/ ' + '<input id="wikipage" type="text" placeholder="Report:Spam_filter_problems" style="width:100"/>' + ' ' +                               ' Block ID Number ' + ' ' +                               '<input id="blockid" type="text" placeholder="12345 (without #)" style="width:400px data-encode="true"/>' +                                ' ' +                                ' ' +                                ' What were you doing? ' +                                ' ' +                                '<textarea name="" id="comment" style="height: 100px; width: 100%;" placeholder="What did you try to do?" class="optional"> ' +                                ' ' +                                ' ' +                            ' ' +                        ' ' +                    ' ',                formParams: {                    '$1': 'wikiurl',                    '$2': 'wikiname',                    '$5': 'wikipage',                    '$3': 'blockid',                    '$4': 'comment'                },                submitText: '*Wiki: http://$1.wikia.com\n' + '*Page: http://$1.wikia.com/wiki/$5\n' + '*Block ID: http://community.wikia.com/wiki/Special:PhalanxStats/$3\n' + '*What were you trying to do?: $4\n' + '*Signature: ~', summary: 'New filter report ($2, #$3)', sectionTitle: 'Block #$3 on $2' },			wiki: { page: 'Report:Wiki', buttonText: 'Report Wiki', form: '<form class="WikiaForm" method="" name="" id="wiki">' + ' ' +                           ' ' +                                ' Wiki Name ' + ' ' +                               '<input id="wikiname" type="text" placeholder="VSTF Wiki" style="width:400px"/>' + ' ' +                               ' ' +                                ' Wiki URL ' + ' ' +                               ' http:// ' + '<input id="wikiurl" type="text" placeholder="vstf" style="width:364px"/>' + ' .wikia.com ' + ' ' +                               ' ' +                                ' Why? ' +                               ' ' +                                '<textarea name="" id="comment" style="height: 100px; width: 100%;" placeholder="How they violated the Terms of Use" class="optional"> ' + ' ' +                               ' ' +                            ' ' +                        ' ' +                    ' ',                formParams: { '$1': 'wikiname', '$2': 'wikiurl', '$3': 'comment' },               submitText: '', summary: '', sectionTitle: '' },       };

/**    * Report form submission handler *    * @param opts {object} */   function submitForm(opts) { var $form = $('#requestWindow form'), $inputs = $form.find('input, textarea'), $button = $('#vandalism #submit'), params = {}, x,           y,            $input, text;

$button.prop('disabled', true);

for (x in opts.formParams) { if (opts.formParams.hasOwnProperty(x)) { $input = $inputs.filter('#' + opts.formParams[x]);

if (!$input.length) { console.log('An error has been found in the form config. Please check the formParams and input ids'); $button.prop('disabled', false); return; }

text = $input.val;

if (!text && !$input.hasClass('optional')) { alert('One or more required fields are missing. Please check your submission and try again.'); $button.prop('disabled', false); return; }

if ($input.attr('data-encode') === 'true') { text = encodeURIComponent(text); }

params[x] = text; }       }

for (x in params) { if (params.hasOwnProperty(x)) { // console.log(x, params[x]); // convert to regex so the same parameter can be used multiple times in each string y = new RegExp(x.replace(/\$/, '\\$'), 'g'); opts.submitText = opts.submitText.replace(y, params[x]); opts.summary = opts.summary.replace(y, params[x]); opts.sectionTitle = opts.sectionTitle.replace(y, params[x]); }       }

console.log(opts.submitText, opts.summary);

(new mw.Api) .post({               action: 'edit',                title: c.wgPageName,                section: 'new',                sectiontitle: opts.sectionTitle,                text: opts.submitText,                summary: opts.summary,                token: mw.user.tokens.get('editToken')

})           .done(function (res) { location.reload('action=purge'); });   }

/**    * Loads the report form */   function loadForm { var $this = $(this), type = $this.attr('id').split('-')[2], opts = options[type];

$.showCustomModal(           opts.buttonText,            opts.form,            {                id: 'requestWindow',                width: 650,                buttons: [{                    id: 'cancel',                    message: 'Cancel',                    handler: function  {                        $('#requestWindow').closeModal;                    }                }, {                    id: 'submit',                    defaultButton: true,                    message: 'Save',                    handler: function  {                        submitForm(opts);                    }                }]            }        ); }

/**    * Loads the report form button *     * @param type {string} */   function loadButton(type) { var opts = options[type];

$('#lang-EN') .empty .append(               $('<a>')                    .attr('id', 'vstf-report-' + type)                    .addClass('wikia-button')                    .on('click', loadForm)                    .text(opts.buttonText)            ); }

/**    * start up method */   function init { var x;

for (x in options) { if (options.hasOwnProperty(x)) { if (options[x].page === c.wgPageName) { loadButton(x); return; }           }        }    }

$(init);

}(jQuery, mediaWiki));