מדיה ויקי:סקריפטים/98.js

הערה: לאחר הפרסום, ייתכן שיהיה צורך לנקות את זיכרון המטמון (cache) של הדפדפן כדי להבחין בשינויים.

  • פיירפוקס / ספארי: להחזיק את המקש Shift בעת לחיצה על טעינה מחדש (Reload) או ללחוץ על צירוף המקשים Ctrl-F5 או Ctrl-R (במחשב מק: ⌘-R).
  • גוגל כרום: ללחוץ על צירוף המקשים Ctrl-Shift-R (במחשב מק: ⌘-Shift-R).
  • אינטרנט אקספלורר / אדג': להחזיק את המקש Ctrl בעת לחיצה על רענן (Refresh) או ללחוץ על צירוף המקשים Ctrl-F5.
  • אופרה: ללחוץ על Ctrl-F5.
if (!mw.config.get('wgPageName').match(/יחסי\s/)) {
	var strings = {
		'saved': 'התיאור נשמר',
		'change': 'עריכת התיאור',
		'add': 'אין תיאור',
		'failed': '.העדכון נכשל. אנא עדכנו את התיאור באתר ויקינתונים',
	}
    mw.loader.using(['mediawiki.util','oojs-ui-core','oojs-ui-windows']).done(function() {
    	function makeText(description, colon) {
    		return (description && colon && ' :' || '') +
    			   (description && description.length > 40 ? description.substr(0, 30) + '...' : description || '');
    	}
        function modifyWD(itemId, newDescription){
            var wikidataApi = new mw.ForeignApi('https://www.wikidata.org/w/api.php')
            wikidataApi.postWithToken('csrf',{
                action: 'wbsetdescription', id:itemId, value:newDescription, language:'he', baseRevId:0,
            }).done(function(d){
                if (d.success) {
//            	mw.notify(strings['saved']);
                	$('#wd-description')
                		.text(newDescription)
                		.attr({title: newDescription})
                		.css('background-color', '');
                }
            }).fail(function() {
                mw.notify(strings['failed']);
			});
        }
        function askAndModify(q, d, item){
            OO.ui.prompt( q, {size:'large', textInput: { value: d }})
            .done( function ( result ) {
                if ( result == null ) {
                    console.log( 'WIKIDATA_SCRIPT: canceled' );
                } else {
                    modifyWD(item.id, result)
                }
            } );
        }
        var title = mw.config.get('wgPageName');
        $.ajax({
            url: '//www.wikidata.org/w/api.php',
            data: {
                'format': 'json',
                'action': 'wbgetentities',
                'sites': mw.config.get('wgDBname'),
                'titles': title,
                'props': 'descriptions',
                'languages': mw.config.get('wgPageContentLanguage')
            },
            dataType: 'jsonp',
            success: function(data) {
                if (data.success) {
                    var lang = mw.config.get('wgPageContentLanguage');
                    if (Object.keys(data.entities).length == 0){
                        console.log('WIKIDATA_SCRIPT: entity not exists')
                        return
                    }
                    if (Object.keys(data.entities).length != 1){
                        console.log('WIKIDATA_SCRIPT: ambiguous entity') // more than one entity with this name on wikidata
                        return
                    }
                    var item = data.entities[Object.keys(data.entities)[0]];

                    if (!item.descriptions) {
                        console.log('WIKIDATA_SCRIPT: no wikidata item associated')
                        return;
                    }
                    if(item.descriptions[lang]){
                        var description = item.descriptions[lang].value
                        console.log('WIKIDATA_SCRIPT: description already exists in wikidata: ', description)
                        return $('.mw-indicators')
                        	.prepend(
	                            $('<div class="mw-indicator" id="wd-description" style="border:1px solid;padding:0 1em"></div>')
	                            .text( makeText(description) )
	                            .attr('title', description)
	                            .click(function(){                                
	                                askAndModify(strings['change'], $(this).attr('title'), item)
	                            })
                        );
                    } else {
                        var firstSectionText = $('#mw-content-text p:first', mw.util.$content).text(); // $mw.util.$content includes sitenotice and such, which is confusing. it shouldn't
                        var match = / (?:הוא|היא|היה|הייתה) (.+?\ישראלית?)/.exec(firstSectionText);
	                    var newDescription = match && match[1] || '';
                        console.log('newDescription', newDescription)
                        $('.mw-indicators').prepend(
                            $('<div class="mw-indicator" id="wd-description" style="border:1px solid;padding:0 1em"></div>')
                            .text(strings['add'] + makeText(newDescription, true))
                            .attr('title', newDescription)
                            .css('background-color', 'coral')
                            .click(function(){          
                                askAndModify(strings['add'], $(this).attr('title'), item)
                            })
                        )
                    }
                }
            }
        });
    });
}