MediaWiki:Common.js/Wikinews:Make lead

From Wikinews, the free news source you can write!
Jump to: navigation, search

/*

  1 This javascript deals with stuff specific to [[User:Bawolff/sandbox/makeLead]]. Functions
  2 specific to lead generation are at User:Bawolff/sanbox/leadGenerator.js
  3 functions relating to api access are at User:Bawolff/mwapilib2.js
  4 */
  5 mw.loader.load( '/w/index.php?title=' + 'User:Bawolff/sanbox/leadGenerator.js' + '&action=raw&ctype=text/javascript' );
  6 
  7 var showPreview = function (html) {
  8  //We assume that the mediawiki api is not malicious...
  9  document.getElementById('LeadPreview').innerHTML = html;
 10 }
 11 var showLead = function (leadText, leadTitle) {
 12  var lsp = document.getElementById('leadSourcePrev');
 13  if (!lsp) {
 14   var src = document.createElement('textarea');
 15   src.id = 'leadSourcePrev';
 16   src.style.width = '48%';
 17   src.rows = 40;
 18   src.appendChild(document.createTextNode(leadText));
 19   document.getElementById('LeadBox').appendChild(src);
 20   document.getElementById('LeadPreview').style.visibility = 'visible';
 21  }
 22  else {
 23   lsp.value = leadText;
 24  }
 25  leadTitle = leadTitle.replace(/(\\|')/g, "\\$1").replace(/"/g, """).replace(/>/g, '>'); //prevent xss
 26  var leadNumb = document.getElementById('leadNum').selectedIndex + 1;
 27  document.getElementById('LeadControls').innerHTML = '<button onclick="syncToServer(' + leadNumb + ', \'' + leadTitle + '\')">Save as lead ' + leadNumb + '</button> <button onclick="regeneratePreview()">Refresh preview</button>';
 28 
 29  api(leadText).replace(/<noinclude>[\d\D]*?<\/noinclude>/, '').parse('Main Page').lift(showPreview).exec();
 30  //this will strip out anything after a noinculde (this is kind of dirty)
 31 
 32 }
 33 var regeneratePreview = function() {
 34 var wikiText = document.getElementById('leadSourcePrev').value;
 35 api(wikiText).replace(/<noinclude>[\d\D]*?<\/noinclude>/, '').parse('Main Page').lift(showPreview).exec();
 36 }
 37 var syncToServer = function(leadNum, page) {
 38  if (leadNum === undefined || !confirm('Are you sure you want to save this to [[template:Lead article ' + leadNum + ']]?')) {
 39   return false;
 40  }
 41  api(document.getElementById('leadSourcePrev').value).setDefaultSummary('Updating lead ' + leadNum + ' to [[' + page + ']] with [[Wikinews:Make Lead|MakeLead JS]]').savePage('Template:Lead article ' + leadNum).sightByRev().inject('Lead successfully updated').alert().lift(function() {location = mw.config.get('wgServer') + mw.config.get('wgArticlePath').replace('$1', mw.config.get('wgPageName')) + '?js-good-update=' + encodeURIComponent(page);}).exec();
 42 }
 43 var startLeadMaker = function () {
 44  if (mw.config.get('wgAction') !== 'view' && mw.config.get('wgAction') !== 'purge') {
 45   return false;
 46  }
 47  if (!window.api || !window.Bawolff || !window.Bawolff.leadGen) {
 48   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.');
 49  }
 50  if (!mw.config.get('wgUserGroups') || mw.config.get('wgUserGroups').join(' ').indexOf("autoconfirmed") === -1) {
 51   jsMsg("<div style='border:solid red medium;font-weight: bold; padding:0.5em'>Please note: You cannot edit which articles are lead articles, since only people logged in with a registered account that has been registered for over four days can edit the lead templates.</div>");
 52  }
 53  var lastUpdate = location.search.match(/[?&]js-good-update=([^&]*).*/);
 54  if (lastUpdate) {
 55   //this is all properly escaped since its in a text node.
 56   window.setTimeout(function () {jsMsg(document.createTextNode('Lead successfully updated to article "' + decodeURIComponent(lastUpdate[1]) + '".'));}, 400); //to get arround other things using jsMSg
 57  }
 58  var setupContainer = document.getElementById('lSetup');
 59  setupContainer.innerHTML += "<form id='leadForm' action='javascript:doMakeLead();void 0;'><label for='pageNameField'>Page:</label> <input type='text' id='pageNameField' size='50'><br/><label for='leadNum'>Lead number:</label> <select id='leadNum' onchange='doMakeLead(true);'><option selected value='1'>1 (Top)</option><option>2 (Middle-left)</option><option>3 (Middle-right)</option> <option>4 (Bottom-left)</option> <option>5 (Bottom-right)</option> </select> <label for='sumMethod'>Summary&nbsp;method:</label> <select id='sumMethod'><option selected value='0'>1st sentence</option><option value='1'>1st 2 sentences</option><option value='2'>1st paragraph</option><option>1st 250 letters</option> <option>1st 500 letters</option> </select> &nbsp;<input type='submit' value='Make lead'></form>";
 60 
 61  var dpl = document.getElementById('lDPL').getElementsByTagName('a');
 62  for (var i = 0; i < dpl.length; i++) {
 63   linkVal = encodeURIComponent(dpl[i].firstChild.data.replace(/(\\|")/g, "\\$1"));
 64   // %22 = "
 65   dpl[i].href = 'javascript:document.getElementById(%22pageNameField%22).value=%22' + linkVal + '%22;doMakeLead();void%200';
 66  }
 67 
 68 // autosuggest
 69 mw.loader.using( 'jquery.ui.autocomplete', function () { $( "#pageNameField" ).autocomplete({
 70 	minLength: 2,
 71 	source: function( request, response ) {
 72 		$.getJSON(
 73 			mw.config.get('wgServer') + mw.config.get('wgScriptPath') + '/api.php?format=json&action=opensearch&search=' +
 74 			mw.util.rawurlencode( request.term ) + '&callback=?',
 75 			function( obj ) {
 76 				if ( obj && obj.length > 1 ) {
 77 					response( obj[1] );
 78 				} else {
 79 					response( [] );
 80 				}
 81 			}
 82 		);
 83 	}
 84 })});
 85 
 86  
 87  var handleAfterLeadInfoTableLoads = function (table) {
 88   document.getElementById('lMetaTable').innerHTML = table;
 89   var pageName = location.search.replace(/.*?[?&]use-page=([^&]*).*/, '$1');
 90 
 91   if (Bawolff.leadGen.oldestLead) {
 92    var oldLeadNum = parseInt(Bawolff.leadGen.oldestLead.charAt(Bawolff.leadGen.oldestLead.length - 1));
 93    document.getElementById('leadNum').selectedIndex = oldLeadNum - 1;
 94    if (oldLeadNum === 1) {
 95     document.getElementById('sumMethod').selectedIndex = 1; //use 2 sentences for longer lead.
 96    }
 97   }
 98   if (pageName !== location.search) { //if it matched
 99    document.getElementById('pageNameField').value = decodeURIComponent(pageName.replace(/\+/g, ' '));
100    location.href = '#leadForm'; //since already filled out.
101    doMakeLead();
102   }
103   
104  }
105  Bawolff.leadGen.makeLeadTable(handleAfterLeadInfoTableLoads);
106 
107 }
108 function doMakeLead (fromLeadNumChange) {
109 var pageName = document.getElementById('pageNameField').value;
110 var leadNum = document.getElementById('leadNum').selectedIndex + 1;
111  if (fromLeadNumChange && leadNum === 1) {
112     document.getElementById('sumMethod').selectedIndex = 1; //use 2 sentences for longer lead.
113  }
114 var sumMethod = document.getElementById('sumMethod').selectedIndex;
115 if (!pageName) return; //stop error if not initialized.
116 Bawolff.leadGen(pageName, leadNum, showLead, sumMethod);
117 }
118 
119 waitAWhileThenLoadLead.times = 0;
120 function waitAWhileThenLoadLead () {
121 //since we're already from an onload event, and mw.loader.load is async. wait a while
122  if ((!window.api || !window.Bawolff || !window.Bawolff.leadGen) && waitAWhileThenLoadLead.times < 10) {
123   waitAWhileThenLoadLead.times++;
124   setTimeout(waitAWhileThenLoadLead, 200);
125  } else {
126  startLeadMaker();
127  }
128 }
129 addOnloadHook(waitAWhileThenLoadLead);
130 /*

*/