Benutzer:Martinnitus/common.js/linkchecker.js: Unterschied zwischen den Versionen
Aus Stupidedia, der sinnfreien Enzyklopädie!
K |
|||
Zeile 8: | Zeile 8: | ||
linkchecker.init = function() { | linkchecker.init = function() { | ||
if ( mw.config.get( 'wgAction' ) == 'view' && global.isArticle && global.pageExists ) { | if ( mw.config.get( 'wgAction' ) == 'view' && global.isArticle && global.pageExists ) { | ||
+ | if( mw.config.get( 'wgNamespaceNumber' ) !== 0 ) | ||
+ | linkchecker.alttext = mw.config.get( 'wgTitle' ); | ||
$( mw.util.addPortletLink( 'p-cactions', '#', 'Linkcheck', 'ca-check' ) ).find( 'a' ).click( linkchecker.search ); | $( mw.util.addPortletLink( 'p-cactions', '#', 'Linkcheck', 'ca-check' ) ).find( 'a' ).click( linkchecker.search ); | ||
+ | $( mw.util.addPortletLink( 'p-cactions', '#', 'Alternativ-Linkcheck', 'ca-altcheck' ) ).find( 'a' ).click( linkchecker.altsearch ); | ||
} | } | ||
+ | }; | ||
+ | linkchecker.pobj = function( p ){ | ||
+ | return { | ||
+ | action: 'parse', | ||
+ | page: p, | ||
+ | disablepp: true, | ||
+ | prop: 'wikitext|links', | ||
+ | }; | ||
+ | }; | ||
+ | linkchecker.altsearch = function( e ) { | ||
+ | e.preventDefault(); | ||
+ | var tex = $('<input />',{ | ||
+ | type: 'text' | ||
+ | }); | ||
+ | var but = $('<input />',{ | ||
+ | type: 'button', | ||
+ | value: 'Suche', | ||
+ | click: function(){ | ||
+ | linkchecker.alttext = tex.val(); | ||
+ | linkchecker.search( e ); | ||
+ | } | ||
+ | }); | ||
+ | $( '#ca-altcheck a' ).unbind( 'click' ).html( '' ).css( 'text-decoration', 'none' ).append( tex, ' ', but ); | ||
}; | }; | ||
Zeile 17: | Zeile 43: | ||
linkchecker.results = []; | linkchecker.results = []; | ||
linkchecker.backlinks = []; | linkchecker.backlinks = []; | ||
− | + | linkchecker.wgname = linkchecker.alttext ? linkchecker.alttext : mw.config.get( 'wgPageName' ).replace( /_/g, ' ' ); | |
+ | linkchecker.wgtitle = linkchecker.alttext ? linkchecker.alttext : mw.config.get( 'wgTitle' ); | ||
+ | linkchecker.altbool = linkchecker.wgname !== mw.config.get( 'wgPageName' ); | ||
$( '#linkchecker-container' ).remove(); | $( '#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>' ); | $( '#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>' ); | ||
Zeile 30: | Zeile 58: | ||
list: 'search', | list: 'search', | ||
srwhat: 'text', | srwhat: 'text', | ||
− | srsearch: '"' + | + | srsearch: '"' + linkchecker.wgtitle + '"', |
srlimit: 'max', | srlimit: 'max', | ||
srnamespace: '0|102|104|106' | srnamespace: '0|102|104|106' | ||
Zeile 37: | Zeile 65: | ||
$.each( results, function() { | $.each( results, function() { | ||
var title = $.trim( this.title ); | var title = $.trim( this.title ); | ||
− | if ( title !== | + | if ( title !== linkchecker.wgname ) { |
linkchecker.results.push( title ); | linkchecker.results.push( title ); | ||
} | } | ||
Zeile 53: | Zeile 81: | ||
action: 'query', | action: 'query', | ||
list: 'backlinks', | list: 'backlinks', | ||
− | bltitle: | + | bltitle: linkchecker.wgname, |
bllimit: 'max' | bllimit: 'max' | ||
}; | }; | ||
Zeile 99: | Zeile 127: | ||
$( '#linkchecker-container p' ).hide(); | $( '#linkchecker-container p' ).hide(); | ||
$( '#linkchecker-results' ).html( 'Bitte warten…' ); | $( '#linkchecker-results' ).html( 'Bitte warten…' ); | ||
− | api. | + | api.request( linkchecker.pobj( linkchecker.arts[0].id ), linkchecker.content, 0 ); |
} | } | ||
}).appendTo( '#linkchecker-container p:first' ); | }).appendTo( '#linkchecker-container p:first' ); | ||
Zeile 107: | Zeile 135: | ||
linkchecker.content = function( c, i ){ | linkchecker.content = function( c, i ){ | ||
$( '#linkchecker-results' ).html( 'Ermittle Artikel ' + (i+1) + ' von ' + linkchecker.arts.length + '…' ); | $( '#linkchecker-results' ).html( 'Ermittle Artikel ' + (i+1) + ' von ' + linkchecker.arts.length + '…' ); | ||
− | var w = | + | var w = linkchecker.wgname.replace( /_/g, ' ' ); |
− | var spl, artext = | + | var spl = [], linked = false, artext; |
+ | $.each( c.parse.links, function( i, link ){ | ||
+ | if( link['*'] === $.ucFirst( mw.config.get('wgPageName').replace( /_/g, ' ' ) ) ){ | ||
+ | linked = true; | ||
+ | return false; | ||
+ | } | ||
+ | }); | ||
function lreplace( e, r, m ){ | function lreplace( e, r, m ){ | ||
var multiline = m === true ? 'm' : ''; | var multiline = m === true ? 'm' : ''; | ||
Zeile 119: | Zeile 153: | ||
} | } | ||
function rep(){ | function rep(){ | ||
+ | artext = c.parse.wikitext['*'].replace( new RegExp( w, 'g'), '[[' + w + ']]'); | ||
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\\||[Kk]lappe\\|)[^]*(\\[\\[' + w + '\\]\\])[^]*\\}\\})', '(\\{\\{[^]*(' + w + '\\]\\])[^]*\\}\\})' ); | lreplace( '(\\{\\{([Vv]orlage:)?(?![Gg]edicht\\||[Tt]oggle\\||[Zz]itat\\||[Kk]lappe\\|)[^]*(\\[\\[' + w + '\\]\\])[^]*\\}\\})', '(\\{\\{[^]*(' + w + '\\]\\])[^]*\\}\\})' ); | ||
Zeile 125: | Zeile 160: | ||
spl = artext.split( '[[' + w + ']]' ); | spl = artext.split( '[[' + w + ']]' ); | ||
} | } | ||
− | rep(); | + | if( !linked ) |
− | if( !spl.slice( 1 ).length && c.match( new RegExp( w.charAt(0).toLowerCase() + w.slice(1) ) ) !== null ){ | + | rep(); |
+ | if( !linked && !spl.slice( 1 ).length && c.parse.wikitext['*'].match( new RegExp( w.charAt(0).toLowerCase() + w.slice(1) ) ) !== null ){ | ||
w = w.charAt(0).toLowerCase() + w.slice(1); | w = w.charAt(0).toLowerCase() + w.slice(1); | ||
− | |||
rep(); | rep(); | ||
} | } | ||
if( spl.slice( 1 ).length ){ //Verlinkbare Treffer gefunden | if( spl.slice( 1 ).length ){ //Verlinkbare Treffer gefunden | ||
artext = [ spl[0], spl.slice( 1 ).join( w ) ].join( '[[' + w + ']]' ); | artext = [ spl[0], spl.slice( 1 ).join( w ) ].join( '[[' + w + ']]' ); | ||
+ | if( linkchecker.altbool ) | ||
+ | artext = artext.replace('[[' + w + ']]','[[' + mw.config.get('wgPageName').replace( /_/g, ' ' ) + '|' + w + ']]'); | ||
linkchecker.pinhaltbool = true; | linkchecker.pinhaltbool = true; | ||
linkchecker.pinhalt[ linkchecker.arts[i].id ] = artext; | linkchecker.pinhalt[ linkchecker.arts[i].id ] = artext; | ||
} | } | ||
if( i + 1 !== linkchecker.arts.length ){ | if( i + 1 !== linkchecker.arts.length ){ | ||
− | api. | + | api.request( linkchecker.pobj( linkchecker.arts[++i].id ), linkchecker.content, i ); |
}else{ | }else{ | ||
linkchecker.change(); | linkchecker.change(); | ||
Zeile 156: | Zeile 193: | ||
action: 'edit', | action: 'edit', | ||
title: i, | title: i, | ||
− | summary: '[[' + mw.config.get('wgPageName') + '|' + mw.config.get('wgPageName').replace( /_/g, ' ' ) + ' verlinkt.]]', | + | summary: '[[' + mw.config.get('wgPageName').replace( /_/g, ' ' ) + '|' + ( linkchecker.altbool ? linkchecker.wgname + ' auf ' : '' ) + mw.config.get('wgPageName').replace( /_/g, ' ' ) + ' verlinkt.]]', |
text: p, | text: p, | ||
token: global.editToken | token: global.editToken |
Version vom 27. Februar 2015, 23:38 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 ) {
if( mw.config.get( 'wgNamespaceNumber' ) !== 0 )
linkchecker.alttext = mw.config.get( 'wgTitle' );
$( mw.util.addPortletLink( 'p-cactions', '#', 'Linkcheck', 'ca-check' ) ).find( 'a' ).click( linkchecker.search );
$( mw.util.addPortletLink( 'p-cactions', '#', 'Alternativ-Linkcheck', 'ca-altcheck' ) ).find( 'a' ).click( linkchecker.altsearch );
}
};
linkchecker.pobj = function( p ){
return {
action: 'parse',
page: p,
disablepp: true,
prop: 'wikitext|links',
};
};
linkchecker.altsearch = function( e ) {
e.preventDefault();
var tex = $('<input />',{
type: 'text'
});
var but = $('<input />',{
type: 'button',
value: 'Suche',
click: function(){
linkchecker.alttext = tex.val();
linkchecker.search( e );
}
});
$( '#ca-altcheck a' ).unbind( 'click' ).html( '' ).css( 'text-decoration', 'none' ).append( tex, ' ', but );
};
linkchecker.search = function( e ) {
e.preventDefault();
linkchecker.results = [];
linkchecker.backlinks = [];
linkchecker.wgname = linkchecker.alttext ? linkchecker.alttext : mw.config.get( 'wgPageName' ).replace( /_/g, ' ' );
linkchecker.wgtitle = linkchecker.alttext ? linkchecker.alttext : mw.config.get( 'wgTitle' );
linkchecker.altbool = linkchecker.wgname !== mw.config.get( 'wgPageName' );
$( '#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: '"' + linkchecker.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 !== linkchecker.wgname ) {
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: linkchecker.wgname,
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.request( linkchecker.pobj( 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 = linkchecker.wgname.replace( /_/g, ' ' );
var spl = [], linked = false, artext;
$.each( c.parse.links, function( i, link ){
if( link['*'] === $.ucFirst( mw.config.get('wgPageName').replace( /_/g, ' ' ) ) ){
linked = true;
return false;
}
});
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(){
artext = c.parse.wikitext['*'].replace( new RegExp( w, 'g'), '[[' + w + ']]');
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 + ']]' );
}
if( !linked )
rep();
if( !linked && !spl.slice( 1 ).length && c.parse.wikitext['*'].match( new RegExp( w.charAt(0).toLowerCase() + w.slice(1) ) ) !== null ){
w = w.charAt(0).toLowerCase() + w.slice(1);
rep();
}
if( spl.slice( 1 ).length ){ //Verlinkbare Treffer gefunden
artext = [ spl[0], spl.slice( 1 ).join( w ) ].join( '[[' + w + ']]' );
if( linkchecker.altbool )
artext = artext.replace('[[' + w + ']]','[[' + mw.config.get('wgPageName').replace( /_/g, ' ' ) + '|' + w + ']]');
linkchecker.pinhaltbool = true;
linkchecker.pinhalt[ linkchecker.arts[i].id ] = artext;
}
if( i + 1 !== linkchecker.arts.length ){
api.request( linkchecker.pobj( 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').replace( /_/g, ' ' ) + '|' + ( linkchecker.altbool ? linkchecker.wgname + ' auf ' : '' ) + 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' );