This task is for creating a user script which will be a proof-of-concept on how the year filter would work. The basic idea is to create a similar year selection widget that is used in Fortepan. This task contains the client side (= code executed in browser part), and as it uses QLever as the backend, it would work independently without creating the server side parts first.
Example UI
Required features
- ability to select specific year using slider
- ability to show images between start and end years as alternative for specific year
- show filtered images as imagegrid
- In the UI, there should be the ability to define a PetScan ID as a photo source. Use 33506148 as the default PetScan ID.
Source photos
- Use photos from Petscan query https://petscan.wmcloud.org/?psid=33506148 (same as json) as source photos
Implementation
- Create separate user script for the task
- Keep code as simple. Idea would be test the idea, but if it works then use it as widget (in Cat-a-lot for example)
- Use https://commons.wikimedia.org/wiki/Special:BlankPage/Cat-a-lot_yearfilter as a page where code will open its UI
- You can use following code to test the page name
if (mw.config.get("wgPageName") !== "Special:BlankPage/Cat-a-lot_yearfilter") { return; }- thumbnail size can be defined using width-parameter:
- do Petscan query only once and use same data until user manually requests update (for different Petscan id for example)
- from petscan Query you will get page_ids which you can convert to mediaitems for SPARQL query.
- page id: 100129757 -> sdc:M100129757 (example sparql query with mediaitems)
Example SPARQL queries for year filtering
SPARQL Query using specific year
PREFIX wd: <http://www.wikidata.org/entity/>
PREFIX wdt: <http://www.wikidata.org/prop/direct/>
SELECT * WHERE {
?media wdt:P9478 ?finna_id .
?media wdt:P571 ?inception .
?media schema:url ?image.
FILTER(YEAR(?inception) = 1965)
}
LIMIT 100SPARQL Query using between start and end year
https://qlever.cs.uni-freiburg.de/wikimedia-commons/Xt4YYw
PREFIX wd: <http://www.wikidata.org/entity/>
PREFIX wdt: <http://www.wikidata.org/prop/direct/>
SELECT * WHERE {
?media wdt:P9478 ?finna_id .
?media wdt:P571 ?inception .
?media schema:url ?image.
FILTER(YEAR(?inception) >= 1923 && YEAR(?inception) <= 1968)
}
LIMIT 100SPARQL query where target photos are defined using mediates
mediaitem = "sdc:M" + page_id
- use POST instead of GET
- basically number of items in VALUES keyword is unlimited. It is OK to list 50000 mediaitems.
PREFIX wd: <http://www.wikidata.org/entity/>
PREFIX wdt: <http://www.wikidata.org/prop/direct/>
SELECT * WHERE {
VALUES ?media { sdc:M100129757 sdc:M100212050 sdc:M100254412 }
?media wdt:P571 ?inception .
?media schema:url ?image.
FILTER(YEAR(?inception) >= 1923 && YEAR(?inception) <= 1968)
}
LIMIT 100SPARQL query using Javascript example
/* <nowiki>
* Minimal QLever SPARQL Query Script for Wikimedia Commons using POST
*/
(function() {
// QLever SPARQL endpoint for Wikimedia Commons
var endpoint = 'https://qlever.cs.uni-freiburg.de/api/wikimedia-commons';
// SPARQL query as a template literal
var sparqlQuery = `
PREFIX wd: <http://www.wikidata.org/entity/>
PREFIX wdt: <http://www.wikidata.org/prop/direct/>
PREFIX schema: <http://schema.org/>
SELECT ?media ?inception ?image WHERE {
?media wdt:P9478 ?finna_id .
?media wdt:P571 ?inception .
?media schema:url ?image .
FILTER(YEAR(?inception) >= 1923 && YEAR(?inception) <= 1968)
}
LIMIT 20
`;
// Prepare POST parameters by URL-encoding the query and desired output format
var postParams = 'query=' + encodeURIComponent(sparqlQuery) + '&format=json';
// Execute the POST request using the fetch API
fetch(endpoint, {
method: 'POST',
headers: {
'Content-Type': 'application/x-www-form-urlencoded'
},
body: postParams
})
.then(function(response) {
if (!response.ok) {
throw new Error('Network response was not ok');
}
return response.json();
})
.then(function(data) {
console.log("SPARQL Query Results:", data);
// Add any additional processing or display logic for the results here.
})
.catch(function(error) {
console.error("Error running SPARQL query:", error);
});
})();