MediaWiki:Common.js/RandomBook.js

Un livre de Wikilivres.

Attention : Depuis MediaWiki 1.18 les pages se terminant avec l'extension .js ou .css sont interprétées comme des pages wiki ! En particulier les modèles (subst ou non) et les liens. Vous devez donc migrer le code source et effectuer vos changements en évitant ces éléments de syntaxe wiki (peu importe leurs emplacements dans le code source : commentaire, chaine) :

  • Double accolades ouvrantes (en particulier avec subst:) : séparer les deux accolades "{"+"{" du reste de la chaine
  • Double crochets ouvrants : même technique de séparation.
  • Signature (tildes ~ multiples) : même technique de séparation.

Note : après avoir enregistré vos préférences, vous devrez attendre que le serveur mette à jour la feuille de style globale avant de forcer le rechargement complet du cache de votre navigateur pour voir les changements.

  • Firefox / Safari : Maintenez la touche Maj (Shift) en cliquant sur le bouton Actualiser ou pressez Ctrl-F5 ou Ctrl-R (⌘-R sur un Mac) ;
  • Google Chrome : Appuyez sur Ctrl-Maj-R (⌘-Shift-R sur un Mac) ;
  • Internet Explorer : Maintenez la touche Ctrl en cliquant sur le bouton Actualiser ou pressez Ctrl-F5 ;
  • Konqueror : Cliquez sur Actualiser ou pressez F5 ;
  • Opera : Videz le cache dans Outils → Préférences.
/* Random Book Finder, version [0.0.3a-r1]
Originally from: http://en.wikibooks.org/wiki/User:Splarka/randbook.js
Based roughly on http://en.wikibooks.org/w/index.php?title=MediaWiki:RandomBook.js&oldid=1209572 by Darklama.
Modified by DavidL to avoid eavy alphabetical category system usage.

Grabs 10 random pages from the API, checks if any are books. Repeats until it finds a book.

Notes:
* Don't grab more than 10 random pages. Logged in users can get more, but anon users cannot. 
** Since this uses callback, all users are limited to 10 anyway.
* Automatically re-queries until it finds a page without a slash.
* Setting window.location.href eats history in some browsers.
** To show a link instead use: var randBookAsLink = true; (users can set this in their monobook individually too);
* Uses curid links, I usually trust these more than trying to generate /wiki links.
*/

var randBookAsLink = false;
var rbrqid = 0;
var rburl = mw.config.get('wgServer') + mw.config.get('wgScriptPath') + '/api.php?action=query&indexpageids=1&generator=random&grnnamespace=0|110&grnlimit=10&prop=categories&cllimit=100&format=json&callback=showRandBookCB&requestid=rb';

function showRandBook()
{
  if (window.randBookAsLink)
    injectSpinner(document.getElementById('n-randbook').firstChild,'randBookSpinner');
  else
    injectSpinner(document.getElementById('n-randbook'),'randBookSpinner');

  mw.loader.load(rburl + rbrqid);
  rbrqid++;
}

// Books should be in one of the following namespace:
// Les livres doivent être dans l'un des espaces de nom suivants :
var book_ns = [ 0 ];

function isBookNS(ns)
{
  for (var i=0;i<book_ns.length;i++)
    if (book_ns[i]==ns) return true;
  return false;
}

function showRandBookCB(obj)
{
  if (!obj['query'] || !obj['query']['pages'] || !obj['query']['pageids'])
  {
    document.getElementById('n-randbook').appendChild(document.createTextNode(' error'));
    removeSpinner('randBookSpinner');
    return;
  }
  var id = obj['query']['pageids'];
  var pages = obj['query']['pages'];
  var found;
  for (var i=0;i<id.length;i++)
  {
    var page = pages[id[i]];
    if ( (isBookNS(page['ns'])) && page['title'].indexOf('/')<0 )
    {
      found = obj['query']['pages'][id[i]];
      break;
    }
  }
  if(!found) {
    // didn't find any, try again
    mw.loader.load(rburl + rbrqid);
    rbrqid++;
  } else {
    removeSpinner('randBookSpinner');
    var link = mw.config.get('wgServer') + mw.config.get('wgScript') + '?curid=' + found['pageid'];

    if (window.randBookAsLink) {
      var a = document.createElement('a');
      a.setAttribute('href',link);
      a.style.display = 'block';
      a.appendChild(document.createTextNode(' \u2022\u00A0' + found['title']))
      document.getElementById('n-randbook').appendChild(a);
    } else if (window.location.assign) {
      window.location.assign(link);
    } else {
      window.location.href = link; 
    }
  }
}

function showRandBookLink()
{
  mw.util.addPortletLink('p-navigation','javascript:showRandBook();','Livre au hasard','n-randbook','Afficher un livre au hasard !','x');
}

$(showRandBookLink);