MediaWiki:Gadget-pageWarnings.js

Da Wikinotizie, le notizie a contenuto aperto

Nota: dopo aver pubblicato, potrebbe essere necessario pulire la cache del proprio browser per vedere i cambiamenti.

  • Firefox / Safari: tieni premuto il tasto delle maiuscole Shift e fai clic su Ricarica, oppure premi Ctrl-F5 o Ctrl-R (⌘-R su Mac)
  • Google Chrome: premi Ctrl-Shift-R (⌘-Shift-R su un Mac)
  • Internet Explorer / Edge: tieni premuto il tasto Ctrl e fai clic su Aggiorna, oppure premi Ctrl-F5
  • Opera: premi Ctrl-F5.
/* jshint
      esversion : 5
    , browser   : true
    , undef     : true
    , unused    : true
    , eqeqeq    : true
    , laxcomma  : true
    , laxbreak  : true
 */
/* globals
      $       : false
    , mw      : false
    , console : false
 */

/*
 *   Esegue dei controlli automatici sulla pagina
 *   e in caso di necessità emette un avviso.
 *
 */

mw.libs.pageWarnings = {
    check : function () {

        var
            key , module
            , infos = {
                  nameSpace  : 'wgCanonicalNamespace'
                , pageName   : 'wgPageName'
                , action     : 'wgAction'
                , categories : 'wgCategories'
            }
            , resolveReport = function ( module , res ) {

                module.report = res;

            }
        ;

        for ( key in infos )
            this.info[ key ] = mw.config.get( infos[ key ] );

        this.info.fullPageName = this.info.nameSpace + ':' + this.info.pageName;

        for ( key in this.modules ) {

            module = this.modules[ key ];
            module.top    = this;
            module.info   = this.info;
            module.report = null;

            if (
                    'skip' in module
                &&  module.skip()
            ) {
                module.report = 'skipped';
                continue;
            }

            module.report = module.check() || module.report || 'ok';

            if ( 'then' in Object( module.report ) )
                module.report.then(
                    resolveReport.bind( null , module )
                );

        }

        //~ setTimeout(                         // DEBUG
            //~ this.logReport.bind( this )
            //~ , 1000
        //~ );

    }
    , logReport : function () {

        var
            report = '\n\n\mw.libs.pageWarnings :: report\n'
            , maxKeyLen = 0
            , key
        ;

        for ( key in this.modules )
            if ( key.length > maxKeyLen )
                maxKeyLen = key.length;

        for ( key in this.modules )
            report += (
                  '\n    - ' + key
                + (
                    'repeat' in String.prototype
                    ? ' '.repeat( maxKeyLen - key.length )
                    : ' '
                )
                + ' : ' + this.modules[ key ].report
            );

        report += '\n\n';

        console.log( report );

    }
    //
    , info : {
          nameSpace    : null
        , pageName     : null
        , fullPageName : null
        , action       : null
        , categories   : null
    }
    //
    , modules : {
        regioniGeografiche : {
            list : [
                  'Africa',
                  'America',
                  //'America centrale',
                  //'America meridionale',
                  //'America settentrionale',
                  'Antartide',
                  'Asia',
                  'Europa',
                  'Medio Oriente',
                  'Mondo',
                  'Oceania'
            ]
            , skip : function () {

                var info = this.info;

                return (
                        info.nameSpace    !== ''
                    ||  info.action       !== 'view'
                    ||  info.fullPageName === ':Pagina_principale'
                );

            }
            , check : function () {

                var
                    categories = this.info.categories
                    , i , pageCat
                ;

                for ( i = 0; i < categories.length; i++ ) {
                    pageCat = categories[ i ];
                    if ( this.list.indexOf( pageCat ) !== -1 )
                        return;
                }

                return this.top.notification.notify(
                    '<p>Non è presente alcuna <a href="/wiki/Special:CategoryTree?target=Regioni_geografiche">categoria geografica principale</a>.</p>'
                );

            }
        }
        , argomenti : {
            list : [
                  'Ambiente'
                , 'Cultura e società'
                , 'Curiosità'
                , 'Disastri e incidenti'
                , 'Economia e finanza'
                , 'Giustizia e criminalità'
                , 'Infortuni'
                , 'Meteo'
                , 'Necrologi'
                , 'Politica e conflitti'
                , 'Scienza e tecnologia'
                , 'Società'
                , 'Sport'
                , 'Trasporti'
            ]
            , skip : function () {

                return  this.top.modules
                        .regioniGeografiche
                        .skip();

            }
            , check : function () {

                var
                    categories = this.info.categories
                    , i , pageCat
                ;

                for ( i = 0; i < categories.length; i++ ) {
                    pageCat = categories[ i ];
                    if ( this.list.indexOf( pageCat ) !== -1 )
                        return;
                }

                return this.top.notification.notify(
                    '<p>Non è presente alcuna <a href="/wiki/Special:CategoryTree?target=Argomenti">categoria principale di argomento</a>.</p>'
                );

            }
        }
    }
    //
    , notification : {
        notify : function ( content ) {

            content = content.replace(
                /<a [^>]+/g , '$& target="_blank"'
            );

            if ( content.startsWith( '<' ) )
                content = $( content );

            return (
                mw.notify(
                    content
                    , this.customOptions || this.defaultOptions
                )
                .then( function ( n ) {

                    return n.$notification.text();

                } )
            );

        }
        //
        , defaultOptions : { autoHide : false , type: 'error' } // type: 'info' | 'warn' | 'error'
        , customOptions  : null
    }
};
mw.libs.pageWarnings.check();