پرش به محتوا

مدیاویکی:Common.js/ویکی‌خبر:ایجاد خبر اصلی

از ویکی‌خبر، مرجع خبری آزاد

/*

This javascript deals with stuff specific to [[User:Bawolff/sandbox/makeLead]]. Functions
specific to lead generation are at User:Bawolff/sanbox/leadGenerator.js
functions relating to api access are at User:Bawolff/mwapilib2.js
*/
importScript('Mediawiki:leadGenerator.js');

var showPreview = function (html) {
 //We assume that the mediawiki api is not malicious...
 document.getElementById('LeadPreview').innerHTML = html;
}
var showLead = function (leadText, leadTitle) {
 var lsp = document.getElementById('leadSourcePrev');
 if (!lsp) {
  var src = document.createElement('textarea');
  src.id = 'leadSourcePrev';
  src.style.width = '48%';
  src.rows = 40;
  src.appendChild(document.createTextNode(leadText));
  document.getElementById('LeadBox').appendChild(src);
  document.getElementById('LeadPreview').style.visibility = 'visible';
 }
 else {
  lsp.value = leadText;
 }
 leadTitle = leadTitle.replace(/(\\|')/g, "\\$1").replace(/"/g, """).replace(/>/g, '>'); //prevent xss
 var leadNumbEn = document.getElementById('leadNum').selectedIndex + 1;
 if( leadNumbEn == 1 ) {
  leadNumb = '۱';
 } else if ( leadNumbEn == 2) {
  leadNumb = '۲';
 } else if ( leadNumbEn == 3 ) {
  leadNumb = '۳';
 } else if ( leadNumbEn  == 4 ) {
  leadNumb = '۴';
 } else {
   throw new Error( 'Cannot convert lead number to Farsi');
 }

 document.getElementById('LeadControls').innerHTML = '<button onclick="syncToServer(\'' + leadNumb + '\', \'' + leadTitle + '\')">ذخیره به عنوان خبر اصلی ' + leadNumb + '</button> <button onclick="regeneratePreview()">تازه سازی پیش نمایش</button>';

 api(leadText).replace(/<noinclude>[\d\D]*?<\/noinclude>/, '').parse('صفحهٔ اصلی').lift(showPreview).exec();
 //this will strip out anything after a noinculde (this is kind of dirty)

}
var regeneratePreview = function() {
var wikiText = document.getElementById('leadSourcePrev').value;
api(wikiText).replace(/<noinclude>[\d\D]*?<\/noinclude>/, '').parse('صفحهٔ اصلی').lift(showPreview).exec();
}
var syncToServer = function(leadNum, page) {
 if (leadNum === undefined || !confirm('آیا مطمئن هستید که می‌خواهید الگو را دخیره کنید؟')) {
  return false;
 }

 api(document.getElementById('leadSourcePrev').value).setDefaultSummary('تعویض خبر اصلی شماره ' + leadNum + ' با استفاده از [[ویکی‌خبر:ایجاد خبر اصلی|ابزار جایگزینی]]').savePage('الگو:مقالهٔ آغازین ' + leadNum).sightByRev().inject('خبر صفحهٔ اصلی با موفقیت بهنگام سازی شد').alert().lift(function() {location = mw.config.get('wgServer') + mw.config.get('wgArticlePath').replace('$1', mw.config.get('wgPageName')) + '?js-good-update=' + encodeURIComponent(page);}).exec();
}
var startLeadMaker = function () {
 if (mw.config.get('wgAction') !== 'view' && mw.config.get('wgAction') !== 'purge') {
  return false;
 }
 if (!window.api || !window.Bawolff || !window.Bawolff.leadGen) {
  alert('Javascript did not load properly, as a result this page may or may not work. If it doesn\'t work, try doing a soft refresh.');
 }
 if (!mw.config.get('wgUserGroups') || mw.config.get('wgUserGroups').join(' ').indexOf("autoconfirmed") === -1) {
  jsMsg("<div style='border:solid red medium;font-weight: bold'>حساب کاری شما اجازهٔ تغییرات در الگوهای صفحهٔ اصلی را ندارد</div>");
 }
 var lastUpdate = location.search.match(/[?&]js-good-update=([^&]*).*/);
 if (lastUpdate) {
  //this is all properly escaped since its in a text node.
  window.setTimeout(function () {jsMsg(document.createTextNode('خبر با موفقیت تعویض شد' + decodeURIComponent(lastUpdate[1]) + '".'));}, 400); //to get arround other things using jsMSg
 }
 var setupContainer = document.getElementById('lSetup');
 setupContainer.innerHTML += "<form id='leadForm' action='javascript:doMakeLead();void 0;'><label for='pageNameField'>صفحه:</label> <input type='text' id='pageNameField' size='50'><br/><label for='leadNum'>شماره:</label> <select id='leadNum' onchange='doMakeLead(true);'><option selected value='1'>۱ (بالا)</option><option>۲ (وسط-راست)</option><option>۳ (وسط-چپ)</option> <option>۴ (پایین-راست)</option> <option>۵ (پایین-چپ)</option> </select> <label for='sumMethod'>روش خلاصه:</label> <select id='sumMethod'><option selected value='0'>جمله اول</option><option value='1'>جمله دوم</option><option value='2'>بند اول</option><option>اولین ۲۵۰ حرف</option> <option>اولین ۵۰۰ حرف</option> </select> &nbsp;<input type='submit' value='تعویض خبر'></form>";

 var dpl = document.getElementById('lDPL').getElementsByTagName('a');
 for (var i = 0; i < dpl.length; i++) {
  linkVal = encodeURIComponent(dpl[i].firstChild.data.replace(/(\\|")/g, "\\$1"));
  // %22 = "
  dpl[i].href = 'javascript:document.getElementById(%22pageNameField%22).value=%22' + linkVal + '%22;doMakeLead();void%200';
 }
 os_initHandlers('pageNameField','leadForm', document.getElementById('pageNameField'));
 
 var handleAfterLeadInfoTableLoads = function (table) {
  document.getElementById('lMetaTable').innerHTML = table;
  var pageName = location.search.replace(/.*?[?&]use-page=([^&]*).*/, '$1');

  if (Bawolff.leadGen.oldestLead) {
   var oldLeadNum = parseInt(Bawolff.leadGen.oldestLead.charAt(Bawolff.leadGen.oldestLead.length - 1));
   document.getElementById('leadNum').selectedIndex = oldLeadNum - 1;
   if (oldLeadNum === 1) {
    document.getElementById('sumMethod').selectedIndex = 1; //use 2 sentences for longer lead.
   }
  }
  if (pageName !== location.search) { //if it matched
   document.getElementById('pageNameField').value = decodeURIComponent(pageName.replace(/\+/g, ' '));
   location.href = '#leadForm'; //since already filled out.
   doMakeLead();
  }
  
 }
 Bawolff.leadGen.makeLeadTable(handleAfterLeadInfoTableLoads);

}
function doMakeLead (fromLeadNumChange) {
var pageName = document.getElementById('pageNameField').value;
var leadNum = document.getElementById('leadNum').selectedIndex + 1;
 if (fromLeadNumChange && leadNum === 1) {
    document.getElementById('sumMethod').selectedIndex = 1; //use 2 sentences for longer lead.
 }
var sumMethod = document.getElementById('sumMethod').selectedIndex;
if (!pageName) return; //stop error if not initialized.
Bawolff.leadGen(pageName, leadNum, showLead, sumMethod);
}

waitAWhileThanLoadLead.times = 0;
function waitAWhileThanLoadLead () {
//since we're already from an onload event, and importScript is async. wait a while
 if ((!window.api || !window.Bawolff || !window.Bawolff.leadGen) && waitAWhileThanLoadLead.times < 10) {
  waitAWhileThanLoadLead.times++;
  setTimeout(waitAWhileThanLoadLead, 200);
 } else {
 startLeadMaker();
 }
}
$(waitAWhileThanLoadLead);
/*

*/