Участник:KPu3uC B Poccuu/common functions.js

Материал из Википедии — свободной энциклопедии
window.cf666=window.cf=
{
//Formatting routines.
	padTime:function(time)
	{
		if(time<10)return '0'+time;else return time;
	}
	fixPageName:function(name)
	{
		name=name.trim();//Namespace : Page → Namespace:Page
		name=name.replace(/_/g,' ').replace(/^(.+?)\s+:\s+/,'$1:');
		return name;
	}
//Editing routines.
	editPage:function(page,summary,content,section,watchlist,minor,timestamp,recreate,nocreate,onSuccess,OnAPIerror,OnRequestError)
	{
		var data=
		{
			format:'json',action:'edit',title:page,text:content,token:mw.user.tokens.get('editToken'),bot:'0'
		}
		data.watchlist=watchlist||"preferences";
		if(minor)data.minor="";else data.notminor="";
		if(summary)data.summary=summary;
		if(section)data.section=section;
		if(recreate)data.recreate="";
		if(nocreate)data.nocreate="";
		if(timestamp)
			{
				data.basetimestamp=timestamp;
				data.starttimestamp=timestamp;
			}
		cf.editpageCustom(data,page,onSuccess,onAPIError,onRequestError);
	}
	cf.editPageCustom=function(data,page,onSuccess,OnAPIerror,OnRequestError)
	{
		data.token=data.token||mw.user.tokens.get('editToken');
		$.ajax(
		{
			url:mw.util.wikiScript('api'),data:data,dataType:'json',type:'POST',success:function(data)
			{
				if(data&&data.edit&&data.edit.result=='Success')onSuccess(data);else if(data&&data.error)OnAPIerror(data);
			},error:OnRequestError
		});
	}
	//Informational.
	pageExists:function(page)
	{
		var result=false;
		$.ajax(
		{
			url:mw.util.wikiScript('api'),async:false,data:'action=query&prop=revisions&format=json&rvprop=content&rvlimit=1&rvdir=older&titles='+mw.util.wikiUrlencode(page),success:function(resp)
			{
				result=(typeof resp['query']['pages']['-1']=="undefined");
			}
		});
		return result;
	}
	pageContent:function(page)
	{
		var result="";
		$.ajax(
		{
			url:mw.util.wikiScript('api'),async:false,data:'action=parse&format=json&prop=wikitext&disablepp=&page='+mw.util.wikiUrlencode(page),success:function(resp)
			{
				result=resp['parse']['wikitext']['*'];
			}
		});
		return result;
	}
}