לדלג לתוכן

מדיה ויקי:Gadget-quickLinker.js: הבדלים בין גרסאות בדף

מתוך צפונות ויקי
מאין תקציר עריכה
מאין תקציר עריכה
שורה 4: שורה 4:
Author: [[:he:User:ערן]]
Author: [[:he:User:ערן]]
*/
*/
$(function(){
if ( mw.config.get( 'wgNamespaceNumber' ) === 0 ) $(function(){
mw.messages.set({
mw.messages.set({
'qlinker-title' : 'עריכה מהירה',
'qlinker-title' : 'עריכה מהירה',
שורה 17: שורה 17:


function createQuickEditorDialog(searchRes){
function createQuickEditorDialog(searchRes){
function QuickEditorDialog( config ) {
function QuickEditorDialog( config ) {
  this.contextRgx = new RegExp('.*[^\[]' + mw.RegExp.escape(mw.config.get('wgTitle')) +'(?!\]\]).*');
  this.contextRgx = new RegExp('.*[^\[]' + mw.RegExp.escape(mw.config.get('wgTitle')) +'(?!\]\]).*');
שורה 187: שורה 185:
indexpageids: 1
indexpageids: 1
}).done(function(d){
}).done(function(d){
if (d.query && d.query.pageids && d.query.pageids.length>1) addLinksWizard.css('font-weight', 'bold')
if (d.query && d.query.pageids && d.query.pageids.length>1) addLinksWizard.css('font-weight', 'bold');
else addLinksWizard.hide();
});
});
}
}
});
});

גרסה מ־07:32, 24 בפברואר 2017

/*
This is EXPERIMENTAL 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:ערן]]
*/
if ( mw.config.get( 'wgNamespaceNumber' ) === 0 ) $(function(){
mw.messages.set({
	'qlinker-title' : 'עריכה מהירה',
	'linkify-summary': 'הוספת קישור',
	'qlinker-save-success': 'שמירת $1 בוצעה בהצלחה!',
	'qlinker-save-continue': 'שמירה',
	'qlinker-skip': 'דילוג לדף הבא',
	'qlinker-invoke': 'הוספת בזק של קישורים',
	'qlinker-sidelink': 'הוספת קישורים',
	'qlinker-no-results': 'לא נמצאו תוצאות'
});

function createQuickEditorDialog(searchRes){
	function QuickEditorDialog( config ) {
	  this.contextRgx = new RegExp('.*[^\[]' + mw.RegExp.escape(mw.config.get('wgTitle')) +'(?!\]\]).*');
	  this.pageI = -1;
	  this.searchData = null;
	  this.curPage = null;
	  QuickEditorDialog.super.call( this, config );
	}
	OO.inheritClass( QuickEditorDialog, OO.ui.ProcessDialog ); 

	// Specify a name for .addWindows()
	QuickEditorDialog.static.name = 'QuickEditorDialog';
	// Specify a title statically (or, alternatively, with data passed to the opening() method). 
	QuickEditorDialog.static.title = mw.msg('qlinker-title');

	QuickEditorDialog.static.actions = [
	  { action: 'saveContinue', label: mw.msg('qlinker-save-continue'), flags: [ 'other', 'constructive' ] },
	  { action: 'skipOne', label: mw.msg('qlinker-skip'), flags: [ 'other', 'progressive' ] },
	  { label: 'Cancel', flags: 'safe' }
	];

	// Customize the initialize() function: This is where to add content to the dialog body and set up event handlers. 
	QuickEditorDialog.prototype.initialize = function () {
	  // Call the parent method
	  QuickEditorDialog.super.prototype.initialize.call( this );
	  // Create and append a layout and some content.
	  this.content = new OO.ui.PanelLayout( { padded: true, expanded: true } );
	  this.$body.append( this.content.$element );
	};
	  
	QuickEditorDialog.prototype.getBodyHeight = function () {
	  return 400;
	};

	QuickEditorDialog.prototype.getSetupProcess = function ( data ) {
	  data = data || {};
	  return QuickEditorDialog.super.prototype.getSetupProcess.call( this, data )
	  .next( function () {
		// Set up contents based on data
	    this.searchData = data.searchData.query;
		this.nextPage();
	  }, this );
	};

	// Use the getActionProcess() method to specify a process to handle the 
	// actions (for the 'save' action, in this example).
	QuickEditorDialog.prototype.getActionProcess = function ( action ) {
	  var dialog = this;
	  switch ( action ) {
		case 'skipOne':
			return new OO.ui.Process( function () {
			  dialog.nextPage();
			}, this );
		case 'saveContinue':
			return new OO.ui.Process( function () {
			  dialog.savePage();
			}, this );
	  }

	  // Fallback to parent handler.
	  return QuickEditorDialog.super.prototype.getActionProcess.call( this, action );
	};

	QuickEditorDialog.prototype.savePage = function ( ) {
		if (this.curPage)
		{
			
			var api = new mw.Api(), pagename = this.curPage;
			api.postWithToken('edit', {
				action: 'edit',
				title: this.curPage,
				summary: mw.msg('linkify-summary'),
				minor: 1,
				basetimestamp: this.timestamp,
				text: this.text
			}).done(function(d) {
				if (d && d.edit && d.edit.result == 'Success')	mw.notify(mw.msg('qlinker-save-success', pagename));
			});
		}
		this.nextPage();
	}

	QuickEditorDialog.prototype.nextPage = function ( ) {
		this.pageI++;
		if (this.searchData.pageids.length==this.pageI)
		{
			this.close();
			return;
		}
		var page = this.searchData.pages[this.searchData.pageids[this.pageI]];

		var pagecontent = page.revisions[0]['*'];
		if ((page.title!=mw.config.get('wgTitle')) && (m = this.contextRgx.exec(pagecontent))) {
			var context = m[0];
			var contextPre = context.substr(0, context.indexOf(mw.config.get('wgTitle'))),
				contextPost = context.substr(contextPre.length+mw.config.get('wgTitle').length),
				newContext = contextPre+'[['+mw.config.get('wgTitle')+']]'+contextPost
			
			this.content.$element.html('<h1>'+page.title+'</h1><p>'+contextPre+'<b>[['+mw.config.get('wgTitle')+']]</b>'+contextPost+'</p>');
			this.curPage = page.title;
			this.text = pagecontent.replace(context, newContext)
			this.timestamp = page.timestamp;
		}
		else {
			this.nextPage();
		}
	}

	// Make the window.
	var qlinkerEditor = new QuickEditorDialog( {
	  size: 'medium'
	} );

	// Create and append a window manager, which will open and close the window. 
	var windowManager = new OO.ui.WindowManager();
	$( 'body' ).append( windowManager.$element );

	// Add the window to the window manager using the addWindows() method.
	windowManager.addWindows( [ qlinkerEditor ] );

	// Open the window!
	windowManager.openWindow( qlinkerEditor,  { searchData: searchRes } );
}

var addLinksWizard = $(mw.util.addPortletLink(
				'p-tb',
				'#',
				mw.msg('qlinker-sidelink'),
				't-quicklinker',
				mw.msg('qlinker-invoke'),
				null,
				'#t-whatlinkshere'
));
addLinksWizard.click(function(e){
	var api = new mw.Api();
	api.get( {
		action: 'query',
		generator: 'search',
		gsrnamespace: 0,
		gsrsearch: '"' + mw.config.get('wgTitle') +'" -linksto:'+mw.config.get('wgPageName'),
		gsrlimit: 50,
		prop: 'revisions',
		rvprop: 'content|timestamp',
		indexpageids: 1
	}).done(function(d){
		if (d.error) mw.notify(d.error.info);
		if (!d.query || !d.query.pageids || d.query.pageids.length<=1) {
			mw.notify(mw.msg('qlinker-no-results'));
			return;
		}
		mw.loader.using('oojs-ui-windows', function() {	createQuickEditorDialog(d) } );
	});
	e.preventDefault();
});



if ($('.orphanpage, .stub').length>0) {
	var api = new mw.Api();
	api.get( {
		action: 'query',
		generator: 'search',
		gsrnamespace: 0,
		gsrsearch: '"' + mw.config.get('wgTitle') +'" -linksto:'+mw.config.get('wgPageName'),
		gsrlimit: 50,
		prop: 'revisions',
		rvprop: 'content|timestamp',
		indexpageids: 1
	}).done(function(d){
		if (d.query && d.query.pageids && d.query.pageids.length>1) addLinksWizard.css('font-weight', 'bold');
		else addLinksWizard.hide();
	});
}
});