User

Difference between revisions of "Yug/common.js"

< User:Yug

m
 
(22 intermediate revisions by the same user not shown)
Line 1: Line 1:
 
// Modules loader, conditional
 
// Modules loader, conditional
 
// Sound library
 
// Sound library
if(/^User:Yug\/LinguaLibre:Explore_the_sound_library(\/[a-z_-]+)?$/.test( mw.config.get( 'wgPageName' ) ) ) {
+
if(/^User:Elfix\/LinguaLibre:Explore_the_sound_library(\/[a-z_-]+)?$/.test( mw.config.get( 'wgPageName' ) ) ) {
mw.loader.load( '/index.php?title=User:Yug/MediaWiki:SoundLibrary.js&action=raw&ctype=text/javascript');
+
mw.loader.load( '/index.ptitle=User:Elfix/MediaWiki:SoundLibrary.js&action=raw&ctype=text/javascript');  
 
}
 
}
  
 +
// ==UserScript==
 +
// @name        Wikidata Editor
 +
var entityId = "Q170146";  // Entity to update (Q170146)
 +
var propertyId = "P4";      // Property to update (P4)
 +
var newValue = "Q359";      // Value to set (Q359)
 +
var numericId = Number(newValue.replace("Q", ""));
 +
var csrfToken = mw.user.tokens.get("csrfToken");
 +
var params = {
 +
action: 'query',
 +
meta: 'tokens',
 +
type: 'csrf',
 +
format: 'json'
 +
},
 +
api = new mw.Api();
  
 +
api.get( params ).done( function ( data ) {
 +
csrfToken = data.query.tokens.csrftoken;
 +
console.log( csrfToken );
 +
} );
  
/* *************************************************************** */
+
console.log( "token2", csrfToken );
/* Words Generator : Wikidata + Language **************************************** */
+
// Description: Creates a word generator for target language, based on lexemes on Wikidata.
+
// Define the API endpoint
// Usage: [[Special:RecordWizard]], step 3
+
var apiUrl = "https://lingualibre.org/api.php";
// Install: copy script or import script to your personal User:*/common.js
 
// Documentations: 
 
// Author: VIGNERON
 
  
'use strict';
+
// Define the edit summary
 +
var editSummary = "Updating property P4";
  
