Jump to content

User:Ucucha/autofc.js

From Wikipedia, the free encyclopedia
Note: After saving, you have to bypass your browser's cache to see the changes. Google Chrome, Firefox, Microsoft Edge and Safari: Hold down the ⇧ Shift key and click the Reload toolbar button. For details and instructions about other browsers, see Wikipedia:Bypass your cache.
/* autofc.js
 * Provides automatic FAC nomination capacity. Based on [[User:AzaToth/twinklexfd.js]].
 * Experimental.
 */

// If TwinkleConfig aint exist.
if( typeof( TwinkleConfig ) == 'undefined' ) {
    TwinkleConfig = {};
}

/**
 TwinkleConfig.xfdWatchDiscussion (string)
 The watchlist setting of the newly created XfD page. Either "yes", "no", or "default". Default is "default" (Duh).
 */
if( typeof( TwinkleConfig.xfdWatchDiscussion ) == 'undefined' ) {
    TwinkleConfig.xfdWatchDiscussion = "default";
}

/**
 TwinkleConfig.xfdWatchPage (string)
 The watchlist setting of the page listed for XfD. Either "yes", "no", or "default". Default is "default" (Duh).
 */
if( typeof( TwinkleConfig.xfdWatchPage) == 'undefined' ) {
    TwinkleConfig.xfdWatchPage= "default";
}

/**
 TwinkleConfig.xfdWatchList (string)
 The watchlist setting of xfd list page, *if* the discussion is on a separate page. Either "yes", "no", or "default". Default is "default".
 */
if( typeof( TwinkleConfig.xfdWatchList ) == 'undefined' ) {
    TwinkleConfig.xfdWatchList = "default";
}

/**
 summaryAdd (string)
 */
var summaryAdd = " using [[User:Ucucha/autofc|autofc]]";

function autofc() {
    if( mw.config.get('wgNamespaceNumber') < 0 || mw.config.get('wgCurRevisionId') == false ) {
        return;
    }
    if (twinkleConfigExists)
    {
        twAddPortletLink( "javascript:autofc.callback()", "FC", "au-fc", "Featured content nomination", "");
    }
    else
    {
        twAddPortletLink( 'javascript:alert("Your account is too new to use Twinkle.");', 'FC', 'au-fc', 'Featured content nomination', '');
    }
}
window.TwinkleInit = (window.TwinkleInit || []).concat(autofc); //schedule initializer

autofc.callback = function autofcCallback() {

    var Window = new SimpleWindow( 600, 325 );
    Window.setTitle( "Featured content nomination" );
    var form = new QuickForm( autofc.callback.evaluate );
    var categories = form.append( {
            type: 'select',
            name: 'category',
            label: 'Select wanted type of category: ',
            tooltip: 'When activated, a default choice is made, based on what namespace you are in. This default should be the most appropriate',
            event: autofc.callback.change_category
        } );
    categories.append( {
            type: 'option',
            label: 'FAC',
            selected: mw.config.get('wgNamespaceNumber') == Namespace.MAIN,
            value: 'fac'
        } );
/*    categories.append( {
            type: 'option',
            label: 'FLC',
            selected: false,
            value: 'flc'
        } );
    categories.append( {
            type: 'option',
            label: 'FPC',
            selected: mw.config.get('wgNamespaceNumber') == Namespace.IMAGE,
            value: 'fpc'
        } );
    categories.append( {
            type: 'option',
            label: 'FSC',
            selected: false,
            value: 'fsc'
        } );
    categories.append( {
            type: 'option',
            label: 'FPOC',
            selected: mw.config.get('wgNamespaceNumber') == Namespace.PORTAL ,
            value: 'fpoc'
        } );*/
    form.append( {
            type: 'field',
            label:'Work area',
            name: 'work_area'
        } );

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

    // We must init the
    var evt = document.createEvent( "Event" );
    evt.initEvent( 'change', true, true );
    result.category.dispatchEvent( evt );

}

