MediaWiki:PrettyLinkWidget.js
/**
* @fileoverview File containing and invoking widget object
* @author Magnus Manske
* Pretty Link Widget
* (c) 2011 by Magnus Manske
* Released under GPL v2 or higher
* USAGE : Add the following line to Special:MyPage/common.js
*
* mw.loader.load('https://en.wikipedia.org/w/index.php?title=MediaWiki:PrettyLinkWidget.js&action=raw&ctype=text/javascript')
*
*/
jQuery.fn.exists = function(){return jQuery(this).length>0;}
var prettylinkwidget =
{
thumbsize : 0 ,
loaded : 0 ,
load_max : 0 ,
desc : 0 ,
title : 0 ,
lang : 0 ,
use_desc : 0 ,
hide_flags : 0 ,
images : {} ,
ignore_images : [ 'Commons-logo.svg','Disambig-dark.svg','Wikisource-logo.svg','Wiktfavicon en.svg','Loudspeaker.svg','Portal.svg','Qsicon Lücke.svg','Wikinews-logo.svg','Wikiquote-logo.svg','Reddot.svg',
'Qsicon lesenswert.svg','Wiktionary-logo-en.svg','Wikiversity-logo-Snorky.svg','Wikibooks-logo.svg','North.svg','South.svg','East.svg','West.svg','Red pog.svg','Compass rose pale-50x50.png',
'Speaker Icon.svg','Cscr-featured.svg','Wikispecies-logo.svg','Padlock-silver.svg' ] ,
createDialog : function () {
if ( jQuery('#prettylinkwidget_dialog').exists() ) return ;
var h = "\
" ;jQuery('#footer').append(h);
} ,
run : function () {
prettylinkwidget.createDialog();
jQuery('#prettylinkwidget_dialog').dialog({
width : 950 ,
height : 650 ,
modal : true
});
prettylinkwidget.title = mw.config.get ( 'wgTitle' ) ;
prettylinkwidget.lang = mediaWiki.user.options.get ( 'language' ) ;
prettylinkwidget.thumbsize = jQuery('#thumbsize').val() ;
prettylinkwidget.images = {} ;
prettylinkwidget.use_desc = jQuery('#use_desc').attr('checked') ;
prettylinkwidget.hide_flags = jQuery('#hide_flags').attr('checked') ;
prettylinkwidget.loaded = 0 ;
prettylinkwidget.desc = '' ;
jQuery('#results').hide() ;
jQuery('#loading').show() ;
prettylinkwidget.load_max = prettylinkwidget.use_desc + 1 ;
// Images
if ( 1 ) {
var url = "//" + prettylinkwidget.lang + ".wikipedia.org/w/api.php?action=query&generator=images&titles=" ;
url += escape(prettylinkwidget.title) ;
url += "&prop=imageinfo&gimlimit=500&format=json&iiprop=url|mime&iiurlwidth="+prettylinkwidget.thumbsize+"&iiurlheight="+prettylinkwidget.thumbsize+"&callback=?" ;
jQuery.getJSON ( url,
function ( data ) {
prettylinkwidget.images = data.query ? data.query.pages : [];
prettylinkwidget.check_loaded ( 1 ) ;
} ) ;
}
// First paragraph
if ( prettylinkwidget.use_desc ) {
var url = "//toolserver.org/~magnus/get_article_intro.php?language=" + prettylinkwidget.lang + "&title=" + prettylinkwidget.title + "&callback=?" ;
jQuery.getJSON ( url,
function ( data ) {
prettylinkwidget.desc = String ( data ) ;
prettylinkwidget.check_loaded ( 1 ) ;
} ) ;
}
} ,
show_images : function () {
var h = '
jQuery('#prettylinkwidget_images').html ( h ) ;
jQuery('#prettylinkwidget_images :radio').click ( function () {
prettylinkwidget.load_image_license() ;
} ) ;
} ,
load_image_license : function () {
var i = jQuery('#prettylinkwidget_images input:radio[name=irb]:checked').val();
if ( i == 'noimage' ) {
prettylinkwidget.show_results() ;
return ;
}
jQuery.each ( prettylinkwidget.images , function ( k , v ) {
if ( k != i ) return ;
var url = '//commons.wikimedia.org' ;
if ( v.imagerepository == 'local' ) url = '//' + prettylinkwidget.lang + '.wikipedia.org' ;
var t = v.title.split(':') ;
t.shift() ;
t = "File:" + t.join(':');
url += '/w/api.php?action=query&prop=categories&format=json&cllimit=500&titles=' + escape ( t ) + '&callback=?' ;
jQuery.getJSON ( url,
function ( data ) {
jQuery.each ( data.query.pages , function ( k2 , v2 ) {
jQuery.each ( v2.categories , function ( k3 , v3 ) {
var cat = v3.title ;
cat = cat.split(':') ;
cat.shift() ; // "Category:"
cat = cat.join(':') ;
if ( cat.match(/^CC-/) ) v.license = cat ;
else if ( cat.match(/^Creative Commons/) ) v.license = cat ;
else if ( cat.match(/^PD-/) ) v.license = cat ;
else if ( cat.match(/^PD/) ) v.license = cat ;
else if ( cat.match(/^Copyright by Wikimedia/) ) v.license = cat ;
if ( v.license === undefined ) { // "Second-rate"...
if ( cat.match(/^GFDL/) ) v.license = cat ;
else if ( cat.match(/^LGPL/) ) v.license = cat ;
}
} ) ;
} ) ;
prettylinkwidget.show_results();
} ) ;
} ) ;
} ,
show_results : function () {
var nl = "\n" ;
var rev = ''
if ( jQuery('#use_rev').attr('checked') ) {
rev = mediaWiki.config.get('wgCurRevisionId');
}
var w = jQuery('#div_width').val() * 50 ;
jQuery('#cur_width').html ( w ) ;
var h = '
h += '
' ;
h += '' ; h += ' h += '' ; h += ' | ' + nl ;
' ;
var url_title = escape ( prettylinkwidget.title.replace(' ','_') ) ; var wp_url ; if ( rev === undefined || rev == '' ) wp_url = '//' + prettylinkwidget.lang + '.wikipedia.org/wiki/' + url_title ; else wp_url = "//" + prettylinkwidget.lang + ".wikipedia.org/w/index.php?title=" + url_title + "&oldid=" + rev ; h += ' ' + prettylinkwidget.title.replace('_',' ') + ' on Wikipedia ' + nl ;
if ( jQuery('#use_desc').attr('checked') ) { var d = prettylinkwidget.desc.split('.') ; var nos = jQuery('#sentences').val() ; while ( d.length > nos ) d.pop(); d = d.join('.') ; if ( d != '' ) d += '.' ; h += ' ' + d + ' ' + nl ;
} h += ' Wikipedia article CC-BY-SA-3.0.' ;
' + nl ;
if ( image_license !== undefined && image_license != '' ) h += ' ' + image_license ; h += ' h += ' | ' ;
h += '
jQuery('#render').html ( h ) ;
jQuery('#source').val ( h ) ;
jQuery('#render a').css('color','blue');
jQuery('#results').show() ;
} ,
check_loaded : function ( add ) {
this.loaded += add ;
if ( this.loaded < this.load_max ) return ;
jQuery('#use_desc').change(this.show_results);
jQuery('#use_rev').change(this.show_results);
jQuery('#sentences').val ( 1 ) ;
jQuery('#sentences').change(this.show_results);
jQuery('#div_width').change(this.show_results);
jQuery('#sentences').click(this.show_results);
jQuery('#div_width').click(this.show_results);
jQuery('#max_sentences').html ( this.desc.split('.').length ) ;
jQuery('#loading').hide() ;
this.show_images() ;
this.show_results () ;
} ,
getParameterByName : function ( name , def ) {
if ( typeof def == 'undefined' ) def = '' ;
name = name.replace(/[\[]/,"\\\[").replace(/[\]]/,"\\\]");
var regexS = "[\\?&]"+name+"=([^]*)";
var regex = new RegExp( regexS );
var results = regex.exec( window.location.href );
if( results == null ) return def;
else return decodeURIComponent(results[1].replace(/\+/g, " "));
} ,
the_end_of_the_object : '' // To avoid worrying about the final comma...
} ;
jQuery(document).ready ( function () {
if ( mw.config.get ( 'wgAction' ) != 'view' ) return ;
// prettylinkwidget.createDialog();
mw.util.addPortletLink('p-tb', 'javascript:prettylinkwidget.run()', 'Pretty link widget', 't-prettylinkwidget', 'Pretty link widget', '', '#t-print');
} ) ;
// mw.loader.load('//toolserver.org/~magnus/lib/jquery-ui-1.7.2.custom/css/ui-lightness/jquery-ui-1.7.2.custom.css', 'text/css');
// mw.loader.load('//toolserver.org/~magnus/lib/jquery-ui-1.7.2.custom/js/jquery-ui-1.7.2.custom.min.js');