1

I am trying to do what this guy says has done using angular on this SO question

His code goes by:

.controller('WikiQueryCtrl', ['$scope', '$http', function($scope, $http) {
    $http({
            //these geo coordinates and the date ranges will eventually be dynamic.
            url: 'https://wdq.wmflabs.org/api?q=AROUND[625,-25.911389,31.957222,5]%20AND%20BETWEEN[585,1985,1987]&callback=JSON_CALLBACK',
            method: 'jsonp'
        })
        .success(function(response) {
            var items = response.items;
            $scope.jason = items;
            var wikiDataString = 'http://www.wikidata.org/w/api.php?action=wbgetentities&format=json&ids=Q' + items + '&props=sitelinks%7Csitelinks%2Furls&callback=JSON_CALLBACK';
            $http({
                    url: wikiDataString,
                    method: 'jsonp'
                })
                .success(function(response2) {
                    $scope.jason2 = response2;
                    var url = response2.entities["Q" + items].sitelinks.enwiki.url;
                    var wikipediaTitle = url.substr(24, url.length);
                    var wikipediaURL = 'http://en.wikipedia.org/w/api.php?action=query&prop=extracts|info&exintro&titles=' + wikipediaTitle + '&format=json&explaintext&redirects&inprop=url&indexpageids&format=json&callback=JSON_CALLBACK';
                    $http({
                        url: wikipediaURL,
                        method: 'jsonp'

                    }).success(function(response4) {
                        var query = response4.query;
                        var pageID = response4.query.pageids;
                        var title = response4.query.pages[pageID].title;
                        var fullurl = response4.query.pages[pageID].fullurl;
                        var content = response4.query.pages[pageID].extract;
                        $scope.title = title;
                        $scope.content = content;
                        $scope.fullurl = fullurl;
                        $scope.jason = query;
                    });
                });
        });
}]);

I know wikipedia has a geosearch but not a date range, I tried to look at wikidata as the guy has done and to convert it into jQuery but with no positive results, here it is a code pen:

https://codepen.io/anon/pen/PjywjR

How would I achieve in jQuery what the guy has said to have achieved using Angular, to search wikipedia content by coordinates and date range.

rob.m
  • 9,843
  • 19
  • 73
  • 162
  • One suggestion: remove the referenced code (link to that) and paste in your code (at the codepen.io link). – klenwell Jul 09 '17 at 06:06

2 Answers2

2

Wikidata site provides a Query Service to write and run SPARQL queries against Wikipedia database. If the Wikidata items you're searching for have properties P585 (point in time) and P625 (coordinate location in WGS84), then it's possible to retrieve them using a SPARQL query. For example, the following query retrieves all items that occurred in Portugal between 2000-01-01 and 2023-01-01:

SELECT DISTINCT ?item ?itemLabel ?when ?where ?url
WHERE {
  ?item wdt:P585 ?when .
  FILTER(?when >= "2000-01-01T00:00:00Z"^^xsd:dateTime && ?when < "2023-01-01T00:00:00Z"^^xsd:dateTime).
  ?url schema:about ?item;
       schema:inLanguage "en" .
  FILTER (STRSTARTS(str(?url), "https://en.wikipedia.org/")).
  SERVICE wikibase:box {
    ?item wdt:P625 ?where .
    bd:serviceParam wikibase:cornerSouthWest "Point(-9.52 36.95)"^^geo:wktLiteral.
    bd:serviceParam wikibase:cornerNorthEast "Point(-6.18 42.16)"^^geo:wktLiteral.
  }
  SERVICE wikibase:label { bd:serviceParam wikibase:language "en" . }
}
ORDER BY ASC(?when)

The query above returns at least 15 items. It uses the box service to specify the area to search for, but it's also possible to use the around service for the same purpose. See SPARQL/SERVICE - around and box for an example of a query that uses the around service, although the box service is much faster IMHO.

metator
  • 397
  • 3
  • 5
0

I haven't worked with the Wikipedia API and the URL https://wdq.wmflabs.org gives me a 502 code both in the browser and in an AJAX request. So first question would be: are you sure that's a valid API endpoint?

Next, I would simplify your AJAX request while you test and debug:

var url = "https://wdq.wmflabs.org/api";
var data = {
  q: "AROUND[625,-25.911389,31.957222,5]%20AND%20BETWEEN[585,1985,1987]",
  callback: "JSON_CALLBACK"
};

$.ajax({
  dataType: "jsonp",
  url: url,
  data: data,
  success: function(data) {
    console.log(data);
  }
});

As noted above, this request receives a 502 Bad Gateway response at present.

klenwell
  • 6,978
  • 4
  • 45
  • 84