autofc.callback.change_category = function autofcCallbackChangeCategory(e) {
    var value = e.target.value;
    var root = e.target.form;
    var old_area;
    var childNodes = root.childNodes;
    for( var i = 0; i < childNodes.length; ++i ) {
        var node = childNodes[i];
        if(
            node instanceof Element &&
            node.getAttribute( 'name' ) == 'work_area'
        ) {
            old_area = node;
            break;
        }
    }
    var work_area = null;

    switch( value ) {
    case 'fac':
        work_area = new QuickForm.element( {
                type: 'field',
                label: 'Featured article candidates',
                name: 'work_area'
            } );
        work_area.append( {
                type: 'textarea',
                name: 'fcreason',
                label: 'Reason: '
            } );
        work_area.append( { type:'submit' } );
        work_area = work_area.render();
        old_area.parentNode.replaceChild( work_area, old_area );
        break;
/*    case 'flc':
        work_area = new QuickForm.element( {
                type: 'field',
                label: 'Featured list candidates',
                name: 'work_area'
            } );
        work_area.append( {
                type: 'textarea',
                name: 'fcreason',
                label: 'Reason: '
            } );
        work_area.append( { type:'submit' } );
        work_area = work_area.render();
        old_area.parentNode.replaceChild( work_area, old_area );
        break;
    case 'fpc':
        work_area = new QuickForm.element( {
                type: 'field',
                label: 'Featured picture candidates',
                name: 'work_area'
            } );
        work_area.append( {
                type: 'textarea',
                name: 'fcreason',
                label: 'Reason: '
            } );
        work_area.append( { type:'submit' } );
        work_area = work_area.render();
        old_area.parentNode.replaceChild( work_area, old_area );
        break;
    case 'fsc':
        work_area = new QuickForm.element( {
                type: 'field',
                label: 'Featured sound candidates',
                name: 'work_area'
            } );
        work_area.append( {
                type: 'textarea',
                name: 'fcreason',
                label: 'Reason: '
            } );
        work_area.append( { type:'submit' } );
        work_area = work_area.render();
        old_area.parentNode.replaceChild( work_area, old_area );
        break;
    case 'fpoc':
        work_area = new QuickForm.element( {
                type: 'field',
                label: 'Featured portal candidates',
                name: 'work_area'
            } );
        work_area.append( {
                type: 'textarea',
                name: 'fcreason',
                label: 'Reason: '
            } );
        work_area.append( { type:'submit' } );
        work_area = work_area.render();
        old_area.parentNode.replaceChild( work_area, old_area );
        break;*/
    default:
        work_area = new QuickForm.element( {
                type: 'field',
                label: 'Nothing for anything',
                name: 'work_area'
            } );
        work_area = work_area.render();
        old_area.parentNode.replaceChild( work_area, old_area );
        break;
    }
}

