I’m trying to edit a section on a page on Wikipedia, but when I do so the whole section gets replaced by whatever text I send through the URL, even the heading, so it stops being a section. Is there any way to edit a section without deleting the heading?
So far, the function I’m using (having admittedly stolen it from a documentation page) is as follows:
function editSection(section, summary, content, editToken) {
$.ajax({
url: mw.util.wikiScript('api'),
data: {
format: 'json',
action: 'edit',
title: mw.config.get('wgPageName'),
section: section,
summary: summary,
text: content,
token: editToken
},
dataType: 'json',
type: 'POST',
success: function(data) {
if (data && data.edit && data.edit.result == 'Success') {
window.location.reload();
} else if (data && data.error) {
alert('Error: API returned error code "' + data.error.code + '": ' + data.error.info);
} else {
alert('Error: Unknown result from API.');
}
},
error: function(xhr) {
alert('Error: Request failed.');
}
});
}
to get my edit token, I use
function getEditToken() {
$.getJSON(
'http://en.wikipedia.org/w/api.php?', {
action: 'tokens',
type: 'edit',
format: 'json'
},
function(data) {
if (data.tokens) {
wgEditToken = data.tokens.edittoken;
}
}
)
}
help please!
Yes, the heading is part of the section. So if you don’t want to replace (or even delete) it, you will need to send it as a part of the text. However, if you don’t want to completely overwrite a section, you have already queried the section’s text [content], don’t you?
Btw: you might use the
mw.user.tokensmap to get the token, instead of manually retrieving it.