|
|
Zeile 24: |
Zeile 24: |
|
| |
|
| <html lang="en"> | | <html lang="en"> |
| <head>
| |
| <meta charset="UTF-8">
| |
| <meta name="viewport" content="width=device-width, initial-scale=1.0">
| |
| <title>Add Item to Wikibase</title>
| |
| <script>
| |
| async function getCsrfToken() {
| |
| const apiUrl = 'http://swb.local/api.php'; // Replace with your Wikibase API URL
| |
|
| |
|
| // Log in if required (replace with actual credentials)
| |
| const loginData = new URLSearchParams();
| |
| loginData.append('action', 'login');
| |
| loginData.append('lgname', 'wkadmin'); // Replace with your username
| |
| loginData.append('lgpassword', '7kKq4gxBtV'); // Replace with your password
| |
| loginData.append('format', 'json');
| |
|
| |
| await fetch(apiUrl, {
| |
| method: 'POST',
| |
| body: loginData,
| |
| headers: {
| |
| 'Content-Type': 'application/x-www-form-urlencoded'
| |
| },
| |
| credentials: 'include' // Include cookies in the request
| |
| });
| |
|
| |
| // Get the CSRF token
| |
| const tokenData = new URLSearchParams();
| |
| tokenData.append('action', 'query');
| |
| tokenData.append('meta', 'tokens');
| |
| tokenData.append('format', 'json');
| |
|
| |
| const response = await fetch(apiUrl + '?' + tokenData.toString(), {
| |
| method: 'GET',
| |
| credentials: 'include' // Include cookies in the request
| |
| });
| |
|
| |
| const data = await response.json();
| |
| return data.query.tokens.csrftoken;
| |
| }
| |
|
| |
| async function addItem(event) {
| |
| event.preventDefault();
| |
|
| |
| const name = document.getElementById('name').value;
| |
| const description = document.getElementById('description').value;
| |
| const subCategoryOf = document.getElementById('subCategoryOf').value;
| |
| const token = await getCsrfToken();
| |
|
| |
| const apiUrl = 'http://swb.local/api.php?action=wbeditentity&new=item&format=json'; // Replace with your Wikibase API URL
| |
|
| |
| const formData = new URLSearchParams();
| |
| formData.append('action', 'wbeditentity');
| |
| //formData.append('new', 'item');
| |
| formData.append('token', token);
| |
| formData.append('format', 'json');
| |
|
| |
| formData.append('data', JSON.stringify(
| |
| {
| |
| "labels":{"en":{"language":"en","value":"term_"+name}},
| |
| "descriptions":{"en":{"language":"en","value":description}},"datatype":"string",
| |
| "claims":[
| |
| {"mainsnak":{"snaktype":"value","property":"P3","datavalue":{"value":name,"type":"string"}},"type":"statement","rank":"normal"},
| |
| {"mainsnak":{"snaktype":"value","property":"P1","datavalue":{"value":"Term","type":"string"}},"type":"statement","rank":"normal"},
| |
| {"mainsnak":{"snaktype":"value","property":"P10","datavalue": {"value": {"entity-type": "item","id": subCategoryOf},"type": "wikibase-entityid"}},"type": "statement","rank": "normal"}
| |
| ]
| |
| }
| |
|
| |
| ));
| |
|
| |
|
| |
| try {
| |
| const response = await fetch(apiUrl, {
| |
| method: 'POST',
| |
| body: formData,
| |
| headers: {
| |
| 'Content-Type': 'application/x-www-form-urlencoded'
| |
| },
| |
| credentials: 'include' // Include cookies in the request
| |
| });
| |
|
| |
| const data = await response.json();
| |
| if (data.success) {
| |
| alert('Item added successfully!');
| |
| } else {
| |
| alert('Error adding item: ' + JSON.stringify(data));
| |
| }
| |
| } catch (error) {
| |
| alert('Error: ' + error.message);
| |
| }
| |
| }
| |
| </script>
| |
| </head>
| |
| <body>
| |
| <h1>Add Item to Wikibase</h1> | | <h1>Add Item to Wikibase</h1> |
| <form id="addItemForm" onsubmit="addItem(event)"> | | <form id="addItemForm" onsubmit="addItem(event)"> |
Zeile 132: |
Zeile 41: |
| <input type="submit" value="Add Item"> | | <input type="submit" value="Add Item"> |
| </form> | | </form> |
| </body>
| | |
| </html> | | </html> |