autofc.callbacks = {
    fac: {
        main: function ( self ) {
            var xmlDoc = self.responseXML;
            var titles = xmlDoc.evaluate( '//allpages/p/@title', xmlDoc, null, XPathResult.UNORDERED_NODE_SNAPSHOT_TYPE, null );

            // There has been no earlier entries with this prefix, just go on.
            if( titles.snapshotLength <= 0 ) {
                self.params.numbering = '/archive1';
                self.params.number = '1';
                numbering = number = '';
            } else {
                var number = 0;
                for( var i = 0; i < titles.snapshotLength; ++i ) {
                    var title = titles.snapshotItem(i).value;

                    // First, simple test, is there an instance with this exact name?
                    if( title == 'WP:Featured article candidates/' + mw.config.get('wgPageName') + '/archive1') {
                        number = Math.max( number, 1 );
                        continue;
                    }

                    var order_re = new RegExp( '^' +
                            RegExp.escape( 'WP:Featured article candidates/' + mw.config.get('wgPageName'), true ) +
                            '/archive?'
                    );
                    var match = order_re.exec( title );

                    // No match; A non-good value
                    if( match == null ) {
                        continue;
                    }

                    // A match, set number to the max of current
                    number = Math.max( number, Number(match[1]) );
                }
                self.params.number = parseInt( number ) + 1;
                self.params.numbering = '/archive' + self.params.number;
            }

            Status.info( 'Next discussion page","[[WP:Featured article candidates/' + mw.config.get('wgPageName') + self.params.numbering + ']]' );

            // Discussion page
            var query = {
                'title': 'WP:Featured article candidates/' + mw.config.get('wgPageName') + self.params.numbering,
                'action': 'submit'
            };

            // Updating data for the action completed event
            Wikipedia.actionCompleted.redirect = query['title'];
            Wikipedia.actionCompleted.notice = "Nomination completed, now redirecting to the discussion page";

            var wikipedia_wiki = new Wikipedia.wiki( 'Creating FAC nomination page', query, autofc.callbacks.fac.discussionPage );
            wikipedia_wiki.params = self.params;
            wikipedia_wiki.get();

            // Tagging article
            var query = {
                'title': 'Talk:' + mw.config.get('wgPageName'),
                'action': 'submit'
            };
            var wikipedia_wiki = new Wikipedia.wiki( 'Adding nomination tag to article talk page', query, autofc.callbacks.fac.article );
            wikipedia_wiki.params = self.params;
            wikipedia_wiki.get();

            query = {
                'title': 'WP:Featured article candidates',
                'action': 'submit'
            };

            var wikipedia_wiki = new Wikipedia.wiki( 'Adding discussion to list', query, autofc.callbacks.fac.todaysList );
            wikipedia_wiki.params = self.params;
            wikipedia_wiki.followRedirect = true;
            wikipedia_wiki.get();
        },
        article: function( self ) {
            var form = self.responseXML.getElementById('editform');

            var postData = {
                'wpMinoredit': undefined,
                'wpWatchthis': (TwinkleConfig.xfdWatchPage=="yes" || (TwinkleConfig.xfdWatchPage=="default"&&form.wpWatchthis.checked) ? '' : undefined),
                'wpStarttime': form.wpStarttime.value,
                'wpEdittime': form.wpEdittime.value,
                'wpAutoSummary': form.wpAutoSummary.value,
                'wpEditToken': form.wpEditToken.value,
                'wpSection': form.wpSection.value,
                'wpSummary': "Nominated as a featured article candidate; see [[WP:Featured article candidates/" + mw.config.get('wgPageName') + self.params.numbering + ']].'+ summaryAdd,
                'wpTextbox1': "\{\{featured article candidates|" + mw.config.get('wgPageName') + self.params.numbering + " \}\}\n" + form.wpTextbox1.value
            };
            self.post( postData );
        },
        discussionPage: function( self ) {
            var form = self.responseXML.getElementById('editform');
            var nomtext = "===\[\[" + mw.config.get('wgPageName') + "\]\]===\n" +
                "\n" +
                "<noinclude>\{\{la|" + mw.config.get('wgPageName') + "\}\}\n" +
                "\{\{Wikipedia:Featured article tools|1=" + mw.config.get('wgPageName') + "\}\}</noinclude>\n" +
                "\n" +
                "<!-- Please don't edit anything above here. Be sure to include your reasons for nominating below. -->\n" +
                ":<small>''Nominator(s): \~\~\~\~''</small>\n";
            var postData = {
                'wpMinoredit': form.wpMinoredit.checked ? '' : undefined,
                'wpWatchthis': (TwinkleConfig.xfdWatchDiscussion=="yes" || (TwinkleConfig.xfdWatchDiscussion=="default"&&form.wpWatchthis.checked) ? '' : undefined),
                'wpStarttime': form.wpStarttime.value,
                'wpEdittime': form.wpEdittime.value,
                'wpAutoSummary': form.wpAutoSummary.value,
                'wpEditToken': form.wpEditToken.value,
                'wpSection': form.wpSection.value,
                'wpSummary': "Creating FAC nomination page for \[\[" + mw.config.get('wgPageName') + '\]\].' + summaryAdd,
                'wpTextbox1': nomtext + self.params.reason + " \~\~\~\~"
            };
            self.post( postData );
        },
        todaysList: function( self ) {
            var form = self.responseXML.getElementById('editform');
            var old_text = form.wpTextbox1.value;

            var text = old_text.replace( /(<\!-- Add new nominations at the top of the list below this comment. Before nominating, please make sure the article meets the FA criteria. -->\n+)/, "$1\{\{Wikipedia:Featured article candidates/" + mw.config.get('wgPageName') + self.params.numbering + "\}\}\n");
            if( text == old_text ) {
                self.statelem.error( 'failed to find target spot for the discussion' );
                return;
            }
            var postData = {
                'wpMinoredit': form.wpMinoredit.checked ? '' : undefined,
                'wpWatchthis': (TwinkleConfig.xfdWatchList=="yes" || (TwinkleConfig.xfdWatchList=="default"&&form.wpWatchthis.checked) ? '' : undefined),
                'wpStarttime': form.wpStarttime.value,
                'wpEdittime': form.wpEdittime.value,
                'wpAutoSummary': form.wpAutoSummary.value,
                'wpEditToken': form.wpEditToken.value,
                'wpSection': form.wpSection.value,
                'wpSummary': "Adding \[\[WP:Featured article candidates/" + mw.config.get('wgPageName') + self.params.numbering + '\|FAC\]\] for \[\[' + mw.config.get('wgPageName') + '\]\].' + summaryAdd,
                'wpTextbox1': text
            };
            self.post( postData );
        },
    }
}

autofc.callback.evaluate = function(e) {

    wgPageName = mw.config.get('wgPageName').replace( /_/g, ' ' ); // for queen/king/whatever and country!

    var type = e.target.category.value;
    var reason = e.target.fcreason.value;

    Status.init( e.target );

    if( type == null ) {
        Status.error( 'Error', 'no action given' );
        return;
    }

    switch( type ) {
    case 'fac': // FAC
        var query = {
            'action': 'query',
            'list': 'allpages',
            'apprefix': 'Featured article candidates/' + wgPageName,
            'apnamespace': 4,
            'apfilterredir': 'nonredirects',
            'aplimit': userIsInGroup( 'sysop' ) ? 5000 : 500
        };
        var wikipedia_api = new Wikipedia.api( 'Tagging article with FAC talk tag', query, autofc.callbacks.fac.main );
        wikipedia_api.params = { reason:reason };
        wikipedia_api.post();
        break;
    }
}