User:Ritchie333/drafts.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.
function getDrafts( projectName, continueId ) {
	$.ajax( {
		type: "GET",
		url: mw.util.wikiScript( 'api' ),
		data: { action:'query',
		  list:'embeddedin',
		  format:'json',
		  rawcontinue:'',
		  eititle:'Template:' + projectName,
		  eidir:'ascending',
		  eicontinue:continueId,
		  einamespace:119 },
		dataType: 'json',
		success: function( data ) {
			var results = data.query.embeddedin;
			if( results.length > 0 ) {
				var titles = '';
				var i;
				var first = true;
				for( i = 0; i < results.length; ++i ) {
					if( first ) {
						first = false;
					} else {
						titles += '|';
				}
					var draftName = results[ i ].title.substr( 'Draft talk:'.length );
					titles += ( 'Draft:' + draftName );
				}
				$.ajax( {
					type: "GET",
				url: mw.util.wikiScript( 'api' ),
					data: {
						action:'query',
						prop:'categories',
						format:'json',
						titles:titles,
						clcategories:'Category:Pending AfC submissions'
					},
					dataType: 'json',
					success: function( redata ) {
						for( var page in redata.query.pages ) {
							nextPage = redata.query.pages[ page ];
							var pageTitle = nextPage.title;
							for( var cat in nextPage.categories ) {
								nextCategory = nextPage.categories[ cat ];
								if( nextCategory.title === 'Category:Pending AfC submissions' ) {
									// Got it
									$('#related-drafts').append( 
										'<a href="' + mw.util.getUrl( pageTitle ) + '">' + pageTitle + '</a><br/>' );
								}
							}
						}
					}
				});
				if ( typeof data[ 'query-continue' ] != 'undefined' ) {
					getDrafts( projectName,  data[ 'query-continue' ].embeddedin.eicontinue );
				}
			}
		}
	});
}

jQuery(function () {
	mw.loader.using( ['mediawiki.util'], function () {
	  if( mw.config.get('wgPageName').indexOf( 'Wikipedia:WikiProject' ) === 0 ) {
	    if(mw.config.get('wgAction') == 'view' || mw.config.get('wgAction') == 'purge' ){
	      var portletLink = mw.util.addPortletLink('p-tb', '#', 'Draft list', 't-draft-list', 'List drafts for this project');
	      $( portletLink ).click( function ( e ) {
			e.preventDefault();
			var projectName = mw.config.get('wgPageName').substr( 'Wikipedia:'.length );
			mw.util.$content.prepend( '<div id="related-drafts"><p>Related drafts</p></div>' );
			getDrafts( projectName, undefined /* no continue */ );
		} );
	    }
	  }
	});
});