ميدياويكي:Gadget-Lastchanges.js

من أرابيكا، الموسوعة الحرة
اذهب إلى التنقل اذهب إلى البحث

ملاحظة: بعد الحفظ، قد يلزمك إفراغ الكاش لرؤية التغييرات.

// [[:fa:Mediawiki:Gadget-lastchenges.js]]
// [[:it:Wikipedia:Monobook.js/MostraModifiche.js]]
// [[:nl:Gebruiker:JePe/recentrevisionsbox.js]]
// <pre>

function removeDiff()
{
   var diffFrame = document.getElementById("revisionsBoxDiff");
   if (diffFrame) diffFrame.parentNode.removeChild(diffFrame);
}

function makeCloseButton(position)
{
    var closeButton = document.createElement("img");  
    with (closeButton) {
       style.cssFloat = position;
       style.styleFloat = position;
       style.verticalAlign = "auto";
       title = closeButtonTitle;
       height = "10";
       width = "10";
       src = "//upload.wikimedia.org/wikipedia/commons/d/da/Crystal_button_cancel.png";    
}                                                                                          
    return closeButton;
}

function processDiff()
{
    if (diffReq.readyState != 4) return;
    if (diffReq.status != 200) return;
    var bodyContent = document.getElementById("bodyContent");                  
    var diffFrame = document.createElement("div");                              
    diffFrame.id = "revisionsBoxDiff";                                           
    diffFrame.innerHTML = diffReq.responseText;                                 
    diffTable = diffFrame.getElementsByTagName("table")[0];   
    if (!diffTable) return;
    if ( $( diffTable ).hasClass( 'diff' ) == false ) return;                   
    diffTable.style.border = "1px solid #ccc";                                  
    diffTable.style.width = "100%";                                             
    diffTable.style.paddingBottom = ".5em";                                     
    diffTable.style.marginBottom = "1em";                                       
    diffTable.style.marginTop = ".5em";                                         
    var closeButton = makeCloseButton("right");
    closeButton.onclick = removeDiff;
    var otitle = diffTable.getElementsByTagName("td");    
    for (i=0; i < otitle.length; i++) {
       if (otitle[i].className == "diff-otitle") {
          otitle[i].insertBefore(closeButton, otitle[i].firstChild); }
    }                                        
    var diffFrameOld = document.getElementById("revisionsBoxDiff");              
    if (diffFrameOld) diffFrameOld.parentNode.removeChild(diffFrameOld);        
    bodyContent.parentNode.insertBefore(diffFrame,bodyContent);                 
    window.location.hash = "revisionsBoxDiff";
    var prevLink = document.getElementById("differences-prevlink");  
    if (prevLink) {      
       var oldid = prevLink.href.replace(/[\s\S]+&oldid=(\d+)[\s\S]*/,"$1");    
       prevLink.href = "javascript:lastchangesLoadDiff("+oldid+",'prev')"; }                                                      
    var nextLink = document.getElementById("differences-nextlink");  
    if (nextLink) {           
       var oldid = nextLink.href.replace(/[\s\S]+&oldid=(\d+)[\s\S]*/,"$1");   
       nextLink.href = "javascript:lastchangesLoadDiff("+oldid+",'next')"; }                                                   
}    

function lastchangesLoadDiff(oldid,diffDir)
{
    var url = "/w/index.php?title=" + mw.config.get('wgPageName');
    url += "&diff=" + diffDir;
    url += "&oldid=" + oldid;
    url += "&diffonly=1&action=render";
    if (window.XMLHttpRequest) { // native XMLHttpRequest
        diffReq = new XMLHttpRequest();
        diffReq.onreadystatechange = processDiff;
        diffReq.open("GET", url, true);
        diffReq.send(null);
    } else if (window.ActiveXObject) { // activeX version
        diffReq = new ActiveXObject("Microsoft.XMLHTTP");
        if (diffReq) {
            diffReq.onreadystatechange = processDiff;
            diffReq.open("GET", url, true);
            diffReq.send("");
        }
    }
}

function nextRevisions()
{
   var revContent = document.getElementById("recRevContent");
   var divNode = revContent.appendChild(document.createElement("div"));
   divNode.id = "nextButton";
   divNode.style.textAlign = "right";   
   var button = divNode.appendChild(document.createElement("button"));
   button.innerHTML = buttonNextText;
   button.title = buttonNextTitle;
   button.onclick = loadXMLDoc;            
   button.style.fontSize = "80%";
   button.style.fontFamily = "inherit";
   button.style.fontWeight = "bold";
   button.style.marginTop = ".3em";
   button.style.marginBottom = "1em";
   button.style.textAlign = "center"; 
}

