Reputation: 805
I need to get all the pages I have created like Templates in my wikimedia webpage. I have to do this with javascript.
Is this possible?
Upvotes: 1
Views: 63
Reputation: 50328
You can do this with a UserContribs API query, like this:
Basically, the parameters you need are:
format=json
to get results in JSON format, which is probably what you want for JavaScript. (I've used jsonfm
in the example link above to get pretty-printed human readable output.)
action=query
to indicate that this is, indeed, a query rather than, say, an edit or a login attempt.
list=usercontribs
to indicate that you want a list of a user's contributions (i.e. the stuff you see on the Special:Contributions page).
ucuser=your_username
to select which user's contributions you want to see. (The example link above shows mine.)
ucnamespace=10
to select only contributions to templates. (10 is the namespace number for the built-in Template namespace).
ucshow=new
to select only contributions that involve creating a new page. (Note that this also includes page moves; I don't see any simple way to filter those out.)
Of course, there are other parameters you may also want to include.
I've also included an empty continue=
parameter to indicate that I want to use the new query continuation syntax, and to suppress the warning about it. Obviously, if you actually want to use query continuation, you'll need to implement the client-side part yourself (or use an MW API client that implements it for you). Here's one simplistic way to do that:
function getNewTemplatesForUser( username ) {
var queryURL = 'https://en.wikipedia.org/w/api.php?format=json&action=query&list=usercontribs&ucnamespace=10&ucshow=new';
queryURL += '&ucuser=' + encodeURIComponent( username );
var callback = function( json ) {
// TODO: actually process the results here
if ( json.continue ) {
var continueURL = queryURL;
for ( var attr in json.continue ) {
continueURL += '&' + attr + '=' + encodeURIComponent( json.continue[attr] );
}
doAjaxRequest( continueURL, callback );
}
};
doAjaxRequest( queryURL + '&continue=', callback );
}
Upvotes: 1