User:Sebschlicht/moocEditor.js

//

function doPageContentRequest(pageTitle, section, sucCallback, errorCallback) { var url = "https://en.wikiversity.org/w/index.php?action=raw&title=" + pageTitle; if (section !== null) { url += "&section=" + section; }	$.ajax({		url: url,		cache: false	}).fail(function(jqXHR) {		console.log('moocEditor.doPageContentRequest: page content request failed for page "' + pageTitle + ' section ' + section + '" (server status ' + jqXHR.status + ')');		if (typeof errorCallback !== 'undefined') {			errorCallback(jqXHR);		}	}).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,			'text': content,			'summary': summary,			'watchlist': 'watch',			'token': editToken		};		if (section !== null) {			editData.section = section;		}		$.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; });	}); } function addSectionToPage(pageTitle, sectionTitle, content, summary, sucCallback) { console.log('add section request: ' + pageTitle + ' section title ' + sectionTitle); doEditTokenRequest([ pageTitle ], function(editTokens) {		var editToken = editTokens[pageTitle];		var editData = {			'title': pageTitle,			'section': 'new',			'sectiontitle': sectionTitle,			'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.addSectionToPage: add section request failed for page "' + pageTitle + '" (server status: ' + jqXHR.status + ')'); }).done(function(response) { console.log('moocEditor.addSectionToPage: 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 getIndex(title, section, sucCallback) { doPageContentRequest(title, section, sucCallback); } function getScript(item, sucCallback, errorCallback) { doPageContentRequest(item.fullPath + '/script', 0, sucCallback, errorCallback); } function getQuiz(item, sucCallback, errorCallback) { doPageContentRequest(item.fullPath + '/quiz', 0, sucCallback, errorCallback); } function updateScript(item, scriptText, summary, sucCallback) { var editSummary = summary; if (editSummary === '') { editSummary = 'script update for MOOC ' + item.header.type + ' ' + item.fullPath; }	doEditRequest(item.fullPath + '/script', 0, scriptText, editSummary, sucCallback); } function updateQuiz(item, quizText, summary, sucCallback) { var editSummary = summary; if (editSummary === '') { editSummary = 'quiz update for MOOC ' + item.header.type + ' ' + item.fullPath; }	doEditRequest(item.fullPath + '/quiz', 0, quizText, editSummary, sucCallback); } function updateIndex(item, summaryAppendix, sucCallback) { var summary = item.header.type + ' ' + item.header.path + ': ' + summaryAppendix; if (item.header.path === null) {// changing root item summary = item.header.type + ':' + summaryAppendix; }	doEditRequest(item.index.title, item.indexSection, item.tostring, summary, sucCallback); } function createPage(pageTitle, content, summary, sucCallback) { doEditRequest(pageTitle, 0, content, summary, sucCallback); } function addChild(type, name, parent, summary, sucCallback) { // add item to parent var parentHeader = parent.header; var header = Header(parentHeader.level + 1, type, name, null); parent.childLines.push(header.tostring); console.log('new child for ' + parentHeader.type + ' ' + parentHeader.title + ': ' + header.tostring); // update MOOC index at parent position var itemIdentifier = type + ' ' + parentHeader.path + '/' + name; if (parentHeader.path === null) {// parent is root itemIdentifier = type + ' ' + name; }	if (summary === '') { summary = itemIdentifier + ' added'; }	updateIndex(parent, summary, function {		// create item page		doEditRequest(parent.fullPath + '/' + name, 0, parent.getInvokeCode, 'invoke page for MOOC ' + itemIdentifier + ' created', sucCallback);	}); } function addLesson(name, item, summary, sucCallback) { addChild('lesson', name, item, summary, sucCallback); } function addUnit(name, item, summary, sucCallback) { addChild('unit', name, item, summary, sucCallback); } function createMooc(title, summary, sucCallback) { createPage('Category:' + title, '\n ', summary, function {		createPage(title + '/MoocIndex', '--MoocIndex for MOOC @ ' + title, summary, sucCallback);	}); } function addThread(item, talkPage, title, content, sucCallback) { item.setParameter(PARAMETER_KEY.NUM_THREADS, (item.discussion.threads.length + 1).toString); item.setParameter(PARAMETER_KEY.NUM_THREADS_OPEN, (item.discussion.getNumOpenThreads + 1).toString); addSectionToPage(talkPage.title, title, content, 'q:' + title, function {		doEditRequest(item.index.title, item.indexSection, item.tostring, 'new thread in item discussion', sucCallback);	}); } function saveThread(item, thread, sucCallback) { item.setParameter(PARAMETER_KEY.NUM_THREADS, item.discussion.threads.length.toString); item.setParameter(PARAMETER_KEY.NUM_THREADS_OPEN, item.discussion.getNumOpenThreads.toString); doEditRequest(thread.talkPage.title, thread.section, thread.tostring, 'replied to "' + thread.title + '"', function {		doEditRequest(item.index.title, item.indexSection, item.tostring, 'new reply in item discussion', sucCallback);	}); } function parseThreads(unparsedContent, sucCallback, errCallback) { console.log('parsing: ' + unparsedContent); var api = new mw.Api; var promise = api.post({		'action': 'parse',		'contentmodel': 'wikitext',		'disablepp': true,		'text': unparsedContent	}); promise.done(function(response) {		console.log('moocEditor.parseThreads: server response: ' + JSON.stringify(response));		var wikitext = response.parse.text['*'];		sucCallback(wikitext);	}); if (typeof errCallback !== 'undefined') { promise.fail(errCallback); } }

//