Jump to content

User:Waggers/wagtools.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.
// <nowiki>

/* WagTools ProjectTag code starts here */

importScript('MediaWiki:Gadget-morebits.js');

// set up config settings
if( typeof( wttagsConfig ) == 'undefined' ) {	wttagsConfig = {};}
 
//config default values...
if( typeof( wttagsConfig.summaryAd ) == 'undefined' ) {	wttagsConfig.summaryAd = "Project tagging/assessment using [[User:Waggers/wt|WagTools]]";}
if( typeof( wttagsConfig.groupByDefault ) == 'undefined' ) {	wttagsConfig.groupByDefault = true;}
if( typeof( wttagsConfig.groupingBanner ) == 'undefined' ) {	wttagsConfig.groupingBanner = "WikiProjectBannerShell";}
if( typeof( wttagsConfig.markTaggedPagesAsMinor ) == 'undefined' ) {	wttagsConfig.markTaggedPagesAsMinor = true;}

 
function wttags() {
	//if( wgNamespaceNumber == 1 || wgNamespaceNumber == 5 || wgNamespaceNumber == 7 || wgNamespaceNumber == 11 || wgNamespaceNumber == 13 ||wgNamespaceNumber == 15 || wgNamespaceNumber == 101 )
	mw.util.addPortletLink( 'p-cactions', "javascript:wttags_showform()", "WT Assess", "ca-wagtoolsassess", "Assess this article for Wikiprojects", "");
}
 
$(wttags);
 

function wttags_showform() {
	var Window = new SimpleWindow( 400, 600 );
	var form = new QuickForm( wttags_submitform);
 
	{
		Window.setTitle( "WagTools Assess" );
 
		/*form.append( {
				type: 'checkbox',
				list: [
					{
						label: 'Group into {{' + wttagsConfig.groupingBanner + '}}',
						value: 'group',
						name: 'group',
						tooltip: 'If adding two or more banners and this box is checked, all newly added banners will be grouped into a single {{' + wttagsConfig.groupingBanner + '}} template.',
						checked: wttagsConfig.groupByDefault
					}
				]
			}
		);*/
 
   var classlist = [
				{	label: "(blank)",		value: "|class="		},
				{	label: "FA",		value: "|class=FA"		},
				{	label: "A",			value: "|class=A"			},
				{ label: "GA",		value: "|class=GA"		},
				{ label: "B",			value: "|class=B"			},
				{ label: "C",			value: "|class=C"			},
				{ label: "Start",	value: "|class=start"	},
				{ label: "Stub",	value: "|class=stub"	},
				{ label: "NA",		value: "|class=NA"		},
				{label: "List",		value: "|class=list"		}
			];
			
		var implist = [
				{					label: "(blank)",			value: "|importance="				},
				{					label: "Top",					value: "|importance=top"				},
				{					label: "High",					value: "|importance=high"				},
				{					label: "mid",					value: "|importance=mid"				},
				{					label: "low",					value: "|importance=low"				},
				{					label: "NA",						value: "|importance=NA"				}
			];
			
		/*	var projList = [
	{
		label: 'Hampshire',
		value: 'hants',
		checked: 'checked'
	}
];
 
		form.append( { type:'header', label:'Projects' } );
		form.append( { type:'checkbox', name: 'proj', list: projList } );*/
		form.append( { type:'header', label:'Class' } );
		form.append( {type:'radio',name:'artclass',list:classlist,label:'Class'});
		form.append( { type:'header', label:'Importance' } );
		form.append( {type:'radio',name:'artimp',list:implist,label:'Importance'});
	  form.append( { type:'submit',label:'Assess' } );

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

 function wttags_submitform(e) {
 //alert($(e.target.artclass).val());
 var tagtext = '\{\{WikiProject Hampshire'+$(e.target.artclass).filter(':checked').val()+$(e.target.artimp).filter(':checked').val()+'}}';
 //var tagtext = '\{\{foo'+$(e.target.artclass).val()+$(e.target.artimp).val()+'}}';
 Status.init( e.target);
 Status.info('Info','Getting talk page wikitext');
 //identify target talk page
 var targetpage;
 if (wgCanonicalNamespace.indexOf("talk")>-1)
 { targetpage = wgPageName; }
 else
 {	targetpage = wgCanonicalNamespace + ' talk:' + wgTitle; }
 targetpage = targetpage.replace(" ","_");
 //alert(targetpage);
 
 //get wikitext of target talk page
 $.getJSON(
        mw.util.wikiScript('api'),
        'format=json&action=query&prop=revisions&rvprop=content&rvlimit=1&titles='+targetpage,
        function ( data ) {
                var page, wikitext;
                try {
                        for ( page in data.query.pages ) {
                                wikitext = data.query.pages[page].revisions[0]['*'];
                                
																Status.info( 'Info', 'Looking for banners already on page' );
																banner_regex = new RegExp( '\{\{(hants|WikiProject Hampshire|WP Hampshire)(\|(.)*)*\}\}','im')
																//banner_regex = new RegExp( '\{\{(foo)(\|(.)*)*\}\}','im') //for testing only
																if(banner_regex.exec(wikitext))
																{
																	Status.info( 'Info', 'Banner found. Need to update this one.' );
																	wikitext = wikitext.replace(banner_regex,tagtext);
																} else {
																  Status.info( 'Info', 'No banner found. Need to add a new one.' );
																	wikitext = tagtext + '\n'+ wikitext;
																	
																}
																
																//apply new wikitext
																Status.info( 'Info', 'Applying changes...' );																
																editPage({
																	title: targetpage,
																	text: wikitext,
																	summary: wttagsConfig.summaryAd
																});
																
                        }
                } catch ( e ) {
												alert('error found...'); alert(e);
                        //err();
                }
        }
);
 }
 
 function editPage( info ) {
        $.ajax({
                url: mw.util.wikiScript( 'api' ),
                type: 'POST',
                dataType: 'json',
                data: {
                        format: 'json',
                        action: 'edit',
                        title: info.title,
                        text: info.text,
                        summary: info.summary,
                        token: mw.user.tokens.get( 'editToken' )
                },
                success: function( data ) {
                        if ( data && data.edit && data.edit.result && data.edit.result == 'Success' ) {
                                Status.info( 'Info', 'ALL DONE!' );	
                        } else {
                                Status.info( 'Info', 'The edit query returned an error. =(' );
                        }
                },
                error: function() {
                        Status.info( 'Info', 'The ajax request failed.' );
                }
        });
}

/* End of WagTools ProjectTag code */
// </nowiki>