מדיה ויקי:Gadget-quickLinker.js: הבדלים בין גרסאות בדף
מאין תקציר עריכה |
מ 25 גרסאות של הדף wikipedia:he:מדיה_ויקי:Gadget-quickLinker.js יובאו |
||
| (18 גרסאות ביניים של 3 משתמשים אינן מוצגות) | |||
| שורה 1: | שורה 1: | ||
/* | /* | ||
This is | This is script for quickly adding links to article from related articles. It is especially useful for orphan articles (invoked automatically), stub articles (invoked automatically) and new articles which usually have less links compared to long existing articles. | ||
Author: [[:he:User:ערן]] | Author: [[:he:User:ערן]] | ||
*/ | */ | ||
if ( mw.config.get( 'wgNamespaceNumber' ) === 0 ) $(function(){ | if ( mw.config.get( 'wgNamespaceNumber' ) === 0 ) $(function(){ | ||
var titleNoBrackets = /(.+?)( \(|$)/.exec(mw.config.get('wgTitle')), allowedSuffix, allowedPrefix; | |||
var titleNoBrackets = /(.+?)( \(|$)/.exec(mw.config.get('wgTitle')); | |||
if (titleNoBrackets) titleNoBrackets = titleNoBrackets[1]; | if (titleNoBrackets) titleNoBrackets = titleNoBrackets[1]; | ||
else return; | else return; | ||
mw. | // allowedSuffix and allowedPrefix to have strict match for valid words (BOOK => BOOKs and not BOOKmark). default is non strict (which works better for long title) | ||
' | switch ( mw.config.get('wgContentLanguage') ) { | ||
case 'he': | |||
allowedSuffix = '(?=ים|ות|[^א-ת])'; | |||
allowedPrefix = '(?:[כלבמשהו]|[^א-ת])'; | |||
break; | |||
default: | |||
allowedSuffix = 's?[^a-z]'; | |||
allowedPrefix = ''; | |||
} | } | ||
function createQuickEditorDialog(searchRes){ | switch (mw.config.get('wgUserLanguage') ) { | ||
case 'he': | |||
mw.messages.set({ | |||
'qlinker-edit' : 'עריכה מהירה', | |||
'qlinker-cancel' : 'ביטול', | |||
'linkify-summary': 'הוספת קישור ל[[$1]]', | |||
'qlinker-save-success': 'שמירת $1 בוצעה בהצלחה!', | |||
'qlinker-save-continue': 'שמירה', | |||
'qlinker-skip': 'דילוג דף', | |||
'qlinker-skip-match': 'דילוג מופע', | |||
'qlinker-safe-search': 'חיפוש בטוח', | |||
'qlinker-invoke': 'הוספת בזק של קישורים', | |||
'qlinker-sidelink': 'הוספת קישורים', | |||
'qlinker-no-results': 'לא נמצאו תוצאות', | |||
'qlinker-confirm-masslink': 'לדף זה יש כבר הרבה קישורים, ונדרשת זהירות בהוספת הקישורים. האם נחוצים קישורים נוספים?' | |||
}); | |||
break; | |||
default: | |||
mw.messages.set({ | |||
'qlinker-edit' : 'Quick edit', | |||
'qlinker-cancel' : 'Cancel', | |||
'linkify-summary': 'Adding link to [[$1]]', | |||
'qlinker-save-success': '$1 has been saved successfully!', | |||
'qlinker-save-continue': 'Save', | |||
'qlinker-skip': 'Skip page', | |||
'qlinker-skip-match': 'Skip match', | |||
'qlinker-safe-search': 'Safe search', | |||
'qlinker-invoke': 'Quickly add links', | |||
'qlinker-sidelink': 'Add links', | |||
'qlinker-no-results': 'No results found', | |||
'qlinker-confirm-masslink': 'This page already has many links. Are you sure?' | |||
}); | |||
break; | |||
} | |||
function createQuickEditorDialog(searchRes, safeMode){ | |||
function QuickEditorDialog( config ) { | function QuickEditorDialog( config ) { | ||
this.contextRgx = new RegExp('.*[^\[]' + mw. | this.contextRgx = new RegExp('(.*[^\\[])(' + mw.util.escapeRegExp(titleNoBrackets) +')((?![^\\[\\{]*[\\]\\}]).*)', 'ig'); | ||
this.contextRgxLink = new RegExp('(.*\\[\\[)([^\\]]+?)\\|(' + mw.util.escapeRegExp(titleNoBrackets) +')(\\]\\].*)', 'ig'); | |||
this.pageI = -1; | this.pageI = -1; | ||
this.matchI = 0; | |||
this.searchData = null; | this.searchData = null; | ||
this.curPage = null; | this.curPage = null; | ||
this.safeMode = safeMode; | |||
this.skipsCounter = 0; | |||
this.starttimestamp = null; | |||
this.timestamp = null; | |||
QuickEditorDialog.super.call( this, config ); | QuickEditorDialog.super.call( this, config ); | ||
} | } | ||
| שורה 34: | שורה 75: | ||
QuickEditorDialog.static.name = 'QuickEditorDialog'; | QuickEditorDialog.static.name = 'QuickEditorDialog'; | ||
// Specify a title statically (or, alternatively, with data passed to the opening() method). | // Specify a title statically (or, alternatively, with data passed to the opening() method). | ||
QuickEditorDialog.static.title = mw.msg('qlinker- | QuickEditorDialog.static.title = mw.msg('qlinker-edit'); | ||
QuickEditorDialog.static.actions = [ | QuickEditorDialog.static.actions = [ | ||
{ action: 'saveContinue', label: mw.msg('qlinker-save-continue'), flags: [ 'other', ' | { action: 'saveContinue', label: mw.msg('qlinker-save-continue'), flags: [ 'other', 'progressive' ], icon: 'link' }, | ||
{ action: 'skipOne', label: mw.msg('qlinker-skip'), flags: [ 'other', 'progressive' ] }, | { action: 'skipOne', label: mw.msg('qlinker-skip'), flags: [ 'other', 'progressive' ], icon: 'next' }, | ||
{ label: ' | { action: 'skipMatch', label: mw.msg('qlinker-skip-match'), flags: [ 'other', 'progressive' ], icon: 'arrowNext' }, | ||
{ action: 'safeSearch', label: mw.msg('qlinker-safe-search'), flags: [ 'other' ], icon: 'search' }, | |||
{ label: mw.msg('qlinker-cancel'), flags: 'safe' } | |||
]; | ]; | ||
| שורה 61: | שורה 104: | ||
// Set up contents based on data | // Set up contents based on data | ||
this.searchData = data.searchData.query; | this.searchData = data.searchData.query; | ||
this.starttimestamp = data.searchData.curtimestamp; | |||
this.nextPage(); | this.nextPage(); | ||
}, this ); | }, this ); | ||
| שורה 72: | שורה 116: | ||
case 'skipOne': | case 'skipOne': | ||
return new OO.ui.Process( function () { | return new OO.ui.Process( function () { | ||
dialog.skipsCounter++; | |||
dialog.nextPage(); | dialog.nextPage(); | ||
}, this ); | |||
case 'skipMatch': | |||
return new OO.ui.Process( function () { | |||
dialog.skipsCounter++; | |||
dialog.nextMatch(); | |||
}, this ); | }, this ); | ||
case 'saveContinue': | case 'saveContinue': | ||
return new OO.ui.Process( function () { | return new OO.ui.Process( function () { | ||
dialog.savePage(); | dialog.savePage(); | ||
}, this ); | |||
case 'safeSearch': | |||
return new OO.ui.Process( function () { | |||
dialog.close() | |||
protectedSearchQuery(true); | |||
}, this ); | }, this ); | ||
} | } | ||
| שורה 87: | שורה 142: | ||
if (this.curPage) | if (this.curPage) | ||
{ | { | ||
var api = new mw.Api(), pagename = this.curPage; | var api = new mw.Api(), pagename = this.curPage; | ||
api.postWithToken(' | api.postWithToken('csrf', { | ||
action: 'edit', | action: 'edit', | ||
title: this.curPage, | title: this.curPage, | ||
| שורה 95: | שורה 149: | ||
minor: 1, | minor: 1, | ||
basetimestamp: this.timestamp, | basetimestamp: this.timestamp, | ||
text: this.text | starttimestamp: this.starttimestamp, | ||
text: this.text, | |||
tags: 'quick linker' | |||
}).done(function(d) { | }).done(function(d) { | ||
if (d && d.edit && d.edit.result == 'Success') mw.notify(mw.msg('qlinker-save-success', pagename)); | if (d && d.edit && d.edit.result == 'Success') mw.notify(mw.msg('qlinker-save-success', pagename)); | ||
}); | }); | ||
} | } | ||
this.nextPage(); | var self = this; | ||
if(this.safeMode) setTimeout(function(){ self.nextPage(); }, 5000/(self.skipsCounter+1)); | |||
else self.nextPage(); | |||
}; | |||
QuickEditorDialog.prototype.nextMatch = function ( ) { | |||
var m, contextPre, contextPost, contextInner, contextInnerOld, newContext, context; | |||
this.matchI++; | |||
if (this.searchData.pageids.length <= this.pageI) { | |||
return this.nextPage(); | |||
} | |||
var page = this.searchData.pages[this.searchData.pageids[this.pageI]], | |||
pagecontent = page.revisions[0]['*']; | |||
if (page.title == mw.config.get('wgTitle')) { | |||
return this.nextPage(); | |||
} | |||
if (m = this.contextRgx.exec(pagecontent)) { | |||
contextPost = m[3]; | |||
contextInner = (m[2] == mw.config.get('wgTitle')) ? '[['+mw.config.get('wgTitle')+']]' : '[['+mw.config.get('wgTitle')+'|' + m[2] + ']]'; | |||
contextInnerOld = ''; | |||
} | |||
else if (m = this.contextRgxLink.exec(pagecontent)) { | |||
if(m[2].endsWith(')')) | |||
{ | |||
m = null; // other existing meaning | |||
} | |||
else | |||
{ | |||
contextPost = m[3] == mw.config.get('wgTitle')? m[4] : '|' + m[3] + m[4]; | |||
contextInner = mw.config.get('wgTitle'); | |||
contextInnerOld = '<s>' + m[2] + (m[3] == mw.config.get('wgTitle')? '|' : '') + '</s>'; | |||
} | |||
} | |||
if (!m) return this.nextPage(); | |||
context = m[0]; | |||
contextPre = m[1]; | |||
newContext = contextPre+contextInner+contextPost; | |||
this.content.$element.html('<h1><a href="/wiki/'+encodeURI(page.title)+'" target="_blank">'+page.title+'</a></h1><p>'+contextPre+contextInnerOld+'<b>'+contextInner+'</b>'+contextPost+'</p>'); | |||
this.curPage = page.title; | |||
this.text = pagecontent.replace(context, newContext) | |||
this.timestamp = page.revisions[0].timestamp; | |||
} | } | ||
QuickEditorDialog.prototype.nextPage = function ( ) { | QuickEditorDialog.prototype.nextPage = function ( ) { | ||
var m, contextPre, contextPost, contextInner, contextInnerOld, newContext; | |||
this.pageI++; | this.pageI++; | ||
if (this.searchData.pageids.length | this.matchI = 0; | ||
if (this.searchData.pageids.length <= this.pageI) | |||
{ | { | ||
this.close(); | if(this.curPage === null) { | ||
this.content.$element.html('<h1><a target="_blank" href="/wiki/Special:Search/'+encodeURI('"' + titleNoBrackets +'" -linksto:"'+mw.config.get('wgPageName') + '"')+'">'+mw.msg('qlinker-no-results')+'</a></h1>'); | |||
this.actions.setAbilities( { | |||
saveContinue: false, skipOne: false | |||
} ); | |||
} else { | |||
this.close(); | |||
} | |||
return; | return; | ||
} | } | ||
this.nextMatch(); | |||
} | } | ||
| שורה 157: | שורה 252: | ||
addLinksWizard.click(function(e){ | addLinksWizard.click(function(e){ | ||
var api = new mw.Api(); | protectedSearchQuery(false); | ||
e.preventDefault(); | |||
}); | |||
return addLinksWizard; | |||
} | |||
function protectedSearchQuery(safeSearch) | |||
{ | |||
var api = new mw.Api(); | |||
api.get( { | |||
action: 'query', | |||
}).done(function( | prop: 'linkshere', | ||
if ( | lhlimit: 500, | ||
if (! | titles: mw.config.get('wgPageName'), | ||
indexpageids: 1 | |||
}).done(function(d) { | |||
var isValid = true, safeMode = false; | |||
if(d.query && d.query.pages && d.query.pages[d.query.pageids[0]] && d.query.pages[d.query.pageids[0]].linkshere && d.query.pages[d.query.pageids[0]].linkshere.length > 100) | |||
{ | |||
isValid = confirm(mw.msg('qlinker-confirm-masslink')); | |||
safeMode = true; | |||
} | |||
if (!isValid) return; | |||
searchLinksApi(safeSearch).done(function(q){ | |||
if (q.error) mw.notify(q.error.info); | |||
if (!q.query || !q.query.pageids || q.query.pageids.length<=1) { | |||
mw.notify(mw.msg('qlinker-no-results')); | mw.notify(mw.msg('qlinker-no-results')); | ||
return; | return; | ||
} | } | ||
mw.loader.using('oojs-ui-windows', function() { createQuickEditorDialog( | mw.loader.using(['oojs-ui-windows', 'oojs-ui.styles.icons-movement', 'oojs-ui.styles.icons-editing-core'], function() { createQuickEditorDialog(q, safeMode) } ); | ||
}); | }); | ||
}) | |||
}) | |||
} | } | ||
function searchLinksApi(protectPrefixSuffix) { | |||
var api = new mw.Api(); | var api = new mw.Api(), | ||
api.get( { | searchRegexPreSufFix = 'insource:/' + allowedPrefix + mw.util.escapeRegExp(titleNoBrackets) + allowedSuffix +'/', | ||
searchRegexNonSafe = 'insource:/' + mw.util.escapeRegExp(titleNoBrackets) +'/', | |||
searchRegex = protectPrefixSuffix? searchRegexPreSufFix : searchRegexNonSafe; | |||
return api.get( { | |||
action: 'query', | action: 'query', | ||
generator: 'search', | generator: 'search', | ||
gsrnamespace: 0, | gsrnamespace: 0, | ||
gsrsearch: | gsrsearch: searchRegex + ' -linksto:'+mw.config.get('wgPageName'), | ||
gsrlimit: 50, | gsrlimit: 50, | ||
prop: 'revisions', | prop: 'revisions', | ||
rvprop: 'content|timestamp', | rvprop: 'content|timestamp', | ||
indexpageids: 1 | indexpageids: 1, | ||
curtimestamp: 1 | |||
}); | }); | ||
} | } | ||
if ($('.dmbox').length === 0) { // Don't show the tool at Disambiguation pages because it is useless | |||
{ | if ($('.orphanpage, .stub').length>0) { | ||
createAddLinksButton(); | searchLinksApi().done(function(d){ | ||
if (d.query && d.query.pageids && d.query.pageids.length>1) createAddLinksButton().css('font-weight', 'bold') | |||
}); | |||
} | |||
else { | |||
createAddLinksButton(); | |||
} | |||
} | } | ||
}); | }); | ||