User:Yurik/common.js

From OpenStreetMap Wiki
Jump to navigation Jump to search

Note: After publishing, you may have to bypass your browser's cache to see the changes.

  • Firefox / Safari: Hold Shift while clicking Reload, or press either Ctrl-F5 or Ctrl-R (⌘-R on a Mac)
  • Google Chrome: Press Ctrl-Shift-R (⌘-Shift-R on a Mac)
  • Internet Explorer / Edge: Hold Ctrl while clicking Refresh, or press Ctrl-F5
  • Opera: Press Ctrl-F5.
// mediaWiki.loader.load('/w/index.php?title=User:Yurik/dataitemeditor.js&action=raw&ctype=text/javascript');

if (0) {

/**
 * Hide references with 0 counts
 *
 * Dependencies: mediawiki.util
 */
( function( $, mw ) {
  $( function () {
    if( !mw.config.exists( 'wbEntityId' ) ) {
      return;
    }
    mw.hook('wikibase.entityPage.entityView.rendered').add(function () {

	// Must have a single digit 0 without any other digits around it
	var noRefRe = RegExp('(^|[^0-9])0($|[^0-9])');
	$("div.wikibase-statementview-references-container span.ui-toggler-label").each(function () {
		if (!noRefRe.test(this.textContent)) return; // Ignore if has non-0 ref count
		var $elemToHide = $(this).parent().parent().parent();
		var $elemToPad = $elemToHide.parent();
		if ($elemToPad.parent().parent().parent()[0].id == 'P6') return; // Ignore for Status
		$elemToPad.css("padding-bottom", 0);
		$elemToHide.hide();
	});
    } );
  } );
} )( jQuery, mediaWiki );


/*
/* Adapted from https://www.wikidata.org/wiki/User_talk:Yair_rand/WikidataInfo.js -- Thanks!
 * dependencies=mediawiki.jqueryMsg,mediawiki.user
 */
location.search.indexOf( 'printable=yes' ) === -1
&& mw.config.get( 'wgPageContentModel' ) !== 'wikibase-item'
&& mw.config.get( 'wgPageContentModel' ) !== 'wikibase-property'
&& mw.config.get( 'wgPageContentModel' ) !== 'wikibase-lexeme'
&& mw.config.get( 'wgPageContentModel' ) !== 'flow-board'
&& mw.loader.using( [ 'mediawiki.jqueryMsg', 'mediawiki.user' ] ).done( function () {
	var translate = {
		'en': {
			notfound: "Item not found.",
			intro: "OpenStreetMap: ",
			nolabel: "(No label)",
			nodescription: "no description given",
			aliases: "{{PLURAL:$1|Alias|Aliases}}: ",
			noaliases: "None"
		},
		'ar': {
			notfound: "لم يعثر على هذه المادة في ويكي بيانات.",
			intro: "ويكي بيانات: ",
			nolabel: "(لا توجد تسمية)",
			nodescription: "لا يوجد توصيف",
			aliases: "أسماء أخرى: ",
			noaliases: "لا توجد أسماء أخرى"
		},
		'ast': {
			notfound: "Nun s'alcontró l'elementu",
			intro: "OpenStreetMap: ",
			nolabel: "(Ensin etiqueta)",
			nodescription: "ensin descripción",
			aliases: "{{PLURAL:$1|Alcuñu|Alcuños}}: ",
			noaliases: "Non"
		},
		'be-tarask': {
			notfound: "Элемэнт ВікіЗьвестак ня знойдзены.",
			intro: "ВікіЗьвесткі: ",
			nolabel: "(Няма меткі)",
			nodescription: "няма апісаньня",
			aliases: "Сынонімы: ",
			noaliases: "Няма"
		},
		'bn': {
			notfound: "উইকিউপাত্তের আইটেম পাওয়া যায়নি।",
			intro: "উইকিউপাত্ত: ",
			nolabel: "(লেবেল নেই)",
			nodescription: "বিবরণ দেওয়া হয়নি",
			aliases: "উপনামগুলি: ",
			noaliases: "কোনটি না"
		},
		'bs': {
			notfound: "Nije pronađena stavka na Wikipodacima.",
			intro: "OpenStreetMap: ",
			nolabel: "(bez oznake)",
			nodescription: "opis nije naveden",
			aliases: "Alternativne oznake: ",
			noaliases: "Nema"
		},
		'ca': {
			notfound: "No s'ha trobat l'element.",
			intro: "OpenStreetMap: ",
			nolabel: "(Cap etiqueta)",
			nodescription: "cap descripció introduïda",
			aliases: "Àlies: ",
			noaliases: "Cap"
		},
		'cs': {
			notfound: "Položka nebyla ve Wikidatech nalezena.",
			intro: "OpenStreetMap: ",
			nolabel: "(bez názvu)",
			nodescription: "bez popisu",
			aliases: "Alternativní názvy: ",
			noaliases: "žádné"
		},
		'cy': {
			notfound: "Eitem heb ei ganfod.",
			intro: "OpenStreetMap: ",
			nolabel: "(Dim label)",
			nodescription: "ni roddwyd disgrifiad",
			aliases: "{{PLURAL:$1|Enw arall|Enwau eraill}}: ",
			noaliases: "Dim"
		},
		'de': {
			notfound: "Eintrag nicht gefunden.",
			intro: "OpenStreetMap: ",
			nolabel: "(Keine Bezeichnung)",
			nodescription: "Keine Beschreibung vorhanden.",
			aliases: "{{PLURAL:$1|Alternative Bezeichnung|Alternative Bezeichnungen}}: ",
			noaliases: "keine"
		},
		'dsb': {
			notfound: "sajźba se njejo namakana.",
			intro: "OpenStreetMap: ",
			nolabel: "(bźez pomjenjenja)",
			nodescription: "bźez wopisa",
			aliases: "Alternatiwne mjenja: ",
			noaliases: "žedne"
		},
		'el': {
			notfound: "Δεν βρέθηκε αντικείμενο στο.",
			intro: "OpenStreetMap: ",
			nolabel: "(Χωρίς ετικέτα)",
			nodescription: "δεν δίνεται περιγραφή",
			aliases: "Επίσης γνωστό ως: ",
			noaliases: "Κανένα"
		},
		'eo': {
			notfound: "Datuma ero ne trovita.",
			intro: "OpenStreetMap: ",
			nolabel: "(sen etikedo)",
			nodescription: "sen priskribo",
			aliases: "{{PLURAL:$1|Alternativa nomo|Alternativaj nomoj}}: ",
			noaliases: "neniu"
		},
		'es': {
			notfound: "Elemento no encontrado",
			intro: "OpenStreetMap: ",
			nolabel: "(No etiquetado)",
			nodescription: "sin descripción",
			aliases: "Nombres alternativos: ",
			noaliases: "No"
		},
		'fa': {
			notfound: "برای این صفحه در ویکی‌داده آیتمی یافت نشد.",
			intro: "ویکی‌داده: ",
			nolabel: "(بدون برچسب)",
			nodescription: "بدون توضیحات",
			aliases: "نام‌های دیگر: ",
			noaliases: "ندارد!"
		},
		'fi': {
			notfound: "kohdetta ei löydy.",
			intro: "OpenStreetMap: ",
			nolabel: "(ei nimeä)",
			nodescription: "kuvausta ei lisätty",
			aliases: "Aliakset: ",
			noaliases: "ei ole"
		},
		'fr': {
			notfound: "Élement non trouvé.",
			intro: "OpenStreetMap: ",
			nolabel: "(Aucun label)",
			nodescription: "Aucune description fournie",
			aliases: "Alias : ",
			noaliases: "Aucun"
		},
		'he': {
			notfound: "אין פריט ויקינתונים.",
			intro: "ויקינתונים: ",
			nolabel: "(ללא תווית)",
			nodescription: "ללא תאור",
			aliases: "כינויים: ",
			noaliases: "אין"
		},
		'hi': {
			notfound: "विकिडाटा प्रविष्टि नहीं मिली।",
			intro: "विकिडाटा: ",
			nolabel: "(कोई लेबल नहीं)",
			nodescription: "कोई विवरण नहीं दिया गया",
			aliases: "{{PLURAL:$1|ऊर्फ|अन्य नाम}}: ",
			noaliases: "कुछ भी नहीं"
		},
		'hsb': {
			notfound: "sadźba njeje so namakała.",
			intro: "OpenStreetMap: ",
			nolabel: "(bjez pomjenowanja)",
			nodescription: "bjez wopisa",
			aliases: "Alternatiwne mjena: ",
			noaliases: "žane"
		},
		'hu': {
			notfound: "Nincs még ilyen fogalom a Wikidatában",
			intro: "OpenStreetMap: ",
			nolabel: "(nincs címke)",
			nodescription: "nincs leírás",
			aliases: "Más nevek: ",
			noaliases: "nincs"
		},
		'id': {
			notfound: "Butir belum ada.",
			intro: "OpenStreetMap: ",
			nolabel: "(Belum ada label)",
			nodescription: "belum ada pemerian",
			aliases: "Nama lain: ",
			noaliases: "Belum ada"
		},
		'ilo': {
			notfound: "Saan a nabirukan ti Wikidata a banag.",
			intro: "OpenStreetMap: ",
			nolabel: "(Awan ti etiketa)",
			nodescription: "awan ti deskripsion a naited",
			aliases: "Sabali a nagan: ",
			noaliases: "Awan"
		},
		'it': {
			notfound: "Elemento non trovato.",
			intro: "OpenStreetMap: ",
			nolabel: "(Nessuna etichetta)",
			nodescription: "Nessuna descrizione fornita",
			aliases: "Alias: ",
			noaliases: "Nessuno"
		},
		'ja': {
			notfound: "項目が見つかりません。",
			intro: "OpenStreetMap: ",
			nolabel: "(ラベルなし)",
			nodescription: "説明がありません",
			aliases: "別名: ",
			noaliases: "なし"
		},
		'jv': {
			notfound: "Butir Wikidata durung ana.",
			intro: "OpenStreetMap: ",
			nolabel: "(Durung ana label)",
			nodescription: "Durung ana wedharan",
			aliases: "Jeneng liya: ",
			noaliases: "Durung ana"
		},
		'ko': {
			notfound: "위키데이터에 항목이 없습니다.",
			intro: "OpenStreetMap: ",
			nolabel: "(레이블이 지정되지 않았습니다)",
			nodescription: "설명이 없습니다",
			aliases: "다른 이름: ",
			noaliases: "없음"
		},
		'min': {
			notfound: "Butir alun ado.",
			intro: "OpenStreetMap: ",
			nolabel: "(Alun ado label)",
			nodescription: "alun ado pemerian",
			aliases: "Namo lain: ",
			noaliases: "Alun ado"
		},
		'nb': {
			notfound: "element ikke funnet.",
			intro: "OpenStreetMap: ",
			nolabel: "(ingen etikett definert)",
			nodescription: "ingen beskrivelse definert",
			aliases: "Alias: ",
			noaliases: "Ingen"
		},
		'nl': {
			notfound: "item niet gevonden.",
			intro: "OpenStreetMap: ",
			nolabel: "(Geen label)",
			nodescription: "geen beschrijving ingegeven",
			aliases: "{{PLURAL:$1|Alias|Aliassen}}: ",
			noaliases: "Geen"
		},
		'nn': {
			notfound: "Fann ikkje noko datasett.",
			intro: "OpenStreetMap: ",
			nolabel: "(ingen merkelapp)",
			nodescription: "inga skildring oppgjeven",
			aliases: "Tilleggsnamn: ",
			noaliases: "Ingen"
		},
		'pl': {
			notfound: "Brak elementu.",
			intro: "OpenStreetMap: ",
			nolabel: "(brak etykiety)",
			nodescription: "brak opisu",
			aliases: "Inne nazwy: ",
			noaliases: "brak"
		},
		'pt': {
			notfound: "Item não encontrado",
			intro: "OpenStreetMap: ",
			nolabel: "(Nenhum rótulo)",
			nodescription: "nenhuma descrição dada",
			aliases: "Nomes alternativos: ",
			noaliases: "Nenhum"
		},
		'ru': {
			notfound: "Объект Викиданных не найден.",
			intro: "Викиданные: ",
			nolabel: "(нет метки)",
			nodescription: "нет описания",
			aliases: "Синонимы: ",
			noaliases: "нет"
		},
		'sk': {
			notfound: "Položka nebola vo Wikiúdajoch nájdená.",
			intro: "Wikiúdaje: ",
			nolabel: "(bez názvu)",
			nodescription: "bez popisu",
			aliases: "Alternatívne názvy: ",
			noaliases: "žiadne"
		},
		'ur': {
			notfound: "یہ مضمون ویکی ڈیٹا میں نہیں ملا۔",
			intro: "ویکی ڈیٹا: ",
			nolabel: "(نام موجود نہیں)",
			nodescription: "تعریف موجود نہیں",
			aliases: "دیگر نام: ",
			noaliases: "دیگر نام موجود نہیں ہے"
		},
		'zh': {
			notfound: "找不到维基数据项。",
			intro: "维基数据: ",
			nolabel: "(无标签)",
			nodescription: "无描述",
			aliases: "别名: ",
			noaliases: "无"
		},
		'zh-cn': {
			notfound: "找不到维基数据项。",
			intro: "维基数据: ",
			nolabel: "(无标签)",
			nodescription: "无描述",
			aliases: "别名: ",
			noaliases: "无"
		},
		'zh-hans': {
			notfound: "找不到维基数据项。",
			intro: "维基数据: ",
			nolabel: "(无标签)",
			nodescription: "无描述",
			aliases: "别名: ",
			noaliases: "无"
		},
		'zh-hant': {
			notfound: "找不到維基數據項。",
			intro: "維基數據: ",
			nolabel: "(無標籤)",
			nodescription: "無描述",
			aliases: "別名: ",
			noaliases: "無"
		},
		'zh-hk': {
			notfound: "找不到維基數據項。",
			intro: "維基數據: ",
			nolabel: "(無標籤)",
			nodescription: "無描述",
			aliases: "別名: ",
			noaliases: "無"
		},
		'zh-mo': {
			notfound: "找不到維基數據項。",
			intro: "維基數據: ",
			nolabel: "(無標籤)",
			nodescription: "無描述",
			aliases: "別名: ",
			noaliases: "無"
		},
		'zh-sg': {
			notfound: "找不到维基数据项。",
			intro: "维基数据: ",
			nolabel: "(无标签)",
			nodescription: "无描述",
			aliases: "别名: ",
			noaliases: "无"
		},
		'zh-tw': {
			notfound: "找不到維基數據項。",
			intro: "維基數據: ",
			nolabel: "(無標籤)",
			nodescription: "無描述",
			aliases: "別名: ",
			noaliases: "無"
		}
	};
	var namespace = mw.config.get( 'wgNamespaceNumber' ),
		lang = mw.config.get( 'wgContentLanguage' ),
		page = ( namespace -= ( namespace > 0 && namespace % 2 ) ) === 0
			? mw.config.get( 'wgTitle' )
			: mw.config.get( 'wgFormattedNamespaces' )[ namespace ] + ':' + mw.config.get( 'wgTitle' );

	namespace !== 2 && namespace !== -1 && $.ajax( {
		url: '//wiki.openstreetmap.org/w/api.php',
		data: {
			'format': 'json',
			'action': 'wbgetentities',
			'sites': mw.config.get( 'wgDBname' ),
			'titles': page,
			'props': 'info|labels|descriptions|aliases',
			'languages': lang,
			'maxage': mw.config.get( "wgUserName" ) === null ? 900 : 30,
			'smaxage': mw.config.get( "wgUserName" ) === null ? 900 : 30,
			//'maxlag': 1
		},
		dataType: 'jsonp',
		cache: true
	} )
	.done( function ( data ) {
		if ( data.success ) {
			$( function () {
				var $d = $( '<div>' ).addClass( 'WDI-box' ),
					userLang = mw.config.get( 'wgUserLanguage' ),
					m = Object.prototype.hasOwnProperty.call( translate, userLang ) ? translate[ userLang ] : translate.en;
				mw.messages.set( 'WDI-aliases', m.aliases );
				$.each( data.entities, function ( entity, item ) {
					if ( entity == -1 ) {
						$d.append( $( '<a>' ).attr( 'href',
							'//wiki.openstreetmap.org/wiki/Special:NewItem?site=' +
							mw.config.get( 'wgDBname' ) +
							'&page=' + encodeURIComponent( page ) +
							'&label=' + encodeURIComponent( page ) +
							'&lang=' + lang
						).text( m.notfound ) );
					} else {
						var label = item.labels[ lang ] && item.labels[ lang ].value,
							description = item.descriptions[ lang ] && item.descriptions[ lang ].value,
							aliases = $.map( item.aliases[ lang ] || [], function ( alias ) {
								return alias.value;
							} );
						if ( window.WDIlimitaliases && WDIlimitaliases < aliases.length ){
							for( ; WDIlimitaliases < aliases.length; aliases.pop() ) ;
							aliases.push( '...' );
						}
						$d.append(
							$( '<a>' )
								.attr( 'href', '//wiki.openstreetmap.org/wiki/' + item.title )
								.text( m.intro + ( label || m.nolabel ) ),
							' (',
							$( '<a>' )
								.attr( 'href', '//wiki.openstreetmap.org/wiki/' + item.title )
								.text( item.title ),
							')',
							$( '<span>' ).text( ', ' ),
							$( '<i>' )
								.text( description || m.nodescription )
								.addClass( description ? 'WDI-desc' : 'WDI-desc WDI-nodesc' ),
							$( '<br>' ),
							$( '<span>' ).text( mw.msg( 'WDI-aliases', aliases.length ) + ( aliases.join( ', ' ) || m.noaliases ) )
						);
					}
				} );
				$d.appendTo( '#contentSub' );
			} );
		}
	} );
} );

/**
 * Gadget which creates a quick filter to find statements by their labels on item pages.
 *
 * @author [[User:Bene*]]
 */
( function( $, mw ) {
	'use strict';

	if ( !mw.config.exists( 'wbEntity' ) ) {
		return;
	}

	var html = '<div class="statements-filter">\
  					<input type="text" placeholder="Filter by label" id="statements-filter-input" style="width: 100%; font-size: 1.2em; margin: 10px 0px;">\
				</div>';

	function matches( label, filters ) {
		label = $.trim( label.toLowerCase() );
		for ( var i in filters ) {
			if ( label.indexOf( filters[i] ) >= 0  ) {
				return true;
			}
		}
		return false;
	}

	function filter() {
		var filters = $( '#statements-filter-input' ).val().toLowerCase().split( /\s*,\s*/ );
		$( '.wikibase-statementgroupview' ).each( function() {
			var label = $( this ).find( '.wikibase-statementgroupview-property-label a' ).text();
			if ( matches( label, filters ) ) {
				$( this ).show();
			} else {
				$( this ).hide();
			}
		} );
	}

	$( function() {
		$( html ).insertAfter( '#claims' );
		$( '#statements-filter-input' ).on( 'input', filter );
	} );

} )( jQuery, mediaWiki );


// /* <nowiki>
//  *
//  * EditSummary.js
//  *
//  * @author [[User:Ricordisamoa]], [[User:Yurik]]
// */
// ( function( $, mw, window ) {
// 	$( function () {
// 		if( !mw.config.exists( 'wbEntityId' ) ) {
// 			return;
// 		}
// 		mw.hook('wikibase.entityPage.entityView.rendered').add(function () {
// 			oldPost = wb.api.RepoApi.prototype._post;
	
// 			wb.api.RepoApi.prototype._post = function ( params ) {
// 				if ( params.summary === undefined
// 					 && params.action !== "wbsetaliases"
// 					 && params.action !== "wbsetlabel"
// 					 && params.action !== "wbsetdescription"
// 				) {	
// 					var summary = window.prompt('Enter edit summary (optional):', '');
// 					if ( summary ) {
// 						params.summary = summary;
// 					}
// 				}
// 				return oldPost.call( this, params );
// 			};
// 		} );
// 	} );
// } )( jQuery, mediaWiki, window );


}