« MediaWiki:Common.js/Wikinews:Article principal/leadGenerator » : différence entre les versions

Une nouvelle de Wikinews, la source d'informations que vous pouvez écrire.
Traduction
(Aucune différence)

Version du 20 novembre 2009 à 17:26

importScriptURI('http://en.wikinews.org/w/index.php?title=user:Bawolff/mwapilib2.js&action=raw&ctype=text/javascript'); if (!window.Bawolff) {

window.Bawolff = {};

} // http://en.wikinews.org/w/api.php?action=query&prop=revisions&titles=template:Lead%20article%201%7Ctemplate:Lead%20article%202%7Ctemplate:Lead%20article%203%7Ctemplate:Lead%20article%204%7Ctemplate:Lead%20article%205&rvprop=timestamp%7Ccontent //Call as Bawolff.leadGen(pageName, 1, alert) //replacing 1 with which lead (1-5), alert with your callback //optionally takes summary method (as numeric) argument. choose 0-4 Bawolff.leadGen = function (title, leadNumb, callback, summaryMethod) {

api(title).getPage().lift(Bawolff.leadGen.extract, title, summaryMethod).lift(Bawolff.leadGen.create, leadNumb).lift(callback, title).exec();

}

//takes the source of a wikipage, extracts the first image name without the leading namespace Bawolff.leadGen.extractImg = function (page) {

var imgRegex = /\[\[(?:[iI][mM][aA][gG][eE]\:|[fF][Ii][lL][eE]\:)((?:[^\|\]])*?\.[pPsSjJgG][nNvVpPiI][gGeEfF][gG]?)[\|\]]?/;
var img = imgRegex.exec(page);
if (img && img.length >= 2) {
 return img[1];
}
else {
 //return "Wikinews-logo.png"; //default
 return "";
} 

} Bawolff.leadGen.extractType = function (page) {

//valid types are: breaking, special, original, exclusive, urgent or none.
//this currently does not detect special or urget.
if (page.match(/\{\{[eE]n(?: cours)?\}\}/)) {
 return "breaking";
}
else if (page.match(/\{\{[iI]nterview(?:\|[^}]*)?\}\}/i)) {
 return "exclusive";
}
else if (page.match(/\{\{[rR]eportage(?: original)?(?:\|[^}]*)?\}\}/i)) {
 return "original";
}
else {
 //default
 return "none";
} 

}

Bawolff.leadGen.takeIntro = function (pageText, method) {

//first test for redirects.
var isRedirect = pageText.match(/^#redirect\s?\[\[([^\]]*)\]\]/i)
if (isRedirect) {
 alert("Il semble que vous essayez d\'utiliser la création d\'articles principaux sur un page de redirection. Utiliser s\'il vous plaît le nom réel de la page au lieu de (" + isRedirect[1] + ").");
 throw new Error("La page est une redirection. Merci de résoudre manuellement à : " + isRedirect[1] );
}
//doesn't handle links that make [1].
//intentionally doesn't strip ' chars (bold or italic)
//as this often marks thigs with periods (E. coli)
//method is a number (must be a number. no type conversion preformed).
// 0: 1st sentence
// 1: 1st two senetences
// 2: 1st paragraph
// 3: 1st 250 characters (+ a couple so we don't end in middle of word), or paragraph
// 4: 1st 500 characters-ish, or paragraph
var stripTemplates = /\{\{[^\}\{]*(?:\{\{[^\}\{]*(?:\{\{[^\}\{]*(?:\{\{[^\}\{]*\}\})?\}\})?\}\})?\}\}/g;
//the img regex, looks for a start of the image, than checks for nested internal links, external links, and for ending ]] in caption
//templates shold already be stripped at this point.
var img = /\[\[[IifF][mMIi][aALl][GgEe][eE]?\:(?:\[\[(?:[^\]]*)\]\]|[^\]]|\](?!\]))*]]/g
pageText = pageText.replace(stripTemplates, )
pageText = pageText.replace(img, );
var pipedLink = /\[\[[^\]\|]*\|([^\]\|]*)\]\]/g;
pageText = pageText.replace(pipedLink, '$1');
var normLink = /\[\[([^\]\|]*)\]\]/g;
pageText = pageText.replace(normLink, '$1');
var extLink = /\[(?:http|ftp|gopher|irc|https)\:[^\]\s]*\s?([^\]]*)]/g
pageText = pageText.replace(extLink, '$1');
var firstPar;
switch (method) {
 case 1: //1st 2 sentence or 1st paragraph
  firstPar = /[^\n]+?\s[\w"]*\.(?=\s)(?:[^\n]+?\s\w*\.(?=\s)|(?=\s))/;
  pageText = pageText.match(firstPar)[0];
  break;
 case 2: //1st paragraph
  firstPar = /[^\n]+?(?=\n)/;
  pageText = pageText.match(firstPar)[0];
  break;
 case 3: // 1st 250 characters-ish
  firstPar = /[^\n]{2,250}.*?\b/;
  pageText = pageText.match(firstPar)[0] + "...";
  break;
 case 5: //1st 500 characters-ish
  firstPar = /[^\n]{2,500}.*?\b/;
  pageText = pageText.match(firstPar)[0] + "...";
  break;
 default: //aka case 0. 1st sentence
  firstPar = /[^\n]+?\s[\w"]*\.(?=\s)/;
  pageText = pageText.match(firstPar)[0];
  break;
}
return pageText;

}

Bawolff.leadGen.extract = function(pageText, pageName, summaryMethod) { //editlink is overriden later. return { width:'100x100px',

        image: Bawolff.leadGen.extractImg(pageText),
        title: pageName,
        synopsis: Bawolff.leadGen.takeIntro(pageText, summaryMethod),
        order: 'right',
        edit_this: 'Wikinews:Sandbox',
        type: Bawolff.leadGen.extractType(pageText)
        };

}