function writeResults(rev)
{
   var showButton = document.getElementById('showButton');
   var nextButton = document.getElementById('nextButton');
   var revContent = document.getElementById("recRevContent");
   if (showButton) {
      showButton.parentNode.removeChild(showButton);
      var closeButton = makeCloseButton("right");
      closeButton.onclick = recentRevisions;
      closeButton.style.marginTop = ".3em";
      revContent.appendChild(closeButton); }
   if (nextButton) nextButton.parentNode.removeChild(nextButton);   
   var date = new Date();
   var dateNow = (date.getFullYear()*10000) + ((date.getMonth()+1)*100) + date.getDate();
   for (i = 0; i < rev.length; i++) {
      var time = rev[i].getAttribute("timestamp");
      var year = time.replace(/^(\d\d\d\d)[\s\S]+/,"$1");
      var month = time.replace(/^\d\d\d\d-(\d\d)[\s\S]+/,"$1");
      var day = time.replace(/^\d\d\d\d-\d\d-(\d\d)[\s\S]+/,"$1");
      var hours = time.replace(/[\s\S]+T(\d\d)[\s\S]+/,"$1");
      var minutes = time.replace(/[\s\S]+T\d\d:(\d\d)[\s\S]+/,"$1");
      date.setUTCFullYear(year,month-1,day);
      date.setUTCHours(hours,minutes);
      var dateRevision = (date.getFullYear()*10000) + ((date.getMonth()+1)*100) + date.getDate();
      if (dateRevision < dateControl) {
         var divNodeDate = revContent.appendChild(document.createElement("div"));
         divNodeDate.style.fontSize = "90%";
         divNodeDate.style.marginTop = ".5em";
         divNodeDate.style.textAlign = "center";
         divNodeDate.style.fontWeight = "bold";
         divNodeDate.style.paddingright = ".5em";
         divNodeDate.innerHTML = date.getDate() + " " + monthName[date.getMonth()];
         if (dateNow - dateRevision > 10000) divNodeDate.innerHTML += " " + date.getFullYear();
         dateControl = dateRevision;   
      }   
      var divNode = revContent.appendChild(document.createElement("div"));
      divNode.style.marginright = "2em";
      //divNode.style.textIndent = "0em";
      divNode.style.fontSize = "95%";
      divNode.style.margin = ".5em 0";
      var aNodeTime = divNode.appendChild(document.createElement("a"));
      aNodeTime.innerHTML = (date.getHours() < 10) ? "0" + date.getHours() : date.getHours();
      aNodeTime.innerHTML += ":";
      aNodeTime.innerHTML += (date.getMinutes() < 10) ? "0" + date.getMinutes() : date.getMinutes();
      aNodeTime.style.fontSize = "70%";
      var revid = rev[i].getAttribute("revid");
      aNodeTime.href = "javascript:lastchangesLoadDiff("+revid+",'prev')";
      aNodeTime.title = timeTitle;
      divNode.appendChild(document.createTextNode(" "));
      var aNodeUser = divNode.appendChild(document.createElement("a"));
      aNodeUser.innerHTML = rev[i].getAttribute("user");
      var anchorURL = rev[i].getAttribute("comment");
      if (anchorURL == null) anchorURL = "";
      aNodeUser.title = anchorURL;
      // convert comment into a usable anchor link
      if (anchorURL.search(/\/\*\s([\s\S]+)\s\*\/[\s\S]*/) != -1) {
         var reg = [ [/\/\*\s([\s\S]+)\s\*\/[\s\S]*/,"$1"], [/\[\[[\s\S]*\|([\s\S]+)\]\]/g,"$1"],
                   [/\[\[\:?([\s\S]+)\]\]/g,"$1"], [/\s/g,"_"] ];
         for (x = 0; x < reg.length; x++) {
            anchorURL = anchorURL.replace(reg[x][0],reg[x][1]); }
         anchorURL = encodeURIComponent(anchorURL);
         var reg = [ [/\%/g,".25"], [/\(/g,".28"], [/\)/g,".29"], [/\!/g,".21"],
                   /*[/\'/g,".27"],*/ [/\*/g,".2A"], [/\:/g,".3A"] ];
         for (x = 0; x < reg.length; x++) {
            anchorURL = anchorURL.replace(reg[x][0],reg[x][1]); }
      } else {
         anchorURL = ""; }   
      aNodeUser.href = document.location.href + "#" + anchorURL;
      aNodeUser.href = aNodeUser.href.replace(/##/,"#");
   }
}

function processReq()
{
    if (apiReq.readyState != 4) return;
    if (apiReq.status != 200) return;                              
    var xmlFile = apiReq.responseXML.documentElement;
    var rev = xmlFile.getElementsByTagName("rev");  
    if (rev.length > 0) writeResults(rev);  
    queryContinue = xmlFile.getElementsByTagName("query-continue")[0];
    if (queryContinue) {
       rvStartId = queryContinue.getElementsByTagName("revisions")[0].getAttribute("rvstartid");
       nextRevisions();
    }   
}    

function loadXMLDoc()
{
    var url = "/w/api.php?action=query&prop=revisions&format=xml";             
    url += "&titles=" + mw.config.get('wgPageName') + "&rvprop=ids|timestamp|user|comment";      
    url += "&rvlimit=" + revCount;                           
    if (rvStartId != 0) url += "&rvstartid=" + rvStartId;
    if (window.XMLHttpRequest) { // native XMLHttpRequest
        apiReq = new XMLHttpRequest();
        apiReq.onreadystatechange = processReq;
        apiReq.open("GET", url, true);
        apiReq.send(null);
    } else if (window.ActiveXObject) { // activeX version
        apiReq = new ActiveXObject("Microsoft.XMLHTTP");
        if (apiReq) {
            apiReq.onreadystatechange = processReq;
            apiReq.open("GET", url, true);
            apiReq.send("");
        }
    }
}

function recentRevisions()
{
   var revContent = document.getElementById("recRevContent");
   if (revContent) { // when pressed the close button
      revContent.innerHTML = "";
      rvStartId = 0;
      dateControl = 99999999;
   } else {   
      var navigationBox = document.getElementById('p-navigation');                                      
      var revBox = document.createElement("div");                                                
      var revBox = navigationBox.parentNode.insertBefore(document.createElement("div"),navigationBox);
      revBox.id = "p-rev";                                                                       
      revBox.className = "portlet";                                                              
      var h5Node = revBox.appendChild(document.createElement("h5"));                             
      h5Node.innerHTML = revBoxTitle;
      h5Node.style.fontSize = "95%";
      h5Node.style.textAlign = "center";                                            
      revContent = revBox.appendChild(document.createElement("div"));                           
      revContent.id = "recRevContent";
      revContent.className = "pBody";                                                        
      revContent.style.padding = "0 2em .3em .3em";                                                
      revContent.style.lineHeight = ".9em";                                                        
   }                                            
   var divNode = revContent.appendChild(document.createElement("div"));
   divNode.id = "showButton";
   divNode.style.textAlign = "right";   
   var button = divNode.appendChild(document.createElement("button"));
   button.innerHTML = buttonBeginText;
   button.onclick = loadXMLDoc;            
   button.style.fontSize = "80%";
   button.style.fontFamily = "inherit";
   button.style.fontWeight = "bold";
   button.style.marginTop = ".3em";
   button.style.marginBottom = "1em";
   button.style.textAlign = "center";
}

var diffReq;
var apiReq;
var rvStartId = 0;
var revCount = 20; // max 50, higher numbers not possible, they give an error
var dateControl = 99999999; // must only be higher than today (yyyymmdd)
if (mw.config.get('wgUserLanguage') === "ar") {
   var timeTitle = "الفرق عن المراجعة السابقة";
   var closeButtonTitle = "أغلق";
   var revBoxTitle = "المراجعات الحديثة";
   var buttonBeginText = "أظهر المراجعات";
   var buttonNextText = revCount + " تالية";
   var buttonNextTitle = "أظهر "+revCount+" من المراجعات التالية";
   var monthName = ["يناير", "فبراير", "مارس", "أبريل", "مايو", "يونيو", "يوليو", "أغسطس", "سبتمبر", "أكتوبر", "نوفمبر", "ديسمبر"];
} else {
   var timeTitle = "Differences with the previous revision";
   var closeButtonTitle = "Close";
   var revBoxTitle = "recent revisions";
   var buttonBeginText = "Show revisions";
   var buttonNextText = "Next " + revCount;
   var buttonNextTitle = "Show the next "+revCount+" revisions";
   var monthName = ["January", "February", "March", "April", "May", "June", "July",
                    "August", "September", "October", "November", "December"];
}
mw.util.addCSS('table.diff td {vertical-align:top;}');
mw.loader.load('mediawiki.action.history.diff');
if (mw.config.get('wgNamespaceNumber') != -1)
   $(recentRevisions);

// </pre>