Benutzer:Martinnitus/common.js/linkchecker.js: Unterschied zwischen den Versionen
Aus Stupidedia, der sinnfreien Enzyklopädie!
K |
|||
Zeile 120: | Zeile 120: | ||
function rep(){ | function rep(){ | ||
lreplace( '(^\\={1,6}[^\\=]*(\\[\\[' + w + '\\]\\])[^\\=]*\\={1,6} *$)', '(^\\={1,6}[^\\=]*(' + w + '\\]\\])[^\\=]*\\={1,6} *$)', true ); | lreplace( '(^\\={1,6}[^\\=]*(\\[\\[' + w + '\\]\\])[^\\=]*\\={1,6} *$)', '(^\\={1,6}[^\\=]*(' + w + '\\]\\])[^\\=]*\\={1,6} *$)', true ); | ||
− | lreplace( '(\\{\\{([Vv]orlage:)?(?![Gg]edicht\\||[Tt]oggle\\||[Zz]itat\\|)[^]*(\\[\\[' + w + '\\]\\])[^]*\\}\\})', '(\\{\\{[^]*(' + w + '\\]\\])[^]*\\}\\})' ); | + | lreplace( '(\\{\\{([Vv]orlage:)?(?![Gg]edicht\\||[Tt]oggle\\||[Zz]itat\\||[Kk]lappe\\|)[^]*(\\[\\[' + w + '\\]\\])[^]*\\}\\})', '(\\{\\{[^]*(' + w + '\\]\\])[^]*\\}\\})' ); |
lreplace( '(\\<\\!\\-\\-[^]*(\\[\\[' + w + '\\]\\])[^]*\\-\\-\\>)', '(\\<\\!\\-\\-[^]*(' + w + '\\]\\])[^]*\\-\\-\\>)' ); | lreplace( '(\\<\\!\\-\\-[^]*(\\[\\[' + w + '\\]\\])[^]*\\-\\-\\>)', '(\\<\\!\\-\\-[^]*(' + w + '\\]\\])[^]*\\-\\-\\>)' ); | ||
lreplace( '(\\[(\\[|http(s)?:\\/\\/)[^\\]]*(\\[\\[' + w + '\\]\\])[^\\]]*(\\])?\\])', '(\\[(\\[|http(s)?:\\/\\/)[^\\[\\]]*(' + w + '\\]\\])[^\\[\\]]*(\\])?\\])' ); | lreplace( '(\\[(\\[|http(s)?:\\/\\/)[^\\]]*(\\[\\[' + w + '\\]\\])[^\\]]*(\\])?\\])', '(\\[(\\[|http(s)?:\\/\\/)[^\\[\\]]*(' + w + '\\]\\])[^\\[\\]]*(\\])?\\])' ); |
Version vom 26. Februar 2015, 18:47 Uhr
/**
* Linkchecker 2.0
* <nowiki>
*/
var linkchecker = {};
linkchecker.pinhaltbool = false;
linkchecker.pinhalt = {};
linkchecker.init = function() {
if ( mw.config.get( 'wgAction' ) == 'view' && global.isArticle && global.pageExists ) {
$( mw.util.addPortletLink( 'p-cactions', '#', 'Linkcheck', 'ca-check' ) ).find( 'a' ).click( linkchecker.search );
}
};
linkchecker.search = function( e ) {
e.preventDefault();
linkchecker.results = [];
linkchecker.backlinks = [];
$( '#linkchecker-container' ).remove();
$( '#mw-content-text' ).hide().before( '<div id="linkchecker-results" style="font-family: Courier; background-color: #DDD;"></div>\n<div id="linkchecker-container"><p>Liste wird geladen …</p><p></p></div>' );
var link = $( '<a>Zurück zum Artikel</a>' ).click( function() {
$( '#linkchecker-container, #linkchecker-results' ).remove();
$( '#mw-content-text' ).show();
} );
$( '#linkchecker-container p:last' ).append( link );
api.request( {
action: 'query',
list: 'search',
srwhat: 'text',
srsearch: '"' + mw.config.get( 'wgTitle' ) + '"',
srlimit: 'max',
srnamespace: '0|102|104|106'
}, function( data ) {
var results = api.makeArray( data );
$.each( results, function() {
var title = $.trim( this.title );
if ( title !== mw.config.get( 'wgPageName' ).replace( /_/g, ' ' ) ) {
linkchecker.results.push( title );
}
} );
if ( linkchecker.results.length === 0 ) {
$( '#linkchecker-container p:first' ).html( 'Suche ergebnislos.' );
return;
}
linkchecker.getBacklinks();
} );
};
linkchecker.getBacklinks = function( cont ) {
var obj = {
action: 'query',
list: 'backlinks',
bltitle: mw.config.get( 'wgPageName' ),
bllimit: 'max'
};
if ( cont ) {
obj.blcontinue = cont;
}
api.request( obj, linkchecker.receiveBacklinks );
};
linkchecker.receiveBacklinks = function( data ) {
var pages = api.makeArray( data );
$.each( pages, function() {
linkchecker.backlinks.push( $.trim( this.title ) );
} );
if ( data['query-continue'] ) {
linkchecker.getBacklinks( data['query-continue'].backlinks.blcontinue );
}
else {
var unlinked = [];
$.each( linkchecker.results, function() {
var title = this.toString();
if ( $.inArray( title, linkchecker.backlinks ) === -1 ) {
unlinked.push( title );
}
} );
if ( unlinked.length === 0 ) {
$( '#linkchecker-container p:first' ).html( 'Keine unverlinkten Treffer gefunden.' );
return;
}
var list = 'In folgende Artikel können Links hierher gesetzt werden:<ul>';
$.each( unlinked, function( i ) {
var title = this.toString();
list += '<li><a href="' + mw.util.wikiGetlink( title ) + '">' + mw.html.escape( title ) + '</a><input type="checkbox" id="' + title + '" checked="checked" /></li>';
} );
list += '</ul>';
$( '#linkchecker-container p:first' ).html( '\n' + list + '\n' );
$( '<input />' ).attr({
type: 'button',
value: 'Ausgewählte verlinken'
}).click(function(){
linkchecker.arts = $( '#linkchecker-container p:first' ).find( 'input[type="checkbox"]:checked' ).toArray();
if( linkchecker.arts.length ){
$( '#linkchecker-container p' ).hide();
$( '#linkchecker-results' ).html( 'Bitte warten…' );
api.getPageContent( linkchecker.arts[0].id, linkchecker.content, 0 );
}
}).appendTo( '#linkchecker-container p:first' );
}
};
linkchecker.content = function( c, i ){
$( '#linkchecker-results' ).html( 'Ermittle Artikel ' + (i+1) + ' von ' + linkchecker.arts.length + '…' );
var w = mw.config.get( 'wgPageName' ).replace( /_/g, ' ' );
var spl, artext = c.replace( new RegExp( w, 'g'), '[[' + w + ']]');
function lreplace( e, r, m ){
var multiline = m === true ? 'm' : '';
while( artext.match( new RegExp( e, multiline ) ) !== null ){
artext = artext.replace( new RegExp( e, multiline ), '$1' );
artext = artext.replace( RegExp.$1, RegExp.$1.replace( new RegExp( '(\\[\\[)(?=' + w + ')', 'g' ), '' ) );
artext = artext.replace( new RegExp( r, multiline ), '$1' );
artext = artext.replace( RegExp.$1, RegExp.$1.split('').reverse().join('').replace( new RegExp( '\\]\\](?=' + w.split('').reverse().join('') + ')', 'g' ) , '' ).split( '' ).reverse().join( '' ) );
}
}
function rep(){
lreplace( '(^\\={1,6}[^\\=]*(\\[\\[' + w + '\\]\\])[^\\=]*\\={1,6} *$)', '(^\\={1,6}[^\\=]*(' + w + '\\]\\])[^\\=]*\\={1,6} *$)', true );
lreplace( '(\\{\\{([Vv]orlage:)?(?![Gg]edicht\\||[Tt]oggle\\||[Zz]itat\\||[Kk]lappe\\|)[^]*(\\[\\[' + w + '\\]\\])[^]*\\}\\})', '(\\{\\{[^]*(' + w + '\\]\\])[^]*\\}\\})' );
lreplace( '(\\<\\!\\-\\-[^]*(\\[\\[' + w + '\\]\\])[^]*\\-\\-\\>)', '(\\<\\!\\-\\-[^]*(' + w + '\\]\\])[^]*\\-\\-\\>)' );
lreplace( '(\\[(\\[|http(s)?:\\/\\/)[^\\]]*(\\[\\[' + w + '\\]\\])[^\\]]*(\\])?\\])', '(\\[(\\[|http(s)?:\\/\\/)[^\\[\\]]*(' + w + '\\]\\])[^\\[\\]]*(\\])?\\])' );
spl = artext.split( '[[' + w + ']]' );
}
rep();
if( !spl.slice( 1 ).length && c.match( new RegExp( w.charAt(0).toLowerCase() + w.slice(1) ) ) !== null ){
w = w.charAt(0).toLowerCase() + w.slice(1);
artext = c.replace( new RegExp( w, 'g'), '[[' + w + ']]');
rep();
}
if( spl.slice( 1 ).length ){ //Verlinkbare Treffer gefunden
artext = [ spl[0], spl.slice( 1 ).join( w ) ].join( '[[' + w + ']]' );
linkchecker.pinhaltbool = true;
linkchecker.pinhalt[ linkchecker.arts[i].id ] = artext;
}
if( i + 1 !== linkchecker.arts.length ){
api.getPageContent( linkchecker.arts[++i].id, linkchecker.content, i );
}else{
linkchecker.change();
}
};
linkchecker.change = function(){
if( !linkchecker.pinhaltbool ){
alert( 'Keine verlinkbaren Treffer gefunden.' );
window.location.reload();
}else{
$( '#linkchecker-results' ).html( '<ul></ul>' );
function linkstring( a ){
return '<a href="/stupi/' + mw.util.wikiUrlencode( a ) + '" title="' + a + '">' + a + '</a>';
}
$.each( linkchecker.pinhalt, function( i, p ){
api.request({
action: 'edit',
title: i,
summary: '[[' + mw.config.get('wgPageName') + '|' + mw.config.get('wgPageName').replace( /_/g, ' ' ) + ' verlinkt.]]',
text: p,
token: global.editToken
},function( data ){
if( data.error ){
$( '#linkchecker-results ul' ).append( '\n<li>' + linkstring( i ) + ': <span style="color: red;">Fehler. Code: ' + data.error.code + ', Info: ' + data.error.info + '.</span></li>' );
}else if( data.edit.result === 'Failure' ){
$( '#linkchecker-results ul' ).append( '\n<li>' + linkstring( i ) + ': <span style="color: red;">Unbekannter Fehler.</span></li>' );
}else{
$( '#linkchecker-results ul' ).append( '\n<li>' + linkstring( i ) + ': <span style="color: green;">Bearbeitet.</span></li>' );
}
});
});
$( '#linkchecker-container p:last' ).show();
}
};
execute( 'linkchecker.init' );