Jump to content

User:Enterprisey/req-helper.js

From Wikipedia, the free encyclopedia
This is an old revision of this page, as edited by Enterprisey (talk | contribs) at 19:50, 10 October 2015 (hotfix). The present address (URL) is a permanent link to this revision, which may differ significantly from the current revision.
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.
//<nowiki>
( function ( $, mw ) {
    if ( mw.config.get( 'wgAction' ) === 'view' &&
         mw.config.get( 'wgPageName' ).indexOf('Wikipedia:Requested_articles') === 0 &&
         mw.config.get( 'wgPageName' ) !== "Wikipedia:Requested_articles" &&
         !$( "#mw-diff-ntitle1" ).length &&
         !$( "#mw-revision-info" ).length ) {
        
        // Load and display form
        $.getJSON(
            mw.util.wikiScript('api'),
            {
                format: 'json',
                action: 'query',
                prop: 'revisions',
                rvprop: 'content',
                rvlimit: 1,
                titles: "User:APerson/req-helper.js/tpl-panel.js"
            }
        ).done( function ( data ) {
            var pageId = Object.keys(data.query.pages)[0];
            var panelHtml = data.query.pages[pageId].revisions[0]['*'];
            $( "#jump-to-nav" ).after( panelHtml );
            
            $( "#req-helper-panel input.view-option" ).click( updateRequestActions );
            $( "#req-helper-panel input[type='radio']" ).click( updateRequestDisplay );
            
            // Enable save button if there are requests marked for deletion
            $( "#mw-content-text" ).on( "click", "a.delete-action", updateSaveChangesButton );
            
            // Save handler
            $( "#save-changes" ).click( function ( event ) {
                $( "#save-changes" ).prop( "disabled", true );
                $( "#req-helper-panel table").append( "<tr><td colspan=4><ul></ul></td></tr>" );
                var status = function ( newStatus ) { return $( "<li> ").appendTo( "#req-helper-panel ul" ).text( newStatus ); };
                
                status( $( ".marked-for-deletion" ).length + " item(s) to delete." );

                var wikitext;
                var getTextStatus = status( "Getting wikitext..." );
                $.getJSON(
                    mw.util.wikiScript('api'),
                    {
                        format: 'json',
                        action: 'query',
                        prop: 'revisions',
                        rvprop: 'content',
                        rvlimit: 1,
                        titles: mw.config.get( 'wgPageName' )
                    }
                ).done( function ( data ) {
                    var pageId = Object.keys(data.query.pages)[0];
                    var wikitext = data.query.pages[pageId].revisions[0]['*'];
                    getTextStatus.text( "Got wikitext." );
                    $( ".marked-for-deletion" ).each( function ( index, element ) {
                        
                        // Regex to sanitize link name from http://stackoverflow.com/a/3561711
                        var linkName = $( element ).children( "a" ).first().text(),
                            sanitizedLinkName = linkName.replace(/[-\/\\^$*+?.()|[\]{}]/g, '\\$&'),
                            regexToRemove = new RegExp( "\\*\\s?\\[\\[" + sanitizedLinkName + "\\]\\].*?\n", "i" );
                        wikitext = wikitext.replace( regexToRemove, "" );
                    } );
                    status( "Processed wikitext." );
                    var saveStatus = status( "Saving page..." );
                    var plural = ( $( ".marked-for-deletion" ).length === 1 ) ? "" : "s";
                    $.ajax( {
                        url: mw.util.wikiScript( 'api' ),
                        type: 'POST',
                        dataType: 'json',
                        data: {
                            format: 'json',
                            action: 'edit',
                            title: mw.config.get( 'wgPageName' ),
                            summary: "Removing " + $( ".marked-for-deletion" ).length + " request" + plural + " ([[User:APerson/req-helper|req-helper]])",
                            token: mw.user.tokens.get( 'editToken' ),
                            text: wikitext
                        }
                    } ).done ( function ( data ) {
                        if( data && data.edit && data.edit.result && data.edit.result == 'Success' ) {
                            saveStatus.html( "<b>Saved page!</b> (" )
				.append( $( "<a>" )
					 .text( "reload" )
					 .attr( "href", "#" )
					 .click( function () { document.location.reload( true ); } ) )
				.append( ")" );
                        } else {
                            saveStatus.text( "There was an error saving the page." );
                        }
                    } );
                } ); // end get-current-page-wikitext handler
            } ); // end save handler
            
            // Mark blue handler
            $( "#mark-bluelinks" ).click( function ( event ) {
                forEachRequest( function ( element ) {
                    
                    var elemClass = $( element ).attr( "class" );
                    if( $( "#also-mark-redirects" ).is( ":checked" ) ) {
                        if( elemClass !== "mw-redirect" && elemClass ) return;
                    } else {
                        if( elemClass ) return;
                    }
                    
                    if( !$( element ).parent().hasClass( "marked-for-deletion" ) ) {
                        $( element ).parent().addClass( "marked-for-deletion" );
                    }
                } );
                updateSaveChangesButton();
            } ); // end mark handler
            
            // Create action links, initially hidden
            forEachRequest( function ( link ) {
                $( "<span>" )
                    .insertAfter( link )
                    .addClass( "request-actions" )
                    .css( "margin-left", "0.25em" )
                    .append( $( "<span>", { "text": "( ", "class": "action-parenthesis" } ) )
                    .append( $( "<a>" )
                        .text( "search" )
                        .addClass( "search-action" )
                        .attr( "href", "https://www.google.com/search?q=" + $( link ).text().replace( / /, "+" ) ) )
                    .append( $( "<span>", { "text": " | ", "class": "action-separator" } ) )
                    .append( $( "<a>" )
                        .text( "delete" )
                        .addClass( "delete-action" )
                        .click( function ( e ) {
                            link.parent().toggleClass( "marked-for-deletion" );
                            if( $( this ).text() === "delete" ) {
                                $( this ).text( "undelete" );
                            } else {
                                $( this ).text( "delete" );
                            }
                        } ) )
                    .append( $( "<span>", { "text": " )", "class": "action-parenthesis" } ) );
            } ); // end action link creation loop
            
            // Make a style element to be used by updateRequestActions
            $( "#req-helper-panel" ).append( $( "<style>", { "id": "visibility-css" } ) );
            updateRequestActions();
            
            updatePageIssues();
        } ); // end form-loaded handler
    }
    
    function updateSaveChangesButton () {
        var numMarkedRequests = $( ".marked-for-deletion" ).length;
        $( "#save-changes" ).prop( "disabled", numMarkedRequests === 0 );
        $( "#save-changes" ).text( numMarkedRequests ? ( "Delete " + numMarkedRequests + " request" + ( ( numMarkedRequests === 1 ) ? "" : "s" ) ) : "Save changes" );
    }
    
    // Takes a callback that takes a link object
    function forEachRequest ( callback ) {
        $( "#mw-content-text a" ).each( function ( index, element ) {
            
            // Test to make sure it's an actual request
            if( $( element ).children( ".tocnumber" ).length ) return;
            if( $( element ).parent().prop( "tagName" ) !== "LI" ) return;
            if( $( element ).parent().children().first().text() !== $( element ).text() ) return;
            
            // Run callback
            callback( element );
        } );
    }
    
    function updateRequestDisplay () {
        var noSourcesAction = $( "input[name=no-sources]:checked" ).val(),
            someSourcesAction = $( "input[name=some-sources]:checked" ).val();
        forEachRequest( function ( link ) {
            // Highlight articles based on # of provided refs
            var listElement = $( link ).parent();
            if( listElement.children( ".external" ).length === 0 ) {
                if( noSourcesAction === "hide" ) {
                    listElement.hide();
                } else {
                    listElement.show();
                    listElement.css( "background-color",  ( $( "input[name=no-sources]:checked" ).val() === "highlight" ) ? "#FEE" : "#FFF" );
                }
            } else {
                if( someSourcesAction === "hide" ) {
                    listElement.hide();
                } else {
                    listElement.show();
                    listElement.css( "background-color",  ( $( "input[name=some-sources]:checked" ).val() === "highlight" ) ? "#EFE" : "#FFF" );
                }
            }
        } );
    }
    
    function updateRequestActions () {
        var showSearch = $( "#show-search-option" ).is( ":checked" ),
            showDelete = $( "#show-delete-option" ).is( ":checked" );
            
        // Make sure we aren't destroying any deletion state
        if( !showDelete && $( ".marked-for-deletion" ).length !== 0 ) {
            if( window.confirm( "You've marked some requests for deletion. " +
				"Are you sure you want to exit mark-for-deletion mode without deleting the requests?" ) ) {
                
                // Wipe deletion state
                $( "a.delete-action" ).text( "delete" );
                $( "li.marked-for-deletion" ).toggleClass( "marked-for-deletion" );
                $( "#save-changes" ).prop( "disabled", true );
            } else {
                
                // The user is indecisive; get out
                $( "#show-delete-option" ).prop( "checked", true );
                return;
            }
        }
        
        var newVisibilityCss = "";
        if( !showSearch && !showDelete ) newVisibilityCss += "span.action-parenthesis{display:none;}";
        if( !showSearch ) newVisibilityCss += "a.search-action{display:none;}";
        if( !showDelete ) newVisibilityCss += "a.delete-action{display:none;}";
        if( !showSearch || !showDelete ) newVisibilityCss += "span.action-separator{display:none;}";
        $( "#visibility-css" ).html( newVisibilityCss );
    }
    
    function updatePageIssues () {
    	$( "#page-issues" ).append( "<ul></ul>" );
    	var addIssue = function ( issue ) { $( "#page-issues ul" ).append( $( "<li>", { "html": issue } ) ); };
        $.getJSON(
            mw.util.wikiScript('api'),
            {
                format: 'json',
                action: 'query',
                prop: 'revisions',
                rvprop: 'content',
                rvlimit: 1,
                titles: mw.config.get( 'wgPageName' )
            }
        ).done( function ( data ) {
            var pageId = Object.keys(data.query.pages)[0];
            var wikitext = data.query.pages[pageId].revisions[0]['*'];
            
            // Regex counter from http://stackoverflow.com/a/4009768
            var refs = ( wikitext.match( /<ref>/g ) || [] ).length;
            if( refs ) {
                addIssue( refs + " &lt;ref&gt; tag" + ( ( refs === 1 ) ? "" : "s" ) );
            }
            
            var noLinkReqs = ( wikitext.match( /\*\s+[^\[]/g ) || [] ).length;
            if( noLinkReqs ) {
                addIssue( noLinkReqs + " req" + ( ( noLinkReqs === 1 ) ? "" : "s" ) + " without a link" );
            }
            
            var timestamps = ( wikitext.match( /\(UTC\)/g ) || [] ).length;
            if( timestamps ) {
            	addIssue( timestamps + " timestamp" + ( ( timestamps === 1 ) ? "" : "s" ) );
            }
            
            // Set state overall
            if( $( "#page-issues ul li").length ) {
                $( "#page-issues" ).css( "background-color", "#FEE" );
            } else {
                $( "#page-issues" ).css( { "vertical-align": "center",
                                           "text-align": "center" } );
                $( "#page-issues" ).css( "background-color", "#EFE" );
                $( "#page-issues" ).text( "(no issues)" );
            }
        } ); // End get-page-wikitext handler
    }
}( jQuery, mediaWiki ) );
//</nowiki>