Line 1: |
Line 1: |
| // taken from https://www.wikidata.org/wiki/User:MichaelSchoenitzer/quickpresets.js | | // taken from https://www.wikidata.org/wiki/User:MichaelSchoenitzer/quickpresets.js |
− | mw.notify("Launching User:Podehaye/quickpresets.js") | + | // non exhaustive list of modifications: |
− | importStylesheet( 'MediaWiki:quickpresets.css' );
| + | // P31 and P(subclass) ---> P3 and P4 (see #P31, pid == 3) |
| + | // wd, wdt --> pdio, pdiot |
| + | // importScript and importStyleSheet replaced |
| + | // turned into a common script |
| + | // see doc at [[Gadget-QuickPresets/doc]] |
| + | |
| + | // mw.notify("QuickPresets") |
| + | function debug(alert){ |
| + | // mw.notify(alert) |
| + | } |
| + | mw.loader.load( '/w/index.php?title=MediaWiki:Gadget-QuickPresets/defaultconf.css&action=raw&ctype=text/css', 'text/css' ); |
| + | |
| if( typeof(quick_props)=="undefined" ) { | | if( typeof(quick_props)=="undefined" ) { |
− | importScript( 'MediaWiki:quickpresets_defaultconf.js' ); | + | mw.loader.load( '/w/index.php?title=MediaWiki:Gadget-QuickPresets/defaultconf.js&action=raw&ctype=text/javascript' ); |
| } | | } |
− | mw.notify("Launching User:Podehaye/quickpresets.js, part 2")
| |
| | | |
| /** | | /** |
Line 14: |
Line 24: |
| try { | | try { |
| $( ".quickpresetsiteminput input" ).entityselector( { | | $( ".quickpresetsiteminput input" ).entityselector( { |
− | url: 'http://wiki.personaldata.io/w/api.php', | + | url: 'https://wiki.personaldata.io/w/api.php', |
| language: mw.config.get('wgUserLanguage') | | language: mw.config.get('wgUserLanguage') |
| } ); | | } ); |
Line 53: |
Line 63: |
| } | | } |
| else if(dflt.type == "string") { | | else if(dflt.type == "string") { |
− | quickpresets_addstringstatement(qid, dflt.pid, dflt.name, $(this).prev('input')[0].value);
| + | quickpresets_addstringstatement(qid, dflt.pid, dflt.name, $(this).prev('input')[0].value); |
| } | | } |
| }); | | }); |
Line 87: |
Line 97: |
| api.post( { | | api.post( { |
| action: 'wbsetclaim', | | action: 'wbsetclaim', |
− | summary: "Added with [[User:Podehaye/quickpresets.js|Quickpresets]]", | + | summary: "Added with [[MediaWiki:Gadget-QuickPresets.js|Quickpresets]]", |
| claim: JSON.stringify(claim), | | claim: JSON.stringify(claim), |
| token: token | | token: token |
Line 93: |
Line 103: |
| function(aw){ | | function(aw){ |
| if(aw.success == 1) | | if(aw.success == 1) |
− | $('.wikibase-statementgrouplistview').first().after( succtext ); | + | //$('.wikibase-statementgrouplistview').first().after( succtext ); |
| + | mw.notify(succtext); |
| }); | | }); |
| } | | } |
Line 106: |
Line 117: |
| */ | | */ |
| function quickpresets_additemstatement(qid, pid, pname, toqid, toname) { | | function quickpresets_additemstatement(qid, pid, pname, toqid, toname) { |
− | var claim = {
| + | var claim = { |
| id: (new wb.utilities.ClaimGuidGenerator("q" + qid)).newGuid(), | | id: (new wb.utilities.ClaimGuidGenerator("q" + qid)).newGuid(), |
| type: "claim", | | type: "claim", |
Line 122: |
Line 133: |
| } | | } |
| }; | | }; |
− | quickpresets_addclaim(claim, "<p>Added "+pname+" "+toname+"</p>"); | + | quickpresets_addclaim(claim, "Added "+pname+" "+toname+""); |
| // Check if we now support it with another element of the config | | // Check if we now support it with another element of the config |
− | if(pid == 3) { // MODIFIED LINE | + | if(pid == 3) { |
| $('#quickpresets').remove(); | | $('#quickpresets').remove(); |
| for( var t = 0; t<quick_props.length; t++) { | | for( var t = 0; t<quick_props.length; t++) { |
Line 142: |
Line 153: |
| */ | | */ |
| function quickpresets_addstringstatement(qid, pid, pname, str) { | | function quickpresets_addstringstatement(qid, pid, pname, str) { |
− | var claim = {
| + | var claim = { |
| id: (new wb.utilities.ClaimGuidGenerator("q" + qid)).newGuid(), | | id: (new wb.utilities.ClaimGuidGenerator("q" + qid)).newGuid(), |
| type: "claim", | | type: "claim", |
Line 155: |
Line 166: |
| } | | } |
| }; | | }; |
− | quickpresets_addclaim(claim, "<p>Added "+pname+" \""+str+"\"</p>"); | + | quickpresets_addclaim(claim, "Added "+pname+" "+str+""); |
| } | | } |
| | | |
Line 162: |
Line 173: |
| */ | | */ |
| function quickpresets_checktype(qid, prop) { | | function quickpresets_checktype(qid, prop) { |
− | var typeqid=prop.qid;
| + | var typeqid = prop.qid; |
− | // Search in HTML-DOM
| + | // Search in HTML-DOM |
− | var typefromdom=$.map($('#P3').find(".wikibase-snakview-value a"), function(value, index) {
| + | // for each "instance of" value, run this function |
− | if(typeof value.attributes.href === "undefined")
| + | var typefromdom = $.map($("#P3").find(".wikibase-snakview-value a"), function( |
− | {
| + | value, |
− | mw.notify("exit 1");
| + | index |
− | return [];
| + | ) { |
− | }
| + | if (typeof value.attributes.href === "undefined") { |
− | else
| + | return []; |
− | {
| + | } else { |
− | mw.notify("exit 2");
| + | return [value.attributes.href.value.substring(6)]; |
− | return [value.attributes.href.value.substring(6)];
| + | } |
− | }
| + | }); |
− | });
| + | // typefromdom is now a list of Item:Qxxx, possibly empty |
− | if ( typefromdom.length === 0) {
| + | if (typefromdom.length === 0) { |
− | if(typeqid === null)
| + | if (typeqid === null) { |
− | {
| + | // the null prescription for when there is no "instance of" |
− | mw.notify("exit 3")
| + | quickpresets_addinterface(qid, prop); |
− | quickpresets_addinterface(qid, prop);
| |
− | }
| |
| } | | } |
− | else {
| + | } else { |
− | mw.notify("exit 4") | + | if (typeqid === 0) { |
− | if ( typeqid === 0 ) {
| + | // the 0 prescription, applicable to all items which are instance of something |
− | mw.notify("exit 5")
| + | quickpresets_addinterface(qid, prop); |
− | quickpresets_addinterface(qid, prop);
| + | } else if (typefromdom.includes("Item:Q" + typeqid)) { |
− | }
| + | // direct relevance |
− | else if ( typefromdom.includes("Q"+typeqid) ) {
| + | quickpresets_addinterface(qid, prop); |
− | mw.notify("exit 6")
| + | } else { |
− | quickpresets_addinterface(qid, prop);
| + | // indirect relevance, through subclassing |
− | }
| + | // Now check via sparql (much slower but includes subtypes) |
− | else {
| + | const endpointUrl = "https://query.personaldata.io/proxy/wdqs/bigdata/namespace/wdq/sparql"; |
− | mw.notify("exit 7")
| + | const sparqlQuery = "Ask { pdio:Q" + qid + " pdiot:P3/pdiot:P4* pdio:Q" + typeqid + '. hint:Prior hint:gearing "forwards" }'; |
− | // Now check via sparql (much slower but includes subtypes)
| + | const fullUrl = endpointUrl + "?query=" + encodeURIComponent(sparqlQuery); |
− | const endpointUrl = 'https://query.personaldata.io/proxy/wdqs/bigdata/namespace/wdq/sparql',
| + | const myHeaders = { Accept: "application/sparql-results+json" }; |
− | sparqlQuery = 'Ask { pdio:Q' + qid + ' pdiot:P3/pdiot:P4* pdio:Q' + typeqid +
| + | debug("qid: " + qid + "\ntypeqid: " + typeqid + "\n" + fullUrl + "\n" + sparqlQuery ); |
− | '. hint:Prior hint:gearing "forwards" }',
| + | fetch(fullUrl, { headers: myHeaders }) |
− | fullUrl = endpointUrl + '?query=' + encodeURIComponent( sparqlQuery ),
| + | .then(function(body) {body.json();}) |
− | headers = { 'Accept': 'application/sparql-results+json' };
| + | .then(function(json) {if (json && json.boolean) {quickpresets_addinterface(qid, prop);}}); |
− | fetch( fullUrl, { headers } ).then( body => body.json() ).then( json => {
| |
− | if ( json.boolean )
| |
− | quickpresets_addinterface(qid, prop);
| |
− | } );
| |
− | }
| |
| } | | } |
| + | } |
| } | | } |
| | | |
Line 229: |
Line 234: |
| var qid = mw.config.get( 'wbEntityId' ); | | var qid = mw.config.get( 'wbEntityId' ); |
| if ( !qid ) { | | if ( !qid ) { |
− | mw.notify("Launching User:Podehaye/quickpresets.js part 3")
| |
| return; | | return; |
| } | | } |
| else { | | else { |
| qid = qid.substring(1); | | qid = qid.substring(1); |
− | mw.notify("Launching User:Podehaye/quickpresets.js part 4")
| |
− |
| |
| } | | } |
| | | |
Line 243: |
Line 245: |
| quickpresets_waitForConfig(function() { | | quickpresets_waitForConfig(function() { |
| for( var t = 0; t<quick_props.length; t++) { | | for( var t = 0; t<quick_props.length; t++) { |
− | mw.notify("Launching User:Podehaye/quickpresets.js part 5")
| |
| quickpresets_checktype(qid, quick_props[t]); | | quickpresets_checktype(qid, quick_props[t]); |
| } | | } |