MediaWiki:Gadget/Twinkle/twinkleprod.js

From OpenWetWare
Jump to navigationJump to search

Note: After publishing, you may have to bypass your browser's cache to see the changes.

  • Firefox / Safari: Hold Shift while clicking Reload, or press either Ctrl-F5 or Ctrl-R (⌘-R on a Mac)
  • Google Chrome: Press Ctrl-Shift-R (⌘-Shift-R on a Mac)
  • Internet Explorer / Edge: Hold Ctrl while clicking Refresh, or press Ctrl-F5
  • Opera: Press Ctrl-F5.
// If TwinkleConfig aint exist.
if( typeof( TwinkleConfig ) == 'undefined' ) {
    TwinkleConfig = {};
}

/**
 TwinkleConfig.summaryAd (string)
 If ad should be added or not to summary, default [[WP:TWINKLE|TWINKLE]]
 */
if( typeof( TwinkleConfig.summaryAd ) == 'undefined' ) {
    TwinkleConfig.summaryAd = " using [[WP:TW|TW]]";
}

/**
 TwinkleConfig.watchProdPages (boolean)
 If, when applying prod template to page, watch it, default true
 */
if( typeof( TwinkleConfig.watchProdPages ) == 'undefined' ) {
    TwinkleConfig.watchProdPages = true;
}

function twinkleprod() {
    if( wgNamespaceNumber != 0 || wgCurRevisionId == false ) {
        return;
    }
    if (twinkleConfigExists)
    {
        addPortletLink( 'p-cactions', "javascript:twinkleprod.callback()", "PROD", "tw-prod", "Propose deletion via WP:PROD", "");
    }
    else
    {
        addPortletLink('p-cactions', 'javascript:alert("Your account is too new to use Twinkle.");', 'PROD', 'tw-prod', 'Propose deletion via WP:PROD', '');
    }
}
addOnloadHook(twinkleprod);

twinkleprod.callback = function twinkleprodCallback() {
    var Window = new SimpleWindow( 800, 400 );
    Window.setTitle( "WP:PROD" );
    var form = new QuickForm( twinkleprod.callback.evaluate );
    form.append( {
            type: 'checkbox',
            list: [
                {
                    label: 'Notify if possible',
                    value: 'notify',
                    name: 'notify',
                    tooltip: 'If a notification is defined in the configuration, then notify if this is true, else no notify',
                    checked: true
                }
            ]
        }
    );
    var field = form.append( {
            type: 'field',
            label: 'Reason for proposed deletion'
        } );
    field.append( {
            type: 'textarea',
            name: 'reason',
            label: 'Reason:'
        } );
    field.append( { type:'submit' } );

    var result = form.render();
    Window.setContent( result );
    Window.display();
}

