User:Evad37/AutoReminders.js

// /* Add any or all of these lines after the importScript line to set various options (the default values are shown):

var remind_portlet = "p-personal";          // Defines which portlet menu the link is added to - see Help:Customizing toolbars for options (choose one of the valid values for portletId) var remind_subpage = "reminders";           // Subpage where the to-do list entry is to be added var remind_viewlabel = "View reminders";    // Custom label for the link. Replace quoted text with your desired name. var remind_addlabel = "Set reminder";       // Custom label for the link. Replace quoted text with your desired name. var remind_addfirst = null;                 // Replace null with any value, e.g. "yes" (including quotation marks) to show the Add link before the View link. var remind_viewnew = null;                  // Replace null with any value, e.g. "yes" (including quotation marks) to make the View link open in a new tab or window.



/* == General setup == */

// Set default options for any that haven't been set function setDefault( option, val ) { if ( window[option] === undefined ) { window[option] = val; } }

setDefault( 'remind_portlet', 'p-personal' ); setDefault( 'remind_subpage', 'reminders' ); setDefault( 'remind_viewlabel', 'View reminders' ); setDefault( 'remind_addlabel', 'Set reminder' ); setDefault( 'remind_addfirst', null ); setDefault( 'remind_viewnew', null );

var remind_addbefore = ( remind_addfirst != null ) ? '#remind_view':null;

var remind_usersubpage = 'User:' + wgUserName + '/' + remind_subpage

//insert view link mw.util.addPortletLink( remind_portlet, '/wiki/' + remind_usersubpage, remind_viewlabel, 'remind_view');

//insert add link mw.util.addPortletLink( remind_portlet, ,                //dummy, will be replaced ,                 //dummy, will be replaced 'remind_add', null, null, remind_addbefore); $('li#remind_add > a, li#remind_add > span > a').replaceWith( "" + remind_addlabel + "" ); //replace dummy values

if ( remind_viewnew != null ) { $('li#remind_view > a, li#remind_view > span > a').attr( "target", "_blank" ); }

/* == Set a reminder == */

function setReminder { var remind_days = parseInt(prompt("Remind me in this many days")); if ( remind_days!=null) { //TODO: input validation var remind_comment = prompt("Enter a comment for the reminder"); if ( remind_comment != null ) { var colon_if_needed = ( wgNamespaceNumber == 6 || wgNamespaceNumber == 14 ) ? '|4=:':''; // Colon needed for File & Category namespaces var append_content = '\n'; edit_add_remind(remind_usersubpage, append_content); }	} }

//Perform edit to add entry function edit_add_remind(edit_title, edit_append) { new mw.Api.postWithToken( 'edit', {		action: 'edit',		title: edit_title,		appendtext: edit_append,		summary: 'AutoReminders setting reminder for ' + wgPageName + ''	} ).done( function( result, jqXHR ) {		alert( "Reminder set successfully" );	} ).fail( function( code, result ) {		if ( code === "http" ) {			alert( "AutoReminders says: HTTP error – " + result.textStatus ); // result.xhr contains the jqXHR object		} else if ( code === "ok-but-empty" ) {			alert( "AutoReminders says: Error – Got an empty response from the server" );		} else {			alert( "AutoReminders says: API error – " + code );		}	} ); }

/* == Check reminders against today's date == */

//TODO: Write code for this.

//Basically, for each reminder, check if it is today is the due date, or is after the due date - if it is, then output (add to) a string of the form " Some page: Some comment". //If output string is empty, do nothing (or set a flag so as not to check again today??), otherwise, execute code to show reminders.

//Outputs a string: either the empty string if no reminders due, or all the reminders that are due in HTML format (separated by line-breaks) function remindersDue(entryID) { new mw.Api.get( {	action: 'query',	titles: remind_usersubpage,	prop: [ 'revisions', 'info' ],	rvprop: 'content',	indexpageids: 1,	rawcontinue: ''	} ).done( function( result, jqXHR ) {		var remind_id = result.query.pageids;		var remind_contents = result.query.pages[ remind_id ].revisions[ 0 ][ '*' ];		//DO STUFF WITH THE CONTENTS OF THE REMIND SUBPAGE		var output = "";

//return output; } ).fail( function( code, result ) { if ( code === "http" ) { alert( "AutoReminders says: HTTP error – " + result.textStatus ); // result.xhr contains the jqXHR object } else if ( code === "ok-but-empty" ) { alert( "AutoReminders says: Error – Got an empty response from the server" ); } else { alert( "AutoReminders says: API error – " + code ); }	} ); }

/* == Show reminders in personal portlet == */

//TODO: Write code for this.

//TODO: Think about how to accomplish this a relatively easy way. Maybe use something like collapsible tables? //                             .--. //Output might look some like:  | You have 2 reminders: [show] | //                             '--'    . // and if you click show:       | Talk:Foo – Follow up on rename discussion (dismiss)   | //                             | Wikipedia talk:Bar – Check merge discussion (dismiss) | //                             '---' //Reimders persist until...? Until dismiss is clicked is probably the easiest. Might be more useful if clicking a link dismisses the corresponding reminder

/* == Remove reminder == */ // After a user dismisses a reminder, it should be removed from the reminders subpage

//TODO: modify the below code (copied from ToDoLister)

//Show remove links on todo list $('li.todolistitem').each(function {	$(this).append( " (remove)" ); });

//Look through page wikitext, return a string with item removed function remove_entry(entryID) { //first check click was intended var conf = confirm("Are you sure you want to remove this item?"); if (conf == true) { //Get content of todo list new mw.Api.get( {			action: 'query',			titles: remind_usersubpage,			prop: [ 'revisions', 'info' ],			rvprop: 'content',			indexpageids: 1,			rawcontinue: ''		} ).done( function( result, jqXHR ) {			var remind_id = result.query.pageids			var remind_contents = result.query.pages[ remind_id ].revisions[ 0 ][ '*' ];			var startloc = remind_contents.indexOf("", startloc) + 6;				var str1 = remind_contents.substr(0, startloc);				var str2 = remind_contents.substr(endloc);				var return_str = str1.concat(str2);				// Get page being removed for edit summary				var es_startloc = remind_contents.indexOf("", startloc);				var es_endloc = remind_contents.indexOf("", startloc) + 2; var es_str = remind_contents.substring(es_startloc, es_endloc); var return_es = 'ToDoLister removing 1 item: ' + es_str; edit_to_remove(return_str, return_es); }		} ).fail( function( code, result ) { if ( code === "http" ) { alert( "HTTP error: " + result.textStatus ); // result.xhr contains the jqXHR object } else if ( code === "ok-but-empty" ) { alert( "Error: Got an empty response from the server" ); } else { alert( "API error: " + code ); }		} );	} }

//Perform edit to remove entry function edit_to_remove(new_text, edit_sum) { new mw.Api.postWithToken( 'edit', {		action: 'edit',		title: 'User:' + wgUserName + '/' + remind_subpage,		text: new_text,		summary: edit_sum	} ).done( function( result, jqXHR ) {		alert( "Removed successfully" );		location.reload;	} ).fail( function( code, result ) {		if ( code === "http" ) {			alert( "HTTP error: " + result.textStatus ); // result.xhr contains the jqXHR object		} else if ( code === "ok-but-empty" ) {			alert( "Error: Got an empty response from the server" );		} else {			alert( "API error: " + code );		}	} ); } //