Benutzer:Martinnitus/common.js/linkchecker.js: Unterschied zwischen den Versionen
Aus Stupidedia, der sinnfreien Enzyklopädie!
(Neu: „//<nowiki>“) |
|||
Zeile 1: | Zeile 1: | ||
− | //< | + | /** |
+ | * Linkchecker 2.0 | ||
+ | * <nowiki> | ||
+ | */ | ||
+ | |||
+ | gLinkchecker.pinhaltbool = false; | ||
+ | gLinkchecker.pinhalt = {}; | ||
+ | gLinkchecker.pobj = function( p ){ | ||
+ | return { | ||
+ | action: 'parse', | ||
+ | page: p, | ||
+ | disablepp: true, | ||
+ | prop: 'wikitext|text|links', | ||
+ | }; | ||
+ | }; | ||
+ | gLinkchecker.init = function() { | ||
+ | if ( mw.config.get( 'wgAction' ) == 'view' && global.isArticle && global.pageExists ) { | ||
+ | $( mw.util.addPortletLink( 'p-cactions', '#', 'Linkcheck', 'ca-check' ) ).find( 'a' ).click( gLinkchecker.search ); | ||
+ | } | ||
+ | }; | ||
+ | |||
+ | gLinkchecker.search = function( e ) { | ||
+ | e.preventDefault(); | ||
+ | |||
+ | gLinkchecker.results = []; | ||
+ | gLinkchecker.backlinks = []; | ||
+ | |||
+ | $( '#gLinkchecker-container' ).remove(); | ||
+ | $( '#mw-content-text' ).hide().before( '<div id="gLinkchecker-results" style="font-family: Courier; background-color: #DDD;"></div>\n<div id="gLinkchecker-container"><p>Liste wird geladen …</p><p></p></div>' ); | ||
+ | var link = $( '<a>Zurück zum Artikel</a>' ).click( function() { | ||
+ | $( '#gLinkchecker-container, #gLinkchecker-results' ).remove(); | ||
+ | $( '#mw-content-text' ).show(); | ||
+ | } ); | ||
+ | $( '#gLinkchecker-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, ' ' ) ) { | ||
+ | gLinkchecker.results.push( title ); | ||
+ | } | ||
+ | } ); | ||
+ | if ( gLinkchecker.results.length === 0 ) { | ||
+ | $( '#gLinkchecker-container p:first' ).html( 'Suche ergebnislos.' ); | ||
+ | return; | ||
+ | } | ||
+ | gLinkchecker.getBacklinks(); | ||
+ | } ); | ||
+ | }; | ||
+ | |||
+ | gLinkchecker.getBacklinks = function( cont ) { | ||
+ | var obj = { | ||
+ | action: 'query', | ||
+ | list: 'backlinks', | ||
+ | bltitle: mw.config.get( 'wgPageName' ), | ||
+ | bllimit: 'max' | ||
+ | }; | ||
+ | if ( cont ) { | ||
+ | obj.blcontinue = cont; | ||
+ | } | ||
+ | api.request( obj, gLinkchecker.receiveBacklinks ); | ||
+ | }; | ||
+ | |||
+ | gLinkchecker.receiveBacklinks = function( data ) { | ||
+ | var pages = api.makeArray( data ); | ||
+ | $.each( pages, function() { | ||
+ | gLinkchecker.backlinks.push( $.trim( this.title ) ); | ||
+ | } ); | ||
+ | |||
+ | if ( data['query-continue'] ) { | ||
+ | gLinkchecker.getBacklinks( data['query-continue'].backlinks.blcontinue ); | ||
+ | } | ||
+ | |||
+ | else { | ||
+ | var unlinked = []; | ||
+ | $.each( gLinkchecker.results, function() { | ||
+ | var title = this.toString(); | ||
+ | if ( $.inArray( title, gLinkchecker.backlinks ) === -1 ) { | ||
+ | unlinked.push( title ); | ||
+ | } | ||
+ | } ); | ||
+ | if ( unlinked.length === 0 ) { | ||
+ | $( '#gLinkchecker-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>'; | ||
+ | $( '#gLinkchecker-container p:first' ).html( '\n' + list + '\n' ); | ||
+ | $( '<input />' ).attr({ | ||
+ | type: 'button', | ||
+ | value: 'Ausgewählte verlinken' | ||
+ | }).click(function(){ | ||
+ | gLinkchecker.arts = $( '#gLinkchecker-container p:first' ).find( 'input[type="checkbox"]:checked' ).toArray(); | ||
+ | if( gLinkchecker.arts.length ){ | ||
+ | $( '#gLinkchecker-container p' ).hide(); | ||
+ | $( '#gLinkchecker-results' ).html( 'Bitte warten…' ); | ||
+ | api.request( gLinkchecker.pobj( gLinkchecker.arts[0].id ), gLinkchecker.parsed, 0 ); | ||
+ | } | ||
+ | }).appendTo( '#gLinkchecker-container p:first' ); | ||
+ | } | ||
+ | }; | ||
+ | |||
+ | gLinkchecker.parsed = function( c, i ){ | ||
+ | $( '#gLinkchecker-results' ).html( 'Ermittle Artikel ' + (i+1) + ' von ' + gLinkchecker.arts.length + '…' ); | ||
+ | var artext, w = mw.config.get( 'wgPageName' ); | ||
+ | function ex( e ){ | ||
+ | return ( artext.match( e ) !== null ); | ||
+ | } | ||
+ | artext = c.parse.wikitext['*'].replace( new RegExp( w, 'gi'), '[[' + w + ']]'); // | ||
+ | var temexp = new RegExp( '(\\{\\{([Vv]orlage:)?(?!Gedicht\\||Toggle\\||Zitat\\|).*(\\[\\[' + w + '\\]\\]).*\\}\\})', 'i' ); | ||
+ | while( ex( temexp ) ){ | ||
+ | artext = artext.replace( ilexp, '$1' ); | ||
+ | artext = artext.replace( RegExp.$1, RegExp.$1.replace( new RegExp( '(\\[\\[)(?=' + w + ')', 'gi' ), '' ) ); | ||
+ | artext = artext.replace( new RegExp( '(\\{\\{[^\\{\\}]*(' + w + '\\]\\])[^\\[\\]]*\\}\\})', 'i' ), '$1' ); | ||
+ | artext = artext.replace( RegExp.$1, RegExp.$1.split('').reverse().join('').replace( new RegExp( '\\]\\](?=' + w.split('').reverse().join('') + ')', 'gi' ) , '' ).split( '' ).reverse().join( '' ) ); | ||
+ | } | ||
+ | var komexp = new RegExp( '(\\<\\!\\-\\-.*(\\[\\[' + w + '\\]\\]).*\\-\\-\\>)', 'i' ); | ||
+ | while( ex( komexp ) ){ | ||
+ | artext = artext.replace( ilexp, '$1' ); | ||
+ | artext = artext.replace( RegExp.$1, RegExp.$1.replace( new RegExp( '(\\[\\[)(?=' + w + ')', 'gi' ), '' ) ); | ||
+ | artext = artext.replace( new RegExp( '(\\<\\!\\-\\-[^\\-]*(' + w + '\\]\\])[^\\[\\]]*\\-\\-\\>)', 'i' ), '$1' ); | ||
+ | artext = artext.replace( RegExp.$1, RegExp.$1.split('').reverse().join('').replace( new RegExp( '\\]\\](?=' + w.split('').reverse().join('') + ')', 'gi' ) , '' ).split( '' ).reverse().join( '' ) ); | ||
+ | } | ||
+ | var ilexp = new RegExp( '(\\[\\[[^\\]]*(\\[\\[' + w + '\\]\\])[^\\]]*\\]\\])', 'i' ); | ||
+ | while( ex( ilexp ) ){ | ||
+ | artext = artext.replace( ilexp, '$1' ); | ||
+ | artext = artext.replace( RegExp.$1, RegExp.$1.replace( new RegExp( '(\\[\\[)(?=' + w + ')', 'gi' ), '' ) ); | ||
+ | artext = artext.replace( new RegExp( '(\\[\\[[^\\[\\]]*(' + w + '\\]\\])[^\\[\\]]*\\]\\])', 'i' ), '$1' ); | ||
+ | artext = artext.replace( RegExp.$1, RegExp.$1.split('').reverse().join('').replace( new RegExp( '\\]\\](?=' + w.split('').reverse().join('') + ')', 'gi' ) , '' ).split( '' ).reverse().join( '' ) ); | ||
+ | } | ||
+ | var spl = artext.split( '[[' + w + ']]' ); | ||
+ | if( spl.slice( 1 ).length ){ //Verlinkbare Treffer gefunden | ||
+ | artext = [ spl[0], spl.slice( 1 ).join( w ) ].join( '[[' + w + ']]' ); | ||
+ | gLinkchecker.pinhaltbool = true; | ||
+ | gLinkchecker.pinhalt[ c.parse.title ] = artext; | ||
+ | } | ||
+ | if( i + 1 !== gLinkchecker.arts.length ){ | ||
+ | api.request( gLinkchecker.pobj( gLinkchecker.arts[++i].id ), gLinkchecker.parsed, i ); | ||
+ | }else{ | ||
+ | gLinkchecker.change(); | ||
+ | } | ||
+ | }; | ||
+ | |||
+ | gLinkchecker.change = function(){ | ||
+ | if( !gLinkchecker.pinhaltbool ){ | ||
+ | alert( 'Keine verlinkbaren Treffer gefunden.' ); | ||
+ | window.location.reload(); | ||
+ | }else{ | ||
+ | $( '#gLinkchecker-results' ).html( '<ul></ul>' ); | ||
+ | function linkstring( a ){ | ||
+ | return '<a href="/stupi/' + mw.util.wikiUrlencode( a ) + '" title="' + a + '">' + a + '</a>'; | ||
+ | } | ||
+ | $.each( gLinkchecker.pinhalt, function( i, p ){ | ||
+ | $( '#gLinkchecker-results ul' ).append( '\n<li>' + linkstring( i ) + ': <span style="color: green;">Bearbeitet.</span></li>' ); | ||
+ | api.request({ | ||
+ | action: 'edit', | ||
+ | title: i, | ||
+ | summary: '[[' + mw.config.get('wgPageName') + '|' + mw.config.get('wgPageName') + ' verlinkt.]]', | ||
+ | text: p, | ||
+ | token: global.editToken | ||
+ | },function( data ){ | ||
+ | if( data.error ){ | ||
+ | $( '#gLinkchecker-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' ){ | ||
+ | $( '#gLinkchecker-results ul' ).append( '\n<li>' + linkstring( i ) + ': <span style="color: red;">Unbekannter Fehler.</span></li>' ); | ||
+ | }else{ | ||
+ | $( '#gLinkchecker-results ul' ).append( '\n<li>' + linkstring( i ) + ': <span style="color: green;">Bearbeitet.</span></li>' ); | ||
+ | } | ||
+ | }); | ||
+ | }); | ||
+ | $( '#gLinkchecker-container p:last' ).show(); | ||
+ | } | ||
+ | }; | ||
+ | |||
+ | execute( 'gLinkchecker.init' ); |
Version vom 20. Februar 2015, 18:44 Uhr
/**
* Linkchecker 2.0
* <nowiki>
*/
gLinkchecker.pinhaltbool = false;
gLinkchecker.pinhalt = {};
gLinkchecker.pobj = function( p ){
return {
action: 'parse',
page: p,
disablepp: true,
prop: 'wikitext|text|links',
};
};
gLinkchecker.init = function() {
if ( mw.config.get( 'wgAction' ) == 'view' && global.isArticle && global.pageExists ) {
$( mw.util.addPortletLink( 'p-cactions', '#', 'Linkcheck', 'ca-check' ) ).find( 'a' ).click( gLinkchecker.search );
}
};
gLinkchecker.search = function( e ) {
e.preventDefault();
gLinkchecker.results = [];
gLinkchecker.backlinks = [];
$( '#gLinkchecker-container' ).remove();
$( '#mw-content-text' ).hide().before( '<div id="gLinkchecker-results" style="font-family: Courier; background-color: #DDD;"></div>\n<div id="gLinkchecker-container"><p>Liste wird geladen …</p><p></p></div>' );
var link = $( '<a>Zurück zum Artikel</a>' ).click( function() {
$( '#gLinkchecker-container, #gLinkchecker-results' ).remove();
$( '#mw-content-text' ).show();
} );
$( '#gLinkchecker-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, ' ' ) ) {
gLinkchecker.results.push( title );
}
} );
if ( gLinkchecker.results.length === 0 ) {
$( '#gLinkchecker-container p:first' ).html( 'Suche ergebnislos.' );
return;
}
gLinkchecker.getBacklinks();
} );
};
gLinkchecker.getBacklinks = function( cont ) {
var obj = {
action: 'query',
list: 'backlinks',
bltitle: mw.config.get( 'wgPageName' ),
bllimit: 'max'
};
if ( cont ) {
obj.blcontinue = cont;
}
api.request( obj, gLinkchecker.receiveBacklinks );
};
gLinkchecker.receiveBacklinks = function( data ) {
var pages = api.makeArray( data );
$.each( pages, function() {
gLinkchecker.backlinks.push( $.trim( this.title ) );
} );
if ( data['query-continue'] ) {
gLinkchecker.getBacklinks( data['query-continue'].backlinks.blcontinue );
}
else {
var unlinked = [];
$.each( gLinkchecker.results, function() {
var title = this.toString();
if ( $.inArray( title, gLinkchecker.backlinks ) === -1 ) {
unlinked.push( title );
}
} );
if ( unlinked.length === 0 ) {
$( '#gLinkchecker-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>';
$( '#gLinkchecker-container p:first' ).html( '\n' + list + '\n' );
$( '<input />' ).attr({
type: 'button',
value: 'Ausgewählte verlinken'
}).click(function(){
gLinkchecker.arts = $( '#gLinkchecker-container p:first' ).find( 'input[type="checkbox"]:checked' ).toArray();
if( gLinkchecker.arts.length ){
$( '#gLinkchecker-container p' ).hide();
$( '#gLinkchecker-results' ).html( 'Bitte warten…' );
api.request( gLinkchecker.pobj( gLinkchecker.arts[0].id ), gLinkchecker.parsed, 0 );
}
}).appendTo( '#gLinkchecker-container p:first' );
}
};
gLinkchecker.parsed = function( c, i ){
$( '#gLinkchecker-results' ).html( 'Ermittle Artikel ' + (i+1) + ' von ' + gLinkchecker.arts.length + '…' );
var artext, w = mw.config.get( 'wgPageName' );
function ex( e ){
return ( artext.match( e ) !== null );
}
artext = c.parse.wikitext['*'].replace( new RegExp( w, 'gi'), '[[' + w + ']]'); //
var temexp = new RegExp( '(\\{\\{([Vv]orlage:)?(?!Gedicht\\||Toggle\\||Zitat\\|).*(\\[\\[' + w + '\\]\\]).*\\}\\})', 'i' );
while( ex( temexp ) ){
artext = artext.replace( ilexp, '$1' );
artext = artext.replace( RegExp.$1, RegExp.$1.replace( new RegExp( '(\\[\\[)(?=' + w + ')', 'gi' ), '' ) );
artext = artext.replace( new RegExp( '(\\{\\{[^\\{\\}]*(' + w + '\\]\\])[^\\[\\]]*\\}\\})', 'i' ), '$1' );
artext = artext.replace( RegExp.$1, RegExp.$1.split('').reverse().join('').replace( new RegExp( '\\]\\](?=' + w.split('').reverse().join('') + ')', 'gi' ) , '' ).split( '' ).reverse().join( '' ) );
}
var komexp = new RegExp( '(\\<\\!\\-\\-.*(\\[\\[' + w + '\\]\\]).*\\-\\-\\>)', 'i' );
while( ex( komexp ) ){
artext = artext.replace( ilexp, '$1' );
artext = artext.replace( RegExp.$1, RegExp.$1.replace( new RegExp( '(\\[\\[)(?=' + w + ')', 'gi' ), '' ) );
artext = artext.replace( new RegExp( '(\\<\\!\\-\\-[^\\-]*(' + w + '\\]\\])[^\\[\\]]*\\-\\-\\>)', 'i' ), '$1' );
artext = artext.replace( RegExp.$1, RegExp.$1.split('').reverse().join('').replace( new RegExp( '\\]\\](?=' + w.split('').reverse().join('') + ')', 'gi' ) , '' ).split( '' ).reverse().join( '' ) );
}
var ilexp = new RegExp( '(\\[\\[[^\\]]*(\\[\\[' + w + '\\]\\])[^\\]]*\\]\\])', 'i' );
while( ex( ilexp ) ){
artext = artext.replace( ilexp, '$1' );
artext = artext.replace( RegExp.$1, RegExp.$1.replace( new RegExp( '(\\[\\[)(?=' + w + ')', 'gi' ), '' ) );
artext = artext.replace( new RegExp( '(\\[\\[[^\\[\\]]*(' + w + '\\]\\])[^\\[\\]]*\\]\\])', 'i' ), '$1' );
artext = artext.replace( RegExp.$1, RegExp.$1.split('').reverse().join('').replace( new RegExp( '\\]\\](?=' + w.split('').reverse().join('') + ')', 'gi' ) , '' ).split( '' ).reverse().join( '' ) );
}
var spl = artext.split( '[[' + w + ']]' );
if( spl.slice( 1 ).length ){ //Verlinkbare Treffer gefunden
artext = [ spl[0], spl.slice( 1 ).join( w ) ].join( '[[' + w + ']]' );
gLinkchecker.pinhaltbool = true;
gLinkchecker.pinhalt[ c.parse.title ] = artext;
}
if( i + 1 !== gLinkchecker.arts.length ){
api.request( gLinkchecker.pobj( gLinkchecker.arts[++i].id ), gLinkchecker.parsed, i );
}else{
gLinkchecker.change();
}
};
gLinkchecker.change = function(){
if( !gLinkchecker.pinhaltbool ){
alert( 'Keine verlinkbaren Treffer gefunden.' );
window.location.reload();
}else{
$( '#gLinkchecker-results' ).html( '<ul></ul>' );
function linkstring( a ){
return '<a href="/stupi/' + mw.util.wikiUrlencode( a ) + '" title="' + a + '">' + a + '</a>';
}
$.each( gLinkchecker.pinhalt, function( i, p ){
$( '#gLinkchecker-results ul' ).append( '\n<li>' + linkstring( i ) + ': <span style="color: green;">Bearbeitet.</span></li>' );
api.request({
action: 'edit',
title: i,
summary: '[[' + mw.config.get('wgPageName') + '|' + mw.config.get('wgPageName') + ' verlinkt.]]',
text: p,
token: global.editToken
},function( data ){
if( data.error ){
$( '#gLinkchecker-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' ){
$( '#gLinkchecker-results ul' ).append( '\n<li>' + linkstring( i ) + ': <span style="color: red;">Unbekannter Fehler.</span></li>' );
}else{
$( '#gLinkchecker-results ul' ).append( '\n<li>' + linkstring( i ) + ': <span style="color: green;">Bearbeitet.</span></li>' );
}
});
});
$( '#gLinkchecker-container p:last' ).show();
}
};
execute( 'gLinkchecker.init' );