twinkleprod.callbacks = {
    main: function( self ) {
        var xmlDoc = self.responseXML;
        var exists = xmlDoc.evaluate( 'boolean(//pages/page[not(@missing)])', xmlDoc, null, XPathResult.BOOLEAN_TYPE, null ).booleanValue;

        if( ! exists ) {
            self.statelem.error( "It seems that the page doesn't exist.  Perhaps it has already been deleted." );
            return;
        }
        
        var query = { 
            'title': wgPageName, 
            'action': 'submit'
        };
        
        var wikipedia_wiki = new Wikipedia.wiki( 'Tagging page', query, twinkleprod.callbacks.tagPage );
        wikipedia_wiki.params = self.params;
        wikipedia_wiki.followRedirect = false;
        wikipedia_wiki.get();
    },
    tagPage: function( self ) {
        var form = self.responseXML.getElementById('editform');
        var text = form.wpTextbox1.value;

        var tag_re = /(\{\{(?:db-?|delete|[aitcmrs]fd|md1)[^{}]*?\|?[^{}]*?\}\})/i;
        if( tag_re.test( text ) ) {
            self.statelem.warn( 'Page already tagged with a deletion template, aborting procedure' );
            return;
        }

        var prod_re = /\{\{\s*dated prod\s*\|(?:\{\{[^\{\}]*\}\}|[^\}\{])*\}\}/i;
        if( !prod_re.test( text ) ) {
            // Notification to first contributor
            var query = {
                'action': 'query',
                'prop': 'revisions',
                'titles': wgPageName,
                'rvlimit': 1,
                'rvprop': 'user',
                'rvdir': 'newer'
            }
            var callback = function( self ) {
                var xmlDoc = self.responseXML;
                var user = xmlDoc.evaluate( '//rev/@user', xmlDoc, null, XPathResult.STRING_TYPE, null ).stringValue;
                var query = {
                    'title': 'User talk:' + user,
                    'action': 'submit'
                };
                var wikipedia_wiki = new Wikipedia.wiki( 'Notifying initial contributor (' + user + ')', query, twinkleprod.callbacks.userNotification );
                wikipedia_wiki.params = self.params;
                wikipedia_wiki.get();
            }

            if( self.params.usertalk ) {
                var wikipedia_api = new Wikipedia.api( 'Grabbing data of initial contributor', query, callback );
                wikipedia_api.params = self.params;
                wikipedia_api.post();
            }
            
            var summaryText = "Proposing article for deletion per [[WP:PROD]].";
            text = "\{\{subst:prod|1=" + self.params.reason + "\}\}\n" + text;
        } else {
            var prod2_re = /\{\{prod-?2.*?\}\}/;
            if( prod2_re.test( text ) ) {
                self.statelem.warn( 'Page already tagged with \{\{prod\}\} and \{\{prod-2\}\} templates, aborting procedure' );
                return;
            }
            self.statelem.info( "A \{\{dated prod\}\} tag was already found on this article" );
            if( !confirm( "Would you like to add a \{\{prod-2\}\} tag with your explanation?" ) ) {
                self.statelem.info( 'Aborted per user request' );
                return;
            }
            
            var summaryText = "Endorsing proposed deletion per [[WP:PROD]].";
            text = text.replace( prod_re, text.match( prod_re ) + "\n\{\{prod-2|1=" + self.params.reason + "\}\}\n" );
        }

        var postData = {
            'wpMinoredit': undefined, // Per memo
            'wpWatchthis': TwinkleConfig.watchProdPages ? '' : form.wpWatchthis.checked ? '' : undefined,
            'wpStarttime': form.wpStarttime.value,
            'wpEdittime': form.wpEdittime.value,
            'wpAutoSummary': form.wpAutoSummary.value,
            'wpEditToken': form.wpEditToken.value,
            'wpSection': '',
            'wpSummary': summaryText + TwinkleConfig.summaryAd,
            'wpTextbox1': text
        };

        self.post( postData );
    },
    userNotification: function( self ) {
        var form = this.responseXML.getElementById( 'editform' );
        var text = form.wpTextbox1.value;
        text += "\n\{\{subst:prodwarning|1=" + wgPageName + "|concern=" + self.params.reason + "\}\} \~\~\~\~";
        var postData = {
            'wpMinoredit': form.wpMinoredit.checked ? '' : undefined,
            'wpWatchthis': form.wpWatchthis.checked ? '' : undefined,
            'wpStarttime': form.wpStarttime.value,
            'wpEdittime': form.wpEdittime.value,
            'wpAutoSummary': form.wpAutoSummary.value,
            'wpEditToken': form.wpEditToken.value,
            'wpSection': '',
            'wpSummary': 'PROD nomination of \[\[' + wgPageName + '\]\].' + TwinkleConfig.summaryAd,
            'wpTextbox1': text
        };

        self.post ( postData );
    }
}

twinkleprod.callback.evaluate = function twinkleprodCallbackEvaluate(e) {
    wgPageName = wgPageName.replace( /_/g, ' ' ); // for queen/king/whatever and country!
    var form = e.target;
    var usertalk = form.notify.checked
    var reason = form.reason.value;

    var params = {
        usertalk: usertalk,
        reason: reason
    };

    Status.init( form );

    Wikipedia.actionCompleted.redirect = wgPageName;
    Wikipedia.actionCompleted.notice = "Tagging complete";

    var query = { 
        'action': 'query',
        'titles': wgPageName
    };

    var wikipedia_api = new Wikipedia.api( 'Checking if page exists', query, twinkleprod.callbacks.main );
    wikipedia_api.params = params;
    wikipedia_api.post();
}