function pageExists(pagename, callback) {
$.ajax(mw.util.getUrl(pagename), {
method: 'HEAD',
success: function() { callback(true); },
error: function(xhr) { callback(xhr.status === 404 ? false : null); }
});
}
function userExists(username, callback) {
pageExists('Special:Contributions/' + username, callback);
}
function findUser(username, callback) {
/* returns an object with the following properties:
name -- username as a string, or null if not found
user -- title of the user page as a string, or null if not found
talk -- title of the user talk page as a string, or null if not found
*/
/* example:
findUser('Surjection', function(result) {
console.log(result);
if (result.name) {
console.log('User exists with the name ' + result.name);
if (result.user)
console.log('User page exists: [[' + result.user + ']]');
else
console.log('User does not have a user page');
if (result.talk)
console.log('Talk page exists: [[' + result.talk + ']]');
else
console.log('User does not have a talk page');
} else {
console.log('User not found');
}
});
*/
function findUserFound(username) {
var userpage = 'User:' + username;
var talkpage = 'User talk:' + username;
pageExists(userpage, function(userexists) {
pageExists(talkpage, function(talkexists) {
var result = { name: username };
result.user = userexists ? userpage : null;
result.talk = talkexists ? talkpage : null;
callback(result);
});
});
}
userExists(username, function(found) {
if (found) findUserFound(username);
else {
var commons = username + '~commonswiki';
userExists(commons, function(found) {
if (found) findUserFound(commons);
else callback({ name: null, user: null, talk: null });
});
}
});
}