MediaWiki:Gadget-CatWatch.js: differenze tra le versioni

Contenuto cancellato Contenuto aggiunto
m ripristino la versione 68828284 (al contrario degli altri OS, il diff non è necessariamente l'ultima versione della pagina)
aggiunti spazi parentesi, tab, jslint=>jshint per mw:Manual:Coding conventions/JavaScript
Riga 14:
* @author [[Utente:Rotpunkt]]
*/
/*jslintjshint unparamunused: truefalse, evil: true, browser: true, devel: true */
/*global mediaWiki: false, jQuery: false */
 
( function ( mw, $ ) {
'use strict';
 
// Massimo numero di pagine monitorate per categoria (max 500)
var maxPagesInCategory = 100,
// Configurazione utente
userConfig = 'User:' + mw.config.get( 'wgUserName' ) + '/CategorieOsservate.js',
// Modello di configurazione
configModel = 'MediaWiki:Gadget-CatWatch.js/CategorieOsservateTemplate.js',
// Messaggio di aiuto per la prima attivazione
msgHelp = '[Gadget-CatWatch] Non hai ancora una lista di categorie da controllare. Vuoi crearne una?';
 
// Ritorna la variabile CategorieOsservate nel CategorieOsservate.js dell'utente
function getUserConfig( categoryHandler ) {
$.ajax( {
url: mw.config.get( 'wgScript' ),
data: {
title: userConfig,
action: 'raw',
ctype: 'text/javascript'
},
},
dataType: 'script',
cache: false
} )
})
.done( function ( data ) {
try {
eval( data );
} catch ( e ) {
alert( '[Gadget-CatWatch] Errore in ' + userConfig );
return;
}
}
if ( window.CategorieOsservate !== undefined ) {
if ( window.CategorieOsservate.length > 0 ) {
categoryHandler( window.CategorieOsservate );
}
}
} else if ( confirm( msgHelp ) ) {
document.___location = mw.config.get( 'wgScript' ) +
'?action=edit&title=' + userConfig +
'&preload=' + configModel;
}
}
} )
})
.fail( function ( jqXHR, textStatus, errorThrown ) {
if ( mw.util.getParamValue( 'debug' ) ) {
alert( '[Gadget-CatWatch] Errore nel leggere ' +
userConfig + ': ' + errorThrown );
}
}
} );
});
}
}
 
// Ritorna la lista delle pagine in una categoria ( in formato JSON )
function getCategoryPages( name, end, pageHandler ) {
$.ajax( {
url: mw.util.wikiScript( 'api' ),
data: {
action: 'query',
list: 'categorymembers',
cmtitle: 'Categoria:' + name,
cmlimit: maxPagesInCategory,
cmprop: 'title|timestamp',
cmsort: 'timestamp',
cmdir: 'desc',
cmend: end,
format: 'json'
},
},
dataType: 'json'
} )
})
.done( function ( data ) {
pageHandler( data.query.categorymembers );
} );
});
}
}
 
function padleft0( num ) {
return ( num < 10 ? '0' : '' ) + num;
}
}
 
// Parsifica un timestamp in date (1 gen 2001) e time (01:23)
function parseTimestamp( timestamp ) {
var date, hours, minutes, months;
 
date = new Date( timestamp );
hours = date.getHours();
minutes = date.getMinutes();
months = mw.config.get( 'wgMonthNamesShort' );
return {
date: date.getDate() + ' ' + months[date.getMonth() + 1] + ' ' +
date.getFullYear(),
time: padleft0( hours ) + ':' + padleft0( minutes )
};
};
}
}
 
function makeTimestamp( text ) {
var date = text.split( ' ' );
date[1] = mw.config.get( 'wgMonthNamesShort' ).indexOf( date[1] );
return date[2] + '-' + padleft0( date[1] ) + '-' +
padleft0( parseInt( date[0], 10 ) ) + 'T00:00:00Z';
}
}
 
// Crea un nuovo elemento di OsservatiSpeciali
function createWatchlistEl( title, time, category ) {
var url, urlcat, $cron, $cat, $page, $last;
 
url = mw.config.get( 'wgScript' ) + '?title=' + title;
urlcat = mw.config.get( 'wgArticlePath' ).replace( '$1', 'Categoria:' + category );
$cron = $( '<a>' ).attr( 'href', url + '&action=history' ).text( 'cron' );
$cat = $( '<a>' ).attr( 'href', urlcat ).text( 'Categoria:' + category );
$page = $( '<a>' ).attr( 'href', mw.config.get( 'wgArticlePath' )
.replace( '$1', encodeURIComponent( title ) ) + '?redirect=no' ).text( title );
$last = $( '<a>' ).attr( 'href', url + '&diff=last' ).text( 'last' );
return $( '<li>' ).css( 'font-weight', 'bold' )
.append( '( diff | ', $cron, ' ) . . ', $cat, '; ', time,
' . . ( + ', $page, ' ( ', $last, ' ) )' );
}
}
 
// Parsifica l'elenco di pagine di OsservatiSpeciali
function parseWatchlistDOM() {
var date, ret = {};
// ogni giorno è un <h4>
$( 'h4' ).each( function () {
date = $( this ).text();
ret[date] = [];
// la pagina di ogni giorno è un <li> in un <ul class='special'>
$( this ).next( 'ul.special' ).find( 'li' ).each( function () {
ret[date].push( {
time: $( this ).find( 'span.mw-changeslist-date' ).text(),
el: $( this )
} );
});
} );
});
// retrocompatibilità con il vecchio CatWatch: con la vista raggruppata
// visualizzava le pagine del CatWatch al fondo del giorno
if ( ret[date].length === 0 ) {
ret[date].push( {
time: '23:59',
el: $( this ).next( 'div' ).find( 'table:last-child()' )
} );
});
}
}
} });
ret.oldestDate = date ? makeTimestamp( date ) : null;
 
return ret;
}
}
 
// Aggiunge una pagina ad OsservatiSpeciali, se quel giorno è visualizzato
function watchlistAdd( watchlist, page, category ) {
var ts, el, daypages, prepended = false;
 
ts = parseTimestamp( page.timestamp );
daypages = watchlist[ts.date];
if ( daypages ) {
el = createWatchlistEl( page.title, ts.time, category );
// per tutte le pagine di quel giorno cerca quella col time antecedente
$.each( daypages, function ( i, entry ) {
if ( ts.time > entry.time ) {
entry.el.before( el );
daypages.splice( i, 0, { time: ts.time, el: el } );
prepended = true;
return false;
}
}
} );
});
if ( !prepended ) {
daypages[daypages.length - 1].el.after( el );
daypages.push( { time: ts.time, el: el } );
}
}
}
}
}
}
 
$( function () {
// Se la pagina corrente è Speciale:OsservatiSpeciali
if ( mw.config.get( 'wgPageName' ) === 'Speciale:OsservatiSpeciali' ) {
// scarica la lista delle categorie da monitorare.
getUserConfig( function ( categories ) {
// Parsifica OsservatiSpeciali.
var watchlist = parseWatchlistDOM();
// Per ogni categoria da monitorare
$.each( categories, function ( i, category ) {
// scarica la lista delle pagine in quella categoria.
getCategoryPages( category, watchlist.oldestDate, function ( categorymembers ) {
// Per ogni pagina nella categoria monitorata
$.each( categorymembers, function ( i, page ) {
// aggiunge la pagina a OsservatiSpeciali.
watchlistAdd( watchlist, page, category );
} );
});
} );
});
} );
});
} );
});
}
}
} });
}( mediaWiki, jQuery ) );