MediaWiki Query and/or WikidataQuery to find Wikipedia article

一笑奈何 提交于 2019-12-22 11:34:07

问题


This isn't so much a question abut AngularJS as it is about the Wikimedia and Wikidata query API's.

Although I am trying to display the content of a Wikipedia article in AngularJS after doing a certain query that isn't the problem. I already know how to display it... the problem is the search for an article or articles.

I'm trying to query Wikipedia by historical event date as well as by geo-location.

Let's pick a random event, any event. Let's say "1986 Mozambican Tupolev Tu-134 crash". Link: http://en.wikipedia.org/wiki/1986_Mozambican_Tupolev_Tu-134_crash

From the article, I can see the exact date: 19 October 1986
as well as the geo-location of the event: -25.911389, 31.957222

I'm trying to build a search in AngularJS that can use either a date-range and/or geolocation coordinates to find an event.

I am aware that mediawiki has a geolocation API now, and I am able to find the above event by either keyword or coordinates. The result also turns up any other articles that exist within a certain radius around it using this:

http://en.wikipedia.org/w/api.php?action=query&list=geosearch&gsradius=10000&gscoord=-25.911389|31.957222&prop=extracts|info&exintro&titles=1986_Mozambican_Tupolev_Tu-134_crash&format=json&explaintext&redirects&inprop=url&indexpageids&format=json&callback=JSON_CALLBACK

However, there is no way to do a search with mediawiki by a point in time or the date of the event.

Wikidata on the other hand has two methods of searching data... it has a date range as well as geolocation.

However, when a query is run, I have no idea what is being returned.

For example, when I use this query string:

https://wdq.wmflabs.org/api?q=AROUND[625,-25.911389,31.957222,5]%20AND%20BETWEEN[585,1985,1987]

it returns this:

{"status":{"error":"OK","items":1,"querytime":"544ms","parsed_query":"(AROUND[625,-25.9114,31.9572,5] AND BETWEEN[585,+00000001985-00-00T00:00:00Z,+00000001987-00-00T00:00:00Z])"},"items":[950273]}

Using wikidata's query tool:

http://tools.wmflabs.org/autolist/autolist1.html?q=AROUND[625,-25.911389,31.957222,5]%20AND%20BETWEEN[585,1985,1987]

I can see that 950273 represents the article in some way. I'm just not sure how to use that to direct me to the actual article in wikipedia.

I don't know what "items": [950273]" represents, or how to use it to get me to the wikipedia article and display the contents of that article in AngularJS.

Is there a way to do both a query by date of the historical event as well as by geolocation. Either by using mediawiki or wikidata or a combination of the two?


EDIT: This is the solution to my question above. It seems like a bit of a hack... but it works. Good enough for now. Here is my controller.

.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;
                    });
                });
        });
}]);

回答1:


950273 is the Wikidata entity ID. You find the entity itself at https://www.wikidata.org/wiki/Q950273

By using that you can query Wikidata for the articles connected to it using the API and the action wbgetentities and ask for sitelinks and sitelinks/url like this: http://wikidata.org/w/api.php?action=wbgetentities&format=json&ids=Q950273&props=sitelinks%7Csitelinks%2Furls

Or try going to the language version of your choice directly using Special:GoToLinkedPage. Eg. https://www.wikidata.org/wiki/Special:GoToLinkedPage/enwiki/Q950273



来源:https://stackoverflow.com/questions/29811410/mediawiki-query-and-or-wikidataquery-to-find-wikipedia-article

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!