User:Renepick/moocEditor.js

function doPageContentRequest(pageTitle, section, sucCallback) { $.ajax({		url: "https://en.wikiversity.org/w/index.php?action=raw&title=" + pageTitle + "&section=" + section,		cache: false	}).fail(function {		console.log('moocEditor.doPageContentRequest: page content request failed for page "' + pageTitle + ' section ' + section + '"');	}).done(sucCallback); } function doEditTokenRequest(pageTitles, sucCallback) { var sPageTitles = pageTitles.join('|'); // get edit tokens var tokenData = { 'intoken': 'edit|watch' };	$.ajax({		type: "POST",		url: "https://en.wikiversity.org/w/api.php?action=query&prop=info&format=json&titles=" + sPageTitles,		data: tokenData	}).fail(function(jqXHR) {		console.log('moocEditor.doEditTokenRequest: edit token request failed for pages "' + sPageTitles + '" (server status ' + jqXHR.status + ')');	}).done(function(response) {		var editTokens = parseEditTokens(response);		if (editTokens !== null) {			sucCallback(editTokens);		} else {			console.log('moocEditor.doEditTokenRequest: failed to get edit tokens for "' + sPageTitles + '" (server response: ' + JSON.stringify(response) + ')');		}	}); } function doEditRequest(pageTitle, section, content, summary, sucCallback) { console.log('edit request: ' + pageTitle + ' section ' + section); doEditTokenRequest([ pageTitle ], function(editTokens) {		var editToken = editTokens[pageTitle];		var editData = {			'title': pageTitle,			'section': section,			'text': content,			'summary': summary,			'watchlist': 'watch',			'token': editToken		};		$.ajax({ type: "POST", url: "https://en.wikiversity.org/w/api.php?action=edit&format=json", data: editData }).fail(function(jqXHR) { console.log('moocEditor.doEditRequest: edit request failed for page "' + pageTitle + '" (server status: ' + jqXHR.status + ')'); }).done(function(response) { console.log('moocEditor.doEditRequest: server response: ' + JSON.stringify(response)); //TODO handle errors sucCallback; });	}); }

/** * Parses a server response containing one or multiple edit tokens. * @param {JSON} tokenResponse * @return {Array} array (mapping: page title -> edit token) or null if 'tokenResponse' did not contain edit tokens */ function parseEditTokens(tokenResponse) { var editTokens = []; var path = ['query', 'pages']; var crr = tokenResponse; for (var i = 0; i < path.length; ++i) { if (crr && crr.hasOwnProperty(path[i])) { crr = crr[path[i]]; } else { console.log('moocEditor.parseEditTokens: missing object "' + path[i] + '"'); crr = null; break; }	}	if (crr) { var pages = crr; for (var pageId in pages) { // page exists if (pages.hasOwnProperty(pageId)) { var page = pages[pageId]; editTokens[page.title] = page.edittoken; console.log('edittoken for "' + page.title + '": ' + page.edittoken); }		}		return editTokens; }	return null; }

function updateIndex(item, summaryAppendix, sucCallback) { var header = item.getHeader; var summary = header.type + ' ' + header.path + ': ' + summaryAppendix; doEditRequest(item.getIndexTitle, item.getIndexSection, item.tostring, summary, sucCallback); } function addChild(type, name, parent, sucCallback) { // add item to parent var parentHeader = parent.getHeader; var header = Header(parentHeader.level + 1, type, name); parent.childLines.push(header.tostring); // update MOOC index at parent position var indexSummary = type + ' ' + name + ' added'; updateIndex(parent, indexSummary, function {		// create item page		doEditRequest(parent.getMoocBase + '/' + parentHeader.path + '/' + name, 0, parent.getInvoke, pageSummary, sucCallback);	}); }