if ( mw.config.get( 'wgCanonicalSpecialPageName' ) === 'RecordWizard' ) {
+
// Create an object with the data to update
    mw.loader.using( 'ext.recordWizard.generator', function() {
+
var postData = {
    alert("ping1");
+
  action: "wbsetclaimvalue",
        var rw = mw.recordWizard;
+
  format: "json",
 
+
    snaktype: "value",
    /*
+
  claim: JSON.stringify({
    * This first part allows the creation of a basic generator
+
    entity: entityId,
    * and allows the RecordWizard to find it (and thus, add a button in the UI for it)
+
    property: propertyId,
    *
+
    snaktype: "value",
    * Replace everywhere in this file "rw.generator.Demo" by a custom
+
    value: {
    * and unique class name, but always in the "rw.generator" namespace.
+
      "entity-type": "item",
    * For example it can be: rw.generator.SomethingCool
+
//     "value": numericId ,
    */
+
      "numeric-id": numericId
    rw.generator.Lexeme = function ( config ) {
+
    }
    rw.generator.Generator.call( this, config );
+
  }),
    };
+
  summary: editSummary,
 
+
  token:csrfToken
    OO.inheritClass( rw.generator.Lexeme, rw.generator.Generator );
+
};
 
 
    // This line defines an internal name for the generator
 
    rw.generator.Lexeme.static.name = 'demo';
 
 
 
    // And this one defines the name for the generator which will be displayed in the UI
 
    rw.generator.Lexeme.static.title = 'My Demo';
 
 
 
    /*
 
    * This function should contain all the popup initialization stuff.
 
    * We create here for example two text fields, with a custom layout.
 
    * For more information, see OOui documentation:
 
    * https://www.mediawiki.org/wiki/OOUI
 
    */
 
    rw.generator.Lexeme.prototype.initialize = function () {
 
    alert("ping2");
 
    // The two text fields
 
    this.aTextField = new OO.ui.TextInputWidget();
 
    this.anotherTextField = new OO.ui.TextInputWidget();
 
 
 
    // The custom layout
 
    this.layout = new OO.ui.Widget( {
 
    content: [
 
    new OO.ui.FieldLayout( this.aTextField, {
 
    align: 'top',
 
    label: 'This field do something cool.'
 
    }
 
    ),
 
    new OO.ui.FieldLayout(
 
    this.anotherTextField, {
 
    align: 'top',
 
    label: 'This one too!'
 
    }
 
    )
 
    ]
 
    } );
 
  
    // To be displayed, all the fields/widgets/... should be appended to "this.content.$element"
+
// Send a POST request to update the item
    this.content.$element.append( this.layout.$element );
+
$.post(apiUrl, postData, function(data) {
 
+
  if (data.success) {
    // Do not remove this line, it will initialize the popup itself
+
    console.log("Item updated successfully!");
    rw.generator.Generator.prototype.initialize.call( this );
+
  } else {
    };
+
    console.error("Error updating item:", data.error.info);
 
 
    /*
 
    * This function will be called when the user press the "Done" button.
 
    *
 
    * Every words that you want to be added to the RecordWizard's word list
 
    * have to be added to an array called "this.list".
 
    *
 
    * The returned value can either be True, or if you want to do some asynchrone
 
    * stuff, you can return a jQuery promise
 
    */
 
    rw.generator.Lexeme.prototype.fetch = function () {
 
    // Get the values of our text fields
 
    var generator = this,
 
    demoText = this.aTextField.getValue(),
 
    anotherDemoText = this.anotherTextField.getValue();
 
 
 
    // Initialize a new promise
 
    this.deferred = $.Deferred();
 
 
 
    // Initialize our word list
 
    this.list = [];
 
var LingualibreIdToWikidataId = function(LingualibreId) {
 
            return LingualibreId == 'Q209'?'Q12107':''
 
            }
 
            var queryByLanguage = function(wikidataId){
 
            wikidataId = wikidataId || 'Q21'; // breton as default
 
            return 'SELECT%20DISTINCT%20%3FlexemeLabel%20%3Flexeme%0AWITH%20{%0A%20%20SELECT%20%3Flexeme%20%3FlexemeLabel%20%3Flexical_category%20WHERE%20{%0A%20%20%20%20%3Flexeme%20a%20ontolex%3ALexicalEntry%20%3B%0A%20%20%20%20%20%20%20%20%20%20%20%20dct%3Alanguage%20wd%3A'+wikidataId+'%20%3B%20%0A%20%20%20%20%20%20%20%20%20%20%20%20wikibase%3Alemma%20%3FlexemeLabel%20.%0A%20%20%20%20OPTIONAL%20{%0A%20%20%20%20%20%20%3Flexeme%20wikibase%3AlexicalCategory%20%3Flexical_category%20.%0A%20%20%20%20}%0A%20%20}%0A}%20AS%20%25results%0AWHERE%20{%0A%20%20INCLUDE%20%25results%0A%20%20OPTIONAL%20{%20%20%20%20%20%20%20%20%0A%20%20%20%20%3Flexical_category%20rdfs%3Alabel%20%3Flexical_categoryLabel%20.%0A%20%20%20%20FILTER%20(LANG(%3Flexical_categoryLabel)%20%3D%20%22en%22)%0A%20%20}%0A}%0A'
 
            /* return `SELECT DISTINCT ?lexemeLabel ?lexeme
 
WITH {
 
  SELECT ?lexeme ?lexemeLabel ?lexical_category WHERE {
 
    ?lexeme a ontolex:LexicalEntry ;
 
            dct:language wd:${wikidataId} ;
 
            wikibase:lemma ?lexemeLabel .
 
    OPTIONAL {
 
      ?lexeme wikibase:lexicalCategory ?lexical_category .
 
    }
 
 
   }
 
   }
} AS %results
+
});
WHERE {
 
  INCLUDE %results
 
  OPTIONAL {       
 
    ?lexical_category rdfs:label ?lexical_categoryLabel .
 
    FILTER (LANG(?lexical_categoryLabel) = "en")
 
  }
 
}` */ }
 
            var wikidataId = LingualibreIdToWikidataId('Q209');
 
var query = queryByLanguage(wikidataId);
 
    // Call to the Query Service (query already built, part to improve later)
 
      this.LexemeQuery = new ( 'https://query.wikidata.org/bigdata/namespace/wdq/sparql?format=json&query='+query );
 
            // We process here the result of our request
 
            // By adding each page title to our list
 
            var i;
 
            for ( var i=0; i < data.Lexemquery.length; i++ ) {
 
                data.results.bindings[ i ].lexemeLabel.value;
 
            }
 
 
 
    // At this point we're not done yet, make the dialog closing process
 
    // to wait the promise to be resolved or rejected
 
    return this.deferred.promise();
 
    };
 
    } );
 
}
 

Latest revision as of 16:01, 2 October 2023

// Modules loader, conditional
// Sound library
if(/^User:Elfix\/LinguaLibre:Explore_the_sound_library(\/[a-z_-]+)?$/.test( mw.config.get( 'wgPageName' ) ) ) {
 mw.loader.load( '/index.ptitle=User:Elfix/MediaWiki:SoundLibrary.js&action=raw&ctype=text/javascript'); 
}

// ==UserScript==
// @name         Wikidata Editor
var entityId = "Q170146";   // Entity to update (Q170146)
var propertyId = "P4";      // Property to update (P4)
var newValue = "Q359";      // Value to set (Q359)
var numericId = Number(newValue.replace("Q", ""));
var csrfToken = mw.user.tokens.get("csrfToken");
var params = {
		action: 'query',
		meta: 'tokens',
		type: 'csrf',
		format: 'json'
	},
	api = new mw.Api();

api.get( params ).done( function ( data ) {
	csrfToken = data.query.tokens.csrftoken;
	console.log( csrfToken );
} );

	console.log( "token2", csrfToken );
	
// Define the API endpoint
var apiUrl = "https://lingualibre.org/api.php";

// Define the edit summary
var editSummary = "Updating property P4";

// Create an object with the data to update
var postData = {
  action: "wbsetclaimvalue",
  format: "json",
    snaktype: "value",
  claim: JSON.stringify({
    entity: entityId,
    property: propertyId,
    snaktype: "value",
    value: {
      "entity-type": "item",
 //     "value": numericId ,
      "numeric-id": numericId 
    }
  }),
  summary: editSummary,
  token:csrfToken
};

// Send a POST request to update the item
$.post(apiUrl, postData, function(data) {
  if (data.success) {
    console.log("Item updated successfully!");
  } else {
    console.error("Error updating item:", data.error.info);
  }
});