User:Novem Linguae/Scripts/DraftCleaner.js: Difference between revisions

Content deleted Content added
fix linter errors (publish.php)
fix linter errors. wrap in IIFE (publish.php)
Line 53:
*/
 
( function () {
async function getWikicode( title ) {
let const pageIsDeleted = ! mw.config.get( 'wgCurRevisionId' );
if ( pageIsDeleted ) {
return '';
}
 
let wikicode = '';
title = encodeURIComponent( title );
await $.ajax( {
url: 'https://en.wikipedia.org/w/api.php?action=parse&page=' + title + '&prop=wikitext&formatversion=2&format=json',
success: function ( result ) {
wikicode = result.parse.wikitext;
},
dataType: 'json'
} );
return wikicode;
}
 
function goToShowChangesScreen( titleWithNamespaceAndUnderscores, wikicode, editSummary ) {
var wikicode = '';
title const titleEncoded = encodeURIComponent(title titleWithNamespaceAndUnderscores );
const wgServer = mw.config.get( 'wgServer' );
await $.ajax({
const wgScriptPath = mw.config.get( 'wgScriptPath' );
url: 'https://en.wikipedia.org/w/api.php?action=parse&page='+title+'&prop=wikitext&formatversion=2&format=json',
const baseURL = wgServer + wgScriptPath + '/';
success: function (result) {
// https://stackoverflow.com/a/12464290/3480193
wikicode = result['parse']['wikitext'];
$( `<form action="${ baseURL }index.php?title=${ titleEncoded }&action=submit" method="POST"/>` )
},
.append( $( '<input type="hidden" name="wpTextbox1">' ).val( wikicode ) )
dataType: "json",
.append( $( '<input type="hidden" name="wpSummary">' ).val( editSummary ) )
});
.append( $( '<input type="hidden" name="mode">' ).val( 'preview' ) )
return wikicode;
.append( $( '<input type="hidden" name="wpDiff">' ).val( 'Show changes' ) )
}
.append( $( '<input type="hidden" name="wpUltimateParam">' ).val( '1' ) )
.appendTo( $( document.body ) ) // it has to be added somewhere into the <body>
.trigger( 'submit' );
}
 
/** returns the pagename, including the namespace name, but with spaces replaced by underscores */
function goToShowChangesScreen(titleWithNamespaceAndUnderscores, wikicode, editSummary) {
function getArticleName() {
let titleEncoded = encodeURIComponent(titleWithNamespaceAndUnderscores);
let wgServer = return mw.config.get( 'wgServerwgPageName' );
}
let wgScriptPath = mw.config.get('wgScriptPath');
let baseURL = wgServer + wgScriptPath + '/';
// https://stackoverflow.com/a/12464290/3480193
$(`<form action="${baseURL}index.php?title=${titleEncoded}&action=submit" method="POST"/>`)
.append($('<input type="hidden" name="wpTextbox1">').val(wikicode))
.append($('<input type="hidden" name="wpSummary">').val(editSummary))
.append($('<input type="hidden" name="mode">').val('preview'))
.append($('<input type="hidden" name="wpDiff">').val('Show changes'))
.append($('<input type="hidden" name="wpUltimateParam">').val('1'))
.appendTo($(document.body)) //it has to be added somewhere into the <body>
.trigger('submit');
}
 
// don't run when not viewing articles
/** returns the pagename, including the namespace name, but with spaces replaced by underscores */
const action = mw.config.get( 'wgAction' );
function getArticleName() {
const isNotViewing = action != 'view';
return mw.config.get('wgPageName');
if ( isNotViewing ) {
}
return;
}
 
// don't run when not viewing articlesdiffs
let const actionisDiff = mw.config.get( 'wgActionwgDiffNewId' );
if ( isDiff ) {
let isNotViewing = action != 'view';
return;
if ( isNotViewing ) {
}
return;
}
 
// donDon't run whenin viewingvirtual diffsnamespaces
let const isDiffisVirtualNamespace = mw.config.get( 'wgDiffNewIdwgNamespaceNumber' ) < 0;
if ( isDiffisVirtualNamespace ) {
return;
}
 
let menuID = 'p-navigation';
// Don't run in virtual namespaces
// @ts-ignore
let isVirtualNamespace = mw.config.get('wgNamespaceNumber') < 0;
if ( isVirtualNamespacewindow.draftCleanerPutInToolsMenu ) {
menuID = 'p-tb';
return;
// @ts-ignore
}
} else if ( window.draftCleanerPutInMoreMenu ) {
menuID = 'p-cactions';
}
 
