MediaWiki:Seitenempfehlung.js: Unterschied zwischen den Versionen

aus Kamelopedia, der wüsten Enzyklopädie
Zur Navigation springen Zur Suche springen
(Warnung @Dufo ;) ...)
(test, deprecated zeuch ersetzt ... hoffe, das ist richtig so)
Zeile 18: Zeile 18:
 
function readHwList(username, action) {
 
function readHwList(username, action) {
 
   jQuery.ajax({
 
   jQuery.ajax({
     url:    wgScript,  
+
     url:    mw.config.get( 'wgScript' ),  
 
     data:    { title: "Kamel:" + username + subpath, action: "raw" },
 
     data:    { title: "Kamel:" + username + subpath, action: "raw" },
 
     cache:  false,
 
     cache:  false,
Zeile 53: Zeile 53:
 
   jQuery.ajax({
 
   jQuery.ajax({
 
     url:    '/api.php',  
 
     url:    '/api.php',  
     data:    { action: "query", prop: "info", intoken: "edit", titles: "Kamel:" + wgUserName + subpath, format: "xml" },
+
     data:    { action: "query", prop: "info", intoken: "edit", titles: "Kamel:" + Kamelname + subpath, format: "xml" },
 
     cache:  false,
 
     cache:  false,
 
     success: function(data) {  
 
     success: function(data) {  
Zeile 64: Zeile 64:
 
         type:    'POST',
 
         type:    'POST',
 
         cache:  false,
 
         cache:  false,
         data:    { action: "edit", title: "Kamel:" + wgUserName + subpath, text: "* " + hwList.join("\n* "), summary: "+ " + entry, token: edittoken },
+
         data:    { action: "edit", title: "Kamel:" + Kamelname + subpath, text: "* " + hwList.join("\n* "), summary: "+ " + entry, token: edittoken },
 
         success: function(data) {
 
         success: function(data) {
           var text = '<p>Die Seite „' + wgPageName.replace(/_/g, ' ') + '“ wurde zu deinen <a href="'
+
           var text = '<p>Die Seite „' + Seitenname.replace(/_/g, ' ') + '“ wurde zu deinen <a href="'
             + wgScript + '/Kamel:' + wgUserName + subpath + '">Empfehlungen</a> hinzugefügt.</p>';
+
             + mw.config.get( 'wgScript' ) + '/Kamel:' + Kamelname + subpath + '">Empfehlungen</a> hinzugefügt.</p>';
 
           jQuery('#mw-js-message').html(text);
 
           jQuery('#mw-js-message').html(text);
 
           jQuery('#mw-js-message').slideDown('fast');
 
           jQuery('#mw-js-message').slideDown('fast');
Zeile 80: Zeile 80:
 
   jQuery.ajax({
 
   jQuery.ajax({
 
     url:    '/api.php',  
 
     url:    '/api.php',  
     data:    { action: "query", prop: "info", intoken: "edit", titles: "Kamel:" + wgUserName + subpath, format: "xml" },
+
     data:    { action: "query", prop: "info", intoken: "edit", titles: "Kamel:" + Kamelname + subpath, format: "xml" },
 
     cache:  false,
 
     cache:  false,
 
     success: function(data) {  
 
     success: function(data) {  
Zeile 92: Zeile 92:
 
         type:    'POST',
 
         type:    'POST',
 
         cache:  false,
 
         cache:  false,
         data:    { action: "edit", title: "Kamel:" + wgUserName + subpath, text: "* " + hwList.join("\n* "), summary: "- " + entry, token: edittoken },
+
         data:    { action: "edit", title: "Kamel:" + Kamelname + subpath, text: "* " + hwList.join("\n* "), summary: "- " + entry, token: edittoken },
 
         success: function(data) {
 
         success: function(data) {
           var text = '<p>Die Seite „' + wgPageName.replace(/_/g, ' ') + '“ wurde von deinen <a href="'
+
           var text = '<p>Die Seite „' + Seitenname.replace(/_/g, ' ') + '“ wurde von deinen <a href="'
             + wgScript + '/Kamel:' + wgUserName + subpath + '">Empfehlungen</a> entfernt.</p>';
+
             + mw.config.get( 'wgScript' ) + '/Kamel:' + Kamelname + subpath + '">Empfehlungen</a> entfernt.</p>';
 
           jQuery('#mw-js-message').html(text);
 
           jQuery('#mw-js-message').html(text);
 
           jQuery('#mw-js-message').slideDown('fast');
 
           jQuery('#mw-js-message').slideDown('fast');
Zeile 123: Zeile 123:
 
jQuery(document).ready(function() {
 
jQuery(document).ready(function() {
 
   //if(typeof(disableGaGA) != 'undefined' && disableGaGA) return;
 
   //if(typeof(disableGaGA) != 'undefined' && disableGaGA) return;
   if(jQuery.inArray(wgCanonicalNamespace, hwNS) != -1) {
+
   if(jQuery.inArray(mw.config.get( 'wgCanonicalNamespace' ), hwNS) != -1) {
     entry = (wgCanonicalNamespace == "File") ? "[[:" + wgPageName.replace(/_/g, ' ') + "]]"  
+
     entry = (mw.config.get( 'wgCanonicalNamespace' ) == "File") ? "[[:" + Seitenname.replace(/_/g, ' ') + "]]"  
                                             : "[[" + wgPageName.replace(/_/g, ' ') + "]]";
+
                                             : "[[" + Seitenname.replace(/_/g, ' ') + "]]";
 
     jQuery('#p-views ul').append('<li class="icon"><span><a href="#"></a></span></li>');
 
     jQuery('#p-views ul').append('<li class="icon"><span><a href="#"></a></span></li>');
 
     jQuery('#p-views li:last').ajaxComplete(function() {
 
     jQuery('#p-views li:last').ajaxComplete(function() {
Zeile 133: Zeile 133:
 
         jQuery('#ca-unhw a').attr('title', 'Seite nicht mehr empfehlen');
 
         jQuery('#ca-unhw a').attr('title', 'Seite nicht mehr empfehlen');
 
         jQuery('#ca-unhw a').unbind('click');
 
         jQuery('#ca-unhw a').unbind('click');
         jQuery('#ca-unhw a').bind('click', function() { readHwList(wgUserName, 'remove') });
+
         jQuery('#ca-unhw a').bind('click', function() { readHwList(Kamelname, 'remove') });
 
       } else {
 
       } else {
 
         jQuery(this).attr('id', 'ca-hw');
 
         jQuery(this).attr('id', 'ca-hw');
Zeile 139: Zeile 139:
 
         jQuery('#ca-hw a').attr('title', 'Seite empfehlen');
 
         jQuery('#ca-hw a').attr('title', 'Seite empfehlen');
 
         jQuery('#ca-hw a').unbind('click');
 
         jQuery('#ca-hw a').unbind('click');
         jQuery('#ca-hw a').bind('click', function() { readHwList(wgUserName, 'add') });
+
         jQuery('#ca-hw a').bind('click', function() { readHwList(Kamelname, 'add') });
 
       }
 
       }
 
     });
 
     });
     readHwList(wgUserName, false);
+
     readHwList(Kamelname, false);
 
   }
 
   }
 
})
 
})

Version vom 19. Mai 2011, 23:21 Uhr

/*
 * Version 2011-05-15.1 sollte halbwegs fehlerfrei funktionieren
 */

var hwList; // HatWas-Liste
var entry;
var subpath = '/Empfehlung';  // Name bzw. Pfad der Kamelbau-Unterseiten

// Es gibt KEINE Regeln bzgl. der persönlichen Empfehlungen, ein automatischer Daumen taucht jedoch nur auf
// Seiten auf, wo es auch potentiell lesenswertes Zeug geben tut
//
// HINWEIS an ---> DUFO <----
// FINGER WEG VON DEN NAMENSRÄUMEN, wehe, du nimmst da was raus
//
var hwNS = (typeof(hwNS) == 'object') ? hwNS : new Array ("", "File", "Kamelionary", "Kamelobooks", "News", "Projekt"); // hierhin die Namensräume, wo der Daumen auftauchen soll (Achtung! canonical namespaces!);
 
// Empfehlungsliste auslesen
function readHwList(username, action) {
  jQuery.ajax({
    url:     mw.config.get( 'wgScript' ), 
    data:    { title: "Kamel:" + username + subpath, action: "raw" },
    cache:   false,
    // Noch keine Empfehlungs-Seite angelegt
    error:   function()     { 
        hwList = new Array(); 
        switch (action) {
          case "add":
            adHW();
            break;
          case "remove":
            removeHW();
            break;
        }
    },
    // Empfehlungs-Seite auslesen
    success: function(data) { 
        hwList = data.match(/\[\[.*\]\]/g);
        switch (action) {
          case "add":
            adHW();
            break;
          case "remove":
            removeHW();
            break;
        }
    }
  });
}

// Seite zur Empfehlungsliste hinzufügen
function adHW() {
  // edit-Token holen
  jQuery.ajax({
    url:     '/api.php', 
    data:    { action: "query", prop: "info", intoken: "edit", titles: "Kamel:" + Kamelname + subpath, format: "xml" },
    cache:   false,
    success: function(data) { 
      var edittoken = data.getElementsByTagName('page')[0].getAttribute('edittoken');
      hwList.push(entry);          // Aktuelle Seite dazu
      hwList = sortHW(hwList); // Sortieren
      // Empfehlungsliste bearbeiten
      jQuery.ajax({
        url:     '/api.php',
        type:    'POST',
        cache:   false,
        data:    { action: "edit", title: "Kamel:" + Kamelname + subpath, text: "* " + hwList.join("\n* "), summary: "+ " + entry, token: edittoken },
        success: function(data) {
          var text = '<p>Die Seite „' + Seitenname.replace(/_/g, ' ') + '“ wurde zu deinen <a href="'
            + mw.config.get( 'wgScript' ) + '/Kamel:' + Kamelname + subpath + '">Empfehlungen</a> hinzugefügt.</p>';
          jQuery('#mw-js-message').html(text);
          jQuery('#mw-js-message').slideDown('fast');
        }
      })
    }
  });
}

function removeHW() {
  // edit-Token holen
  jQuery.ajax({
    url:     '/api.php', 
    data:    { action: "query", prop: "info", intoken: "edit", titles: "Kamel:" + Kamelname + subpath, format: "xml" },
    cache:   false,
    success: function(data) { 
      var edittoken = data.getElementsByTagName('page')[0].getAttribute('edittoken');
      var i = jQuery.inArray(entry, hwList);
      delete hwList[i];            // Aktuelle Seite wech
      hwList = sortHW(hwList); // Sortieren
      // Empfehlungsliste bearbeiten
      jQuery.ajax({
        url:     '/api.php',
        type:    'POST',
        cache:   false,
        data:    { action: "edit", title: "Kamel:" + Kamelname + subpath, text: "* " + hwList.join("\n* "), summary: "- " + entry, token: edittoken },
        success: function(data) {
          var text = '<p>Die Seite „' + Seitenname.replace(/_/g, ' ') + '“ wurde von deinen <a href="'
            + mw.config.get( 'wgScript' ) + '/Kamel:' + Kamelname + subpath + '">Empfehlungen</a> entfernt.</p>';
          jQuery('#mw-js-message').html(text);
          jQuery('#mw-js-message').slideDown('fast');
        }
      })
    }
  });
}

function showHW(username) {
  /* soll später html-Code für Empfehlungslisten ausspucken */
}

// doppelte & leere Einträge entfernen + alphabetisch sortieren
function sortHW(list) {
  var sorted = new Array();
  for(var i=0; i<list.length; i++){
    for(var j=i+1; j<list.length; j++){
      if (list[i] == list[j]) j = ++i;
    }
    if (typeof(list[i]) != 'undefined') sorted.push(list[i]);        
  }
  return sorted.sort();
}

// Das ganze starten
jQuery(document).ready(function() {
  //if(typeof(disableGaGA) != 'undefined' && disableGaGA) return;
  if(jQuery.inArray(mw.config.get( 'wgCanonicalNamespace' ), hwNS) != -1) {
    entry = (mw.config.get( 'wgCanonicalNamespace' ) == "File") ? "[[:" + Seitenname.replace(/_/g, ' ') + "]]" 
                                             : "[[" + Seitenname.replace(/_/g, ' ') + "]]";
    jQuery('#p-views ul').append('<li class="icon"><span><a href="#"></a></span></li>');
    jQuery('#p-views li:last').ajaxComplete(function() {
      if(jQuery.inArray(entry, hwList) != -1) {
        jQuery(this).attr('id', 'ca-unhw');
        jQuery('#ca-unhw a').html('Entfehlen');
        jQuery('#ca-unhw a').attr('title', 'Seite nicht mehr empfehlen');
        jQuery('#ca-unhw a').unbind('click');
        jQuery('#ca-unhw a').bind('click', function() { readHwList(Kamelname, 'remove') });
      } else {
        jQuery(this).attr('id', 'ca-hw');
        jQuery('#ca-hw a').html('Empfehlen');
        jQuery('#ca-hw a').attr('title', 'Seite empfehlen');
        jQuery('#ca-hw a').unbind('click');
        jQuery('#ca-hw a').bind('click', function() { readHwList(Kamelname, 'add') });
      }
    });
    readHwList(Kamelname, false);
  }
})