MediaWiki:Gadget-Fill Index.js

Nota: Depois de publicar, poderá ter de contornar a cache do seu navegador para ver as alterações.

  • Firefox / Safari: Pressione Shift enquanto clica Recarregar, ou pressione Ctrl-F5 ou Ctrl-R (⌘-R no Mac)
  • Google Chrome: Pressione Ctrl-Shift-R (⌘-Shift-R no Mac)
  • Internet Explorer / Edge: Pressione Ctrl enquanto clica Recarregar, ou pressione Ctrl-F5
  • Opera: Pressione Ctrl-F5.
/*
 * imported from fr.source on 19 Oct 2020
 *
 * Author: w:fr:Phe
 * Preenche os campos ao criar uma Galeria: a partir das informações
 * contidas na predefinição Book no Commons */
( function ( mw, $ ) {
    'use strict';

    var extract_dict = {};

    function setup_extract_dict() {
        extract_dict = self.fill_index_data.extract_dict;
    }

    function extract_field( name, contents ) {
        var regex = new RegExp('\\|[ ]*' + name + '[ ]*=[ ]*([^\n]*)');
        var match = regex.exec(contents);
        if( match ) {
            return {
                'field name' : extract_dict[name],
                'value' : match[1]
            };
        }
        return null;
    }

    function extract_content( data ) {
        var importationDone = false;
        $.each( data.query.pages, function( ids, page ) {
            if( ids < 0 ) {
                return;
            }

            var content = page.revisions[0]['*'];
            for( var idx in extract_dict ) {
                var dict = extract_field( idx, content );
                if( dict ) {
                    var f = document.getElementsByName("wpprpindex-" + dict['field name'])[0];
                    if( f ) {
                        var data = dict['value'];
                        //var regexp = new RegExp("{{[Ll]ang[ ]*\|[ ]*" + mw.config.get("wgPageContentLanguage") + "[ ]*\|[ ]*(.*)[ ]*}}");
                        //data = data.replace(regexp, "$1");
                        data = data.replace(/{{[Cc]reator[ ]*:[ ]*(.*)[ ]*}}/, "[[" + self.fill_index_data.ns_author_name + ":$1|$1]]")
                        f.value = data;
                    }
                }
            }
            importationDone = true;
        } );

        return importationDone;
    }

    function common_content( data ) {
        if( !extract_content( data ) ) {
            $.ajax( {
                url: mw.util.wikiScript( 'api' ),
                data: {
                    'format': 'json',
                    'action': 'query',
                    'prop': 'revisions',
                    'rvprop': 'content',
                    'titles': 'File:' + mw.config.get( 'wgTitle' )
                }
            } )
            .done( extract_content );
        }
    }

    function setup() {
        setup_extract_dict();
        if( $( ".mw-newarticletext" ).length === 0 ) {
            return;
        }

        $.ajax( {
            url: '//commons.wikimedia.org/w/api.php',
            data: {
                'format': 'json',
                'action': 'query',
                'prop': 'revisions',
                'rvprop': 'content',
                'titles': 'File:' + mw.config.get( 'wgTitle' )
            },
            dataType: 'jsonp',
            cache: true
        } )
        .done( common_content );
    }

/* Localisation section, you can provide your own data before loading this script to
 * change the script behavior
 */
if( !self.fill_index_data ) {
    self.fill_index_data = {}
}

if( !self.fill_index_data.ns_author_name ) {
    self.fill_index_data.ns_author_name = 'Autor';
}

/* Localisation section, you can provide your own data before loading this script to
 * change the script behavior
 */
if( !self.fill_index_data ) {
    self.fill_index_data = {}
}

if( !self.fill_index_data.extract_dict ) {
    self.fill_index_data.extract_dict = {
        'Editor'      : 'Editor',
        'Publisher'   : 'Editora',
        'Author'      : 'Autor',
        'Translator'  : 'Tradutor',
        'Illustrator' : 'Ilustrador',
        'Image page'  : 'Capa',
        'Title'       : 'Título',
        'Date'        : 'Ano',
        'City'        : 'Local',
        'Volume'      : 'Volume',
//    	'Source'      : 'Fonte', // Predefinições incompatíveis.
	    'LCCN'        : 'LCCN', 
    };
}
/* end of localisation section */

$( function() {
    if( mw.config.get( "wgCanonicalNamespace" ) === "Index" && mw.config.get( "wgAction" ) === "edit" ) {
        setup();
    }
} );
} ( mediaWiki, jQuery ) );