const titleWithNamespaceAndUnderscores = getArticleName();
let menuID = 'p-navigation';
const namespaceNumber = mw.config.get( 'wgNamespaceNumber' );
// @ts-ignore
if ( window.draftCleanerPutInToolsMenu ) {
menuID = 'p-tb';
// @ts-ignore
} else if ( window.draftCleanerPutInMoreMenu ) {
menuID = 'p-cactions';
}
 
let running = false;
let titleWithNamespaceAndUnderscores = getArticleName();
let namespaceNumber = mw.config.get('wgNamespaceNumber');
 
// Add DraftCleaner to the toolbar
let running = false;
mw.loader.using( [ 'mediawiki.util' ], () => {
mw.util.addPortletLink( menuID, '#', 'Run DraftCleaner', 'DraftCleanerLink' );
$( '#DraftCleanerLink' ).on( 'click', async () => {
// prevent running the script while script is already in progress
if ( running ) {
return;
}
running = true;
 
mw.notify( 'Parsing page content...' );
// Add DraftCleaner to the toolbar
mw.loader.using(['mediawiki.util'], function () {
mw.util.addPortletLink(menuID, '#', 'Run DraftCleaner', 'DraftCleanerLink');
$('#DraftCleanerLink').on('click', async function() {
// prevent running the script while script is already in progress
if ( running ) {
return;
}
running = true;
 
mw.notify('Parsing // get page content...');wikicode
const titleWithNamespaceAndSpaces = titleWithNamespaceAndUnderscores.replace( /_/g, ' ' );
const originalWikicode = await getWikicode( titleWithNamespaceAndUnderscores );
// get page wikicode
let wikicode = originalWikicode;
let titleWithNamespaceAndSpaces = titleWithNamespaceAndUnderscores.replace(/_/g, ' ');
let originalWikicode = await getWikicode(titleWithNamespaceAndUnderscores);
let wikicode = originalWikicode;
 
let const dc = new DraftCleaner();
wikicode = dc.cleanDraft( wikicode, namespaceNumber, titleWithNamespaceAndSpaces );
 
const needsChanges = wikicode != originalWikicode;
if ( needsChanges ) {
const summary = 'clean up ([[User:Novem Linguae/Scripts/DraftCleaner.js|DraftCleaner]])';
await goToShowChangesScreen( titleWithNamespaceAndUnderscores, wikicode, summary );
} else {
mw.notify( 'No changes needed!' );
}
} );
} );
}() );
 
let needsChanges = wikicode != originalWikicode;
if ( needsChanges ) {
let summary = 'clean up ([[User:Novem Linguae/Scripts/DraftCleaner.js|DraftCleaner]])';
await goToShowChangesScreen(titleWithNamespaceAndUnderscores, wikicode, summary);
} else {
mw.notify('No changes needed!');
}
});
});
 
// === modules/DraftCleaner.js ======================================================
Line 612 ⟶ 615:
fixExternalLinksToWikipediaArticles( wikicode ) {
// [https://en.wikipedia.org/wiki/Article] and [https://en.wikipedia.org/wiki/Article Article name]
return wikicode.replace( /(?<!\[)\[https?:\/\/en\.wikipedia\.org\/wiki\/([^ \]]*)( [^\]]*)?\]/gs, function ( match, p1 ) => {
p1 = decodeURIComponent( p1 );
p1 = p1.replace( /_/g, ' ' );