Bawolff.leadGen.create = function (leadObj, leadNumb) {

if (leadNumb === 1) {
 leadObj.width = "150x150px"; //ugly i know, but whatever.
}
var res = '{' + '{Article principal';
res += "\n |modif=" + leadNumb;
res += "\n |Image=" + leadObj.image;
res += "\n |Image width=" + leadObj.width;
res += "\n |type=" + leadObj.type;
res += "\n |Titre=" + leadObj.title;
res += "\n |Synopsis=" + leadObj.synopsis;
res += "\n}}\n{{"
res += "Article principal/Documentation}}";
return res;

} //calls its argument giving it an object with meta info about the current leads. //probably want to use Bawolff.leadGen.makeLeadTable instead.

Bawolff.leadGen.makeLeadMetaObject = function (callback) {

var leadObj = function (doc) {
 var exTitle = function (text) {
  text = text.replace(/[\s\S]*?\|title=([^\|]*)[\s\S]*/, '$1');
  text = text.replace(/\s*$/, );
  return text;
 }
 var pages = doc.getElementsByTagName('page');
 var obj = {}, time, time2 = Infinity;
 for (var i = 0; i < pages.length; i++) {
  pages[i].getElementsByTagName('rev')[0].normalize();
  time = Bawolff.mwapi.parseAPIDate(pages[i].getElementsByTagName('rev')[0].getAttribute('timestamp'));
  //This is really ugly...
  //put the oldest lead in a global variable.
  if (time < time2) { //this compares miliseconds after epoch
   Bawolff.leadGen.oldestLead = pages[i].getAttribute('title');
   time2 = time;
  }
  obj[pages[i].getAttribute('title')] = {timestamp: time,
  title: exTitle(pages[i].getElementsByTagName('rev')[0].firstChild.data)};
 }
 return obj;
}
api().makeRequest({action: 'query', prop: 'revisions', titles: 'Modèle:Une 1|Modèle:Une 2', prop: 'revisions', rvprop: 'timestamp|content'}, leadObj).lift(callback).exec();

}


Bawolff.leadGen.oldestLead = null; Bawolff.leadGen.makeLeadTable = function (callback) {

var wrapper = function(leadObj) {

var html = '

'; html += '<thead></thead><tbody>';
 for (var i in leadObj) {
  if (leadObj.hasOwnProperty(i)) {
   var numb = i.charAt(i.length - 1);
html += '"; } } html += '</tbody>
Articles principaux actuels
#PositionArticleÂge
';
   html += numb; //last character
html += '
';
   html += Bawolff.leadGen.leadToPosition(numb);
html += '
';
   html += leadObj[i].title;
html += '
';
   var time = leadObj[i].timestamp.getTime(); //convert to miliseconds
   var d = (new Date).getTime();
   var delta = Math.round((d - time) / (1000*60*60));
   var timeStr;
   if (delta === 1) {
    timeStr = delta + ' heure';
   }
   else {
    timeStr = delta + ' heures';
   }
   html += timeStr;
html += "

';

 callback(html);
}
Bawolff.leadGen.makeLeadMetaObject(wrapper);

}

Bawolff.leadGen.leadToPosition = function (numb) { //double equal sign intentional to convert from string.

if (numb == 1) {
 return "gauche";
}
if (numb == 2) {
 return "droite";
}
else {
 throw new Error("Chiffre de l\'article principal invalide (" + numb +") trasmis à Bawolff.leadGen.leadToPosition");
}

}