User:Sophivorus/ExcerptHunter.js

var ExcerptHunter = {

messages: { 'excerpthunter-source-label': 'Merge the contents of $1:', 'excerpthunter-target-label': 'Into the lead section of $1:', 'excerpthunter-publish-label': "Then click Publish to save the changes to the lead section and have the contents of the first section automatically replaced for an excerpt of the lead section.", 'excerpthunter-publish': 'Publish' },

init: function { mw.messages.set( ExcerptHunter.messages );

var $container = $( '#ExcerptHunter' ); if ( !$container.length ) { return; }       ExcerptHunter.hunt.done( ExcerptHunter.skip ); },

hunt: function { const params = { action: 'query', list: 'search', srsearch: 'hastemplate:Main', formatversion: 2 };       return new mw.Api.get( params ).fail( console.log ).done( function ( data ) {            const results = data.query.search;            ExcerptHunter.results = results;        } ); },

skip: function { const result = ExcerptHunter.results.shift; const sourceTitle = result.title; ExcerptHunter.getSourceWikitext( sourceTitle ).done( function ( data ) {           let sourceWikitext = data.parse.wikitext;

// Match the first section containing const sourceMatch = sourceWikitext.match( /^=+([^=]+?)=+(\s*?{{[Mm]ain.*?)^=/sm ); if ( !sourceMatch ) { ExcerptHunter.skip; return; }

// Extract the title and wikitext of the section sourceWikitext = sourceMatch[2].trim; const sourceSection = sourceMatch[1].trim;

// Match the template const targetMatch = sourceWikitext.match( /{{[Mm]ain\|(.*?)}}/ ); if ( !targetMatch ) { ExcerptHunter.skip; return; }

// Extract the title of the target article const targetTitle = targetMatch[1].trim;

// Get the wikitext of the target article ExcerptHunter.getTargetWikitext( targetTitle ).done( function ( data ) {               const targetWikitext = data.parse.wikitext;                ExcerptHunter.makeGUI( sourceTitle, sourceSection, sourceWikitext, targetTitle, targetWikitext );            } ); } );   },

getSourceWikitext: function ( sourceTitle ) { const params = { action: 'parse', page: sourceTitle, prop: 'wikitext', redirects: 1, formatversion: 2 };       return new mw.Api.get( params ).fail( console.log ); },

getTargetWikitext: function ( targetTitle ) { const params = { action: 'parse', page: targetTitle, prop: 'wikitext', section: 0, // Only the lead section redirects: 1, formatversion: 2 };       return new mw.Api.get( params ).fail( console.log ); },

makeGUI: function ( sourceTitle, sourceSection, sourceWikitext, targetTitle, targetWikitext ) { // Define the elements const sourceUrl = new mw.Title( sourceTitle + '#' + sourceSection ).getUrl; const sourceLink = '' + sourceTitle + ' § ' + sourceSection + ''; const sourceLabel = mw.message( 'excerpthunter-source-label', sourceLink ).plain; const $sourceLabel = $( ' ' + sourceLabel + ' ' ); const sourceInput = new OO.ui.MultilineTextInputWidget( { name: 'source', value: sourceWikitext, rows: 10, readOnly: true } ); const sourceLayout = new OO.ui.FieldLayout( sourceInput, { label: $sourceLabel, align: 'top' } );

const targetUrl = new mw.Title( targetTitle ).getUrl; const targetLink = '' + targetTitle + ''; const targetLabel = mw.message( 'excerpthunter-target-label', targetLink ).plain; const $targetLabel = $( ' ' + targetLabel + ' ' ); const targetInput = new OO.ui.MultilineTextInputWidget( { name: 'target', value: targetWikitext, rows: 10 } ); const targetLayout = new OO.ui.FieldLayout( targetInput, { label: $targetLabel, align: 'top' } );

const publishButton = new OO.ui.ButtonInputWidget( { label: mw.msg( 'excerpthunter-publish' ), flags: [ 'primary', 'progressive' ] } ); const publishLayout = new OO.ui.FieldLayout( publishButton, { label: mw.msg( 'excerpthunter-publish-label' ), align: 'top' } ); const formLayout = new OO.ui.FormLayout( { items: [ sourceLayout, targetLayout, publishLayout ] } );

// Do some CSS tweaks sourceInput.$element.css( { 'font-family': 'monospace', 'max-width': '100%' } ); targetInput.$element.css( { 'font-family': 'monospace', 'max-width': '100%' } );

// Add to the DOM const $container = $( '#ExcerptHunter' ); $container.html( formLayout.$element ); } };

mw.loader.using( [	'mediawiki.api',	'oojs-ui-core',	'oojs-ui-widgets' ], ExcerptHunter.init );