User:DreZhsh/common.js: Difference between revisions
From Test Wiki
Content deleted Content added
No edit summary |
No edit summary |
||
| Line 1: | Line 1: | ||
/** |
|||
var IPtocheck = mw.config.get('wgRelevantUserName') ; |
|||
* @name AbuseFilterEditTools |
|||
* @author DreZhsh <https://fr.wikipedia.org/wiki/Utilisateur:DreZhsh> |
|||
* |
|||
* Améiore l'interface d'édition des filtres |
|||
* en ajoutant différents outils : |
|||
* - Transformation des variables dépréciées par leurs alternatives |
|||
* - Mise en forme du code (séparation des opérateurs, mots-clés... par des espaces) |
|||
* - Amélioration de "Vérifier la syntaxe" |
|||
* @todo Nouveaux outils, regex constructor, contructeur d'expressions, adaptation pour éditeur normal |
|||
*/ |
|||
$( function () { |
|||
if ( document.getElementById( 'mw-abusefilter-syntaxcheck' ) !== null ) { |
|||
mw.loader.using( [ 'mediawiki.api', 'ext.abuseFilter.edit', 'oojs-ui-core' ], function () { |
|||
const buttonDeprecate = new OO.ui.ButtonWidget( { |
|||
label: 'Corriger le code' |
|||
} ); |
|||
/** |
|||
function reqListener () { |
|||
* getInputContent |
|||
* |
|||
* Extrait le contenu de |
|||
* Ace Editor |
|||
* |
|||
* @return {string} Le contenu de Ace Editor |
|||
*/ |
|||
function getInputContent() { |
|||
const useAce = $( document.getElementById( 'wpFilterRules' ) ).css( 'display' ) === 'none'; |
|||
if ( useAce ) { |
|||
const globalArray = []; |
|||
$( document.getElementsByClassName( 'ace_line' ) ).each( function () { |
|||
globalArray.push( $( this ).text() ); |
|||
} ); |
|||
return globalArray.join( '\n' ); |
|||
} else { |
|||
return document.getElementById( 'wpFilterRules' ).value; |
|||
} |
|||
var obj = JSON.parse(this.responseText); |
|||
} |
|||
document.getElementById('proxy').innerHTML = obj.proxy; |
|||
document.getElementById('vpn').innerHTML = obj.vpn; |
|||
/** |
|||
document.getElementById('tor').innerHTML = obj.tor; |
|||
* beautifyCode |
|||
document.getElementById('fraud_score').innerHTML = obj.fraud_score; |
|||
* |
|||
} |
|||
* @return {string} Le contenu de Ace Editor |
|||
function Request () { |
|||
* mais avec les éléments de la syntaxe séparés par |
|||
var oReq = new XMLHttpRequest(); |
|||
* des espaces |
|||
oReq.onload = reqListener; |
|||
*/ |
|||
oReq.open("get", "https://ipqualityscore.com/api/json/report/dd7359a59516a3ccd7cbe98563d2fb1f?ip=" + IPtocheck, true); |
|||
function beautifyCode() { |
|||
oReq.send(); |
|||
const globalArray = []; |
|||
} |
|||
$( document.getElementsByClassName( 'ace_line' ) ).each( function () { |
|||
////////// Initialisation ////////// |
|||
let inputContent = String( $( this ).html() ); |
|||
var re = /^(([0-9]|[1-9][0-9]|1[0-9]{2}|2[0-4][0-9]|25[0-5])\.){3}([0-9]|[1-9][0-9]|1[0-9]{2}|2[0-4][0-9]|25[0-5])$|^((?:[0-9A-Fa-f]{1,4}))((?::[0-9A-Fa-f]{1,4}))*::((?:[0-9A-Fa-f]{1,4}))((?::[0-9A-Fa-f]{1,4}))*|((?:[0-9A-Fa-f]{1,4}))((?::[0-9A-Fa-f]{1,4})){7}$/gi ; |
|||
inputContent = inputContent.replace( /&/g, '&' ); |
|||
if (mw.config.get('wgCanonicalSpecialPageName') === 'Block') { |
|||
inputContent = inputContent.replace( />/g, '>' ); |
|||
if ( re.test(IPtocheck) === true ) { |
|||
inputContent = inputContent.replace( /</g, '<' ); |
|||
$('div #contentSub2').prepend('<div align="center" style="font-size:2em;"><big>Vérification de l\'IP</big></div><br><b>Proxy</b> : <div id="proxy"></div><br><b>VPN</b> : <div id="vpn"></div><br><b>TOR</b> : <div id="tor"></div><br><b>Score de fraude</b> : <div id="fraud_score"></div>'); |
|||
const array = inputContent.split( /<\/span>/ ); |
|||
reqListener; |
|||
for ( const i in array ) { |
|||
} |
|||
array[ i ] = array[ i ].replace( /^\s/, '' ); |
|||
} |
|||
array[ i ] = array[ i ].replace( /<span class="[^"]*">/, '' ); |
|||
} |
|||
let string = array.join( ' ' ); |
|||
string = string.replace( /[\s];(?!\S)/g, ';' ); |
|||
globalArray.push( string ); |
|||
} ); |
|||
return globalArray.join( '\n' ); |
|||
} |
|||
// Initalisation |
|||
$( document.getElementById( 'mw-abusefilter-syntaxcheck' ) ).after( buttonDeprecate.$element ); |
|||
$( document.getElementsByClassName( 'mw-abusefilter-javascript-tools' ) ).first().after( |
|||
$( '<span>' ) |
|||
.attr( 'id', 'abusefilter-checksyntaxresultfield' ) |
|||
); |
|||
$( document.getElementsByClassName( 'noime' ) ).attr( 'id', 'abuseFilterEditor' ); |
|||
// Corriger le code |
|||
buttonDeprecate.on( 'click', () => { |
|||
const useAce = $( document.getElementById( 'wpFilterRules' ) ).css( 'display' ) === 'none'; |
|||
if ( $( document.getElementsByClassName( 'ace_text-layer' ) ).children().find( '.ace_deprecated' ).length !== 0 ) { |
|||
const articleVar = { |
|||
articleid: 'page_id', |
|||
namespace: 'page_namespace', |
|||
text: 'page_title', |
|||
prefixedtext: 'page_prefixedtitle', |
|||
restrictionsEdit: 'page_restrictions_edit', |
|||
restrictionsMove: 'page_restrictions_move', |
|||
restrictionsUpload: 'page_restrictions_upload', |
|||
restrictionsCreate: 'page_restrictions_create', |
|||
recentContributors: 'page_recent_contributors', |
|||
firstContributor: 'page_first_contributor' |
|||
}, |
|||
moved = { |
|||
toArticleid: 'moved_to_id', |
|||
toText: 'moved_to_title', |
|||
toPrefixedtext: 'moved_to_prefixedtitle', |
|||
fromArticleid: 'moved_from_id', |
|||
fromText: 'moved_from_title', |
|||
fromPrefixedtext: 'moved_from_prefixedtitle' |
|||
}, |
|||
extensions = { |
|||
boardArticleid: 'board_id', |
|||
boardText: 'board_title', |
|||
boardPrefixedtext: 'board_prefixedtitle', |
|||
articleViews: 'page_views' |
|||
}; |
|||
const inputContent = getInputContent(); |
|||
// article_ |
|||
let abusefilterNewText = inputContent.replace( |
|||
/article_articleid/ig, articleVar.articleid |
|||
); |
|||
abusefilterNewText = abusefilterNewText.replace( |
|||
/article_namespace/ig, articleVar.namespace |
|||
); |
|||
abusefilterNewText = abusefilterNewText.replace( |
|||
/article_text/ig, articleVar.text |
|||
); |
|||
abusefilterNewText = abusefilterNewText.replace( |
|||
/article_prefixedtext/ig, articleVar.prefixedtext |
|||
); |
|||
abusefilterNewText = abusefilterNewText.replace( |
|||
/article_restrictions_edit/ig, articleVar.restrictionsEdit |
|||
); |
|||
abusefilterNewText = abusefilterNewText.replace( |
|||
/article_restrictions_move/ig, articleVar.restrictionsMove |
|||
); |
|||
abusefilterNewText = abusefilterNewText.replace( |
|||
/article_restrictions_upload/ig, articleVar.restrictionsUpload |
|||
); |
|||
abusefilterNewText = abusefilterNewText.replace( |
|||
/article_restrictions_create/ig, articleVar.restrictionsCreate |
|||
); |
|||
abusefilterNewText = abusefilterNewText.replace( |
|||
/article_recent_contributors/ig, articleVar.recentContributors |
|||
); |
|||
abusefilterNewText = abusefilterNewText.replace( |
|||
/article_first_contributor/ig, articleVar.firstContributor |
|||
); |
|||
// moved_ |
|||
abusefilterNewText = abusefilterNewText.replace( |
|||
/moved_to_articleid/ig, moved.toArticleid |
|||
); |
|||
abusefilterNewText = abusefilterNewText.replace( |
|||
/moved_to_text/ig, moved.toText |
|||
); |
|||
abusefilterNewText = abusefilterNewText.replace( |
|||
/moved_to_prefixedtext/ig, moved.toPrefixedtext |
|||
); |
|||
abusefilterNewText = abusefilterNewText.replace( |
|||
/moved_from_articleid/ig, moved.fromArticleid |
|||
); |
|||
abusefilterNewText = abusefilterNewText.replace( |
|||
/moved_from_text/ig, moved.fromText |
|||
); |
|||
abusefilterNewText = abusefilterNewText.replace( |
|||
/moved_from_prefixedtext/ig, moved.fromPrefixedtext |
|||
); |
|||
// extensions |
|||
abusefilterNewText = abusefilterNewText.replace( |
|||
/board_articleid/ig, extensions.boardArticleid |
|||
); |
|||
abusefilterNewText = abusefilterNewText.replace( |
|||
/board_text/ig, extensions.boardText |
|||
); |
|||
abusefilterNewText = abusefilterNewText.replace( |
|||
/board_prefixedtext/ig, extensions.boardPrefixedtext |
|||
); |
|||
abusefilterNewText = abusefilterNewText.replace( |
|||
/article_views/ig, extensions.articleViews |
|||
); |
|||
// Application des modifications |
|||
if ( useAce === true ) { |
|||
ace.edit( 'wpAceFilterEditor' ).session.setValue( abusefilterNewText ); |
|||
} else { |
|||
document.getElementById( 'wpFilterRules' ).value = abusefilterNewText; |
|||
} |
|||
} else { |
|||
if ( useAce ) { |
|||
ace.edit( 'wpAceFilterEditor' ).session.setValue( beautifyCode() ); |
|||
} |
|||
} |
|||
} ); |
|||
// Vérifier la syntaxe |
|||
$( document.getElementById( 'mw-abusefilter-syntaxcheck' ) ).off(); |
|||
$( document.getElementById( 'mw-abusefilter-syntaxcheck' ) ).on( 'click', function () { |
|||
const text = $( document.getElementById( 'wpFilterRules' ) ).val(), |
|||
useAce = $( document.getElementById( 'wpFilterRules' ) ).css( 'display' ) === 'none', |
|||
filterEditor = ace.edit( 'wpAceFilterEditor' ); |
|||
$( this ).prop( 'disabled', true ).injectSpinner( { |
|||
id: 'abusefilter-syntaxcheck', |
|||
size: 'large' |
|||
} ); |
|||
new mw.Api().post( { |
|||
action: 'abusefilterchecksyntax', |
|||
format: 'json', |
|||
filter: text, |
|||
formatversion: '2' |
|||
} ).then( ( result ) => { |
|||
const data = result.abusefilterchecksyntax, |
|||
// eslint-disable-next-line max-len |
|||
regexDeprecated = /(article_(articleid|namespace|text|prefixedtext|restrictions_(edit|move|upload|create)|recent_contributors|first_contributor)|moved_(to|from)_(articleid|text|prefixedtext)|board_(articleid|text|prefixedtext)|article_views)/g, |
|||
// eslint-disable-next-line max-len |
|||
performanceVariable = /((edit_diff|added_lines|new)_pst|(added|removed)_links|page_(recent_contributors|first_contributor)|new_(html|text)|old_wikitext)/g; |
|||
$.removeSpinner( 'abusefilter-syntaxcheck' ); |
|||
$( document.getElementById( 'abusefilter-checksyntaxresultfield' ) ).empty(); |
|||
if ( data.status === 'ok' ) { |
|||
const messageOk = new OO.ui.MessageWidget( { |
|||
type: 'success', |
|||
inline: true, |
|||
label: mw.msg( 'abusefilter-edit-syntaxok' ) |
|||
} ); |
|||
$( document.getElementById( 'abusefilter-checksyntaxresultfield' ) ).append( messageOk.$element ); |
|||
} else if ( data.status === 'error' ) { |
|||
const messageError = new OO.ui.MessageWidget( { |
|||
type: 'error', |
|||
inline: true, |
|||
label: `${mw.msg( 'abusefilter-edit-syntaxerr' ).replace( /: \$1/, '' )}: ${data.message}` |
|||
} ); |
|||
$( document.getElementById( 'abusefilter-checksyntaxresultfield' ) ).append( messageError.$element ); |
|||
if ( useAce === true ) { |
|||
const position = filterEditor |
|||
.session |
|||
.getDocument() |
|||
.indexToPosition( data.character ); |
|||
filterEditor.focus(); |
|||
filterEditor.navigateTo( position.row, position.column ); |
|||
filterEditor.scrollToRow( position.row ); |
|||
} else { |
|||
$( document.getElementById( 'wpFilterRules' ) ).trigger( 'focus' ).textSelection( 'setSelection', { |
|||
start: data.character |
|||
} ); |
|||
} |
|||
} |
|||
// Variables obsolètes |
|||
if ( regexDeprecated.test( text ) ) { |
|||
const deprectatedArray = text.match( regexDeprecated ); |
|||
const messageDeprecated = new OO.ui.MessageWidget( { |
|||
type: 'warning', |
|||
inline: true, |
|||
label: `Problème potentiel identifié : Ce code contient des variables obsolètes (${deprectatedArray.join( ', ' )}).` |
|||
} ); |
|||
$( document.getElementById( 'abusefilter-checksyntaxresultfield' ) ).append( messageDeprecated.$element ); |
|||
} |
|||
// Performances |
|||
if ( |
|||
( /\/\*[\s]?aftools-disable-perfs[\s]?\*\//ig ).test( text ) === false && |
|||
performanceVariable.test( text ) |
|||
) { |
|||
const performanceArray = text.match( performanceVariable ); |
|||
const messagePerformance = new OO.ui.MessageWidget( { |
|||
type: 'notice', |
|||
inline: true, |
|||
label: new OO.ui.HtmlSnippet( ` |
|||
Suggestion d'amélioration : ce code contient des variables gourmandes en ressources (${performanceArray.join( ', ' )}), |
|||
por des raisons de performance, remplacez les par <code>added_lines</code> ou <code>removed_lines</code> si possible |
|||
et/ou placez les en fin de condition puis ajoutez <code>/* aftools-disable-perfs */</code> au début du code. |
|||
Lire <a href="Special:MyLanguage/Extension:AbuseFilter/Rules_format#Performance">ceci</a> pour plus de détails.` ) |
|||
} ); |
|||
$( document.getElementById( 'abusefilter-checksyntaxresultfield' ) ).append( messagePerformance.$element ); |
|||
} |
|||
}, () => { |
|||
$.removeSpinner( 'abusefilter-syntaxcheck' ); |
|||
} ); |
|||
} ); |
|||
} ); |
|||
} |
|||
} ); |
|||