Vés al contingut

Usuari:Joancreus/jswikipedia.js

De la Viquipèdia, l'enciclopèdia lliure

Nota: Després de desar, heu de netejar la memòria cau del navegador per veure els canvis. En la majoria de navegadors amb Windows o Linux, premeu Ctrl+F5 o bé premeu Shift i cliqueu el botó "Actualitza" (Ctrl i "Actualitza" amb Internet Explorer). Vegeu més informació i instruccions per a cada navegador a Viquipèdia:Neteja de la memòria cau.

function HTTPRequest() {
    if (typeof XMLHttpRequest != 'undefined') {
        return new XMLHttpRequest();
    }
    try {
        return new ActiveXObject("Msxml2.XMLHTTP");
    } catch (e) {
        try {
            return new ActiveXObject("Microsoft.XMLHTTP");
        } catch (e) {}
    }
    return false;
}

/* Page class. Only the title argument is needed. */
Page = function(title, lang, fam) {
  r = HTTPRequest();

  if (typeof(title) == 'undefined') {
    alert("Error: Undefined title of page");
  }
  lang = typeof(lang) != 'undefined' ? lang : wgContentLanguage;
  fam = typeof(fam) != 'undefined' ? fam : 'wikipedia';
  
  /* Returns de title of the page. */
  this.getTitle = function () { return title; }

  /* Returns de language of the page. */
  this.getLanguage = function () { return lang; }

  /* Returns de family of the page. */
  this.getFamily = function () { return family; }

  this.getBaseURL = function () {
    return "http://"+lang+"."+fam+".org";
  }
  
  /* Gets the edit token for editing the page. */
  this.getEditToken = function () {
    var url = this.getBaseURL() + '/w/api.php?format=json&action=query&prop=info&indexpageids=1&intoken=edit&titles='+title;
    r.open('GET', url, false);
    r.send(null);
    var resp = eval('(' + r.responseText + ')');
    return resp['query']['pages'][resp['query']['pageids'][0]]['edittoken'];
  }
  
  /* Gets the move token for moving the page. */
  this.getMoveToken = function () {
    var url = this.getBaseURL() + '/w/api.php?format=json&action=query&prop=info&indexpageids=1&intoken=move&titles='+title;
    r.open('GET', url, false);
    r.send(null);
    var resp = eval('(' + r.responseText + ')');
    return resp['query']['pages'][resp['query']['pageids'][0]]['movetoken'];
  }

  /* Returns de wiki content of the page. */
  this.get = function () {
    r = HTTPRequest();
    r.open("GET", this.getBaseURL()+"/w/index.php?title="+title+"&action=raw", false);
    r.send(null);
    return r.responseText;
  }

  /* Returns de rendered html content of the page. */
  this.getHTML = function () {
    r.open("GET", this.getBaseURL()+"/w/index.php?title="+title+"&action=render", false);
    r.send(null);
    return r.responseText;
  }

  /* Posts content to a page. */
  this.put = function (text) {
       var pars = 'action=edit&format=json&title='+title+'&text='+text+'&token=' + encodeURIComponent(this.getEditToken());
       r.open('POST', this.getBaseURL()+"/w/api.php", false);
       r.setRequestHeader('Content-type', 'application/x-www-form-urlencoded');
       r.setRequestHeader('Connection', 'keep-alive');
       r.setRequestHeader('Content-length', pars.length);
       r.send(pars);
       e = eval('('+r.responseText+')');
       if (typeof(e["edit"]) != 'undefined') { if (e["edit"]["result"] == "Success") { return true; } }
       else { return e["error"]["code"]; }
  }

  /* Moves a page to another one. */
  this.move = function (newtitle) {
       var pars = 'action=move&format=json&from='+title+'&to='+newtitle+'&token=' + encodeURIComponent(this.getMoveToken());
       r.open('POST', this.getBaseURL()+"/w/api.php", false);
       r.setRequestHeader('Content-type', 'application/x-www-form-urlencoded');
       r.setRequestHeader('Connection', 'keep-alive');
       r.setRequestHeader('Content-length', pars.length);
       r.send(pars);
       alert(r.responseText);
       e = eval('('+r.responseText+')');
       if (typeof(e["edit"]) != 'undefined') { if (e["edit"]["result"] == "Success") { return true; } }
       else { return e["error"]["code"]; }
  }
}